|
@ -0,0 +1,100 @@ |
|
|
|
|
|
package Auth |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
|
"fmt" |
|
|
|
|
|
"net/http" |
|
|
|
|
|
"net/http/httptest" |
|
|
|
|
|
"os" |
|
|
|
|
|
"path" |
|
|
|
|
|
"runtime" |
|
|
|
|
|
"strings" |
|
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/gorilla/mux" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
|
|
// Fix working directory for tests
|
|
|
|
|
|
_, filename, _, _ := runtime.Caller(0) |
|
|
|
|
|
dir := path.Join(path.Dir(filename), "..") |
|
|
|
|
|
err := os.Chdir(dir) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
panic(err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Database.InitTest() |
|
|
|
|
|
|
|
|
|
|
|
r = mux.NewRouter() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func Test_UpdatePassword(t *testing.T) { |
|
|
|
|
|
t.Log("Testing UpdatePassword...") |
|
|
|
|
|
|
|
|
|
|
|
r.HandleFunc("/admin/login", Logout).Methods("POST") |
|
|
|
|
|
r.HandleFunc("/admin/user/{userID}/update-password", UpdatePassword).Methods("PUT") |
|
|
|
|
|
|
|
|
|
|
|
ts := httptest.NewServer(r) |
|
|
|
|
|
|
|
|
|
|
|
defer ts.Close() |
|
|
|
|
|
|
|
|
|
|
|
userData, err := createTestUser(true) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
postJson := ` |
|
|
|
|
|
{ |
|
|
|
|
|
"email": "%s", |
|
|
|
|
|
"password": "password" |
|
|
|
|
|
} |
|
|
|
|
|
` |
|
|
|
|
|
postJson = fmt.Sprintf(postJson, userData.Email) |
|
|
|
|
|
|
|
|
|
|
|
res, err := http.Post(ts.URL+"/admin/login", "application/json", strings.NewReader(postJson)) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if res.StatusCode != http.StatusOK { |
|
|
|
|
|
t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(res.Cookies()) != 1 { |
|
|
|
|
|
t.Errorf("Expected cookies len 1, recieved %d", len(res.Cookies())) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
postJson = ` |
|
|
|
|
|
{ |
|
|
|
|
|
"password": "new_password", |
|
|
|
|
|
"confirm_password": "new_password" |
|
|
|
|
|
} |
|
|
|
|
|
` |
|
|
|
|
|
req, err := http.NewRequest("PUT", fmt.Sprintf( |
|
|
|
|
|
"%s/admin/user/%s/update-password", |
|
|
|
|
|
ts.URL, |
|
|
|
|
|
userData.ID, |
|
|
|
|
|
), strings.NewReader(postJson)) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
req.AddCookie(res.Cookies()[0]) |
|
|
|
|
|
|
|
|
|
|
|
res, err = http.DefaultClient.Do(req) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if res.StatusCode != http.StatusOK { |
|
|
|
|
|
t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
} |