27
27
USER_MGT_URL_PREFIX = 'https://identitytoolkit.googleapis.com/v2beta1/projects/mock-project-id'
28
28
OIDC_PROVIDER_CONFIG_RESPONSE = testutils .resource ('oidc_provider_config.json' )
29
29
SAML_PROVIDER_CONFIG_RESPONSE = testutils .resource ('saml_provider_config.json' )
30
+ LIST_OIDC_PROVIDER_CONFIGS_RESPONSE = testutils .resource ('list_oidc_provider_configs.json' )
30
31
LIST_SAML_PROVIDER_CONFIGS_RESPONSE = testutils .resource ('list_saml_provider_configs.json' )
31
32
32
33
CONFIG_NOT_FOUND_RESPONSE = """{
@@ -237,6 +238,109 @@ def test_delete(self, user_mgt_app):
237
238
assert req .method == 'DELETE'
238
239
assert req .url == '{0}{1}' .format (USER_MGT_URL_PREFIX , '/oauthIdpConfigs/oidc.provider' )
239
240
241
+ @pytest .mark .parametrize ('arg' , [None , 'foo' , list (), dict (), 0 , - 1 , 101 , False ])
242
+ def test_invalid_max_results (self , user_mgt_app , arg ):
243
+ with pytest .raises (ValueError ):
244
+ auth .list_oidc_provider_configs (max_results = arg , app = user_mgt_app )
245
+
246
+ @pytest .mark .parametrize ('arg' , ['' , list (), dict (), 0 , - 1 , 101 , False ])
247
+ def test_invalid_page_token (self , user_mgt_app , arg ):
248
+ with pytest .raises (ValueError ):
249
+ auth .list_oidc_provider_configs (page_token = arg , app = user_mgt_app )
250
+
251
+ def test_list_single_page (self , user_mgt_app ):
252
+ recorder = _instrument_provider_mgt (user_mgt_app , 200 , LIST_OIDC_PROVIDER_CONFIGS_RESPONSE )
253
+ page = auth .list_oidc_provider_configs (app = user_mgt_app )
254
+
255
+ self ._assert_page (page )
256
+ provider_configs = list (config for config in page .iterate_all ())
257
+ assert len (provider_configs ) == 2
258
+
259
+ assert len (recorder ) == 1
260
+ req = recorder [0 ]
261
+ assert req .method == 'GET'
262
+ assert req .url == '{0}{1}' .format (USER_MGT_URL_PREFIX , '/oauthIdpConfigs?pageSize=100' )
263
+
264
+ def test_list_multiple_pages (self , user_mgt_app ):
265
+ sample_response = json .loads (OIDC_PROVIDER_CONFIG_RESPONSE )
266
+ configs = _create_list_response (sample_response )
267
+
268
+ # Page 1
269
+ response = {
270
+ 'oauthIdpConfigs' : configs [:2 ],
271
+ 'nextPageToken' : 'token'
272
+ }
273
+ recorder = _instrument_provider_mgt (user_mgt_app , 200 , json .dumps (response ))
274
+ page = auth .list_oidc_provider_configs (max_results = 10 , app = user_mgt_app )
275
+
276
+ self ._assert_page (page , next_page_token = 'token' )
277
+ assert len (recorder ) == 1
278
+ req = recorder [0 ]
279
+ assert req .method == 'GET'
280
+ assert req .url == '{0}/oauthIdpConfigs?pageSize=10' .format (USER_MGT_URL_PREFIX )
281
+
282
+ # Page 2 (also the last page)
283
+ response = {'oauthIdpConfigs' : configs [2 :]}
284
+ recorder = _instrument_provider_mgt (user_mgt_app , 200 , json .dumps (response ))
285
+ page = page .get_next_page ()
286
+
287
+ self ._assert_page (page , count = 1 , start = 2 )
288
+ assert len (recorder ) == 1
289
+ req = recorder [0 ]
290
+ assert req .method == 'GET'
291
+ assert req .url == '{0}/oauthIdpConfigs?pageSize=10&pageToken=token' .format (
292
+ USER_MGT_URL_PREFIX )
293
+
294
+ def test_paged_iteration (self , user_mgt_app ):
295
+ sample_response = json .loads (OIDC_PROVIDER_CONFIG_RESPONSE )
296
+ configs = _create_list_response (sample_response )
297
+
298
+ # Page 1
299
+ response = {
300
+ 'oauthIdpConfigs' : configs [:2 ],
301
+ 'nextPageToken' : 'token'
302
+ }
303
+ recorder = _instrument_provider_mgt (user_mgt_app , 200 , json .dumps (response ))
304
+ page = auth .list_oidc_provider_configs (app = user_mgt_app )
305
+ iterator = page .iterate_all ()
306
+
307
+ for index in range (2 ):
308
+ provider_config = next (iterator )
309
+ assert provider_config .provider_id == 'oidc.provider{0}' .format (index )
310
+ assert len (recorder ) == 1
311
+ req = recorder [0 ]
312
+ assert req .method == 'GET'
313
+ assert req .url == '{0}/oauthIdpConfigs?pageSize=100' .format (USER_MGT_URL_PREFIX )
314
+
315
+ # Page 2 (also the last page)
316
+ response = {'oauthIdpConfigs' : configs [2 :]}
317
+ recorder = _instrument_provider_mgt (user_mgt_app , 200 , json .dumps (response ))
318
+
319
+ provider_config = next (iterator )
320
+ assert provider_config .provider_id == 'oidc.provider2'
321
+ assert len (recorder ) == 1
322
+ req = recorder [0 ]
323
+ assert req .method == 'GET'
324
+ assert req .url == '{0}/oauthIdpConfigs?pageSize=100&pageToken=token' .format (
325
+ USER_MGT_URL_PREFIX )
326
+
327
+ with pytest .raises (StopIteration ):
328
+ next (iterator )
329
+
330
+ def test_list_empty_response (self , user_mgt_app ):
331
+ response = {'oauthIdpConfigs' : []}
332
+ _instrument_provider_mgt (user_mgt_app , 200 , json .dumps (response ))
333
+ page = auth .list_oidc_provider_configs (app = user_mgt_app )
334
+ assert len (page .provider_configs ) == 0
335
+ provider_configs = list (config for config in page .iterate_all ())
336
+ assert len (provider_configs ) == 0
337
+
338
+ def test_list_error (self , user_mgt_app ):
339
+ _instrument_provider_mgt (user_mgt_app , 500 , '{"error":"test"}' )
340
+ with pytest .raises (exceptions .InternalError ) as excinfo :
341
+ auth .list_oidc_provider_configs (app = user_mgt_app )
342
+ assert str (excinfo .value ) == 'Unexpected error response: {"error":"test"}'
343
+
240
344
def test_config_not_found (self , user_mgt_app ):
241
345
_instrument_provider_mgt (user_mgt_app , 500 , CONFIG_NOT_FOUND_RESPONSE )
242
346
@@ -257,6 +361,22 @@ def _assert_provider_config(self, provider_config, want_id='oidc.provider'):
257
361
assert provider_config .issuer == 'https://oidc.com/issuer'
258
362
assert provider_config .client_id == 'CLIENT_ID'
259
363
364
+ def _assert_page (self , page , count = 2 , start = 0 , next_page_token = '' ):
365
+ assert isinstance (page , auth .ListProviderConfigsPage )
366
+ index = start
367
+ assert len (page .provider_configs ) == count
368
+ for provider_config in page .provider_configs :
369
+ self ._assert_provider_config (provider_config , want_id = 'oidc.provider{0}' .format (index ))
370
+ index += 1
371
+
372
+ if next_page_token :
373
+ assert page .next_page_token == next_page_token
374
+ assert page .has_next_page is True
375
+ else :
376
+ assert page .next_page_token == ''
377
+ assert page .has_next_page is False
378
+ assert page .get_next_page () is None
379
+
260
380
261
381
class TestSAMLProviderConfig :
262
382
@@ -497,7 +617,7 @@ def test_list_single_page(self, user_mgt_app):
497
617
498
618
def test_list_multiple_pages (self , user_mgt_app ):
499
619
sample_response = json .loads (SAML_PROVIDER_CONFIG_RESPONSE )
500
- configs = self . _create_list_response (sample_response )
620
+ configs = _create_list_response (sample_response )
501
621
502
622
# Page 1
503
623
response = {
@@ -527,7 +647,7 @@ def test_list_multiple_pages(self, user_mgt_app):
527
647
528
648
def test_paged_iteration (self , user_mgt_app ):
529
649
sample_response = json .loads (SAML_PROVIDER_CONFIG_RESPONSE )
530
- configs = self . _create_list_response (sample_response )
650
+ configs = _create_list_response (sample_response )
531
651
532
652
# Page 1
533
653
response = {
@@ -602,10 +722,11 @@ def _assert_page(self, page, count=2, start=0, next_page_token=''):
602
722
assert page .has_next_page is False
603
723
assert page .get_next_page () is None
604
724
605
- def _create_list_response (self , sample_response , count = 3 ):
606
- configs = []
607
- for idx in range (count ):
608
- config = dict (sample_response )
609
- config ['name' ] += str (idx )
610
- configs .append (config )
611
- return configs
725
+
726
+ def _create_list_response (sample_response , count = 3 ):
727
+ configs = []
728
+ for idx in range (count ):
729
+ config = dict (sample_response )
730
+ config ['name' ] += str (idx )
731
+ configs .append (config )
732
+ return configs
0 commit comments