|
| 1 | +import azure.functions as func |
| 2 | +import dotenv |
| 3 | +import function_app |
| 4 | +import hashlib |
| 5 | +import hmac |
| 6 | +import json |
| 7 | +import os |
| 8 | +import psycopg2 |
| 9 | +import pytest |
| 10 | + |
| 11 | +ENV_FILE = os.getenv("ENV_FILE", ".env.test") |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture() |
| 15 | +def setup(mocker): |
| 16 | + dotenv.load_dotenv(dotenv_path=ENV_FILE) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def callback_request_body(): |
| 21 | + return { |
| 22 | + "data": [ |
| 23 | + { |
| 24 | + "type": "ChannelStatus", |
| 25 | + "attributes": { |
| 26 | + "messageId": "2WL3qFTEFM0qMY8xjRbt1LIKCzM", |
| 27 | + "messageReference": "1642109b-69eb-447f-8f97-ab70a74f5db4", |
| 28 | + "cascadeType": "primary", |
| 29 | + "cascadeOrder": 1, |
| 30 | + "channel": "nhsapp", |
| 31 | + "channelStatus": "delivered", |
| 32 | + "channelStatusDescription": " ", |
| 33 | + "supplierStatus": "delivered", |
| 34 | + "timestamp": "2023-11-17T14:27:51.413Z", |
| 35 | + "retryCount": 1 |
| 36 | + }, |
| 37 | + "links": { |
| 38 | + "message": "https://api.service.nhs.uk/comms/v1/messages/2WL3qFTEFM0qMY8xjRbt1LIKCzM" |
| 39 | + }, |
| 40 | + "meta": { |
| 41 | + "idempotencyKey": "2515ae6b3a08339fba3534f3b17cd57cd573c57d25b25b9aae08e42dc9f0a445" #gitleaks:allow |
| 42 | + } |
| 43 | + } |
| 44 | + ] |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +def assert_channel_status_record_created(): |
| 49 | + connection = psycopg2.connect( |
| 50 | + dbname=os.environ["DATABASE_NAME"], |
| 51 | + user=os.environ["DATABASE_USER"], |
| 52 | + host=os.environ["DATABASE_HOST"], |
| 53 | + password=os.environ["DATABASE_PASSWORD"] |
| 54 | + ) |
| 55 | + with connection as conn: |
| 56 | + with conn.cursor() as cur: |
| 57 | + cur.execute("SELECT details, message_id, status FROM channel_statuses ORDER BY created_at") |
| 58 | + records = cur.fetchall() |
| 59 | + |
| 60 | + assert len(records) == 1 |
| 61 | + |
| 62 | + details, message_id, status = records[0] |
| 63 | + attributes = details["attributes"] |
| 64 | + |
| 65 | + assert message_id == "2WL3qFTEFM0qMY8xjRbt1LIKCzM" |
| 66 | + assert status == "delivered" |
| 67 | + assert attributes["messageReference"] == "1642109b-69eb-447f-8f97-ab70a74f5db4" |
| 68 | + assert attributes["cascadeType"] == "primary" |
| 69 | + assert attributes["channel"] == "nhsapp" |
| 70 | + assert status == "delivered" |
| 71 | + |
| 72 | + |
| 73 | +def test_notify_callback_to_channel_status_saved(monkeypatch, callback_request_body): |
| 74 | + """Test that a callback request creates database records.""" |
| 75 | + monkeypatch.setenv('APPLICATION_ID', 'application_id') |
| 76 | + monkeypatch.setenv('NOTIFY_API_KEY', 'api_key') |
| 77 | + req_body = json.dumps(callback_request_body) |
| 78 | + signature = hmac.new( |
| 79 | + bytes('application_id.api_key', 'ASCII'), |
| 80 | + msg=bytes(req_body, 'ASCII'), |
| 81 | + digestmod=hashlib.sha256 |
| 82 | + ).hexdigest() |
| 83 | + headers = { |
| 84 | + "Content-Type": "application/json", |
| 85 | + "x-api-key": "api_key", |
| 86 | + "x-hmac-sha256-signature": signature, |
| 87 | + } |
| 88 | + req = func.HttpRequest( |
| 89 | + method="POST", |
| 90 | + headers=headers, |
| 91 | + body=bytes(req_body, "utf-8"), |
| 92 | + url="/api/notify/message/send", |
| 93 | + route_params={"notification_type": "message"}, |
| 94 | + ) |
| 95 | + |
| 96 | + func_call = function_app.main.build().get_user_function() |
| 97 | + assert 200 == func_call(req).status_code |
| 98 | + assert_channel_status_record_created() |
0 commit comments