-
Notifications
You must be signed in to change notification settings - Fork 4
VED:1099: Create Test SQS Queue for MNS #1268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
10730f9
d236e98
8b54902
315ca08
e6a22c9
dcbde02
fc31344
6933833
55c18d3
e0c2134
337e745
0657096
8f8fc87
0520b95
163082a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # TODO: Remove when MNS platform authorizes imms-vaccinations-1 event type | ||
| # Temporary SQS queue for testing MNS notifications until MNS HTTP endpoint is available | ||
|
|
||
| resource "aws_sqs_queue" "mns_test_notification" { | ||
| name = "${var.mns_test_notification_name_prefix}-queue" | ||
| fifo_queue = false | ||
| message_retention_seconds = 14400 | ||
| visibility_timeout_seconds = 300 | ||
| } | ||
|
|
||
|
|
||
| data "aws_iam_policy_document" "mns_test_notification_sqs_policy" { | ||
| statement { | ||
| sid = "mns-test-notification-allow-lambda-access" | ||
| effect = "Allow" | ||
|
|
||
| principals { | ||
| type = "AWS" | ||
| identifiers = [aws_iam_role.mns_publisher_lambda_exec_role.arn] | ||
|
dlzhry2nhs marked this conversation as resolved.
|
||
| } | ||
|
|
||
| actions = [ | ||
| "sqs:SendMessage", | ||
| ] | ||
|
|
||
| resources = [ | ||
| aws_sqs_queue.mns_test_notification.arn | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| resource "aws_sqs_queue_policy" "mns_test_notification_sqs" { | ||
| queue_url = aws_sqs_queue.mns_test_notification.id | ||
| policy = data.aws_iam_policy_document.mns_test_notification_sqs_policy.json | ||
| } | ||
|
|
||
| output "mns_test_queue_url" { | ||
| value = aws_sqs_queue.mns_test_notification.url | ||
| description = "URL of the MNS test notifications queue" | ||
| } | ||
|
|
||
| output "mns_test_queue_arn" { | ||
| value = aws_sqs_queue.mns_test_notification.arn | ||
| description = "ARN of the MNS test notifications queue" | ||
| } | ||
|
|
||
|
edhall-nhs marked this conversation as resolved.
Outdated
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import json | ||
| import os | ||
|
|
||
| import boto3 | ||
|
|
||
| from common.clients import logger | ||
|
|
||
| MNS_TEST_QUEUE_URL = os.getenv("MNS_TEST_QUEUE_URL") | ||
| sqs_client = boto3.client("sqs", region_name="eu-west-2") | ||
|
Check warning on line 9 in lambdas/mns_publisher/src/mns_test_queue.py
|
||
|
|
||
|
|
||
| # TODO: Remove when MNS platform authorizes imms-vaccinations-1 event type | ||
|
Check warning on line 12 in lambdas/mns_publisher/src/mns_test_queue.py
|
||
| # Temporary function to send MNS to SQS Queue for testing MNS notifications until MNS HTTP endpoint is available | ||
| def send_notification_to_test_queue(mns_payload: dict) -> None: | ||
| """ | ||
| Send MNS notification payload to test SQS queue as fallback. | ||
| Args: payload: MNS notification payload | ||
| """ | ||
| if not MNS_TEST_QUEUE_URL: | ||
| logger.error("MNS_TEST_QUEUE_URL environment variable is not set") | ||
| return | ||
|
|
||
| try: | ||
| response = sqs_client.send_message( | ||
| QueueUrl=MNS_TEST_QUEUE_URL, | ||
| MessageBody=json.dumps(mns_payload), | ||
| MessageAttributes={"source": {"StringValue": "mns-publisher-lambda", "DataType": "String"}}, | ||
| ) | ||
| logger.info("Successfully sent notification to test queue", extra={"message_id": response["MessageId"]}) | ||
| except Exception: | ||
| logger.exception("Failed to send to test SQS queue") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import json | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| import boto3 | ||
| from moto import mock_aws | ||
|
|
||
| from mns_test_queue import send_notification_to_test_queue | ||
|
|
||
|
|
||
| @mock_aws | ||
| class TestMnsSqsQueue(unittest.TestCase): | ||
|
Akol125 marked this conversation as resolved.
Outdated
|
||
| def setUp(self): | ||
| self.sqs = boto3.client("sqs", region_name="eu-west-2") | ||
| response = self.sqs.create_queue(QueueName="mns-test-notifications-int") | ||
| self.queue_url = response["QueueUrl"] | ||
|
|
||
| self.mns_payload = { | ||
| "specversion": "1.0", | ||
| "id": "236a1d4a-5d69-4fa9-9c7f-e72bf505aa5b", | ||
| "source": "https://int.api.service.nhs.uk/immunisation-fhir-api", | ||
| "type": "imms-vaccinations-1", | ||
| "time": "20260212T174437+00:00", | ||
| "subject": "9481152782", | ||
| "dataref": "https://int.api.service.nhs.uk/immunisation-fhir-api/Immunization/d058014c-b0fd-4471-8db9-3316175eb825", | ||
| "filtering": { | ||
| "generalpractitioner": "Y12345", | ||
| "sourceorganisation": "B0C4P", | ||
| "sourceapplication": "TPP", | ||
| "subjectage": 21, | ||
| "immunisationtype": "HIB", | ||
| "action": "CREATE", | ||
| }, | ||
| } | ||
|
|
||
| def test_send_notification_to_queue_success(self): | ||
| """Test successful send to SQS queue with complete payload.""" | ||
| with patch("mns_test_queue.MNS_TEST_QUEUE_URL", self.queue_url): | ||
| send_notification_to_test_queue(self.mns_payload) | ||
|
|
||
| messages = self.sqs.receive_message( | ||
| QueueUrl=self.queue_url, MaxNumberOfMessages=1, MessageAttributeNames=["All"] | ||
| ) | ||
|
|
||
| self.assertIn("Messages", messages) | ||
| self.assertEqual(len(messages["Messages"]), 1) | ||
|
|
||
| # Verify message body | ||
| mns_payload = json.loads(messages["Messages"][0]["Body"]) | ||
| attributes = messages["Messages"][0]["MessageAttributes"] | ||
| self.assertEqual(attributes["source"]["StringValue"], "mns-publisher-lambda") | ||
|
|
||
| self.assertEqual(mns_payload["subject"], "9481152782") | ||
| self.assertEqual(mns_payload["filtering"]["generalpractitioner"], "Y12345") | ||
| self.assertEqual(mns_payload["filtering"]["sourceorganisation"], "B0C4P") | ||
| self.assertEqual(mns_payload["filtering"]["sourceapplication"], "TPP") | ||
| self.assertEqual(mns_payload["filtering"]["immunisationtype"], "HIB") | ||
| self.assertEqual(mns_payload["filtering"]["action"], "CREATE") | ||
| self.assertEqual(mns_payload["filtering"]["subjectage"], 21) | ||
Uh oh!
There was an error while loading. Please reload this page.