aboutsummaryrefslogtreecommitdiff
path: root/internal/auth/handler_test.go
blob: abf82a87be8f3740c48eb044f9216a9fd88dd724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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")
	}
}