-
Notifications
You must be signed in to change notification settings - Fork 21
/
tokenFactory_test.go
206 lines (189 loc) · 5.12 KB
/
tokenFactory_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
// SPDX-FileCopyrightText: 2017 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"errors"
"net/http/httptest"
"testing"
"github.com/golang-jwt/jwt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/bascule/basculehttp"
)
func TestRawAttributesBearerTokenFactory(t *testing.T) {
tests := []struct {
description string
value string
jwtToken *jwt.Token
parseCalled bool
parseErr error
isValid bool
expectedToken bascule.Token
expectedErr error
}{
{
description: "Success",
value: "abcd",
jwtToken: jwt.NewWithClaims(jwt.SigningMethodHS256, &bascule.ClaimsWithLeeway{
MapClaims: jwt.MapClaims(
map[string]interface{}{
jwtPrincipalKey: "test",
},
),
}),
parseCalled: true,
isValid: true,
expectedToken: bascule.NewToken("jwt", "test", rawAttributes(map[string]interface{}{jwtPrincipalKey: "test"})),
},
{
description: "Empty Value",
value: "",
expectedErr: errors.New("empty value"),
},
{
description: "Parse Failed",
value: "abcd",
jwtToken: jwt.NewWithClaims(jwt.SigningMethodHS256, &bascule.ClaimsWithLeeway{
MapClaims: jwt.MapClaims(
map[string]interface{}{
jwtPrincipalKey: "test",
},
),
}),
parseCalled: true,
parseErr: errors.New("parse fail test"),
expectedErr: errors.New("parse fail test"),
},
{
description: "Invalid Token",
value: "abcd",
jwtToken: jwt.NewWithClaims(jwt.SigningMethodHS256, &bascule.ClaimsWithLeeway{
MapClaims: jwt.MapClaims(
map[string]interface{}{
jwtPrincipalKey: "test",
},
),
}),
parseCalled: true,
expectedErr: basculehttp.ErrInvalidToken,
},
{
description: "Unexpected Claims",
value: "abcd",
jwtToken: jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims(
map[string]interface{}{
jwtPrincipalKey: "test",
},
)),
parseCalled: true,
isValid: true,
expectedErr: basculehttp.ErrUnexpectedClaims,
},
{
description: "Principal Value not Found",
value: "abcd",
jwtToken: jwt.NewWithClaims(jwt.SigningMethodHS256, &bascule.ClaimsWithLeeway{
MapClaims: jwt.MapClaims(
map[string]interface{}{
"test": "test",
},
),
}),
parseCalled: true,
isValid: true,
expectedErr: basculehttp.ErrInvalidPrincipal,
},
{
description: "Principal Value not a String",
value: "abcd",
jwtToken: jwt.NewWithClaims(jwt.SigningMethodHS256, &bascule.ClaimsWithLeeway{
MapClaims: jwt.MapClaims(
map[string]interface{}{
jwtPrincipalKey: 123,
},
),
}),
parseCalled: true,
isValid: true,
expectedErr: basculehttp.ErrInvalidPrincipal,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
r := new(MockResolver)
p := new(mockJWTParser)
if tc.parseCalled {
p.On("ParseJWT", mock.Anything, mock.Anything, mock.Anything).Return(tc.jwtToken, tc.parseErr).Once()
tc.jwtToken.Valid = tc.isValid
}
f := RawAttributesBearerTokenFactory{
DefaultKeyID: "default-key",
Resolver: r,
Parser: p,
}
req := httptest.NewRequest("get", "/", nil)
token, err := f.ParseAndValidate(context.Background(), req, "", tc.value)
assert.Equal(tc.expectedToken, token)
if tc.expectedErr == nil || err == nil {
assert.Equal(tc.expectedErr, err)
} else {
assert.Contains(err.Error(), tc.expectedErr.Error())
}
})
}
}
func TestDefaultKeyFunc(t *testing.T) {
defaultKeyID := "default-key"
tests := []struct {
description string
token *jwt.Token
useDefaultKey bool
resolveErr error
expectedPublicKey interface{}
expectedErr error
}{
{
description: "Success",
token: jwt.New(jwt.SigningMethodHS256),
expectedPublicKey: "public-key",
},
{
description: "Success with Default Key",
useDefaultKey: true,
token: jwt.New(jwt.SigningMethodHS256),
expectedPublicKey: "public-key",
},
{
description: "Resolve Error",
token: jwt.New(jwt.SigningMethodHS256),
resolveErr: errors.New("resolve error"),
expectedErr: errors.New("resolve error"),
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
r := new(MockResolver)
pair := new(mockKey)
pair.On("Public").Return(tc.expectedPublicKey).Once()
if tc.useDefaultKey {
r.On("Resolve", mock.Anything, defaultKeyID).Return(pair, tc.resolveErr).Once()
} else {
tc.token.Header = map[string]interface{}{
"kid": "some-value",
}
r.On("Resolve", mock.Anything, "some-value").Return(pair, tc.resolveErr).Once()
}
publicKey, err := defaultKeyFunc(context.Background(), defaultKeyID, r)(tc.token)
assert.Equal(tc.expectedPublicKey, publicKey)
if tc.expectedErr == nil || err == nil {
assert.Equal(tc.expectedErr, err)
} else {
assert.Contains(err.Error(), tc.expectedErr.Error())
}
})
}
}