-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticator_test.go
159 lines (124 loc) · 4.25 KB
/
authenticator_test.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package auth
import (
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestAuthenticator(t *testing.T) {
if !testing.Verbose() {
t.Parallel()
}
Convey("Given an Authenticator", t, func() {
auth := NewMockAuthenticator("test_uid", "test_user", "test_pass", []string{"permission1", "permission2"})
Convey("When a valid user is authenticated", func() {
token, _, err := auth.Authenticate("test_user", "test_pass")
Convey("Then a token should be generated", func() {
So(err, ShouldBeNil)
So(token, ShouldNotBeEmpty)
})
})
Convey("When an invalid user is authenticated", func() {
token, _, err := auth.Authenticate("invalid_user", "invalid_pass")
Convey("Then a token should not be generated", func() {
So(err, ShouldNotBeNil)
So(token, ShouldBeEmpty)
})
})
Convey("When a valid token validated", func() {
token, _, err := auth.Authenticate("test_user", "test_pass")
So(err, ShouldBeNil)
user, err := auth.ValidateToken(token)
Convey("Then the user infomation should be returned", func() {
So(err, ShouldBeNil)
So(user, ShouldNotBeNil)
So(user.UID, ShouldEqual, "test_uid")
So(user.Permissions, ShouldContain, "permission1")
So(user.Permissions, ShouldContain, "permission2")
})
})
Convey("When a refresh valid token validated", func() {
_, token, err := auth.Authenticate("test_user", "test_pass")
So(err, ShouldBeNil)
user, err := auth.ValidateRefresh(token)
Convey("Then the user infomation should be returned", func() {
So(err, ShouldBeNil)
So(user, ShouldNotBeNil)
So(user.UID, ShouldEqual, "test_uid")
So(user.Permissions, ShouldContain, "permission1")
So(user.Permissions, ShouldContain, "permission2")
})
})
Convey("When an token is validated as a refresh token", func() {
token, _, err := auth.Authenticate("test_user", "test_pass")
So(err, ShouldBeNil)
user, err := auth.ValidateRefresh(token)
Convey("Then an error should be returned", func() {
So(user, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
Convey("When an refresh token is validated as a token", func() {
_, token, err := auth.Authenticate("test_user", "test_pass")
So(err, ShouldBeNil)
user, err := auth.ValidateToken(token)
Convey("Then an error should be returned", func() {
So(user, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
Convey("When an invalid token is validated", func() {
user, err := auth.ValidateToken("bad_token")
Convey("Then an error should be returned", func() {
So(user, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
Convey("When an invalid refresh token is validated", func() {
user, err := auth.ValidateRefresh("bad_token")
Convey("Then an error should be returned", func() {
So(user, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
Convey("When a token with no UID is validated", func() {
tok := auth.generator.Create()
tok.Claims["permissions"] = []string{"permission1"}
token, err := auth.generator.Sign(tok)
So(err, ShouldBeNil)
user, err := auth.ValidateToken(token)
Convey("hen an error should be returned", func() {
So(user, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
Convey("When a token with invalid permissions is validated", func() {
tok := auth.generator.Create()
tok.Claims["uid"] = "uid"
tok.Claims["type"] = "token"
token, err := auth.generator.Sign(tok)
So(err, ShouldBeNil)
user, err := auth.ValidateToken(token)
Convey("Then an error should be returned", func() {
So(user, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
Convey("When a token with invalid type is validated", func() {
tok := auth.generator.Create()
tok.Claims["uid"] = "uid"
tok.Claims["permissions"] = []string{"permission1"}
token, err := auth.generator.Sign(tok)
So(err, ShouldBeNil)
user, err := auth.ValidateToken(token)
Convey("Then an error should be returned", func() {
So(user, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
})
}
func NewMockAuthenticator(uid string, user string, pass string, permissions []string) *Authenticator {
generator := NewMockTokenGenerator()
storage := NewMockStorage(uid, user, pass, permissions)
return NewAuthenticator(generator, storage, time.Hour, time.Hour*24)
}