forked from martini-contrib/auth
-
Notifications
You must be signed in to change notification settings - Fork 9
/
basic_test.go
162 lines (128 loc) · 3.67 KB
/
basic_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
package auth
import (
"encoding/base64"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/urfave/negroni"
)
func Test_BasicAuth(t *testing.T) {
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte("foo:bar"))
h := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("hello"))
})
m := negroni.New()
m.Use(Basic("foo", "bar"))
m.UseHandler(h)
r, _ := http.NewRequest("GET", "foo", nil)
recorder := httptest.NewRecorder()
m.ServeHTTP(recorder, r)
if recorder.Code != 401 {
t.Error("Response not 401")
}
respBody := recorder.Body.String()
if respBody == "hello" {
t.Error("Auth block failed")
}
recorder = httptest.NewRecorder()
r.Header.Set("Authorization", auth)
m.ServeHTTP(recorder, r)
if recorder.Code == 401 {
t.Error("Response is 401")
}
if recorder.Body.String() != "hello" {
t.Error("Auth failed, got: ", recorder.Body.String())
}
}
type MockDataStore struct {
HashedPassword []byte
}
func (ds *MockDataStore) Get(key string) ([]byte, bool) {
return ds.HashedPassword, true
}
func Test_CacheBasic(t *testing.T) {
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte("foo:bar"))
invalidAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte("foo:barbar"))
hashedPassword, err := Hash("bar")
if err != nil {
t.Error("Hashing password failed")
}
dataStore := &MockDataStore{hashedPassword}
h := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("hello"))
})
cacheExpireTime := 500 * time.Millisecond
cachePurseTime := 200 * time.Millisecond
m := negroni.New()
m.Use(CacheBasic(dataStore, cacheExpireTime, cachePurseTime))
m.UseHandler(h)
// Test request fails without credential.
r, _ := http.NewRequest("GET", "foo", nil)
recorder := httptest.NewRecorder()
m.ServeHTTP(recorder, r)
if recorder.Code != 401 {
t.Error("Response not 401")
}
respBody := recorder.Body.String()
if respBody == "hello" {
t.Error("Auth block failed")
}
// Test request succeeds with valid credential.
r.Header.Set("Authorization", auth)
recorder = httptest.NewRecorder()
m.ServeHTTP(recorder, r)
if recorder.Code == 401 {
t.Error("Response is 401")
}
if recorder.Body.String() != "hello" {
t.Error("Auth failed, got: ", recorder.Body.String())
}
// Test request fails with invalid credential.
r.Header.Set("Authorization", invalidAuth)
recorder = httptest.NewRecorder()
m.ServeHTTP(recorder, r)
if recorder.Code != 401 {
t.Error("Response not 401")
}
respBody = recorder.Body.String()
if respBody == "hello" {
t.Error("Auth block failed")
}
// Test cache expiration.
// 1. Expect 1st request: succeed
// 2. Change password in data store to empty, wait for cache to expire
// 3. Expect 2nd request: fail
// 4. Change password in data store back to valid password, wait for cache to expire
// 5. Expect 3nd request: success
r.Header.Set("Authorization", auth)
recorder = httptest.NewRecorder()
m.ServeHTTP(recorder, r)
if recorder.Code == 401 {
t.Error("Response is 401")
}
if recorder.Body.String() != "hello" {
t.Error("Auth failed, got: ", recorder.Body.String())
}
dataStore.HashedPassword = nil
time.Sleep(cacheExpireTime)
recorder = httptest.NewRecorder()
m.ServeHTTP(recorder, r)
if recorder.Code != 401 {
t.Error("Response not 401")
}
respBody = recorder.Body.String()
if respBody == "hello" {
t.Error("Auth block failed")
}
dataStore.HashedPassword = hashedPassword
time.Sleep(cacheExpireTime)
recorder = httptest.NewRecorder()
m.ServeHTTP(recorder, r)
if recorder.Code == 401 {
t.Error("Response is 401")
}
if recorder.Body.String() != "hello" {
t.Error("Auth failed, got: ", recorder.Body.String())
}
}