|
36 | 36 |
|
37 | 37 | ADMIN_DASHBOARD_PORT = 1 |
38 | 38 | COREAPI_PORT = 2 |
| 39 | +MANAGE_API_PORT = 3 |
39 | 40 |
|
40 | 41 | MAS_ADMIN_URL = f"https://admin-dashboard.{MAS_CORE_NAMESPACE}.svc.cluster.local:{ADMIN_DASHBOARD_PORT}" |
41 | 42 | MAS_API_URL = f'https://coreapi.{MAS_CORE_NAMESPACE}.svc.cluster.local:{COREAPI_PORT}' |
| 43 | +MANAGE_API_URL = f'https://{MAS_INSTANCE_ID}-{MAS_WORKSPACE_ID}.{MANAGE_NAMESPACE}.svc.cluster.local:{MANAGE_API_PORT}' |
42 | 44 |
|
43 | 45 | PEM_PATH = "pempath" |
44 | 46 |
|
45 | 47 |
|
46 | | -def additional_matcher(req, json=None, verify=PEM_PATH): |
| 48 | +def additional_matcher(req, json=None, verify=PEM_PATH, cert=None): |
47 | 49 | if json is not None: |
48 | 50 | assert req.json() == json |
49 | 51 | assert req.verify == verify |
| 52 | + assert req.cert == cert |
50 | 53 | return True |
51 | 54 |
|
52 | 55 |
|
@@ -120,7 +123,8 @@ def user_utils(mock_v1_secrets, mock_logininitial_endpoint, mock_named_temporary |
120 | 123 | MAS_WORKSPACE_ID, |
121 | 124 | k8s_client, |
122 | 125 | coreapi_port=COREAPI_PORT, |
123 | | - admin_dashboard_port=ADMIN_DASHBOARD_PORT |
| 126 | + admin_dashboard_port=ADMIN_DASHBOARD_PORT, |
| 127 | + manage_api_port=MANAGE_API_PORT |
124 | 128 | ) |
125 | 129 |
|
126 | 130 | yield user_utils |
@@ -905,14 +909,141 @@ def test_check_user_sync_appstate_persistent_error(user_utils, requests_mock): |
905 | 909 | assert patche.call_count == get.call_count / 2 |
906 | 910 |
|
907 | 911 |
|
908 | | -def test_create_or_get_manage_api_key_for_user(user_utils, requests_mock): |
909 | | - pass |
910 | | - # TODO |
| 912 | +def test_get_manage_api_key_for_user_exists(user_utils, requests_mock): |
| 913 | + user_id = "user1" |
| 914 | + apikey = {"userid": user_id, "href": f"https://{MANAGE_API_URL}/maximo/api/os/mxapikey/theapikeyid"} |
911 | 915 |
|
| 916 | + get = requests_mock.get( |
| 917 | + f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey?ccm=1&lean=1&oslc.select=*&oslc.where=userid=\"{user_id}\"", |
| 918 | + request_headers={"accept": "application/json"}, |
| 919 | + json={"member": [apikey]}, |
| 920 | + status_code=200, |
| 921 | + additional_matcher=lambda req: additional_matcher(req, cert=PEM_PATH) |
| 922 | + ) |
912 | 923 |
|
913 | | -def test_get_manage_api_key_for_user(user_utils, requests_mock): |
914 | | - pass |
915 | | - # TODO |
| 924 | + assert user_utils.get_manage_api_key_for_user(user_id) == apikey |
| 925 | + assert get.call_count == 1 |
| 926 | + |
| 927 | + |
| 928 | +def test_get_manage_api_key_for_user_notfound(user_utils, requests_mock): |
| 929 | + user_id = "user1" |
| 930 | + |
| 931 | + get = requests_mock.get( |
| 932 | + f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey?ccm=1&lean=1&oslc.select=*&oslc.where=userid=\"{user_id}\"", |
| 933 | + request_headers={"accept": "application/json"}, |
| 934 | + json={"member": []}, |
| 935 | + status_code=200, |
| 936 | + additional_matcher=lambda req: additional_matcher(req, cert=PEM_PATH) |
| 937 | + ) |
| 938 | + |
| 939 | + assert user_utils.get_manage_api_key_for_user(user_id) is None |
| 940 | + assert get.call_count == 1 |
| 941 | + |
| 942 | + |
| 943 | +def test_get_manage_api_key_for_user_error(user_utils, requests_mock): |
| 944 | + user_id = "user1" |
| 945 | + |
| 946 | + get = requests_mock.get( |
| 947 | + f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey?ccm=1&lean=1&oslc.select=*&oslc.where=userid=\"{user_id}\"", |
| 948 | + request_headers={"accept": "application/json"}, |
| 949 | + text="boom", |
| 950 | + status_code=500, |
| 951 | + additional_matcher=lambda req: additional_matcher(req, cert=PEM_PATH) |
| 952 | + ) |
| 953 | + |
| 954 | + with pytest.raises(Exception) as excinfo: |
| 955 | + user_utils.get_manage_api_key_for_user(user_id) |
| 956 | + assert str(excinfo.value) == "500 boom" |
| 957 | + assert get.call_count == 1 |
| 958 | + |
| 959 | + |
| 960 | +@pytest.mark.parametrize("temporary", [(True), (False)]) |
| 961 | +def test_create_or_get_manage_api_key_for_user_new_api_key(temporary, user_utils, requests_mock, mock_atexit): |
| 962 | + user_id = "user1" |
| 963 | + apikey = {"userid": user_id, "href": f"https://{MANAGE_API_URL}/maximo/api/os/mxapikey/theapikeyid"} |
| 964 | + |
| 965 | + post = requests_mock.post( |
| 966 | + f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey?ccm=1&lean=1", |
| 967 | + request_headers={"content-type": "application/json"}, |
| 968 | + json={"id": user_id}, |
| 969 | + status_code=201, |
| 970 | + additional_matcher=lambda req: additional_matcher(req, json={"expiration": -1, "userid": user_id}, cert=PEM_PATH) |
| 971 | + ) |
| 972 | + |
| 973 | + get = requests_mock.get( |
| 974 | + f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey?ccm=1&lean=1&oslc.select=*&oslc.where=userid=\"{user_id}\"", |
| 975 | + request_headers={"accept": "application/json"}, |
| 976 | + json={"member": [apikey]}, |
| 977 | + status_code=200, |
| 978 | + additional_matcher=lambda req: additional_matcher(req, cert=PEM_PATH) |
| 979 | + ) |
| 980 | + |
| 981 | + assert user_utils.create_or_get_manage_api_key_for_user(user_id, temporary=temporary) == apikey |
| 982 | + assert post.call_count == 1 |
| 983 | + assert get.call_count == 1 |
| 984 | + |
| 985 | + # if temporary, check we registered the exit hook to delete the temporary Manage API Key |
| 986 | + if temporary: |
| 987 | + assert call(user_utils.delete_manage_api_key, apikey) in mock_atexit.mock_calls, "delete_manage_api_key exit hook not registered for temporary api key that we created" |
| 988 | + else: |
| 989 | + assert call(user_utils.delete_manage_api_key, apikey) not in mock_atexit.mock_calls, "delete_manage_api_key exit hook registered unexpectedly for non-temporary api key that we created" |
| 990 | + |
| 991 | + |
| 992 | +@pytest.mark.parametrize("temporary", [(True), (False)]) |
| 993 | +def test_create_or_get_manage_api_key_for_user_existing_api_key(temporary, user_utils, requests_mock, mock_atexit): |
| 994 | + user_id = "user1" |
| 995 | + apikey = {"userid": user_id, "href": f"https://{MANAGE_API_URL}/maximo/api/os/mxapikey/theapikeyid"} |
| 996 | + |
| 997 | + post = requests_mock.post( |
| 998 | + f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey?ccm=1&lean=1", |
| 999 | + request_headers={"content-type": "application/json"}, |
| 1000 | + json={"Error": {"reasonCode": "BMXAA10051E"}}, |
| 1001 | + status_code=400, |
| 1002 | + additional_matcher=lambda req: additional_matcher(req, json={"expiration": -1, "userid": user_id}, cert=PEM_PATH) |
| 1003 | + ) |
| 1004 | + |
| 1005 | + get = requests_mock.get( |
| 1006 | + f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey?ccm=1&lean=1&oslc.select=*&oslc.where=userid=\"{user_id}\"", |
| 1007 | + request_headers={"accept": "application/json"}, |
| 1008 | + json={"member": [apikey]}, |
| 1009 | + status_code=200, |
| 1010 | + additional_matcher=lambda req: additional_matcher(req, cert=PEM_PATH) |
| 1011 | + ) |
| 1012 | + |
| 1013 | + assert user_utils.create_or_get_manage_api_key_for_user(user_id, temporary=temporary) == apikey |
| 1014 | + assert post.call_count == 1 |
| 1015 | + assert get.call_count == 1 |
| 1016 | + |
| 1017 | + # even if temporary is set, because we did not create the api key, we should not registered a hook to delete it |
| 1018 | + assert call(user_utils.delete_manage_api_key, apikey) not in mock_atexit.mock_calls, "delete_manage_api_key exit hook registered unexpectedly for existing API Key that we did not create" |
| 1019 | + |
| 1020 | + |
| 1021 | +def test_create_or_get_manage_api_key_for_user_error(user_utils, requests_mock, mock_atexit): |
| 1022 | + user_id = "user1" |
| 1023 | + apikey = {"userid": user_id, "href": f"https://{MANAGE_API_URL}/maximo/api/os/mxapikey/theapikeyid"} |
| 1024 | + |
| 1025 | + post = requests_mock.post( |
| 1026 | + f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey?ccm=1&lean=1", |
| 1027 | + request_headers={"content-type": "application/json"}, |
| 1028 | + text="boom", |
| 1029 | + status_code=400, |
| 1030 | + additional_matcher=lambda req: additional_matcher(req, json={"expiration": -1, "userid": user_id}, cert=PEM_PATH) |
| 1031 | + ) |
| 1032 | + |
| 1033 | + get = requests_mock.get( |
| 1034 | + f"{MANAGE_API_URL}/maximo/api/os/mxapiapikey?ccm=1&lean=1&oslc.select=*&oslc.where=userid=\"{user_id}\"", |
| 1035 | + request_headers={"accept": "application/json"}, |
| 1036 | + json={"member": [apikey]}, |
| 1037 | + status_code=200, |
| 1038 | + additional_matcher=lambda req: additional_matcher(req, cert=PEM_PATH) |
| 1039 | + ) |
| 1040 | + |
| 1041 | + with pytest.raises(Exception) as excinfo: |
| 1042 | + user_utils.create_or_get_manage_api_key_for_user(user_id, temporary=True) |
| 1043 | + assert str(excinfo.value) == "400 boom" |
| 1044 | + assert post.call_count == 1 |
| 1045 | + assert get.call_count == 0 |
| 1046 | + assert call(user_utils.delete_manage_api_key, apikey) not in mock_atexit.mock_calls, "delete_manage_api_key exit hook not registered even though we failed to create the api key" |
916 | 1047 |
|
917 | 1048 |
|
918 | 1049 | def test_delete_manage_api_key(user_utils, requests_mock): |
|
0 commit comments