Skip to content

Commit ea45197

Browse files
committed
unit tests
1 parent 481a76f commit ea45197

2 files changed

Lines changed: 237 additions & 12 deletions

File tree

src/mas/devops/users.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,17 +711,13 @@ def add_user_to_manage_group(self, user_id, group_name, manage_api_key):
711711
}
712712
]
713713
}
714-
self.logger.debug(f" > {url} {querystring}")
715-
716714
response = requests.post(
717715
url,
718716
headers=headers,
719717
params=querystring,
720718
json=payload,
721719
verify=self.manage_internal_ca_pem_file_path,
722720
)
723-
self.logger.debug(f" < {response.status_code}")
724-
725721
if response.status_code == 204:
726722
return None
727723

test/src/test_users.py

Lines changed: 237 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ def test_get_manage_group_id(user_utils, requests_mock):
11531153

11541154
get = requests_mock.get(
11551155
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup?ccm=1&lean=1&oslc.select=maxgroupid&oslc.where=groupname=\"{group_name}\"",
1156-
request_headers={"accept": "application/json"},
1156+
request_headers={"accept": "application/json", "apikey": apikey["apikey"]},
11571157
json={"member": [{"maxgroupid": group_id}]},
11581158
status_code=200,
11591159
additional_matcher=lambda req: additional_matcher(req)
@@ -1170,7 +1170,7 @@ def test_get_manage_group_id_error(user_utils, requests_mock):
11701170

11711171
get = requests_mock.get(
11721172
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup?ccm=1&lean=1&oslc.select=maxgroupid&oslc.where=groupname=\"{group_name}\"",
1173-
request_headers={"accept": "application/json"},
1173+
request_headers={"accept": "application/json", "apikey": apikey["apikey"]},
11741174
text="boom",
11751175
status_code=500,
11761176
additional_matcher=lambda req: additional_matcher(req)
@@ -1188,7 +1188,7 @@ def test_get_manage_group_id_notfound(user_utils, requests_mock):
11881188

11891189
get = requests_mock.get(
11901190
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup?ccm=1&lean=1&oslc.select=maxgroupid&oslc.where=groupname=\"{group_name}\"",
1191-
request_headers={"accept": "application/json"},
1191+
request_headers={"accept": "application/json", "apikey": apikey["apikey"]},
11921192
json={"member": [{}]},
11931193
status_code=200,
11941194
additional_matcher=lambda req: additional_matcher(req)
@@ -1197,14 +1197,243 @@ def test_get_manage_group_id_notfound(user_utils, requests_mock):
11971197
assert get.call_count == 1
11981198

11991199

1200-
def test_is_user_in_manage_group(user_utils, requests_mock):
1201-
pass
1202-
# TODO
1200+
def test_is_user_in_manage_group_yes(user_utils, requests_mock):
1201+
user_id = "user1"
1202+
apikey = {"userid": user_id, "apikey": "342fwasdasd", "href": f"https://{MANAGE_API_URL}/maximo/api/os/mxapikey/theapikeyid"} # pragma: allowlist secret
1203+
group_name = "thegroup"
1204+
group_id = "39231234"
1205+
1206+
get_group_id = requests_mock.get(
1207+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup?ccm=1&lean=1&oslc.select=maxgroupid&oslc.where=groupname=\"{group_name}\"",
1208+
request_headers={"accept": "application/json"},
1209+
json={"member": [{"maxgroupid": group_id}], "apikey": apikey["apikey"]},
1210+
status_code=200,
1211+
additional_matcher=lambda req: additional_matcher(req)
1212+
)
1213+
1214+
get_group_user = requests_mock.get(
1215+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup/{group_id}/groupuser?lean=1&oslc.where=userid=\"{user_id}\"",
1216+
request_headers={"accept": "application/json", "apikey": apikey["apikey"]},
1217+
json={"member": [{}]}, # <--- member length non-empty indicates that the user is a member of the group
1218+
status_code=200,
1219+
additional_matcher=lambda req: additional_matcher(req)
1220+
)
1221+
1222+
assert user_utils.is_user_in_manage_group(group_name, user_id, apikey)
1223+
assert get_group_id.call_count == 1
1224+
assert get_group_user.call_count == 1
1225+
1226+
1227+
def test_is_user_in_manage_group_no(user_utils, requests_mock):
1228+
user_id = "user1"
1229+
apikey = {"userid": user_id, "apikey": "342fwasdasd", "href": f"https://{MANAGE_API_URL}/maximo/api/os/mxapikey/theapikeyid"} # pragma: allowlist secret
1230+
group_name = "thegroup"
1231+
group_id = "39231234"
1232+
1233+
get_group_id = requests_mock.get(
1234+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup?ccm=1&lean=1&oslc.select=maxgroupid&oslc.where=groupname=\"{group_name}\"",
1235+
request_headers={"accept": "application/json"},
1236+
json={"member": [{"maxgroupid": group_id}], "apikey": apikey["apikey"]},
1237+
status_code=200,
1238+
additional_matcher=lambda req: additional_matcher(req)
1239+
)
1240+
1241+
get_group_user = requests_mock.get(
1242+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup/{group_id}/groupuser?lean=1&oslc.where=userid=\"{user_id}\"",
1243+
request_headers={"accept": "application/json", "apikey": apikey["apikey"]},
1244+
json={"member": []},
1245+
status_code=200,
1246+
additional_matcher=lambda req: additional_matcher(req)
1247+
)
1248+
1249+
assert not user_utils.is_user_in_manage_group(group_name, user_id, apikey)
1250+
assert get_group_id.call_count == 1
1251+
assert get_group_user.call_count == 1
1252+
1253+
1254+
def test_is_user_in_manage_group_error(user_utils, requests_mock):
1255+
user_id = "user1"
1256+
apikey = {"userid": user_id, "apikey": "342fwasdasd", "href": f"https://{MANAGE_API_URL}/maximo/api/os/mxapikey/theapikeyid"} # pragma: allowlist secret
1257+
group_name = "thegroup"
1258+
group_id = "39231234"
1259+
1260+
get_group_id = requests_mock.get(
1261+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup?ccm=1&lean=1&oslc.select=maxgroupid&oslc.where=groupname=\"{group_name}\"",
1262+
request_headers={"accept": "application/json"},
1263+
json={"member": [{"maxgroupid": group_id}], "apikey": apikey["apikey"]},
1264+
status_code=200,
1265+
additional_matcher=lambda req: additional_matcher(req)
1266+
)
1267+
1268+
get_group_user = requests_mock.get(
1269+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup/{group_id}/groupuser?lean=1&oslc.where=userid=\"{user_id}\"",
1270+
request_headers={"accept": "application/json", "apikey": apikey["apikey"]},
1271+
text="boom",
1272+
status_code=500,
1273+
additional_matcher=lambda req: additional_matcher(req)
1274+
)
1275+
1276+
with pytest.raises(Exception) as excinfo:
1277+
user_utils.is_user_in_manage_group(group_name, user_id, apikey)
1278+
assert str(excinfo.value) == "500 boom"
1279+
assert get_group_id.call_count == 1
1280+
assert get_group_user.call_count == 1
1281+
1282+
1283+
def test_is_user_in_manage_group_no_group_found(user_utils, requests_mock):
1284+
user_id = "user1"
1285+
apikey = {"userid": user_id, "apikey": "342fwasdasd", "href": f"https://{MANAGE_API_URL}/maximo/api/os/mxapikey/theapikeyid"} # pragma: allowlist secret
1286+
group_name = "thegroup"
1287+
group_id = "39231234"
1288+
1289+
get_group_id = requests_mock.get(
1290+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup?ccm=1&lean=1&oslc.select=maxgroupid&oslc.where=groupname=\"{group_name}\"",
1291+
request_headers={"accept": "application/json"},
1292+
json={"member": [], "apikey": apikey["apikey"]},
1293+
status_code=200,
1294+
additional_matcher=lambda req: additional_matcher(req)
1295+
)
1296+
1297+
get_group_user = requests_mock.get(
1298+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup/{group_id}/groupuser?lean=1&oslc.where=userid=\"{user_id}\"",
1299+
request_headers={"accept": "application/json", "apikey": apikey["apikey"]},
1300+
text="boom",
1301+
status_code=500,
1302+
additional_matcher=lambda req: additional_matcher(req)
1303+
)
1304+
1305+
with pytest.raises(Exception) as excinfo:
1306+
user_utils.is_user_in_manage_group(group_name, user_id, apikey)
1307+
assert str(excinfo.value) == f"No Manage group found with name {group_name}"
1308+
assert get_group_id.call_count == 1
1309+
assert get_group_user.call_count == 0
12031310

12041311

12051312
def test_add_user_to_manage_group(user_utils, requests_mock):
1206-
pass
1207-
# TODO
1313+
user_id = "user1"
1314+
apikey = {"userid": user_id, "apikey": "342fwasdasd", "href": f"https://{MANAGE_API_URL}/maximo/api/os/mxapikey/theapikeyid"} # pragma: allowlist secret
1315+
group_name = "thegroup"
1316+
group_id = "39231234"
1317+
1318+
get_group_id = requests_mock.get(
1319+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup?ccm=1&lean=1&oslc.select=maxgroupid&oslc.where=groupname=\"{group_name}\"",
1320+
request_headers={"accept": "application/json"},
1321+
json={"member": [{"maxgroupid": group_id}], "apikey": apikey["apikey"]},
1322+
status_code=200,
1323+
additional_matcher=lambda req: additional_matcher(req)
1324+
)
1325+
1326+
get_group_user = requests_mock.get(
1327+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup/{group_id}/groupuser?lean=1",
1328+
request_headers={"accept": "application/json", "apikey": apikey["apikey"]},
1329+
json={"member": []},
1330+
status_code=200,
1331+
additional_matcher=lambda req: additional_matcher(req)
1332+
)
1333+
1334+
add_group_user = requests_mock.post(
1335+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup/{group_id}",
1336+
request_headers={
1337+
"accept": "application/json",
1338+
"content-type": "application/json",
1339+
"x-method-override": "PATCH",
1340+
"patchtype": "MERGE",
1341+
"apikey": apikey["apikey"]
1342+
},
1343+
json={},
1344+
status_code=204,
1345+
additional_matcher=lambda req: additional_matcher(req, json={"groupuser": [{"userid": user_id}]})
1346+
)
1347+
1348+
assert user_utils.add_user_to_manage_group(user_id, group_name, apikey) is None
1349+
assert get_group_id.call_count == 2
1350+
assert get_group_user.call_count == 1
1351+
assert add_group_user.call_count == 1
1352+
1353+
1354+
def test_add_user_to_manage_group_already_member(user_utils, requests_mock):
1355+
user_id = "user1"
1356+
apikey = {"userid": user_id, "apikey": "342fwasdasd", "href": f"https://{MANAGE_API_URL}/maximo/api/os/mxapikey/theapikeyid"} # pragma: allowlist secret
1357+
group_name = "thegroup"
1358+
group_id = "39231234"
1359+
1360+
get_group_id = requests_mock.get(
1361+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup?ccm=1&lean=1&oslc.select=maxgroupid&oslc.where=groupname=\"{group_name}\"",
1362+
request_headers={"accept": "application/json"},
1363+
json={"member": [{"maxgroupid": group_id}], "apikey": apikey["apikey"]},
1364+
status_code=200,
1365+
additional_matcher=lambda req: additional_matcher(req)
1366+
)
1367+
1368+
get_group_user = requests_mock.get(
1369+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup/{group_id}/groupuser?lean=1",
1370+
request_headers={"accept": "application/json", "apikey": apikey["apikey"]},
1371+
json={"member": [{}]}, # <--- member length non-empty indicates that the user is a member of the group
1372+
status_code=200,
1373+
additional_matcher=lambda req: additional_matcher(req)
1374+
)
1375+
1376+
add_group_user = requests_mock.post(
1377+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup/{group_id}",
1378+
request_headers={
1379+
"accept": "application/json",
1380+
"content-type": "application/json",
1381+
"x-method-override": "PATCH",
1382+
"patchtype": "MERGE",
1383+
"apikey": apikey["apikey"]
1384+
},
1385+
json={},
1386+
status_code=204,
1387+
additional_matcher=lambda req: additional_matcher(req, json={"groupuser": [{"userid": user_id}]})
1388+
)
1389+
1390+
assert user_utils.add_user_to_manage_group(user_id, group_name, apikey) is None
1391+
assert get_group_id.call_count == 1
1392+
assert get_group_user.call_count == 1
1393+
assert add_group_user.call_count == 0
1394+
1395+
1396+
def test_add_user_to_manage_group_error(user_utils, requests_mock):
1397+
user_id = "user1"
1398+
apikey = {"userid": user_id, "apikey": "342fwasdasd", "href": f"https://{MANAGE_API_URL}/maximo/api/os/mxapikey/theapikeyid"} # pragma: allowlist secret
1399+
group_name = "thegroup"
1400+
group_id = "39231234"
1401+
1402+
get_group_id = requests_mock.get(
1403+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup?ccm=1&lean=1&oslc.select=maxgroupid&oslc.where=groupname=\"{group_name}\"",
1404+
request_headers={"accept": "application/json"},
1405+
json={"member": [{"maxgroupid": group_id}], "apikey": apikey["apikey"]},
1406+
status_code=200,
1407+
additional_matcher=lambda req: additional_matcher(req)
1408+
)
1409+
1410+
get_group_user = requests_mock.get(
1411+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup/{group_id}/groupuser?lean=1",
1412+
request_headers={"accept": "application/json", "apikey": apikey["apikey"]},
1413+
json={"member": []},
1414+
status_code=200,
1415+
additional_matcher=lambda req: additional_matcher(req)
1416+
)
1417+
1418+
add_group_user = requests_mock.post(
1419+
f"{MANAGE_API_URL}/maximo/api/os/mxapigroup/{group_id}",
1420+
request_headers={
1421+
"accept": "application/json",
1422+
"content-type": "application/json",
1423+
"x-method-override": "PATCH",
1424+
"patchtype": "MERGE",
1425+
"apikey": apikey["apikey"]
1426+
},
1427+
text="boom",
1428+
status_code=500,
1429+
additional_matcher=lambda req: additional_matcher(req, json={"groupuser": [{"userid": user_id}]})
1430+
)
1431+
with pytest.raises(Exception) as excinfo:
1432+
user_utils.add_user_to_manage_group(user_id, group_name, apikey)
1433+
assert str(excinfo.value) == "500 boom"
1434+
assert get_group_id.call_count == 2
1435+
assert get_group_user.call_count == 1
1436+
assert add_group_user.call_count == 1
12081437

12091438

12101439
def test_get_mas_applications_in_workspace(user_utils, requests_mock):

0 commit comments

Comments
 (0)