Skip to content
This repository was archived by the owner on Sep 12, 2025. It is now read-only.

Commit 40cc3f7

Browse files
committed
Add end to end test for recording channel status
1 parent 704f4d5 commit 40cc3f7

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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()

tests/end_to_end/test_notify_callback_to_message_status_saved.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def assert_message_status_record_created():
6767
assert len(records) == 1
6868

6969
details, message_id, status = records[0]
70-
attributes = details["data"][0]["attributes"]
70+
attributes = details["attributes"]
7171

7272
assert message_id == "2WL3qFTEFM0qMY8xjRbt1LIKCzM"
7373
assert status == "sending"

0 commit comments

Comments
 (0)