|
| 1 | +import hashlib |
| 2 | +import hmac |
| 3 | +import json |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from django.test import TestCase |
| 8 | +from test.support.os_helper import EnvironmentVarGuard |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.integration |
| 12 | +class TestCallbackEndpoint(TestCase): |
| 13 | + def setUp(self): |
| 14 | + self.env = EnvironmentVarGuard() |
| 15 | + self.env.set("NHS_NOTIFY_APPLICATION_ID", "application_id") |
| 16 | + self.env.set("NHS_NOTIFY_API_KEY", "api_key") |
| 17 | + |
| 18 | + def test_endpoint_responds_with_json_400(self): |
| 19 | + response = self.client.post( |
| 20 | + "/notifications/message-status/create", |
| 21 | + {}, |
| 22 | + enforce_csrf_checks=True, |
| 23 | + content_type="application/json", |
| 24 | + ) |
| 25 | + assert response.status_code == 400 |
| 26 | + assert response.json() == { |
| 27 | + "error": { |
| 28 | + "message": "Missing API key header", |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + def test_endpoint_responds_with_200(self): |
| 33 | + body = {"some": "data"} |
| 34 | + signature = hmac.new( |
| 35 | + bytes("application_id.api_key", "ASCII"), |
| 36 | + msg=bytes(json.dumps(body), "ASCII"), |
| 37 | + digestmod=hashlib.sha256, |
| 38 | + ).hexdigest() |
| 39 | + headers = { |
| 40 | + "X-Api-Key": "api_key", |
| 41 | + "X-HMAC-sha256-signature": signature, |
| 42 | + } |
| 43 | + |
| 44 | + with patch( |
| 45 | + "manage_breast_screening.notifications.views.Queue.MessageStatusUpdates" |
| 46 | + ) as mock_queue: |
| 47 | + queue_instance = MagicMock() |
| 48 | + mock_queue.return_value = queue_instance |
| 49 | + |
| 50 | + response = self.client.post( |
| 51 | + "/notifications/message-status/create", |
| 52 | + body, |
| 53 | + enforce_csrf_checks=True, |
| 54 | + content_type="application/json", |
| 55 | + headers=headers, |
| 56 | + ) |
| 57 | + assert response.status_code == 200 |
| 58 | + assert response.json() == { |
| 59 | + "result": { |
| 60 | + "message": "Message status update queued", |
| 61 | + }, |
| 62 | + } |
0 commit comments