4343PEM_PATH = "pempath"
4444
4545
46+ def additional_matcher (req , json = None , verify = PEM_PATH ):
47+ if json is not None :
48+ assert req .json () == json
49+ assert req .verify == verify
50+ return True
51+
52+
4653def get_secret (name , namespace ):
4754 if name == f"{ MAS_INSTANCE_ID } -credentials-superuser" :
4855 data = {
@@ -97,15 +104,25 @@ def mock_v1_secrets():
97104
98105
99106@fixture
100- def user_utils (mock_v1_secrets , requests_mock , mock_named_temporary_file , mock_atexit ):
107+ def mock_logininitial_endpoint (requests_mock ):
108+ yield requests_mock .post (
109+ f"{ MAS_ADMIN_URL } /logininitial" ,
110+ json = dict (token = TOKEN ),
111+ additional_matcher = lambda req : additional_matcher (req , json = {"username" : SUPERUSER_USERNAME , "password" : SUPERUSER_PASSWORD })
112+ )
113+
114+
115+ @fixture
116+ def user_utils (mock_v1_secrets , mock_logininitial_endpoint , mock_named_temporary_file , mock_atexit ):
117+ k8s_client = MagicMock () # DynamicClient is mocked out, no methods will be called on the k8s_client
101118 user_utils = MASUserUtils (
102119 MAS_INSTANCE_ID ,
103120 MAS_WORKSPACE_ID ,
104- None ,
121+ k8s_client ,
105122 coreapi_port = COREAPI_PORT ,
106123 admin_dashboard_port = ADMIN_DASHBOARD_PORT
107124 )
108- requests_mock . post ( f" { MAS_ADMIN_URL } /logininitial" , json = dict ( token = TOKEN ))
125+
109126 yield user_utils
110127
111128
@@ -125,7 +142,8 @@ def mock_get_user(requests_mock, user_id, json, status_code):
125142 f"{ MAS_API_URL } /v3/users/{ user_id } " ,
126143 request_headers = {"x-access-token" : TOKEN },
127144 json = json ,
128- status_code = status_code
145+ status_code = status_code ,
146+ additional_matcher = lambda req : additional_matcher (req )
129147 )
130148
131149
@@ -187,9 +205,14 @@ def test_core_internal_ca_pem_file_path(user_utils, mock_named_temporary_file, m
187205 assert mock_atexit .mock_calls == [call (os .remove , PEM_PATH )]
188206
189207
190- def test_superuser_auth_token ():
191- pass
192- # TODO
208+ def test_superuser_auth_token (user_utils , mock_logininitial_endpoint ):
209+ assert mock_logininitial_endpoint .call_count == 0
210+ assert user_utils .superuser_auth_token == TOKEN
211+ assert mock_logininitial_endpoint .call_count == 1
212+
213+ # verify caching
214+ user_utils .superuser_auth_token
215+ assert mock_logininitial_endpoint .call_count == 1
193216
194217
195218def test_manage_internal_tls_secret (user_utils , mock_v1_secrets ):
@@ -266,7 +289,8 @@ def test_get_or_create_user_exists(user_utils, requests_mock):
266289 f"{ MAS_API_URL } /v3/users" ,
267290 request_headers = {"x-access-token" : TOKEN },
268291 json = {"id" : user_id },
269- status_code = 201
292+ status_code = 201 ,
293+ additional_matcher = lambda req : additional_matcher (req , json = {"id" : user_id })
270294 )
271295
272296 assert user_utils .get_or_create_user ({"id" : user_id }) == {"id" : user_id , "displayName" : user_id }
@@ -282,7 +306,8 @@ def test_get_or_create_user_notfound(user_utils, requests_mock):
282306 f"{ MAS_API_URL } /v3/users" ,
283307 request_headers = {"x-access-token" : TOKEN },
284308 json = {"id" : user_id , "displayName" : user_id },
285- status_code = 201
309+ status_code = 201 ,
310+ additional_matcher = lambda req : additional_matcher (req , json = {"id" : user_id })
286311 )
287312
288313 assert user_utils .get_or_create_user ({"id" : user_id }) == {"id" : user_id , "displayName" : user_id }
@@ -297,7 +322,8 @@ def test_get_or_create_user_error(user_utils, requests_mock):
297322 f"{ MAS_API_URL } /v3/users" ,
298323 request_headers = {"x-access-token" : TOKEN },
299324 json = {"error" : "unknown" },
300- status_code = 500
325+ status_code = 500 ,
326+ additional_matcher = lambda req : additional_matcher (req , json = {"id" : user_id })
301327 )
302328
303329 with pytest .raises (Exception ):
@@ -312,7 +338,8 @@ def test_update_user(user_utils, requests_mock):
312338 f"{ MAS_API_URL } /v3/users/{ user_id } " ,
313339 request_headers = {"x-access-token" : TOKEN },
314340 json = {"id" : user_id },
315- status_code = 200
341+ status_code = 200 ,
342+ additional_matcher = lambda req : additional_matcher (req , json = {"id" : user_id })
316343 )
317344 user_utils .update_user ({"id" : user_id })
318345 assert put .call_count == 1
@@ -324,7 +351,8 @@ def test_update_user_error(user_utils, requests_mock):
324351 f"{ MAS_API_URL } /v3/users/{ user_id } " ,
325352 request_headers = {"x-access-token" : TOKEN },
326353 json = {"error" : "nofound" },
327- status_code = 404
354+ status_code = 404 ,
355+ additional_matcher = lambda req : additional_matcher (req , json = {"id" : user_id })
328356 )
329357 with pytest .raises (Exception ):
330358 user_utils .update_user ({"id" : user_id })
@@ -337,7 +365,8 @@ def test_update_user_display_name(user_utils, requests_mock):
337365 f"{ MAS_API_URL } /v3/users/{ user_id } " ,
338366 request_headers = {"x-access-token" : TOKEN },
339367 json = {"id" : user_id },
340- status_code = 200
368+ status_code = 200 ,
369+ additional_matcher = lambda req : additional_matcher (req , json = {"displayName" : "display_name" })
341370 )
342371 user_utils .update_user_display_name (user_id , "display_name" )
343372 assert patche .call_count == 1
@@ -349,7 +378,8 @@ def test_update_user_display_name_error(user_utils, requests_mock):
349378 f"{ MAS_API_URL } /v3/users/{ user_id } " ,
350379 request_headers = {"x-access-token" : TOKEN },
351380 json = {"error" : "notfound" },
352- status_code = 404
381+ status_code = 404 ,
382+ additional_matcher = lambda req : additional_matcher (req , json = {"displayName" : "display_name" })
353383 )
354384 with pytest .raises (Exception ):
355385 user_utils .update_user_display_name (user_id , "display_name" )
@@ -365,7 +395,8 @@ def test_link_user_to_local_idp(user_utils, requests_mock):
365395 f"{ MAS_API_URL } /v3/users/{ user_id } /idps/local?emailPassword={ email_password } " ,
366396 request_headers = {"x-access-token" : TOKEN },
367397 json = {"id" : user_id },
368- status_code = 200
398+ status_code = 200 ,
399+ additional_matcher = lambda req : additional_matcher (req , json = {"idpUserId" : user_id })
369400 )
370401
371402 user_utils .link_user_to_local_idp (user_id , email_password = email_password )
@@ -379,6 +410,7 @@ def test_link_user_to_local_idp_usernotfound(user_utils, requests_mock):
379410 get = mock_get_user_404 (requests_mock , user_id )
380411 put = requests_mock .put (
381412 f"{ MAS_API_URL } /v3/users/{ user_id } /idps/local" ,
413+ additional_matcher = lambda req : additional_matcher (req , json = {"idpUserId" : user_id })
382414 )
383415
384416 with pytest .raises (Exception ):
@@ -399,7 +431,8 @@ def test_link_user_to_local_idp_already_linked(user_utils, requests_mock):
399431 f"{ MAS_API_URL } /v3/users/{ user_id } /idps/local?emailPassword={ email_password } " ,
400432 request_headers = {"x-access-token" : TOKEN },
401433 json = {"identities" : {}},
402- status_code = 200
434+ status_code = 200 ,
435+ additional_matcher = lambda req : additional_matcher (req , json = {"idpUserId" : user_id })
403436 )
404437
405438 user_utils .link_user_to_local_idp (user_id , email_password = email_password )
@@ -414,7 +447,8 @@ def test_get_user_workspaces(user_utils, requests_mock):
414447 f"{ MAS_API_URL } /v3/users/{ user_id } /workspaces" ,
415448 request_headers = {"x-access-token" : TOKEN },
416449 json = [{"id" : "masdev" }],
417- status_code = 200
450+ status_code = 200 ,
451+ additional_matcher = lambda req : additional_matcher (req )
418452 )
419453 workspaces = user_utils .get_user_workspaces (user_id )
420454 assert workspaces == [{"id" : "masdev" }]
@@ -427,7 +461,8 @@ def test_get_user_workspaces_usernotfound(user_utils, requests_mock):
427461 f"{ MAS_API_URL } /v3/users/{ user_id } /workspaces" ,
428462 request_headers = {"x-access-token" : TOKEN },
429463 json = {},
430- status_code = 404
464+ status_code = 404 ,
465+ additional_matcher = lambda req : additional_matcher (req )
431466 )
432467 with pytest .raises (Exception ):
433468 user_utils .get_user_workspaces (user_id )
@@ -440,7 +475,8 @@ def test_get_user_workspaces_error(user_utils, requests_mock):
440475 f"{ MAS_API_URL } /v3/users/{ user_id } /workspaces" ,
441476 request_headers = {"x-access-token" : TOKEN },
442477 json = {"error" : "internal" },
443- status_code = 500
478+ status_code = 500 ,
479+ additional_matcher = lambda req : additional_matcher (req )
444480 )
445481 with pytest .raises (Exception ):
446482 user_utils .get_user_workspaces (user_id )
@@ -453,13 +489,15 @@ def test_add_user_to_workspace_already_a_member(user_utils, requests_mock):
453489 f"{ MAS_API_URL } /v3/users/{ user_id } /workspaces" ,
454490 request_headers = {"x-access-token" : TOKEN },
455491 json = [{"id" : "someotherworkspace" }, {"id" : MAS_WORKSPACE_ID }],
456- status_code = 200
492+ status_code = 200 ,
493+ additional_matcher = lambda req : additional_matcher (req )
457494 )
458- put = requests_mock .get (
495+ put = requests_mock .put (
459496 f"{ MAS_API_URL } /workspaces/{ MAS_WORKSPACE_ID } /users/{ user_id } " ,
460497 request_headers = {"x-access-token" : TOKEN },
461498 json = [{"id" : "masdev" }],
462- status_code = 200
499+ status_code = 200 ,
500+ additional_matcher = lambda req : additional_matcher (req , json = {"permissions" : {"workspaceAdmin" : True }})
463501 )
464502 user_utils .add_user_to_workspace (user_id , is_workspace_admin = True )
465503 assert get .call_count == 1
@@ -478,7 +516,8 @@ def test_add_user_to_workspace(user_utils, requests_mock):
478516 f"{ MAS_API_URL } /workspaces/{ MAS_WORKSPACE_ID } /users/{ user_id } " ,
479517 request_headers = {"x-access-token" : TOKEN },
480518 json = {},
481- status_code = 200
519+ status_code = 200 ,
520+ additional_matcher = lambda req : additional_matcher (req , json = {"permissions" : {"workspaceAdmin" : True }})
482521 )
483522 user_utils .add_user_to_workspace (user_id , is_workspace_admin = True )
484523 assert get .call_count == 1
@@ -497,7 +536,8 @@ def test_add_user_to_workspace_error(user_utils, requests_mock):
497536 f"{ MAS_API_URL } /workspaces/{ MAS_WORKSPACE_ID } /users/{ user_id } " ,
498537 request_headers = {"x-access-token" : TOKEN },
499538 json = {"error" : "internal" },
500- status_code = 500
539+ status_code = 500 ,
540+ additional_matcher = lambda req : additional_matcher (req , json = {"permissions" : {"workspaceAdmin" : True }})
501541 )
502542 with pytest .raises (Exception ):
503543 user_utils .add_user_to_workspace (user_id , is_workspace_admin = True )
@@ -519,7 +559,8 @@ def test_get_user_application_permissions(user_utils, requests_mock):
519559 f"{ MAS_API_URL } /workspaces/{ MAS_WORKSPACE_ID } /applications/{ application_id } /users/{ user_id } " ,
520560 request_headers = {"x-access-token" : TOKEN },
521561 json = response_json ,
522- status_code = 200
562+ status_code = 200 ,
563+ additional_matcher = lambda req : additional_matcher (req )
523564 )
524565 assert user_utils .get_user_application_permissions (user_id , application_id ) == response_json
525566 assert get .call_count == 1
@@ -532,7 +573,8 @@ def test_get_user_application_permissions_notfound(user_utils, requests_mock):
532573 f"{ MAS_API_URL } /workspaces/{ MAS_WORKSPACE_ID } /applications/{ application_id } /users/{ user_id } " ,
533574 request_headers = {"x-access-token" : TOKEN },
534575 json = {"error" : "notfound" },
535- status_code = 404
576+ status_code = 404 ,
577+ additional_matcher = lambda req : additional_matcher (req )
536578 )
537579 assert user_utils .get_user_application_permissions (user_id , application_id ) is None
538580 assert get .call_count == 1
@@ -545,7 +587,8 @@ def test_get_user_application_permissions_error(user_utils, requests_mock):
545587 f"{ MAS_API_URL } /workspaces/{ MAS_WORKSPACE_ID } /applications/{ application_id } /users/{ user_id } " ,
546588 request_headers = {"x-access-token" : TOKEN },
547589 json = {"error" : "internal" },
548- status_code = 500
590+ status_code = 500 ,
591+ additional_matcher = lambda req : additional_matcher (req )
549592 )
550593 with pytest .raises (Exception ):
551594 user_utils .get_user_application_permissions (user_id , application_id )
@@ -559,13 +602,15 @@ def test_set_user_application_permissions(user_utils, requests_mock):
559602 f"{ MAS_API_URL } /workspaces/{ MAS_WORKSPACE_ID } /applications/{ application_id } /users/{ user_id } " ,
560603 request_headers = {"x-access-token" : TOKEN },
561604 json = {"error" : "notfound" },
562- status_code = 404
605+ status_code = 404 ,
606+ additional_matcher = lambda req : additional_matcher (req )
563607 )
564608 put = requests_mock .put (
565609 f"{ MAS_API_URL } /workspaces/{ MAS_WORKSPACE_ID } /applications/{ application_id } /users/{ user_id } " ,
566610 request_headers = {"x-access-token" : TOKEN },
567611 json = {},
568- status_code = 200
612+ status_code = 200 ,
613+ additional_matcher = lambda req : additional_matcher (req , json = {"role" : "USER" })
569614 )
570615 user_utils .set_user_application_permission (user_id , application_id , "USER" )
571616 assert get .call_count == 1
@@ -586,13 +631,15 @@ def test_set_user_application_permissions_alreadyset(user_utils, requests_mock):
586631 f"{ MAS_API_URL } /workspaces/{ MAS_WORKSPACE_ID } /applications/{ application_id } /users/{ user_id } " ,
587632 request_headers = {"x-access-token" : TOKEN },
588633 json = get_response_json ,
589- status_code = 200
634+ status_code = 200 ,
635+ additional_matcher = lambda req : additional_matcher (req )
590636 )
591637 put = requests_mock .put (
592638 f"{ MAS_API_URL } /workspaces/{ MAS_WORKSPACE_ID } /applications/{ application_id } /users/{ user_id } " ,
593639 request_headers = {"x-access-token" : TOKEN },
594640 json = {},
595- status_code = 200
641+ status_code = 200 ,
642+ additional_matcher = lambda req : additional_matcher (req , json = {"role" : "USER" })
596643 )
597644 user_utils .set_user_application_permission (user_id , application_id , "USER" )
598645 assert get .call_count == 1
@@ -612,7 +659,9 @@ def test_resync_users(user_utils, requests_mock):
612659 f"{ MAS_API_URL } /v3/users/{ user_id } " ,
613660 request_headers = {"x-access-token" : TOKEN },
614661 json = {"id" : user_id },
615- status_code = 200
662+ status_code = 200 ,
663+ # uid=user_id captures the current value of user_id during each loop iteration, ensuring that the lambda uses the correct value when it is eventually called.
664+ additional_matcher = lambda req , uid = user_id : additional_matcher (req , json = {"displayName" : uid })
616665 )
617666 )
618667
0 commit comments