From da114c560b95c86c5e84ff6424d29c94be9823ce Mon Sep 17 00:00:00 2001 From: kekskurse Date: Thu, 4 Nov 2021 17:58:23 +0100 Subject: [PATCH] Tests --- pkg/user/user_test.go | 28 ++++++++++++++++++ pkg/user/userclient_test.go | 59 +++++++++++++++++++++++++++++++++++++ readme.md | 2 -- 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 pkg/user/user_test.go create mode 100644 pkg/user/userclient_test.go diff --git a/pkg/user/user_test.go b/pkg/user/user_test.go new file mode 100644 index 0000000..defa207 --- /dev/null +++ b/pkg/user/user_test.go @@ -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") + } +} diff --git a/pkg/user/userclient_test.go b/pkg/user/userclient_test.go new file mode 100644 index 0000000..6af9b64 --- /dev/null +++ b/pkg/user/userclient_test.go @@ -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") + } +} \ No newline at end of file diff --git a/readme.md b/readme.md index 192a6f0..7e9ba59 100644 --- a/readme.md +++ b/readme.md @@ -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 \ No newline at end of file