diff options
| author | Max Resnick <max@ofmax.li> | 2020-08-14 23:13:41 -0700 |
|---|---|---|
| committer | Max Resnick <max@ofmax.li> | 2020-11-08 07:57:13 -0800 |
| commit | 689a57ec4a444f8233fe2e5ec7ceb0903218218d (patch) | |
| tree | 1bcfe6786c38b4ae11997d5d97dc3c5fba747b97 /internal/auth/handler_test.go | |
| parent | 77c2e6aca2dc0f851f55e30a0f49c9ee7c2c952e (diff) | |
| download | iserv-689a57ec4a444f8233fe2e5ec7ceb0903218218d.tar.gz | |
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") + } +} |