@@ -130,6 +130,33 @@ def user_utils(mock_v1_secrets, mock_logininitial_endpoint, mock_named_temporary
130130 yield user_utils
131131
132132
133+ @fixture
134+ def mock_manage_api_key (requests_mock ):
135+ '''
136+ Setup mock Manage APIs for setting up an API Key
137+ '''
138+ user_id = "user1"
139+ apikey = {"userid" : user_id , "href" : f"https://{ MANAGE_API_URL } /maximo/api/os/mxapikey/theapikeyid" }
140+
141+ requests_mock .post (
142+ f"{ MANAGE_API_URL } /maximo/api/os/mxapiapikey?ccm=1&lean=1" ,
143+ request_headers = {"content-type" : "application/json" },
144+ json = {"id" : user_id },
145+ status_code = 201 ,
146+ additional_matcher = lambda req : additional_matcher (req , json = {"expiration" : - 1 , "userid" : user_id }, cert = PEM_PATH )
147+ )
148+
149+ requests_mock .get (
150+ f"{ MANAGE_API_URL } /maximo/api/os/mxapiapikey?ccm=1&lean=1&oslc.select=*&oslc.where=userid=\" { user_id } \" " ,
151+ request_headers = {"accept" : "application/json" },
152+ json = {"member" : [apikey ]},
153+ status_code = 200 ,
154+ additional_matcher = lambda req : additional_matcher (req , cert = PEM_PATH )
155+ )
156+
157+ yield apikey
158+
159+
133160def test_admin_internal_ca_pem_file_path (user_utils , mock_named_temporary_file , mock_atexit ):
134161 assert str (user_utils .admin_internal_ca_pem_file_path ) == PEM_PATH
135162 assert mock_named_temporary_file .mock_calls == [call .write (ADMINDASHBOARD_CA_CRT .encode ()), call .flush (), call .close ()]
@@ -1119,8 +1146,55 @@ def test_delete_manage_api_key_error(user_utils, requests_mock):
11191146
11201147
11211148def test_get_manage_group_id (user_utils , requests_mock ):
1122- pass
1123- # TODO
1149+ user_id = "user1"
1150+ apikey = {"userid" : user_id , "apikey" : "342fwasdasd" , "href" : f"https://{ MANAGE_API_URL } /maximo/api/os/mxapikey/theapikeyid" } # pragma: allowlist secret
1151+ group_name = "thegroup"
1152+ group_id = "39231234"
1153+
1154+ get = requests_mock .get (
1155+ 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" },
1157+ json = {"member" : [{"maxgroupid" : group_id }]},
1158+ status_code = 200 ,
1159+ additional_matcher = lambda req : additional_matcher (req )
1160+ )
1161+
1162+ assert user_utils .get_manage_group_id (group_name , apikey ) == group_id
1163+ assert get .call_count == 1
1164+
1165+
1166+ def test_get_manage_group_id_error (user_utils , requests_mock ):
1167+ user_id = "user1"
1168+ apikey = {"userid" : user_id , "apikey" : "342fwasdasd" , "href" : f"https://{ MANAGE_API_URL } /maximo/api/os/mxapikey/theapikeyid" } # pragma: allowlist secret
1169+ group_name = "thegroup"
1170+
1171+ get = requests_mock .get (
1172+ 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" },
1174+ text = "boom" ,
1175+ status_code = 500 ,
1176+ additional_matcher = lambda req : additional_matcher (req )
1177+ )
1178+ with pytest .raises (Exception ) as excinfo :
1179+ user_utils .get_manage_group_id (group_name , apikey )
1180+ assert str (excinfo .value ) == "500 boom"
1181+ assert get .call_count == 1
1182+
1183+
1184+ def test_get_manage_group_id_notfound (user_utils , requests_mock ):
1185+ user_id = "user1"
1186+ apikey = {"userid" : user_id , "apikey" : "342fwasdasd" , "href" : f"https://{ MANAGE_API_URL } /maximo/api/os/mxapikey/theapikeyid" } # pragma: allowlist secret
1187+ group_name = "thegroup"
1188+
1189+ get = requests_mock .get (
1190+ 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" },
1192+ json = {"member" : [{}]},
1193+ status_code = 200 ,
1194+ additional_matcher = lambda req : additional_matcher (req )
1195+ )
1196+ assert user_utils .get_manage_group_id (group_name , apikey ) is None
1197+ assert get .call_count == 1
11241198
11251199
11261200def test_is_user_in_manage_group (user_utils , requests_mock ):
0 commit comments