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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
package auth_test
import (
"errors"
"strings"
"testing"
"golang.org/x/oauth2"
"github.com/brianvoe/gofakeit"
"github.com/golang/mock/gomock"
"git.ofmax.li/iserv/internal/auth"
"git.ofmax.li/iserv/internal/mock/mock_auth"
)
type serviceSuite struct {
as auth.Servicer
repo *mock_auth.MockRepo
}
func TestServices(t *testing.T) {
ctrl := gomock.NewController(t)
repo := mock_auth.NewMockRepo(ctrl)
defer ctrl.Finish()
ts := &serviceSuite{
auth.NewService(repo),
repo,
}
t.Run("token generation", ts.testGenStateToken())
t.Run("token validation", ts.testValidateStateToken())
t.Run("auth profile registration", ts.testLoginOrRegsiterSessionId())
}
func (s *serviceSuite) testGenStateToken() func(t *testing.T) {
return func(t *testing.T) {
token, _ := s.as.GenerateStateToken()
parts := strings.Split(token, ".")
if len(parts) != 3 {
t.Errorf("token doesn't match format")
}
}
}
func (s *serviceSuite) testValidateStateToken() func(t *testing.T) {
validToken, _ := s.as.GenerateStateToken()
return func(t *testing.T) {
cases := []struct {
name string
token string
sessionToken string
wanted bool
errz error
}{
{name: "valid matching token",
token: validToken,
sessionToken: validToken,
wanted: true, errz: nil},
{name: "non matching token",
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
sessionToken: "sadfsaf",
wanted: false, errz: auth.ErrInvalidToken},
{name: "matching but not real",
token: "eyJhbGciOTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
sessionToken: "eyJhbGciOTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
wanted: false, errz: auth.ErrInvalidJWT},
}
for _, tc := range cases {
isValid, err := s.as.ValidateStateToken(tc.token, tc.sessionToken)
if (isValid != tc.wanted) || (err != tc.errz) {
t.Fatalf("%s: expected: %v, got: %v err: %v errgot: %v", tc.name, tc.wanted, isValid, tc.errz, err)
}
}
}
}
func (s *serviceSuite) testLoginOrRegsiterSessionId() func(t *testing.T) {
return func(t *testing.T) {
// is authorized err
gp := &auth.GoogleAuthProfile{}
token := &oauth2.Token{}
gofakeit.Struct(token)
gofakeit.Struct(gp)
s.repo.
EXPECT().
IsAuthorized(gomock.Any()).
Return(false, errors.New("foo"))
id, isNew, err := s.as.LoginOrRegisterSessionID(token, gp)
if err == nil && id == "" && isNew == false {
t.Fatalf("%s", id)
}
// not authorized no error
s.repo.
EXPECT().
IsAuthorized(gomock.Any()).
Return(false, nil)
id, isNew, err = s.as.LoginOrRegisterSessionID(token, gp)
if err != auth.ErrUnauthorized || id != "" || isNew != false {
t.Fatalf("unauthorized isnew: %v id: %v err: %v", isNew, id, err.Error())
}
// authorized
s.repo.
EXPECT().
IsAuthorized(gomock.Any()).
Return(true, nil)
s.repo.
EXPECT().
LookUpAuthProfileID(gomock.Any()).
Return("asdfsafaf", nil)
id, isNew, err = s.as.LoginOrRegisterSessionID(token, gp)
if err != nil && isNew == false && id == "" {
t.Fatalf("auth profile exists isnew: %v id: %v err: %v", isNew, id, err.Error())
}
// new registration
s.repo.
EXPECT().
IsAuthorized(gomock.Any()).
Return(true, nil)
s.repo.
EXPECT().
LookUpAuthProfileID(gomock.Any()).
Return("", nil)
s.repo.
EXPECT().
SaveAuthProfile(gomock.Any()).
Return(nil)
id, isNew, err = s.as.LoginOrRegisterSessionID(token, gp)
if err != nil && isNew == false && id == "" {
t.Fatalf("isnew: %v id: %v err: %v", isNew, id, err.Error())
}
}
}
|