Skip to content

Commit 523394c

Browse files
authored
Merge pull request #486 from NHSDigital/add-csrf-exempt-to-notifications-callback-endpoint
Add csrf exempt to notifications callback endpoint
2 parents ea7f44c + 37d68dc commit 523394c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

manage_breast_screening/notifications/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from django.contrib.auth.decorators import login_not_required
44
from django.http import JsonResponse
5+
from django.views.decorators.csrf import csrf_exempt
56
from django.views.decorators.http import require_http_methods
67

78
from manage_breast_screening.core.decorators import (
@@ -16,6 +17,7 @@
1617
@require_http_methods(["POST"])
1718
@login_not_required
1819
@basic_auth_exempt
20+
@csrf_exempt
1921
def create_message_status(request):
2022
valid, message = RequestValidator(request).valid()
2123

0 commit comments

Comments
 (0)