@@ -41,6 +41,21 @@ func TestServer(t *testing.T) {
41
41
assert .Equal (t , want , got )
42
42
})
43
43
})
44
+
45
+ t .Run ("when provided invalid basic auth should fail" , func (t * testing.T ) {
46
+ svr := allsrv .NewServer (new (allsrv.InmemDB ), "dodgers@stink.com" , "PaSsWoRd" )
47
+
48
+ req := httptest .NewRequest ("POST" , "/foo" , newJSONBody (t , allsrv.Foo {
49
+ Name : "first-foo" ,
50
+ Note : "some note" ,
51
+ }))
52
+ req .SetBasicAuth ("dodgers@rule.com" , "wrongO" )
53
+ rec := httptest .NewRecorder ()
54
+
55
+ svr .ServeHTTP (rec , req )
56
+
57
+ assert .Equal (t , http .StatusUnauthorized , rec .Code )
58
+ })
44
59
})
45
60
46
61
t .Run ("foo read" , func (t * testing.T ) {
@@ -71,6 +86,18 @@ func TestServer(t *testing.T) {
71
86
assert .Equal (t , want , got )
72
87
})
73
88
})
89
+
90
+ t .Run ("when provided invalid basic auth should fail" , func (t * testing.T ) {
91
+ svr := allsrv .NewServer (new (allsrv.InmemDB ), "dodgers@stink.com" , "PaSsWoRd" )
92
+
93
+ req := httptest .NewRequest ("GET" , "/foo?id=reader1" , nil )
94
+ req .SetBasicAuth ("dodgers@rule.com" , "wrongO" )
95
+ rec := httptest .NewRecorder ()
96
+
97
+ svr .ServeHTTP (rec , req )
98
+
99
+ assert .Equal (t , http .StatusUnauthorized , rec .Code )
100
+ })
74
101
})
75
102
76
103
t .Run ("foo update" , func (t * testing.T ) {
@@ -98,6 +125,30 @@ func TestServer(t *testing.T) {
98
125
// note: lame we don't get the updated foo back
99
126
assert .Equal (t , http .StatusOK , rec .Code )
100
127
})
128
+
129
+ t .Run ("when provided invalid basic auth should fail" , func (t * testing.T ) {
130
+ db := new (allsrv.InmemDB )
131
+ err := db .CreateFoo (allsrv.Foo {
132
+ ID : "id1" ,
133
+ Name : "first_name" ,
134
+ Note : "first note" ,
135
+ })
136
+ require .NoError (t , err )
137
+
138
+ svr := allsrv .NewServer (db , "dodgers@stink.com" , "PaSsWoRd" )
139
+
140
+ req := httptest .NewRequest ("PUT" , "/foo" , newJSONBody (t , allsrv.Foo {
141
+ ID : "id1" ,
142
+ Name : "second_name" ,
143
+ Note : "second note" ,
144
+ }))
145
+ req .SetBasicAuth ("dodgers@rule.com" , "wrongO" )
146
+ rec := httptest .NewRecorder ()
147
+
148
+ svr .ServeHTTP (rec , req )
149
+
150
+ assert .Equal (t , http .StatusUnauthorized , rec .Code )
151
+ })
101
152
})
102
153
103
154
t .Run ("foo delete" , func (t * testing.T ) {
@@ -120,6 +171,18 @@ func TestServer(t *testing.T) {
120
171
121
172
assert .Equal (t , http .StatusOK , rec .Code )
122
173
})
174
+
175
+ t .Run ("when provided invalid basic auth should fail" , func (t * testing.T ) {
176
+ svr := allsrv .NewServer (new (allsrv.InmemDB ), "dodgers@stink.com" , "PaSsWoRd" )
177
+
178
+ req := httptest .NewRequest ("DELETE" , "/foo?id=id1" , nil )
179
+ req .SetBasicAuth ("dodgers@rule.com" , "wrongO" )
180
+ rec := httptest .NewRecorder ()
181
+
182
+ svr .ServeHTTP (rec , req )
183
+
184
+ assert .Equal (t , http .StatusUnauthorized , rec .Code )
185
+ })
123
186
})
124
187
}
125
188
0 commit comments