Skip to content

Commit 45fad67

Browse files
committed
unit tests
1 parent 5435b2b commit 45fad67

2 files changed

Lines changed: 185 additions & 14 deletions

File tree

src/mas/devops/users.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ def check_user_sync(self, user_id, application_id, timeout_secs=60 * 10, retry_i
490490
time.sleep(retry_interval_secs)
491491
raise Exception(f"User {user_id} sync failed to complete for app within {timeout_secs} seconds")
492492

493-
def resync_user(self, user_ids):
493+
def resync_users(self, user_ids):
494494
self.logger.info(f"Issuing resync request(s) for user(s) {user_ids}")
495495

496496
# The "/v3/users/utils/resync" API is only available in MAS Core >= 9.1 (coreapi >= 25.2.3)

test/src/test_users.py

Lines changed: 184 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def mock_get_user(requests_mock, user_id, json, status_code):
8585

8686
def mock_get_user_200(requests_mock, user_id):
8787
return mock_get_user(
88-
requests_mock, user_id, {"id": user_id}, 200
88+
requests_mock, user_id, {"id": user_id, "displayName": user_id}, 200
8989
)
9090

9191

@@ -104,7 +104,7 @@ def mock_get_user_500(requests_mock, user_id):
104104
def test_get_user_exists(user_utils, requests_mock):
105105
user_id = "user1"
106106
get = mock_get_user_200(requests_mock, user_id)
107-
assert user_utils.get_user(user_id) == {"id": user_id}
107+
assert user_utils.get_user(user_id) == {"id": user_id, "displayName": user_id}
108108
assert get.call_count == 1
109109

110110

@@ -139,7 +139,7 @@ def test_get_or_create_user_exists(user_utils, requests_mock):
139139
status_code=201
140140
)
141141

142-
assert user_utils.get_or_create_user({"id": user_id}) == {"id": user_id}
142+
assert user_utils.get_or_create_user({"id": user_id}) == {"id": user_id, "displayName": user_id}
143143
assert get.call_count == 1
144144
assert post.call_count == 0
145145

@@ -151,11 +151,11 @@ def test_get_or_create_user_notfound(user_utils, requests_mock):
151151
post = requests_mock.post(
152152
f"{MAS_API_URL}/v3/users",
153153
request_headers={"x-access-token": TOKEN},
154-
json={"id": user_id},
154+
json={"id": user_id, "displayName": user_id},
155155
status_code=201
156156
)
157157

158-
assert user_utils.get_or_create_user({"id": user_id}) == {"id": user_id}
158+
assert user_utils.get_or_create_user({"id": user_id}) == {"id": user_id, "displayName": user_id}
159159
assert get.call_count == 1
160160
assert post.call_count == 1
161161

@@ -469,6 +469,32 @@ def test_set_user_application_permissions_alreadyset(user_utils, requests_mock):
469469
assert put.call_count == 0
470470

471471

