|
| 1 | +"""Basic client class for managing interactions with the Apigee API""" |
| 2 | + |
| 3 | +import uuid |
| 4 | + |
| 5 | +import requests |
| 6 | + |
| 7 | +from utilities.apigee.apigee_env_helpers import get_apigee_access_token, get_apigee_username, get_proxy_name |
| 8 | +from utilities.apigee.ApigeeApp import ApigeeApp |
| 9 | + |
| 10 | + |
| 11 | +class ApigeeOnDemandAppManager: |
| 12 | + """Manager class that provides required Apigee functionality for PR env e2e tests. E.g. creating an app, subscribing it to |
| 13 | + products and teardown""" |
| 14 | + |
| 15 | + # We only use the Apigee API in the non-prod organisation and the internal-dev environment |
| 16 | + _BASE_URL = "https://api.enterprise.apigee.com/v1/organizations/nhsd-nonprod" |
| 17 | + _APPS_PATH = "apps" |
| 18 | + _DEVELOPERS_PATH = "developers" |
| 19 | + _PRODUCTS_PATH = "apiproducts" |
| 20 | + _INTERNAL_DEV_ENV_NAME = "internal-dev" |
| 21 | + _TEST_APP_SUPPLIERS = ("EMIS", "MAVIS", "MEDICUS", "Postman_Auth", "RAVS", "SONAR", "TPP") |
| 22 | + |
| 23 | + def __init__(self): |
| 24 | + self.pr_proxy_name = get_proxy_name() |
| 25 | + self.created_product_name_uuid: str = "" |
| 26 | + self.created_app_name_uuids = [] |
| 27 | + self.display_name = f"test-{self.pr_proxy_name}" |
| 28 | + |
| 29 | + self.logged_in_username = get_apigee_username() |
| 30 | + self.access_token = get_apigee_access_token() |
| 31 | + |
| 32 | + self.requests_session = requests.Session() |
| 33 | + self.requests_session.headers.update({"Authorization": f"Bearer {self.access_token}"}) |
| 34 | + |
| 35 | + def _create_app(self, target_product_name: str, supplier_name: str) -> ApigeeApp: |
| 36 | + app_name_uuid = str(uuid.uuid4()) |
| 37 | + app_data = { |
| 38 | + "name": app_name_uuid, |
| 39 | + "callbackUrl": "https://oauth.pstmn.io/v1/callback", |
| 40 | + "status": "approved", |
| 41 | + "attributes": [ |
| 42 | + {"name": "DisplayName", "value": f"{self.display_name}-{supplier_name}"}, |
| 43 | + {"name": "SupplierSystem", "value": supplier_name}, |
| 44 | + ], |
| 45 | + "apiProducts": [target_product_name, "identity-service-internal-dev"], |
| 46 | + } |
| 47 | + |
| 48 | + response = self.requests_session.post( |
| 49 | + url=f"{self._BASE_URL}/{self._DEVELOPERS_PATH}/{self.logged_in_username}/{self._APPS_PATH}", json=app_data |
| 50 | + ) |
| 51 | + response.raise_for_status() |
| 52 | + |
| 53 | + self.created_app_name_uuids.append(app_name_uuid) |
| 54 | + response_dict = response.json() |
| 55 | + |
| 56 | + return ApigeeApp( |
| 57 | + callback_url=response_dict.get("callbackUrl"), |
| 58 | + client_id=response_dict["credentials"][0]["consumerKey"], |
| 59 | + client_secret=response_dict["credentials"][0]["consumerSecret"], |
| 60 | + supplier=supplier_name, |
| 61 | + ) |
| 62 | + |
| 63 | + def _create_product(self) -> str: |
| 64 | + product_name_uuid = str(uuid.uuid4()) |
| 65 | + apigee_product_data = { |
| 66 | + "name": product_name_uuid, |
| 67 | + "apiResources": [], |
| 68 | + "approvalType": "auto", |
| 69 | + "description": "Autogenerated API product for E2E tests", |
| 70 | + "displayName": self.display_name, |
| 71 | + "environments": [self._INTERNAL_DEV_ENV_NAME], |
| 72 | + "proxies": [self.pr_proxy_name], |
| 73 | + "scopes": [ |
| 74 | + f"urn:nhsd:apim:app:level3:{self.pr_proxy_name}", |
| 75 | + f"urn:nhsd:apim:user-nhs-cis2:aal3:{self.pr_proxy_name}", |
| 76 | + ], |
| 77 | + } |
| 78 | + |
| 79 | + response = self.requests_session.post( |
| 80 | + url=f"{self._BASE_URL}/{self._PRODUCTS_PATH}", |
| 81 | + json=apigee_product_data, |
| 82 | + ) |
| 83 | + response.raise_for_status() |
| 84 | + |
| 85 | + self.created_product_name_uuid = product_name_uuid |
| 86 | + return product_name_uuid |
| 87 | + |
| 88 | + def setup_apps_and_product(self) -> list[ApigeeApp]: |
| 89 | + """Orchestration method to setup the required product and on-demand apps required for PR testing""" |
| 90 | + created_apps: list[ApigeeApp] = [] |
| 91 | + product_name_uuid = self._create_product() |
| 92 | + |
| 93 | + for supplier_name in self._TEST_APP_SUPPLIERS: |
| 94 | + created_apps.append(self._create_app(product_name_uuid, supplier_name)) |
| 95 | + |
| 96 | + return created_apps |
| 97 | + |
| 98 | + def teardown_apps_and_product(self): |
| 99 | + """Orchestration method to remove the Apigee resources in a teardown step""" |
| 100 | + for created_app_name_uuid in self.created_app_name_uuids: |
| 101 | + self.requests_session.delete( |
| 102 | + url=f"{self._BASE_URL}/{self._DEVELOPERS_PATH}/{self.logged_in_username}/{self._APPS_PATH}/{created_app_name_uuid}" |
| 103 | + ) |
| 104 | + |
| 105 | + self.requests_session.delete(url=f"{self._BASE_URL}/{self._PRODUCTS_PATH}/{self.created_product_name_uuid}") |
0 commit comments