Skip to content

Commit f6c2cb9

Browse files
committed
unit tests
1 parent dfcb873 commit f6c2cb9

2 files changed

Lines changed: 146 additions & 7 deletions

File tree

src/mas/devops/users.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ def create_or_get_manage_api_key_for_user(self, user_id):
545545
self.logger.info(f"Creating new Manage API Key for user {user_id}")
546546
else:
547547
# any other status code is unexpected
548-
raise Exception(response.text)
548+
raise Exception(f"{response.status_code} {response.text}")
549549

550550
# otherwise, retrieve the apikey (either it already existed, or we just created it)
551551

@@ -562,8 +562,6 @@ def create_or_get_manage_api_key_for_user(self, user_id):
562562
# NOTE: the manage sanity test seems to create maxadmin apikey and not clean it up
563563
atexit.register(self.delete_manage_api_key, apikey)
564564

565-
pass
566-
567565
return apikey
568566

569567
def get_manage_api_key_for_user(self, user_id):
@@ -763,17 +761,16 @@ def get_mas_application_availability(self, mas_application_id):
763761
return response.json()
764762
raise Exception(f"{response.status_code} {response.text}")
765763

766-
def await_mas_application_availability(self, mas_application_id, timeout_secs=60 * 10):
764+
def await_mas_application_availability(self, mas_application_id, timeout_secs=60 * 10, retry_interval_secs=5):
767765
t_end = time.time() + timeout_secs
768766
self.logger.info(f"Waiting for {mas_application_id} to become ready and available: {t_end - time.time():.2f} seconds remaining")
769767
while time.time() < t_end:
770768
app = self.get_mas_application_availability(mas_application_id)
771769
if "available" in app and "ready" in app and app["ready"] and app["available"]:
772770
return
773-
# TODO: error state?
774771
else:
775-
self.logger.info(f"{mas_application_id} is not ready or available, retry in 5 seconds: {t_end - time.time():.2f} seconds remaining")
776-
time.sleep(5)
772+
self.logger.info(f"{mas_application_id} is not ready or available, retry in {retry_interval_secs} seconds: {t_end - time.time():.2f} seconds remaining")
773+
time.sleep(retry_interval_secs)
777774
raise Exception(f"{mas_application_id} did not become ready and available in time, aborting")
778775

779776
def parse_initial_users_from_aws_secret_json(self, secret_json):

test/src/test_users.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,3 +724,145 @@ def test_check_user_sync_appstate_persistent_error(user_utils, requests_mock):
724724

725725
# an "update_user_display_name" should have been triggered for every 2 get calls (1 call by check_user_sync, 1 by resync)
726726
assert patche.call_count == get.call_count / 2
727+
728+
729+
def test_create_or_get_manage_api_key_for_user(user_utils, requests_mock):
730+
pass
731+
# TODO
732+
733+
734+
def test_get_manage_api_key_for_user(user_utils, requests_mock):
735+
pass
736+
# TODO
737+
738+
739+
def test_delete_manage_api_key(user_utils, requests_mock):
740+
pass
741+
# TODO
742+
743+
744+
def test_get_manage_group_id(user_utils, requests_mock):
745+
pass
746+
# TODO
747+
748+
749+
def test_is_user_in_manage_group(user_utils, requests_mock):
750+
pass
751+
# TODO
752+
753+
754+
def test_add_user_to_manage_group(user_utils, requests_mock):
755+
pass
756+
# TODO
757+
758+
759+
def test_get_mas_applications_in_workspace(user_utils, requests_mock):
760+
get = requests_mock.get(
761+
f"{MAS_API_URL}/workspaces/{MAS_WORKSPACE_ID}/applications",
762+
request_headers={"x-access-token": TOKEN},
763+
json=[{"id": "manage"}],
764+
status_code=200
765+
)
766+
assert user_utils.get_mas_applications_in_workspace() == [{"id": "manage"}]
767+
assert get.call_count == 1
768+
769+
770+
def test_get_mas_applications_in_workspace_error(user_utils, requests_mock):
771+
get = requests_mock.get(
772+
f"{MAS_API_URL}/workspaces/{MAS_WORKSPACE_ID}/applications",
773+
request_headers={"x-access-token": TOKEN},
774+
json={"error": "internal"},
775+
status_code=500
776+
)
777+
with pytest.raises(Exception) as excinfo:
778+
user_utils.get_mas_applications_in_workspace()
779+
assert get.call_count == 1
780+
assert str(excinfo.value) == '500 {"error": "internal"}'
781+
782+
783+
def test_get_mas_application_availability(user_utils, requests_mock):
784+
application_id = "manage"
785+
get = requests_mock.get(
786+
f"{MAS_API_URL}/workspaces/{MAS_WORKSPACE_ID}/applications/{application_id}",
787+
request_headers={"x-access-token": TOKEN},
788+
json={"id": "manage"},
789+
status_code=200
790+
)
791+
assert user_utils.get_mas_application_availability(application_id) == {"id": "manage"}
792+
assert get.call_count == 1
793+
794+
795+
def test_get_mas_application_availability_error(user_utils, requests_mock):
796+
application_id = "manage"
797+
get = requests_mock.get(
798+
f"{MAS_API_URL}/workspaces/{MAS_WORKSPACE_ID}/applications/{application_id}",
799+
request_headers={"x-access-token": TOKEN},
800+
json={"error": "internal"},
801+
status_code=500
802+
)
803+
with pytest.raises(Exception) as excinfo:
804+
user_utils.get_mas_application_availability(application_id)
805+
assert get.call_count == 1
806+
assert str(excinfo.value) == '500 {"error": "internal"}'
807+
808+
809+
def test_await_mas_application_availability(user_utils, requests_mock):
810+
pass
811+
# TODO
812+
813+
814+
def test_parse_initial_users_from_aws_secret_json(user_utils):
815+
816+
actual_initial_users = user_utils.parse_initial_users_from_aws_secret_json(
817+
{
818+
"user1@example.com": "primary,joe,bloggs",
819+
"user2@example.com": " primary , ben , bob ",
820+
"user3@example.com": "secondary ,bill, bibb"
821+
}
822+
)
823+
824+
expected_initial_users = {
825+
"users": {
826+
"primary": [
827+
{
828+
"email": "user1@example.com",
829+
"given_name": "joe",
830+
"family_name": "bloggs"
831+
},
832+
{
833+
"email": "user2@example.com",
834+
"given_name": "ben",
835+
"family_name": "bob"
836+
}
837+
],
838+
"secondary": [
839+
{
840+
"email": "user3@example.com",
841+
"given_name": "bill",
842+
"family_name": "bibb"
843+
}
844+
]
845+
}
846+
}
847+
848+
assert actual_initial_users == expected_initial_users
849+
850+
with pytest.raises(Exception) as excinfo:
851+
user_utils.parse_initial_users_from_aws_secret_json({
852+
"user1@example.com": "primary"
853+
})
854+
assert "Wrong number of CSV values for user1@example.com (expected 3 but got 1)" == str(excinfo.value)
855+
856+
with pytest.raises(Exception) as excinfo:
857+
user_utils.parse_initial_users_from_aws_secret_json({
858+
"user1@example.com": "unknown,x,y"
859+
})
860+
assert "Unknown user type for user1@example.com: unknown" == str(excinfo.value)
861+
862+
863+
def test_create_initial_user_for_saas(user_utils, requests_mock):
864+
pass
865+
866+
867+
def test_create_initial_users_for_saas(user_utils, requests_mock):
868+
pass

0 commit comments

Comments
 (0)