472+
def test_resync_users(user_utils, requests_mock):
473+
user_ids = ["user1", "user2"]
474+
475+
gets = []
476+
patches = []
477+
for user_id in user_ids:
478+
gets.append(mock_get_user_200(requests_mock, user_id))
479+
480+
patches.append(
481+
requests_mock.patch(
482+
f"{MAS_API_URL}/v3/users/{user_id}",
483+
request_headers={"x-access-token": TOKEN},
484+
json={"id": user_id},
485+
status_code=200
486+
)
487+
)
488+
489+
user_utils.resync_users(user_ids)
490+
491+
for get in gets:
492+
assert get.call_count == 1
493+
494+
for patche in patches:
495+
assert patche.call_count == 1
496+
497+
472498
def test_check_user_sync(user_utils, requests_mock):
473499
user_id = "user1"
474500
application_id = "manage"
@@ -491,7 +517,7 @@ def json_callback(request, context):
491517
"state": "ERROR"
492518
}
493519
},
494-
"manage": {
520+
application_id: {
495521
"sync": {
496522
"state": state
497523
}
@@ -525,7 +551,7 @@ def test_check_user_sync_timeout(user_utils, requests_mock):
525551
"state": "ERROR"
526552
}
527553
},
528-
"manage": {
554+
application_id: {
529555
"sync": {
530556
"state": "PENDING"
531557
}
@@ -541,15 +567,160 @@ def test_check_user_sync_timeout(user_utils, requests_mock):
541567

542568

543569
def test_check_user_sync_appstate_notfound(user_utils, requests_mock):
544-
pass
545-
# TODO
570+
user_id = "user1"
571+
application_id = "manage"
572+
573+
# first call (made bvy check_user_sync) returns user record with missing sync status for app
574+
# subsequent calls will include sync status and so should succeed
575+
# a single resync should have been triggered
576+
attempts = 0
577+
578+
def json_callback(request, context):
579+
nonlocal attempts
580+
if attempts >= 1:
581+
ret = {
582+
"id": user_id,
583+
"displayName": user_id,
584+
"applications": {
585+
"other": {
586+
"sync": {
587+
"state": "ERROR"
588+
}
589+
},
590+
application_id: {
591+
"sync": {
592+
"state": "SUCCESS"
593+
}
594+
}
595+
}
596+
}
597+
else:
598+
ret = {
599+
"id": user_id,
600+
"displayName": user_id,
601+
"applications": {
602+
"other": {
603+
"sync": {
604+
"state": "ERROR"
605+
}
606+
},
607+
}
608+
}
609+
attempts = attempts + 1
610+
return ret
611+
612+
patch = requests_mock.patch(
613+
f"{MAS_API_URL}/v3/users/{user_id}",
614+
request_headers={"x-access-token": TOKEN},
615+
json={"id": user_id},
616+
status_code=200
617+
)
618+
619+
get = mock_get_user(
620+
requests_mock,
621+
user_id,
622+
json_callback,
623+
200
624+
)
625+
626+
user_utils.check_user_sync(user_id, application_id, timeout_secs=8, retry_interval_secs=0)
627+
assert get.call_count == 3
628+
629+
# a single resync should have been triggered
630+
assert patch.call_count == 1
546631

547632

548633
def test_check_user_sync_appstate_transient_error(user_utils, requests_mock):
549-
pass
550-
# TODO
634+
user_id = "user1"
635+
application_id = "manage"
636+
637+
# first call (made bvy check_user_sync) returns user record with sync state error
638+
# subsequent calls will have successful sync state and so should succeed
639+
# a single resync should have been triggered
640+
attempts = 0
641+
642+
def json_callback(request, context):
643+
nonlocal attempts
644+
if attempts >= 1:
645+
ret = {
646+
"id": user_id,
647+
"displayName": user_id,
648+
"applications": {
649+
application_id: {
650+
"sync": {
651+
"state": "SUCCESS"
652+
}
653+
}
654+
}
655+
}
656+
else:
657+
ret = {
658+
"id": user_id,
659+
"displayName": user_id,
660+
"applications": {
661+
application_id: {
662+
"sync": {
663+
"state": "ERROR"
664+
}
665+
}
666+
}
667+
}
668+
attempts = attempts + 1
669+
return ret
670+
671+
patche = requests_mock.patch(
672+
f"{MAS_API_URL}/v3/users/{user_id}",
673+
request_headers={"x-access-token": TOKEN},
674+
json={"id": user_id},
675+
status_code=200
676+
)
677+
678+
get = mock_get_user(
679+
requests_mock,
680+
user_id,
681+
json_callback,
682+
200
683+
)
684+
685+
user_utils.check_user_sync(user_id, application_id, timeout_secs=8, retry_interval_secs=0)
686+
assert get.call_count == 3
687+
688+
# a single resync should have been triggered
689+
assert patche.call_count == 1
551690

552691

553692
def test_check_user_sync_appstate_persistent_error(user_utils, requests_mock):
554-
pass
555-
# TODO
693+
user_id = "user1"
694+
application_id = "manage"
695+
696+
patche = requests_mock.patch(
697+
f"{MAS_API_URL}/v3/users/{user_id}",
698+
request_headers={"x-access-token": TOKEN},
699+
json={"id": user_id},
700+
status_code=200
701+
)
702+
703+
get = mock_get_user(
704+
requests_mock,
705+
user_id,
706+
{
707+
"id": user_id,
708+
"displayName": user_id,
709+
"applications": {
710+
application_id: {
711+
"sync": {
712+
"state": "ERROR"
713+
}
714+
}
715+
}
716+
},
717+
200
718+
)
719+
720+
with pytest.raises(Exception) as excinfo:
721+
user_utils.check_user_sync(user_id, application_id, timeout_secs=0.3, retry_interval_secs=0.05)
722+
assert str(excinfo.value) == f"User {user_id} sync failed to complete for app within {0.3} seconds"
723+
assert get.call_count > 1
724+
725+
# an "update_user_display_name" should have been triggered for every 2 get calls (1 call by check_user_sync, 1 by resync)
726+
assert patche.call_count == get.call_count / 2

0 commit comments

Comments
 (0)