Skip to content

Commit 6478088

Browse files
committed
Add support for dynamic apps across more environments
1 parent 5382bb2 commit 6478088

6 files changed

Lines changed: 54 additions & 21 deletions

File tree

.github/workflows/run-e2e-automation-tests.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@ on:
4141
- internal-qa
4242
- int
4343
- ref
44-
- prod
4544
environment:
4645
type: string
4746
description: Select the backend environment
4847
options:
4948
- dev
5049
- preprod
51-
- prod
5250
sub_environment:
5351
type: string
5452
description: Set the sub environment name e.g. pr-xxx, or green/blue in higher environments
@@ -71,6 +69,7 @@ on:
7169
- proxy_smoke
7270

7371
env:
72+
APIGEE_AUTH_ENV: ${{ inputs.apigee_environment == 'int' && inputs.apigee_environment || 'internal-dev' }}
7473
APIGEE_ENVIRONMENT: ${{ inputs.apigee_environment }}
7574
ENVIRONMENT: ${{ inputs.environment }}
7675
SUB_ENVIRONMENT: ${{ inputs.sub_environment }}
@@ -211,14 +210,14 @@ jobs:
211210
working-directory: tests/e2e_automation
212211
env:
213212
S3_env: ${{ inputs.sub_environment }}
214-
auth_url: https://${{ inputs.apigee_environment }}.api.service.nhs.uk/oauth2-mock/authorize
215-
token_url: https://${{ inputs.apigee_environment }}.api.service.nhs.uk/oauth2-mock/token
213+
auth_url: https://${{ env.APIGEE_AUTH_ENV }}.api.service.nhs.uk/oauth2-mock/authorize
214+
token_url: https://${{ env.APIGEE_AUTH_ENV }}.api.service.nhs.uk/oauth2-mock/token
216215
baseUrl: https://${{ inputs.apigee_environment }}.api.service.nhs.uk/${{env.SERVICE_BASE_PATH}}
217216
callback_url: "https://oauth.pstmn.io/v1/callback"
218-
Postman_Auth_client_Id: ${{ secrets.Postman_Auth_client_Id }}
219-
Postman_Auth_client_Secret: ${{ secrets.Postman_Auth_client_Secret }}
220217
username: ${{ secrets.CIS2_E2E_USERNAME }}
221218
scope: "nhs-cis2"
219+
Postman_Auth_client_Id: ${{ secrets.Postman_Auth_client_Id }} # The following static app values are only needed in INT
220+
Postman_Auth_client_Secret: ${{ secrets.Postman_Auth_client_Secret }}
222221
RAVS_client_Id: ${{ secrets.RAVS_client_Id }}
223222
RAVS_client_Secret: ${{ secrets.RAVS_client_Secret }}
224223
MAVIS_client_Id: ${{ secrets.MAVIS_client_Id }}

tests/e2e_automation/.env.example.pr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ PROXY_NAME=immunisation-fhir-api-pr-123
2121
# See README for details on how to obtain this
2222
APIGEE_ACCESS_TOKEN={use-the-apigee-get-token-utility}
2323
APIGEE_USERNAME={your-apigee-developer-email}
24+
APIGEE_ENVIRONMENT={the-relevant-apigee-env} # E.g. internal-dev, internal-qa

tests/e2e_automation/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ This directory contains End-to-end Automation Tests for the Immunisation FHIR AP
44

55
## Setting up e2e_automation tests to run locally
66

