From f46f2b12df14704f56d1ebaaee453a693d2d5d9c Mon Sep 17 00:00:00 2001 From: acarriedev Date: Tue, 28 Jan 2025 16:16:22 +0000 Subject: [PATCH 1/5] EM-1514 Uncomment product ID tests --- tests/test_endpoints.py | 57 ++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index a2a494d..25ab9f3 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -103,35 +103,34 @@ def test_post_event( 403, "Unauthorised", ), - # UNCOMMENT AFTER BACKEND CHECKS FOR NHSE-Product-ID HEADER - # ( - # [ - # { - # "name": "permissions", - # "value": "events:create:mns-test-signal-1", - # }, - # { - # "name": "product-device-id", - # "value": str(uuid.uuid4()), - # }, - # ], - # 500, - # "Missing required attribute 'product-id' on calling client application", - # ), - # ( - # [ - # { - # "name": "permissions", - # "value": "events:create:mns-test-signal-1", - # }, - # { - # "name": "product-id", - # "value": "P-MNS-TST", - # }, - # ], - # 500, - # "Missing required attribute 'product-device-id' on calling client application", - # ), + ( + [ + { + "name": "permissions", + "value": "events:create:mns-test-signal-1", + }, + { + "name": "product-device-id", + "value": str(uuid.uuid4()), + }, + ], + 500, + "Missing required attribute 'product-id' on calling client application", + ), + ( + [ + { + "name": "permissions", + "value": "events:create:mns-test-signal-1", + }, + { + "name": "product-id", + "value": "P-MNS-TST", + }, + ], + 500, + "Missing required attribute 'product-device-id' on calling client application", + ), ] ) @pytest.mark.nhsd_apim_authorization({"access": "application", "level": "level3"}) From e579257e62a365daf257ea5ec56b48fdd0619817 Mon Sep 17 00:00:00 2001 From: acarriedev Date: Tue, 28 Jan 2025 16:37:41 +0000 Subject: [PATCH 2/5] EM-1514 Check error message --- tests/test_endpoints.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index 25ab9f3..2817c1d 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -168,5 +168,5 @@ def test_missing_required_target_attributes( json=mns_test_signal_event ) - assert resp.status_code == expected_status_code + assert resp.status_code == expected_status_code, resp.text assert resp.json() == {"errors": expected_error_message} From 94b6a880cb643ffd85eb63a32952fbe48f508919 Mon Sep 17 00:00:00 2001 From: acarriedev Date: Wed, 29 Jan 2025 09:25:11 +0000 Subject: [PATCH 3/5] EM-1514 Retry on tests --- tests/test_endpoints.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index 2817c1d..9f6af0d 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -161,12 +161,21 @@ def test_missing_required_target_attributes( # Wait 5 seconds as apigee can take time to register update sleep(5) + retries = 0 - resp = requests.post( - f"{nhsd_apim_proxy_url}/events", - headers=nhsd_apim_auth_headers, - json=mns_test_signal_event - ) + while retries < 5: + resp = requests.post( + f"{nhsd_apim_proxy_url}/events", + headers=nhsd_apim_auth_headers, + json=mns_test_signal_event + ) + + if resp.status_code != expected_status_code: + retries = retries + 1 + sleep(1) + continue + + break assert resp.status_code == expected_status_code, resp.text assert resp.json() == {"errors": expected_error_message} From 5231035a241eee0e8d80782100f6f3a468b65dcf Mon Sep 17 00:00:00 2001 From: acarriedev Date: Wed, 29 Jan 2025 14:11:45 +0000 Subject: [PATCH 4/5] EM-1514 Correct status code for missing product id and device id --- tests/test_endpoints.py | 95 +++++++++++------------------------------ 1 file changed, 26 insertions(+), 69 deletions(-) diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index 9f6af0d..05c3a84 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -29,66 +29,26 @@ def test_api_not_accessible_by_api_key(proxy_path, nhsd_apim_proxy_url, nhsd_api assert resp.status_code == 401 -@pytest.mark.nhsd_apim_authorization({"access": "application", "level": "level3"}) -def test_post_event( - nhsd_apim_proxy_url, - nhsd_apim_auth_headers, - mns_test_signal_event, - _apigee_app_base_url, - _create_test_app -): - # Update client app via Apigee with necessary custom attributes - created_test_app_name = _create_test_app["name"] - apigee_update_url = f"{_apigee_app_base_url}/{created_test_app_name}" - attributes_to_update = { - "attributes": [ - { - "name": "permissions", - "value": "events:create:mns-test-signal-1" - }, - { - "name": "product-id", - "value": "P-MNS-TST" - }, - { - "name": "product-device-id", - "value": str(uuid.uuid4()) - }, - ] - } - - update_response = requests.put( - f"{apigee_update_url}", - json=attributes_to_update, - headers={"Authorization": f"Bearer {os.environ['APIGEE_ACCESS_TOKEN']}"} - ) - update_response.raise_for_status() - - # POST event to /events endpoint - nhsd_apim_auth_headers["X-Correlation-ID"] = f"apim-smoketests-{uuid.uuid4()}" - nhsd_apim_auth_headers["Content-Type"] = "application/cloudevents+json" - retries = 0 - - while retries < 5: - resp = requests.post( - f"{nhsd_apim_proxy_url}/events", - headers=nhsd_apim_auth_headers, - json=mns_test_signal_event - ) - - if resp.status_code == 403: - retries = retries + 1 - continue - - break - - assert resp.status_code == 200 - assert resp.json() == {"id": mns_test_signal_event["id"]} - - @pytest.mark.parametrize( - "attributes_to_update, expected_status_code, expected_error_message", + "attributes_to_update, expected_status_code, expected_error", [ + ( + [ + { + "name": "permissions", + "value": "events:create:mns-test-signal-1" + }, + { + "name": "product-id", + "value": "P-MNS-TST" + }, + { + "name": "product-device-id", + "value": str(uuid.uuid4()) + }, + ], + 200, + ), ( [ { @@ -101,7 +61,7 @@ def test_post_event( }, ], 403, - "Unauthorised", + {"errors": "Unauthorised"}, ), ( [ @@ -114,8 +74,8 @@ def test_post_event( "value": str(uuid.uuid4()), }, ], - 500, - "Missing required attribute 'product-id' on calling client application", + 502, + {"message": "Internal server error"}, ), ( [ @@ -128,13 +88,13 @@ def test_post_event( "value": "P-MNS-TST", }, ], - 500, - "Missing required attribute 'product-device-id' on calling client application", + 502, + {"message": "Internal server error"}, ), ] ) @pytest.mark.nhsd_apim_authorization({"access": "application", "level": "level3"}) -def test_missing_required_target_attributes( +def test_post_event( nhsd_apim_proxy_url, nhsd_apim_auth_headers, mns_test_signal_event, @@ -142,7 +102,7 @@ def test_missing_required_target_attributes( _create_test_app, attributes_to_update, expected_status_code, - expected_error_message + expected_error ): # Update client app via Apigee with necessary custom attributes created_test_app_name = _create_test_app["name"] @@ -159,10 +119,7 @@ def test_missing_required_target_attributes( nhsd_apim_auth_headers["X-Correlation-ID"] = f"apim-smoketests-{uuid.uuid4()}" nhsd_apim_auth_headers["Content-Type"] = "application/cloudevents+json" - # Wait 5 seconds as apigee can take time to register update - sleep(5) retries = 0 - while retries < 5: resp = requests.post( f"{nhsd_apim_proxy_url}/events", @@ -178,4 +135,4 @@ def test_missing_required_target_attributes( break assert resp.status_code == expected_status_code, resp.text - assert resp.json() == {"errors": expected_error_message} + assert resp.json() == expected_error if expected_error else {"id": mns_test_signal_event["id"]} From a3be25e2609c4132ddab410ea9bf0dab463fe238 Mon Sep 17 00:00:00 2001 From: acarriedev Date: Wed, 29 Jan 2025 14:28:56 +0000 Subject: [PATCH 5/5] EM-1514 Add back in None value --- tests/test_endpoints.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index 05c3a84..a4a502f 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -48,6 +48,7 @@ def test_api_not_accessible_by_api_key(proxy_path, nhsd_apim_proxy_url, nhsd_api }, ], 200, + None, ), ( [