forked from u5surf/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
134 lines (111 loc) · 2.64 KB
/
auth_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
// Copyright 2018 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-kit/kit/log"
)
func TestAuth__extractUserId(t *testing.T) {
auth, err := createTestAuthable()
if err != nil {
t.Fatal(err)
}
defer auth.cleanup()
knownUserId := generateID()
// Write our user
cookie, err := createCookie(knownUserId, auth)
if err != nil {
t.Fatal(err)
}
if err := auth.writeCookie(knownUserId, cookie); err != nil {
t.Fatal(err)
}
// happy path
req := httptest.NewRequest("GET", "/users/login", nil)
req.Header.Set("cookie", fmt.Sprintf("moov_auth=%s", cookie.Value))
// make our request
userId, err := extractUserId(auth, req)
if err != nil {
t.Error(err)
}
if userId == "" {
t.Error("empty userId")
}
if userId != knownUserId {
t.Errorf("got %q, expected %q", userId, knownUserId)
}
// sad path
req.Header.Set("cookie", "moov_auth=bad")
userId, err = extractUserId(auth, req)
if err == nil {
t.Error("expected error")
}
if userId != "" {
t.Errorf("got %s", userId)
}
}
func TestAuth__checkAuth(t *testing.T) {
auth, err := createTestAuthable()
if err != nil {
t.Fatal(err)
}
defer auth.cleanup()
o, err := createTestOAuth()
if err != nil {
t.Fatal(err)
}
defer o.cleanup()
repo, err := createTestUserRepository()
if err != nil {
t.Fatal(err)
}
defer repo.cleanup()
// Setup our request
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/auth/check", nil)
// Make HTTP request
checkAuth(log.NewNopLogger(), auth, o.svc, repo)(w, r)
w.Flush()
// Since no auth information was provided we should 403
if w.Code != http.StatusForbidden {
t.Errorf("got %d", w.Code)
}
if v := w.Header().Get("X-User-Id"); v != "" {
t.Error("expected empty X-User-Id")
}
}
func TestAuth__forwardedPreflight(t *testing.T) {
auth, err := createTestAuthable()
if err != nil {
t.Fatal(err)
}
defer auth.cleanup()
o, err := createTestOAuth()
if err != nil {
t.Fatal(err)
}
defer o.cleanup()
repo, err := createTestUserRepository()
if err != nil {
t.Fatal(err)
}
defer repo.cleanup()
// Setup our request
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/auth/check", nil)
r.Header.Set("Origin", "http://localhost:8080")
r.Header.Set("X-Forwarded-Method", "OPTIONS")
checkAuth(log.NewNopLogger(), auth, o.svc, repo)(w, r)
w.Flush()
// Check response
if w.Code != http.StatusOK {
t.Errorf("got %d", w.Code)
}
if v := w.Header().Get("Access-Control-Allow-Origin"); v != "http://localhost:8080" {
t.Errorf("got %s", v)
}
}