From d2eb1c218cd25032d49c98a4faae47e5009c8bd7 Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Mon, 21 Mar 2022 05:15:23 +1030 Subject: [PATCH] Add tests for logout and UpdatePassword --- Api/Auth/Logout_test.go | 90 ++++++++++++++++ .../{ChangePassword.go => UpdatePassword.go} | 0 Api/Auth/UpdatePassword_test.go | 100 ++++++++++++++++++ Api/Routes.go | 11 +- 4 files changed, 196 insertions(+), 5 deletions(-) create mode 100644 Api/Auth/Logout_test.go rename Api/Auth/{ChangePassword.go => UpdatePassword.go} (100%) create mode 100644 Api/Auth/UpdatePassword_test.go diff --git a/Api/Auth/Logout_test.go b/Api/Auth/Logout_test.go new file mode 100644 index 0000000..56cc7f9 --- /dev/null +++ b/Api/Auth/Logout_test.go @@ -0,0 +1,90 @@ +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_Logout(t *testing.T) { + t.Log("Testing Logout...") + + r.HandleFunc("/admin/login", Logout).Methods("POST") + r.HandleFunc("/admin/logout", Logout).Methods("GET") + + 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 + } + + req, err := http.NewRequest("GET", ts.URL+"/admin/logout", nil) + 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 + } +} diff --git a/Api/Auth/ChangePassword.go b/Api/Auth/UpdatePassword.go similarity index 100% rename from Api/Auth/ChangePassword.go rename to Api/Auth/UpdatePassword.go diff --git a/Api/Auth/UpdatePassword_test.go b/Api/Auth/UpdatePassword_test.go new file mode 100644 index 0000000..1347495 --- /dev/null +++ b/Api/Auth/UpdatePassword_test.go @@ -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 + } +} diff --git a/Api/Routes.go b/Api/Routes.go index e29633a..e71544f 100644 --- a/Api/Routes.go +++ b/Api/Routes.go @@ -30,11 +30,12 @@ func InitApiEndpoints() *mux.Router { router.HandleFunc("/post/{postID}/image/{imageID}", deletePostImage).Methods("DELETE") // Define routes for users api - router.HandleFunc("/user", getUsers).Methods("GET") - router.HandleFunc("/user", createUser).Methods("POST") - router.HandleFunc("/user/{userID}", getUser).Methods("GET") - router.HandleFunc("/user/{userID}", updatePost).Methods("PUT") - router.HandleFunc("/user/{userID}", deletePost).Methods("DELETE") + router.HandleFunc("/admin/user", getUsers).Methods("GET") + router.HandleFunc("/admin/user", createUser).Methods("POST") + router.HandleFunc("/admin/user/{userID}", getUser).Methods("GET") + router.HandleFunc("/admin/user/{userID}", updatePost).Methods("PUT") + router.HandleFunc("/admin/user/{userID}", deletePost).Methods("DELETE") + router.HandleFunc("/admin/user/{userID}/update-password", Auth.UpdatePassword).Methods("PUT") // Define routes for authentication router.HandleFunc("/admin/login", Auth.Login).Methods("POST")