7+
Update README with the changes to use get_token
8+
79
1. Follow the instructions in the root level README.md to setup the [dependencies](../../README.md#environment-setup) and create a [virtual environment](../../README.md#setting-up-a-virtual-environment-with-poetry) for this folder (`e2e_automation`).
810

911
2. Add values to the .env file.
@@ -62,6 +64,8 @@ This directory contains End-to-end Automation Tests for the Immunisation FHIR AP
6264
6365
## Running e2e_automation tests against PR environments
6466
67+
Need to update this in PR and explain how to use static apps if desired locally.
68+
6569
The environment variables define a client ID and client secret for each of the Apigee test apps we use in static
6670
environments such as `internal-dev`, `internal-qa` and so on.
6771

tests/e2e_automation/features/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from utilities.api_fhir_immunization_helper import empty_folder
88
from utilities.api_gen_token import get_tokens
99
from utilities.api_get_header import get_delete_url_header
10-
from utilities.apigee.apigee_env_helpers import is_pr_env
10+
from utilities.apigee.apigee_env_helpers import use_temp_apigee_apps
1111
from utilities.apigee.ApigeeApp import ApigeeApp
1212
from utilities.apigee.ApigeeOnDemandAppManager import ApigeeOnDemandAppManager
1313
from utilities.aws_token import refresh_sso_token, set_aws_session_token
@@ -58,7 +58,7 @@ def global_context():
5858

5959
@pytest.fixture(scope="session")
6060
def temp_apigee_apps():
61-
if is_pr_env():
61+
if use_temp_apigee_apps():
6262
apigee_app_mgr = ApigeeOnDemandAppManager()
6363
created_apps = apigee_app_mgr.setup_apps_and_product()
6464

tests/e2e_automation/utilities/apigee/ApigeeOnDemandAppManager.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,33 @@
44

55
import requests
66

7-
from utilities.apigee.apigee_env_helpers import get_apigee_access_token, get_apigee_username, get_proxy_name
7+
from utilities.apigee.apigee_env_helpers import (
8+
get_apigee_access_token,
9+
get_apigee_environment,
10+
get_apigee_username,
11+
get_proxy_name,
12+
)
813
from utilities.apigee.ApigeeApp import ApigeeApp
914

1015

1116
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"""
17+
"""Manager class that provides required Apigee functionality for Apigee non-prod env e2e tests. E.g. creating an
18+
app, subscribing it to products and teardown"""
1419

15-
# We only use the Apigee API in the non-prod organisation and the internal-dev environment
20+
# We only use the Apigee API in the non-prod organisation i.e. all environments except INT and PROD
1621
_BASE_URL = "https://api.enterprise.apigee.com/v1/organizations/nhsd-nonprod"
1722
_APPS_PATH = "apps"
1823
_DEVELOPERS_PATH = "developers"
1924
_PRODUCTS_PATH = "apiproducts"
20-
_INTERNAL_DEV_ENV_NAME = "internal-dev"
25+
_INTERNAL_DEV_ID_SERVICE_PRODUCT = "identity-service-internal-dev"
2126
_TEST_APP_SUPPLIERS = ("EMIS", "MAVIS", "MEDICUS", "Postman_Auth", "RAVS", "SONAR", "TPP")
2227

2328
def __init__(self):
24-
self.pr_proxy_name = get_proxy_name()
29+
self.proxy_name = get_proxy_name()
30+
self.apigee_environment = get_apigee_environment()
2531
self.created_product_name_uuid: str = ""
2632
self.created_app_name_uuids = []
27-
self.display_name = f"test-{self.pr_proxy_name}"
33+
self.display_name = f"test-{self.proxy_name}"
2834

2935
self.logged_in_username = get_apigee_username()
3036
self.access_token = get_apigee_access_token()
@@ -42,7 +48,7 @@ def _create_app(self, target_product_name: str, supplier_name: str) -> ApigeeApp
4248
{"name": "DisplayName", "value": f"{self.display_name}-{supplier_name}"},
4349
{"name": "SupplierSystem", "value": supplier_name},
4450
],
45-
"apiProducts": [target_product_name, "identity-service-internal-dev"],
51+
"apiProducts": [target_product_name, self._INTERNAL_DEV_ID_SERVICE_PRODUCT],
4652
}
4753

4854
response = self.requests_session.post(
@@ -68,11 +74,11 @@ def _create_product(self) -> str:
6874
"approvalType": "auto",
6975
"description": "Autogenerated API product for E2E tests",
7076
"displayName": self.display_name,
71-
"environments": [self._INTERNAL_DEV_ENV_NAME],
72-
"proxies": [self.pr_proxy_name],
77+
"environments": [self.apigee_environment],
78+
"proxies": [self.proxy_name],
7379
"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}",
80+
f"urn:nhsd:apim:app:level3:{self.proxy_name}",
81+
f"urn:nhsd:apim:user-nhs-cis2:aal3:{self.proxy_name}",
7682
],
7783
}
7884

tests/e2e_automation/utilities/apigee/apigee_env_helpers.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import os
22

3+
PROXY_PR_PREFIX = "immunisation-fhir-api-pr-"
4+
INT_PROXY_NAME = "immunisation-fhir-api-int"
5+
36

47
def get_env_var(var_name: str) -> str:
58
value = os.getenv(var_name)
@@ -18,11 +21,31 @@ def get_proxy_name() -> str:
1821
return get_env_var("PROXY_NAME")
1922

2023

24+
def use_temp_apigee_apps() -> bool:
25+
"""
26+
Determines if temporary Apigee Apps are required for the test run based on the following business logic:
27+
- dynamic PR environments always require temporary apps
28+
- Apigee non-prod environments (everything except INT and PROD) use dynamic apps unless the user provides the
29+
USE_STATIC_APPS env var to override this
30+
"""
31+
if is_pr_env():
32+
return True
33+
34+
if get_proxy_name() == INT_PROXY_NAME:
35+
return False
36+
37+
return os.getenv("USE_STATIC_APPS", "false") != "true"
38+
39+
2140
def is_pr_env() -> bool:
2241
"""Checks if the tests are running against a dynamic PR environment"""
2342
proxy_name = get_proxy_name()
24-
return proxy_name.startswith("immunisation-fhir-api-pr-")
43+
return proxy_name.startswith(PROXY_PR_PREFIX)
2544

2645

2746
def get_apigee_access_token() -> str:
2847
return get_env_var("APIGEE_ACCESS_TOKEN")
48+
49+
50+
def get_apigee_environment() -> str:
51+
return get_env_var("APIGEE_ENVIRONMENT")

0 commit comments

Comments
 (0)