Skip to content

Commit 60a5004

Browse files
committed
unit tests
1 parent caaad9f commit 60a5004

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

src/mas/devops/users.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,9 @@ def delete_manage_api_key(self, manage_api_key):
596596

597597
# extract the apikey's identifier from the href
598598
match = re.search(r'\/maximo\/api\/os\/mxapiapikey\/(.*)', manage_api_key['href'])
599+
if match is None:
600+
raise Exception(f"Could not parse API Key href: {manage_api_key['href']}")
601+
599602
id = match.group(1)
600603

601604
url = f"{self.manage_api_url_internal}/maximo/api/os/mxapiapikey/{id}"
@@ -618,7 +621,7 @@ def delete_manage_api_key(self, manage_api_key):
618621
raise Exception(f"{response.status_code} {response.text}")
619622

620623
def get_manage_group_id(self, group_name, manage_api_key):
621-
self.logger.debug(f"Getting ID for Manage group {group_name}")
624+
self.logger.debug(f"Getting ID for Manage group with name {group_name}")
622625
url = f"{self.manage_api_url_internal}/maximo/api/os/mxapigroup"
623626
querystring = {
624627
"ccm": 1,
@@ -637,7 +640,7 @@ def get_manage_group_id(self, group_name, manage_api_key):
637640
verify=self.manage_internal_ca_pem_file_path,
638641
)
639642
if response.status_code != 200:
640-
raise Exception(response.text)
643+
raise Exception(f"{response.status_code} {response.text}")
641644

642645
json = response.json()
643646

@@ -647,6 +650,7 @@ def get_manage_group_id(self, group_name, manage_api_key):
647650
return None
648651

649652
def is_user_in_manage_group(self, group_name, user_id, manage_api_key):
653+
self.logger.debug(f"Checking if {user_id} is a member of Manage group with name {group_name}")
650654

651655
group_id = self.get_manage_group_id(group_name, manage_api_key)
652656

test/src/test_users.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,60 @@ def test_delete_manage_api_key(user_utils, requests_mock):
10621062
user_utils.delete_manage_api_key(apikey)
10631063
assert delete.call_count == 1
10641064

1065-
# TODO: href_does_not_parse
1065+
1066+
def test_delete_manage_api_key_notfound(user_utils, requests_mock):
1067+
user_id = "user1"
1068+
apikey_id = "theapikeyid"
1069+
apikey = {"userid": user_id, "href": f"https://{MANAGE_API_URL}:{MANAGE_API_PORT}/maximo/api/os/mxapiapikey/{apikey_id}"}
1070+
1071+
delete = requests_mock.delete(
1072+
f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey/{apikey_id}?ccm=1&lean=1",
1073+
request_headers={"accept": "application/json"},
1074+
text="notused",
1075+
status_code=404,
1076+
additional_matcher=lambda req: additional_matcher(req, cert=PEM_PATH)
1077+
)
1078+
1079+
user_utils.delete_manage_api_key(apikey)
1080+
assert delete.call_count == 1
1081+
1082+
1083+
def test_delete_manage_api_key_bad_href(user_utils, requests_mock):
1084+
user_id = "user1"
1085+
apikey_id = "theapikeyid"
1086+
apikey = {"userid": user_id, "href": f"notgood/{apikey_id}"}
1087+
1088+
delete = requests_mock.delete(
1089+
f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey/{apikey_id}?ccm=1&lean=1",
1090+
request_headers={"accept": "application/json"},
1091+
text="notused",
1092+
status_code=204,
1093+
additional_matcher=lambda req: additional_matcher(req, cert=PEM_PATH)
1094+
)
1095+
1096+
with pytest.raises(Exception) as excinfo:
1097+
user_utils.delete_manage_api_key(apikey)
1098+
assert str(excinfo.value) == f"Could not parse API Key href: notgood/{apikey_id}"
1099+
assert delete.call_count == 0
1100+
1101+
1102+
def test_delete_manage_api_key_error(user_utils, requests_mock):
1103+
user_id = "user1"
1104+
apikey_id = "theapikeyid"
1105+
apikey = {"userid": user_id, "href": f"https://{MANAGE_API_URL}:{MANAGE_API_PORT}/maximo/api/os/mxapiapikey/{apikey_id}"}
1106+
1107+
delete = requests_mock.delete(
1108+
f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey/{apikey_id}?ccm=1&lean=1",
1109+
request_headers={"accept": "application/json"},
1110+
text="boom",
1111+
status_code=500,
1112+
additional_matcher=lambda req: additional_matcher(req, cert=PEM_PATH)
1113+
)
1114+
1115+
with pytest.raises(Exception) as excinfo:
1116+
user_utils.delete_manage_api_key(apikey)
1117+
assert str(excinfo.value) == "500 boom"
1118+
assert delete.call_count == 1
10661119

10671120

10681121
def test_get_manage_group_id(user_utils, requests_mock):

0 commit comments

Comments
 (0)