diff options
Diffstat (limited to 'internal/auth/handler_test.go')
| -rw-r--r-- | internal/auth/handler_test.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/internal/auth/handler_test.go b/internal/auth/handler_test.go new file mode 100644 index 0000000..abf82a8 --- /dev/null +++ b/internal/auth/handler_test.go @@ -0,0 +1,60 @@ +package auth_test + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/alexedwards/scs/v2" + _ "github.com/brianvoe/gofakeit" + "github.com/golang/mock/gomock" + + "git.ofmax.li/iserv/internal/auth" + "git.ofmax.li/iserv/internal/mock/mock_auth" + testhelper "git.ofmax.li/iserv/internal/test" +) + +type handlerSuite struct { + as *mock_auth.MockServicer + ses *scs.SessionManager +} + +func TestHandler(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + session := scs.New() + ts := &handlerSuite{ + as: mock_auth.NewMockServicer(ctrl), + ses: session, + } + ah := auth.NewHandler(ts.as, session, testhelper.NewConf("testhost")) + t.Run("test login", ts.testLogin(ah)) +} + +func (s *handlerSuite) testLogin(ah auth.Handler) func(t *testing.T) { + return func(t *testing.T) { + s.as.EXPECT().GenerateStateToken().Return("asfdas", nil) + r, _ := http.NewRequest("GET", "/auth/login/", nil) + w := httptest.NewRecorder() + handler := s.ses.LoadAndSave(http.HandlerFunc(ah.Login)) + handler.ServeHTTP(w, r) + response := w.Result() + if response.StatusCode != http.StatusFound { + t.Errorf("login http status not 302") + } + redirectURL, _ := response.Location() + if !strings.Contains(redirectURL.String(), "REDIRECT_URL") { + t.Errorf("redirect url not foukd %s", redirectURL) + } + if !strings.Contains(redirectURL.String(), "asfdas") { + t.Errorf("state token not found in url") + } + } +} + +func (s *handlerSuite) testCallBack(ah auth.Handler) func(t *testing.T) { + return func(t *testing.T) { + t.Logf("not doing this at the moment") + } +} |