forked from evergreen-ci/gimlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware_auth_user_test.go
More file actions
262 lines (243 loc) · 7.06 KB
/
middleware_auth_user_test.go
File metadata and controls
262 lines (243 loc) · 7.06 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package gimlet
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/negroni"
)
func TestUserMiddleware(t *testing.T) {
assert := assert.New(t)
// set up test fixtures
buf := []byte{}
body := bytes.NewBuffer(buf)
counter := 0
next := func(rw http.ResponseWriter, r *http.Request) {
counter++
rw.WriteHeader(http.StatusOK)
}
user := &MockUser{
ID: "test-user",
APIKey: "better",
}
usermanager := &MockUserManager{Users: []*MockUser{user}}
conf := UserMiddlewareConfiguration{}
// make sure the constructor works right
m := UserMiddleware(usermanager, conf)
assert.NotNil(m)
assert.Implements((*Middleware)(nil), m)
assert.Implements((*negroni.Handler)(nil), m)
assert.Equal(m.(*userMiddleware).conf, conf)
assert.Equal(m.(*userMiddleware).manager, usermanager)
// first test: make sure that if nothing is enabled, we pass
conf = UserMiddlewareConfiguration{SkipHeaderCheck: true, SkipCookie: true}
m = UserMiddleware(usermanager, conf)
assert.NotNil(m)
req := httptest.NewRequest("GET", "http://localhost/bar", body)
rw := httptest.NewRecorder()
m.ServeHTTP(rw, req, next)
assert.Equal(1, counter)
assert.Equal(http.StatusOK, rw.Code)
rusr := GetUser(req.Context())
assert.Nil(rusr)
// now check if we enable one or the other or both and its not configured, there is no user attached:
for _, conf := range []UserMiddlewareConfiguration{
{SkipHeaderCheck: true, SkipCookie: false},
{SkipHeaderCheck: false, SkipCookie: true},
{SkipHeaderCheck: false, SkipCookie: false},
} {
m = UserMiddleware(usermanager, conf)
assert.NotNil(m)
req = httptest.NewRequest("GET", "http://localhost/bar", body)
rw = httptest.NewRecorder()
m.ServeHTTP(rw, req, next)
assert.Equal(http.StatusOK, rw.Code)
rusr = GetUser(req.Context())
assert.Nil(rusr)
}
counter = 0
// Check that the header check works
conf = UserMiddlewareConfiguration{
SkipHeaderCheck: false,
SkipCookie: true,
HeaderUserName: "api-user",
HeaderKeyName: "api-key",
}
m = UserMiddleware(usermanager, conf)
assert.NotNil(m)
req = httptest.NewRequest("GET", "http://localhost/bar", body)
req.Header[conf.HeaderUserName] = []string{user.ID}
req.Header[conf.HeaderKeyName] = []string{user.APIKey}
rw = httptest.NewRecorder()
m.ServeHTTP(rw, req, func(rw http.ResponseWriter, r *http.Request) {
rusr := GetUser(r.Context())
assert.Equal(user, rusr)
})
assert.Equal(http.StatusOK, rw.Code)
// double check with a bad user
req = httptest.NewRequest("GET", "http://localhost/bar", body)
req.Header[conf.HeaderUserName] = []string{user.ID}
req.Header[conf.HeaderKeyName] = []string{"worse"}
rw = httptest.NewRecorder()
m.ServeHTTP(rw, req, func(rw http.ResponseWriter, r *http.Request) {
rusr := GetUser(r.Context())
assert.Nil(rusr)
})
assert.Equal(http.StatusUnauthorized, rw.Code)
// check reading the cookie
conf = UserMiddlewareConfiguration{
SkipHeaderCheck: true,
SkipCookie: false,
CookieName: "gimlet-token",
}
m = UserMiddleware(usermanager, conf)
var err error
// begin with the wrong cookie value
req, err = http.NewRequest("GET", "http://localhost/bar", body)
assert.NoError(err)
req.AddCookie(&http.Cookie{
Name: "foo",
Value: "better",
})
rw = httptest.NewRecorder()
m.ServeHTTP(rw, req, func(rw http.ResponseWriter, r *http.Request) {
rusr := GetUser(r.Context())
assert.Nil(rusr)
})
assert.Equal(http.StatusOK, rw.Code)
// try with the right token but wrong value
req, err = http.NewRequest("GET", "http://localhost/bar", body)
assert.NoError(err)
assert.NotNil(req)
req.AddCookie(&http.Cookie{
Name: "gimlet-token",
Value: "false",
})
rw = httptest.NewRecorder()
m.ServeHTTP(rw, req, func(rw http.ResponseWriter, r *http.Request) {
rusr := GetUser(r.Context())
assert.Nil(rusr)
})
assert.Equal(http.StatusOK, rw.Code)
// try with something that should work
user.Token = "42"
req, err = http.NewRequest("GET", "http://localhost/bar", body)
assert.NoError(err)
assert.NotNil(req)
req.AddCookie(&http.Cookie{
Name: "gimlet-token",
Value: "42",
})
rw = httptest.NewRecorder()
m.ServeHTTP(rw, req, func(rw http.ResponseWriter, r *http.Request) {
rusr := GetUser(r.Context())
assert.Equal(user, rusr)
})
assert.Equal(http.StatusOK, rw.Code)
// test that if get-or-create fails that the op does
usermanager.FailGetOrCreateUser = true
req, err = http.NewRequest("GET", "http://localhost/bar", body)
assert.NoError(err)
assert.NotNil(req)
req.AddCookie(&http.Cookie{
Name: "gimlet-token",
Value: "42",
})
rw = httptest.NewRecorder()
counter = 0
m.ServeHTTP(rw, req, func(rw http.ResponseWriter, r *http.Request) {
rusr := GetUser(r.Context())
assert.Nil(rusr)
})
assert.Equal(http.StatusOK, rw.Code)
}
func TestUserMiddlewareConfiguration(t *testing.T) {
conf := UserMiddlewareConfiguration{
HeaderUserName: "u",
HeaderKeyName: "k",
CookieName: "c",
CookieTTL: time.Hour,
CookiePath: "/p",
}
require.NoError(t, conf.Validate())
t.Run("DiabledChecksAreValid", func(t *testing.T) {
emptyConf := UserMiddlewareConfiguration{
SkipCookie: true,
SkipHeaderCheck: true,
}
assert.NoError(t, emptyConf.Validate())
})
t.Run("ZeroValueIsNotValid", func(t *testing.T) {
emptyConf := UserMiddlewareConfiguration{}
assert.Zero(t, emptyConf.CookiePath)
assert.Error(t, emptyConf.Validate())
// also we expect that the validate will populate the Tl
assert.NotZero(t, emptyConf.CookiePath)
})
t.Run("Cookie", func(t *testing.T) {
rw := httptest.NewRecorder()
assert.Len(t, rw.Header(), 0)
conf.AttachCookie("foo", rw)
assert.Len(t, rw.Header(), 1)
conf.ClearCookie(rw)
assert.Len(t, rw.Header(), 1)
})
t.Run("InvalidConfigurations", func(t *testing.T) {
for _, test := range []struct {
name string
op func(UserMiddlewareConfiguration) UserMiddlewareConfiguration
}{
{
name: "MissingCoookieName",
op: func(conf UserMiddlewareConfiguration) UserMiddlewareConfiguration {
conf.CookieName = ""
return conf
},
},
{
name: "TooShortTTL",
op: func(conf UserMiddlewareConfiguration) UserMiddlewareConfiguration {
conf.CookieTTL = time.Millisecond
return conf
},
},
{
name: "MalformedPath",
op: func(conf UserMiddlewareConfiguration) UserMiddlewareConfiguration {
conf.CookiePath = "foo"
return conf
},
},
{
name: "MissingUserName",
op: func(conf UserMiddlewareConfiguration) UserMiddlewareConfiguration {
conf.HeaderUserName = ""
return conf
},
},
{
name: "MissingKeyName",
op: func(conf UserMiddlewareConfiguration) UserMiddlewareConfiguration {
conf.HeaderKeyName = ""
return conf
},
},
} {
t.Run(test.name, func(t *testing.T) {
conf := UserMiddlewareConfiguration{
HeaderUserName: "u",
HeaderKeyName: "k",
CookieName: "c",
CookieTTL: time.Hour,
CookiePath: "/p",
}
require.NoError(t, conf.Validate())
conf = test.op(conf)
require.Error(t, conf.Validate())
})
}
})
}