This commit is contained in:
Kekskurse 2021-11-04 17:58:23 +01:00
parent 1bb7e7e157
commit da114c560b
Signed by: kekskurse
GPG key ID: 728ACCB59341E7E4
3 changed files with 87 additions and 2 deletions

28
pkg/user/user_test.go Normal file
View file

@ -0,0 +1,28 @@
package user
import (
"github.com/go-chi/chi/v5"
"io"
"net/http/httptest"
"testing"
)
func TestGetLoginForm(t *testing.T) {
req := httptest.NewRequest("GET", "/login", nil)
rec := httptest.NewRecorder()
router := chi.NewRouter()
router.Group(func(r chi.Router) {
Register(r)
})
loginForm(rec, req)
_, err := io.ReadAll(rec.Body)
if err != nil {
t.Error("Get login form should not return an error")
}
if rec.Code != 200 {
t.Error("Login form should not return an non 200 status code")
}
}

View file

@ -0,0 +1,59 @@
package user
import (
"testing"
)
func TestAdminUserExists(t *testing.T) {
u := GetUserClient()
res, err := u.login("admin", "password")
if err != nil {
t.Error("Login should not return a error")
}
if res == false {
t.Error("Login should return true for admin/password")
} else {
t.Log("Login for admin/password works")
}
}
func TestLoginWithWrongUserDataFailed(t *testing.T) {
u := GetUserClient()
res, err := u.login("invalide", "data")
if err != nil {
t.Error("Login shouldnt return an erroro")
}
if res == true {
t.Error("Login shouldnt be true with invalide data")
} else {
t.Log("Login failed successfully")
}
}
func TestRegisterAndLogin(t *testing.T) {
u := GetUserClient()
res, err := u.register("test", "test")
if err != nil {
t.Error("Register should not return an error")
}
if res == false {
t.Error("Register schould return true")
} else {
t.Log("User Register ok")
}
res, err = u.login("test", "test")
if err != nil {
t.Error("Login should not return an error")
}
if res == false {
t.Error("Login schould return true")
} else {
t.Log("User Login ok")
}
}

View file

@ -10,7 +10,5 @@ Alles ist in einem binary
next steps:
* auth über mehrer pkg sharen z.b. durch config object statt chi router in die Register funktion reichen
* tests für user client bauen
* tests für http functionen bauen, rausfinden wie das geht
* mysql migrationen oder andere datenbank für user einbauen
* ci/Cd bis docker image bauen