-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_test.go
377 lines (337 loc) · 9.55 KB
/
handlers_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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
import (
"fmt"
"io/ioutil"
log "github.com/dougjohnson/logrus"
"net/http"
"net/http/cookiejar"
"strings"
"testing"
"code.google.com/p/gcfg"
)
type scenario struct {
identityRequired bool
whitelisted bool
incompleteProfile bool
missingFromOrg bool
expectedStatus int
expectedBody string
}
var proxyPort = 9060
const githubUserFullProfileJSON = `{"login":"test_user","name":"Test User","email":"[email protected]"}`
const githubUserEmailJSON = `[{"email":"[email protected]","verified":true,"primary":true},{"email":"[email protected]","verified":true,"primary":false}]`
const githubUserEmptyProfileJSON = `{"login":"test_user","name":"","email":""}`
const githubOrgJSON = `[{"login":"TestOrg"}]`
func init() {
log.SetLevel(log.DebugLevel)
startDummyGitHub(githubUserEmptyProfileJSON, 9049)
startDummyGitHub(githubUserFullProfileJSON, 9050)
startDummyBackend()
}
func TestScenario1(t *testing.T) {
s := scenario{
identityRequired: true,
whitelisted: false,
incompleteProfile: false,
missingFromOrg: false,
expectedStatus: 200,
expectedBody: "[Test User]",
}
s.test(t)
}
func TestScenario2(t *testing.T) {
s := scenario{
identityRequired: true,
whitelisted: false,
incompleteProfile: true,
missingFromOrg: false,
expectedStatus: 401,
expectedBody: "Not Authorized. Please edit your GitHub profile and add your Name and Email first.",
}
s.test(t)
}
func TestScenario3(t *testing.T) {
s := scenario{
identityRequired: true,
whitelisted: true,
incompleteProfile: true,
missingFromOrg: false,
expectedStatus: 401,
expectedBody: "Not Authorized. Please edit your GitHub profile and add your Name and Email first.",
}
s.test(t)
}
func TestScenario4(t *testing.T) {
s := scenario{
identityRequired: true,
whitelisted: true,
incompleteProfile: false,
missingFromOrg: false,
expectedStatus: 200,
expectedBody: "[Test User]",
}
s.test(t)
}
func TestScenario5(t *testing.T) {
s := scenario{
identityRequired: false,
whitelisted: false,
incompleteProfile: false,
missingFromOrg: false,
expectedStatus: 200,
expectedBody: "[Test User]",
}
s.test(t)
}
func TestScenario6(t *testing.T) {
s := scenario{
identityRequired: false,
whitelisted: true,
incompleteProfile: false,
missingFromOrg: false,
expectedStatus: 200,
expectedBody: "[]",
}
s.test(t)
}
func TestScenario7(t *testing.T) {
s := scenario{
identityRequired: false,
whitelisted: false,
incompleteProfile: true,
missingFromOrg: false,
expectedStatus: 200,
expectedBody: "[]",
}
s.test(t)
}
func TestScenario8(t *testing.T) {
s := scenario{
identityRequired: false,
whitelisted: true,
incompleteProfile: true,
missingFromOrg: false,
expectedStatus: 200,
expectedBody: "[]",
}
s.test(t)
}
func TestScenario9(t *testing.T) {
s := scenario{
identityRequired: true,
whitelisted: false,
incompleteProfile: false,
missingFromOrg: true,
expectedStatus: 401,
expectedBody: "Not Authorized. Please check with your local GitHub owner that you are added to the correct Organization.",
}
s.test(t)
}
func TestScenario10(t *testing.T) {
s := scenario{
identityRequired: true,
whitelisted: false,
incompleteProfile: true,
missingFromOrg: true,
expectedStatus: 401,
expectedBody: "Not Authorized. Please check with your local GitHub owner that you are added to the correct Organization.",
}
s.test(t)
}
func TestScenario11(t *testing.T) {
s := scenario{
identityRequired: true,
whitelisted: true,
incompleteProfile: true,
missingFromOrg: true,
expectedStatus: 401,
expectedBody: "Not Authorized. Please check with your local GitHub owner that you are added to the correct Organization.",
}
s.test(t)
}
func TestScenario12(t *testing.T) {
s := scenario{
identityRequired: true,
whitelisted: true,
incompleteProfile: false,
missingFromOrg: true,
expectedStatus: 401,
expectedBody: "Not Authorized. Please check with your local GitHub owner that you are added to the correct Organization.",
}
s.test(t)
}
func TestScenario13(t *testing.T) {
s := scenario{
identityRequired: false,
whitelisted: false,
incompleteProfile: false,
missingFromOrg: true,
expectedStatus: 401,
expectedBody: "Not Authorized. Please check with your local GitHub owner that you are added to the correct Organization.",
}
s.test(t)
}
func TestScenario14(t *testing.T) {
s := scenario{
identityRequired: false,
whitelisted: true,
incompleteProfile: false,
missingFromOrg: true,
expectedStatus: 200,
expectedBody: "[]",
}
s.test(t)
}
func TestScenario15(t *testing.T) {
s := scenario{
identityRequired: false,
whitelisted: false,
incompleteProfile: true,
missingFromOrg: true,
expectedStatus: 401,
expectedBody: "Not Authorized. Please check with your local GitHub owner that you are added to the correct Organization.",
}
s.test(t)
}
func TestScenario16(t *testing.T) {
s := scenario{
identityRequired: false,
whitelisted: true,
incompleteProfile: true,
missingFromOrg: true,
expectedStatus: 200,
expectedBody: "[]",
}
s.test(t)
}
func startDummyGitHub(userJSON string, port int) {
log.Printf("Starting Dummy GitHub on port %d...", port)
mux := http.NewServeMux()
mux.HandleFunc("/login/oauth/authorize", func(w http.ResponseWriter, r *http.Request) {
redirectURI := r.FormValue("redirect_uri")
state := r.FormValue("state")
http.Redirect(w, r, fmt.Sprintf("%s?code=test_code&state=%s", redirectURI, state), http.StatusFound)
})
mux.HandleFunc("/login/oauth/access_token", func(w http.ResponseWriter, r *http.Request) {
if r.FormValue("code") != "test_code" {
http.Error(w, "Invalid Code", 400)
return
}
if r.FormValue("client_id") == "" {
http.Error(w, "Missing client_id", 400)
return
}
if r.FormValue("client_secret") == "" {
http.Error(w, "Missing client_secret", 400)
return
}
if r.FormValue("redirect_uri") == "" {
http.Error(w, "Missing redirect_uri", 400)
return
}
fmt.Fprint(w, "access_token=test_token")
})
mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
if r.Header["Authorization"][0] != "token test_token" {
http.Error(w, "Invalid token", 401)
return
}
fmt.Fprint(w, userJSON)
})
mux.HandleFunc("/user/orgs", func(w http.ResponseWriter, r *http.Request) {
if r.Header["Authorization"][0] != "token test_token" {
http.Error(w, "Invalid token", 401)
return
}
fmt.Fprint(w, githubOrgJSON)
})
mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) {
if r.Header["Authorization"][0] != "token test_token" {
http.Error(w, "Invalid token", 401)
return
}
fmt.Fprint(w, githubUserEmailJSON)
})
go http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), mux)
}
func startDummyBackend() {
log.Println("Starting BackEnd...")
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, r.Header["Remote_user_full_name"])
})
go http.ListenAndServe("127.0.0.1:9051", mux)
}
func (s scenario) test(t *testing.T) {
configTemplate := getConfigTemplate()
var conf config
gcfg.ReadStringInto(&conf, configTemplate)
conf.ReverseProxy[fmt.Sprintf("backend.lvh.me:%d", proxyPort)] = conf.ReverseProxy["backend.lvh.me:9000"]
if s.identityRequired == false {
conf.ReverseProxy[fmt.Sprintf("backend.lvh.me:%d", proxyPort)].IdentityRequired = false
}
if s.whitelisted {
conf.IPWhitelist.IP[0] = "127.0.0.1"
}
if s.incompleteProfile {
conf.GitHub.Authurl = strings.Replace(conf.GitHub.Authurl, "9050", "9049", 1)
conf.GitHub.Tokenurl = strings.Replace(conf.GitHub.Tokenurl, "9050", "9049", 1)
conf.GitHub.Apiurl = strings.Replace(conf.GitHub.Apiurl, "9050", "9049", 1)
}
if s.missingFromOrg {
conf.GitHub.Organization = "Bad Org"
}
conf.Server.Port = proxyPort
conf.Server.Fqdn = fmt.Sprintf("auth.lvh.me:%d", proxyPort)
go http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Server.Bind, conf.Server.Port), (Handlers(conf)))
req, _ := http.NewRequest("GET", fmt.Sprintf("http://backend.lvh.me:%d", proxyPort), nil)
options := cookiejar.Options{}
jar, err := cookiejar.New(&options)
if err != nil {
log.Fatal(err)
}
client := &http.Client{Jar: jar}
resp, err := client.Do(req)
if err != nil {
t.Error(err)
return
}
defer resp.Body.Close()
body, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
t.Error(err)
return
}
if strings.Index(string(body[:]), s.expectedBody) == -1 {
t.Error(fmt.Sprintf("Expected >>%s<< in body. Got >>%s<<\nScenario: %+v\nConfig: %+v", s.expectedBody, string(body[:]), s, conf))
}
if resp.StatusCode != s.expectedStatus {
t.Error(fmt.Sprintf("Expected StatusCode %d. Got %d\nScenario: %+v", s.expectedStatus, resp.StatusCode))
}
proxyPort = proxyPort + 1
}
func getConfigTemplate() string {
return `[GitHub]
authurl = http://github.lvh.me:9050/login/oauth/authorize
tokenurl = http://github.lvh.me:9050/login/oauth/access_token
apiurl = http://github.lvh.me:9050
client-id = dummy-client-id
client-secret = dummy-client-secret
scope = user:email,read:org
organization = TestOrg
[Session]
authentication-key = 0PO4hnd7wBz732wodks3118UjkdtyJlD
encryption-key = Plot85uj32hG32MDpe4yrusednlse982
max-age = 60
[Server]
bind = 127.0.0.1
port = 9000
fqdn = auth.lvh.me:9000
loglevel = debug
[ReverseProxy "backend.lvh.me:9000"]
to = http://127.0.0.1:9051
identity-required
[IPWhiteList]
ip = 127.0.1.1
`
}