@@ -265,7 +265,7 @@ func mockConnectorDataTestStorage(t *testing.T, s storage.Storage) {
265
265
require .NoError (t , err )
266
266
}
267
267
268
- func TestPasswordConnectorDataNotEmpty (t * testing.T ) {
268
+ func TestHandlePassword (t * testing.T ) {
269
269
t0 := time .Now ()
270
270
271
271
ctx , cancel := context .WithCancel (context .Background ())
@@ -280,33 +280,46 @@ func TestPasswordConnectorDataNotEmpty(t *testing.T) {
280
280
281
281
mockConnectorDataTestStorage (t , s .storage )
282
282
283
- u , err := url .Parse (s .issuerURL .String ())
284
- require .NoError (t , err )
283
+ makeReq := func (username , password string ) * httptest.ResponseRecorder {
284
+ u , err := url .Parse (s .issuerURL .String ())
285
+ require .NoError (t , err )
285
286
286
- u .Path = path .Join (u .Path , "/token" )
287
- v := url.Values {}
288
- v .Add ("scope" , "openid offline_access email" )
289
- v .Add ("grant_type" , "password" )
290
- v .Add ("username" , "test" )
291
- v .Add ("password" , "test" )
287
+ u .Path = path .Join (u .Path , "/token" )
288
+ v := url.Values {}
289
+ v .Add ("scope" , "openid offline_access email" )
290
+ v .Add ("grant_type" , "password" )
291
+ v .Add ("username" , username )
292
+ v .Add ("password" , password )
292
293
293
- req , _ := http .NewRequest ("POST" , u .String (), bytes .NewBufferString (v .Encode ()))
294
- req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded; param=value" )
295
- req .SetBasicAuth ("test" , "barfoo" )
294
+ req , _ := http .NewRequest ("POST" , u .String (), bytes .NewBufferString (v .Encode ()))
295
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded; param=value" )
296
+ req .SetBasicAuth ("test" , "barfoo" )
296
297
297
- rr := httptest .NewRecorder ()
298
- s .ServeHTTP (rr , req )
298
+ rr := httptest .NewRecorder ()
299
+ s .ServeHTTP (rr , req )
299
300
300
- require .Equal (t , 200 , rr .Code )
301
+ return rr
302
+ }
301
303
302
- // Check that we received expected refresh token
303
- var ref struct {
304
- Token string `json:"refresh_token"`
304
+ // Check unauthorized error
305
+ {
306
+ rr := makeReq ("test" , "invalid" )
307
+ require .Equal (t , 401 , rr .Code )
305
308
}
306
- err = json .Unmarshal (rr .Body .Bytes (), & ref )
307
- require .NoError (t , err )
308
309
309
- newSess , err := s .storage .GetOfflineSessions ("0-385-28089-0" , "test" )
310
- require .NoError (t , err )
311
- require .Equal (t , `{"test": "true"}` , string (newSess .ConnectorData ))
310
+ // Check that we received expected refresh token
311
+ {
312
+ rr := makeReq ("test" , "test" )
313
+ require .Equal (t , 200 , rr .Code )
314
+
315
+ var ref struct {
316
+ Token string `json:"refresh_token"`
317
+ }
318
+ err := json .Unmarshal (rr .Body .Bytes (), & ref )
319
+ require .NoError (t , err )
320
+
321
+ newSess , err := s .storage .GetOfflineSessions ("0-385-28089-0" , "test" )
322
+ require .NoError (t , err )
323
+ require .Equal (t , `{"test": "true"}` , string (newSess .ConnectorData ))
324
+ }
312
325
}
0 commit comments