-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
269 lines (214 loc) · 7.29 KB
/
handler_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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package auth
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"golang.org/x/net/context"
. "github.com/smartystreets/goconvey/convey"
)
func TestHandler(t *testing.T) {
if !testing.Verbose() {
t.Parallel()
}
Convey("Given an test server", t, func() {
handler := NewMockHandler()
server := httptest.NewServer(handler)
Reset(func() {
server.Close()
})
Convey("When logging in with valid JSON", func() {
body := `{"username": "test_user", "password": "test_pass"}`
response, err := http.Post(server.URL, "application/json", strings.NewReader(body))
So(err, ShouldBeNil)
Convey("Then the response should contain a valid token", func() {
body := make(map[string]string)
decoder := json.NewDecoder(response.Body)
err := decoder.Decode(&body)
So(err, ShouldBeNil)
So(body, ShouldNotContainKey, "error")
So(body, ShouldContainKey, "token")
So(body["token"], ShouldNotBeEmpty)
So(body["refresh_token"], ShouldNotBeEmpty)
})
})
Convey("When logging in with valid refresh token", func() {
token, err := handler.auth.GenerateRefresh(&User{UID: "someuid"})
So(err, ShouldBeNil)
body := fmt.Sprintf(`{"refresh_token": "%s"}`, token)
response, err := http.Post(server.URL, "application/json", strings.NewReader(body))
So(err, ShouldBeNil)
Convey("Then the response should contain a valid token", func() {
body := make(map[string]string)
decoder := json.NewDecoder(response.Body)
err := decoder.Decode(&body)
So(err, ShouldBeNil)
So(body, ShouldNotContainKey, "error")
So(body, ShouldContainKey, "token")
So(body["token"], ShouldNotBeEmpty)
So(body["refresh_token"], ShouldNotBeEmpty)
})
})
Convey("When logging in with invalid refresh token", func() {
body := `{"refresh_token": "invalid"}`
response, err := http.Post(server.URL, "application/json", strings.NewReader(body))
So(err, ShouldBeNil)
Convey("Then the response should contain an error", func() {
body := make(map[string]string)
decoder := json.NewDecoder(response.Body)
err := decoder.Decode(&body)
So(err, ShouldBeNil)
So(body, ShouldContainKey, "error")
So(body, ShouldNotContainKey, "token")
So(body["error"], ShouldNotBeEmpty)
})
})
Convey("When logging in with valid POST data", func() {
response, err := http.PostForm(server.URL, url.Values{
"username": []string{"test_user"},
"password": []string{"test_pass"},
})
So(err, ShouldBeNil)
Convey("Then the response should contain a valid token", func() {
body := make(map[string]string)
decoder := json.NewDecoder(response.Body)
err := decoder.Decode(&body)
So(err, ShouldBeNil)
So(body, ShouldNotContainKey, "error")
So(body, ShouldContainKey, "token")
So(body["token"], ShouldNotBeEmpty)
})
})
Convey("When logging in with incorrect user details in valid JSON", func() {
body := `{"username": "invalid", "password": "invalid"}`
response, err := http.Post(server.URL, "application/json", strings.NewReader(body))
So(err, ShouldBeNil)
Convey("Then the response should contain an error", func() {
body := make(map[string]string)
decoder := json.NewDecoder(response.Body)
err := decoder.Decode(&body)
So(err, ShouldBeNil)
So(body, ShouldContainKey, "error")
So(body, ShouldNotContainKey, "token")
So(body["error"], ShouldNotBeEmpty)
})
})
Convey("When logging in with invalid JSON", func() {
response, err := http.Post(server.URL, "application/json", strings.NewReader(""))
So(err, ShouldBeNil)
Convey("Then the response should contain an error", func() {
body := make(map[string]string)
decoder := json.NewDecoder(response.Body)
err := decoder.Decode(&body)
So(err, ShouldBeNil)
So(body, ShouldContainKey, "error")
So(body, ShouldNotContainKey, "token")
So(body["error"], ShouldNotBeEmpty)
})
})
})
Convey("Given a server request with a valid token in the POST data", t, func() {
handler := NewMockHandler()
user := NewMockUser("someid")
token, err := handler.auth.Generate(user)
So(err, ShouldBeNil)
data := url.Values{"token": []string{token}}
req, err := http.NewRequest("POST", "/", strings.NewReader(data.Encode()))
So(err, ShouldBeNil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
Convey("When the request is parsed", func() {
user, err := handler.UserFromRequest(req)
Convey("Then the user data should be returned", func() {
So(user, ShouldNotBeNil)
So(user.UID, ShouldEqual, "someid")
So(err, ShouldBeNil)
})
})
})
Convey("Given a server request with a valid token using JSON data", t, func() {
handler := NewMockHandler()
user := NewMockUser("someid")
token, err := handler.auth.Generate(user)
So(err, ShouldBeNil)
data := fmt.Sprintf(`{"token":"%s"}`, token)
req, err := http.NewRequest("POST", "/", strings.NewReader(data))
So(err, ShouldBeNil)
req.Header.Set("Content-Type", "application/json")
Convey("When request is parsed", func() {
user, err := handler.UserFromRequest(req)
Convey("Then the user data should be returned", func() {
So(user, ShouldNotBeNil)
So(user.UID, ShouldEqual, "someid")
So(err, ShouldBeNil)
})
})
})
Convey("Given a server request with invalid JSON data", t, func() {
handler := NewMockHandler()
req, err := http.NewRequest("POST", "/", strings.NewReader(""))
So(err, ShouldBeNil)
req.Header.Set("Content-Type", "application/json")
Convey("When request is parsed", func() {
user, err := handler.UserFromRequest(req)
Convey("Then an error should be returned", func() {
So(user, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
})
Convey("Given an context instance", t, func() {
ctx := context.Background()
Convey("When context with user information is created", func() {
user := NewMockUser("someid")
ctx = NewUserContext(ctx, user)
Convey("Then the user information should be in the context", func() {
retrieved := UserFromContext(ctx)
So(retrieved, ShouldResemble, user)
})
})
})
Convey("Given an context instance", t, func() {
ctx := context.Background()
Convey("When no user information in the context", func() {
Convey("Then no user information should be in the context", func() {
user := UserFromContext(ctx)
So(user, ShouldBeNil)
})
})
})
}
func BenchmarkHandler(b *testing.B) {
handler := NewMockHandler()
server := httptest.NewServer(handler)
body := `{"username": "test_user", "password": "test_pass"}`
reader := strings.NewReader(body)
client := &http.Client{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
reader.Seek(0, 0)
req, err := http.NewRequest("POST", server.URL, reader)
if err != nil {
b.Errorf("Error creating login request: %s", err.Error())
continue
}
req.Header.Set("Content-Type", "application/json")
req.Close = true
resp, err := client.Do(req)
if err != nil {
b.Errorf("Error performing login request: %s", err.Error())
continue
}
resp.Body.Close()
}
server.Close()
}
func NewMockHandler() *Handler {
method := NewMockSigningMethod()
storage := NewMockStorage("test_uid", "test_user", "test_pass", []string{"permission1", "permission2"})
handler, _ := NewHandlerAndAuthenticator(method, storage, time.Hour, time.Hour*24)
return handler
}