-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_unit_test.go
246 lines (192 loc) · 5 KB
/
jwt_unit_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package jwt
import (
"testing"
"time"
)
type subTester struct {
Str string `json:"str,omitempty"`
}
type authTester struct {
Val string `json:"val,omitempty"`
Sub *subTester `json:"sub,omitempty"`
}
var (
subTest = &subTester{Str: "subtest"}
authTest = &authTester{Val: "test", Sub: subTest}
)
type newArgs struct {
iss string
aud string
key string
exp time.Duration
}
type genArgs struct {
uid string
auth *authTester
}
func TestUnitNew(t *testing.T) {
a := &newArgs{exp: time.Second}
a.iss, a.aud, a.key = "this", "this", "123"
t.Run("new - invalid args", func(t *testing.T) {
testUnitNewXxInvalidArgs(t, a)
})
t.Run("new - valid args", func(t *testing.T) {
testUnitNewXxValidArgs(t, a)
})
}
func testUnitNewXxInvalidArgs(t *testing.T, a *newArgs) {
if _, err := New("", a.aud, a.key, a.exp); err == nil {
t.Errorf("nil err for empty issuer")
}
if _, err := New(a.iss, "", a.key, a.exp); err == nil {
t.Errorf("nil err for empty audience")
}
if _, err := New(a.iss, a.aud, "", a.exp); err == nil {
t.Errorf("nil err for empty key")
}
if _, err := New(a.iss, a.aud, a.key, 0); err == nil {
t.Errorf("nil err for 0 duration")
}
}
func testUnitNewXxValidArgs(t *testing.T, a *newArgs) {
j, err := New(a.iss, a.aud, a.key, a.exp)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if j == nil {
t.Fatalf("got nil, want instance of jwt")
}
if j.issuer == "" {
t.Errorf("missing issuer")
}
if j.audience == "" {
t.Errorf("missing audience")
}
if len(j.key) == 0 {
t.Errorf("missing key")
}
if j.expiry == 0 {
t.Errorf("missing (or bad) expiry")
}
}
func TestUnitNewClaims(t *testing.T) {
iss, aud, key := "this", "this", "123"
exp := time.Second * 32
j, err := New(iss, aud, key, exp)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
a := &genArgs{uid: "9001", auth: authTest}
t.Run("new claims - invalid args", func(t *testing.T) {
testUnitNewClaimsXxInvalidArgs(t, j, a)
})
t.Run("new claims - valid args", func(t *testing.T) {
testUnitNewClaimsXxValidArgs(t, j, a)
})
}
func testUnitNewClaimsXxInvalidArgs(t *testing.T, j *JWT, a *genArgs) {
if _, err := j.newClaims("", a.auth); err == nil {
t.Errorf("nil err for empty uid")
}
}
func testUnitNewClaimsXxValidArgs(t *testing.T, j *JWT, a *genArgs) {
c, err := j.newClaims(a.uid, a.auth)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if c == nil {
t.Fatalf("got nil, want instance of claims")
}
if c.Issuer == "" {
t.Errorf("missing issuer")
}
if c.Audience == "" {
t.Errorf("missing audience")
}
if c.Subject != a.uid {
t.Errorf("subject: got %s, want %s", c.Subject, a.uid)
}
if c.IssuedAt == 0 {
t.Errorf("missing (or bad) issuedat")
}
if c.IssuedAt > time.Now().Unix() {
t.Errorf("issuedat set late")
}
if c.ExpiresAt == 0 {
t.Errorf("missing (or bad) expiresat")
}
if c.ExpiresAt < time.Now().Unix()+30 {
t.Errorf("expiresat set early")
}
}
func TestUnitGenerate(t *testing.T) {
iss, aud, key := "this", "this", "123"
exp := time.Second * 32
j, err := New(iss, aud, key, exp)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
a := &genArgs{uid: "9001", auth: authTest}
t.Run("gen - invalid args", func(t *testing.T) {
testUnitGenerateXxInvalidArgs(t, j, a)
})
t.Run("gen - valid args", func(t *testing.T) {
testUnitGenerateXxValidArgs(t, j, a)
})
}
func testUnitGenerateXxInvalidArgs(t *testing.T, j *JWT, a *genArgs) {
if _, err := j.Generate("", a.auth); err == nil {
t.Errorf("nil err for empty uid")
}
}
func testUnitGenerateXxValidArgs(t *testing.T, j *JWT, a *genArgs) {
tk, err := j.Generate(a.uid, a.auth)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if len(tk) < 16 {
t.Errorf("token appears to not be created correctly")
}
}
func TestUnitParseWithJWTClaims(t *testing.T) {
iss, aud, key := "this", "this", "123"
exp := time.Second
j, err := New(iss, aud, key, exp)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
a := &genArgs{uid: "9001", auth: authTest}
tk, err := j.Generate(a.uid, a.auth)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
t.Run("invalid parsetoken args", func(t *testing.T) {
testUnitParseWithJWTClaimsXxInvalidArgs(t, j, tk)
})
t.Run("valid claims", func(t *testing.T) {
testUnitParseWithJWTClaimsXxValidArgs(t, j, tk)
})
}
func testUnitParseWithJWTClaimsXxInvalidArgs(t *testing.T, j *JWT, tk string) {
a := &authTester{Sub: &subTester{}}
if _, err := j.parseWithJWTClaims("", a); err == nil {
t.Errorf("nil err for empty token")
}
if _, err := j.parseWithJWTClaims(tk+"x", a); err == nil {
t.Errorf("nil err for corrupted token")
}
}
func testUnitParseWithJWTClaimsXxValidArgs(t *testing.T, j *JWT, tk string) {
a := &authTester{Sub: &subTester{}}
c, err := j.parseWithJWTClaims(tk, a)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
at, ok := c.Auth.(*authTester)
if !ok {
t.Fatalf("cannot assert auth as *authTester")
}
if at.Val != authTest.Val {
t.Errorf("authTest: got %s, want %s", at.Val, authTest.Val)
}
}