Browse Source

Add tests for logout and UpdatePassword

pull/2/head
Tovi Jaeschke-Rogers 3 years ago
parent
commit
d2eb1c218c
4 changed files with 196 additions and 5 deletions
  1. +90
    -0
      Api/Auth/Logout_test.go
  2. +0
    -0
      Api/Auth/UpdatePassword.go
  3. +100
    -0
      Api/Auth/UpdatePassword_test.go
  4. +6
    -5
      Api/Routes.go

+ 90
- 0
Api/Auth/Logout_test.go View File

@ -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
}
}

Api/Auth/ChangePassword.go → Api/Auth/UpdatePassword.go View File


+ 100
- 0
Api/Auth/UpdatePassword_test.go View File

@ -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
}
}

+ 6
- 5
Api/Routes.go View File

@ -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")


Loading…
Cancel
Save