generated from nhs-england-tools/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds /api/message/batch which follows the same HMAC verification and request body schema validation as with status creation.
- Loading branch information
Showing
8 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from flask import request | ||
import app.validators.request_validator as request_validator | ||
import app.services.message_batch_dispatcher as message_batch_dispatcher | ||
|
||
|
||
def batch(): | ||
json_data = request.json or {} | ||
valid_headers, error_message = request_validator.verify_headers(dict(request.headers)) | ||
|
||
if not valid_headers: | ||
return {"status": "failed", "error": error_message}, 401 | ||
|
||
if not request_validator.verify_signature(dict(request.headers), json_data): | ||
return {"status": "failed", "error": "Invalid signature"}, 403 | ||
|
||
valid_body, error_message = request_validator.verify_body(json_data) | ||
|
||
if not valid_body: | ||
return {"status": "failed", "error": error_message}, 422 | ||
|
||
status_code, response = message_batch_dispatcher.dispatch(json_data) | ||
status = "success" if status_code == 201 else "failed" | ||
|
||
return {"status": status, "response": response}, status_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/integration/notify/app/route_handlers/test_message_batch.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from app import create_app | ||
from app.validators.request_validator import API_KEY_HEADER_NAME, SIGNATURE_HEADER_NAME, signature_secret | ||
import app.utils.hmac_signature as hmac_signature | ||
import json | ||
import pytest | ||
import requests_mock | ||
|
||
|
||
@pytest.fixture | ||
def setup(monkeypatch): | ||
"""Set up environment variables for tests.""" | ||
monkeypatch.setenv('APPLICATION_ID', 'application_id') | ||
monkeypatch.setenv('NOTIFY_API_KEY', 'api_key') | ||
monkeypatch.setenv("NOTIFY_API_URL", "http://example.com") | ||
|
||
|
||
@pytest.fixture | ||
def client(): | ||
app = create_app() | ||
yield app.test_client() | ||
|
||
|
||
def test_message_batch_request_validation_fails(setup, client, message_batch_post_body): | ||
"""Test that invalid request header values fail HMAC signature validation.""" | ||
headers = {API_KEY_HEADER_NAME: "api_key", SIGNATURE_HEADER_NAME: "signature"} | ||
|
||
response = client.post('/api/message/batch', json=message_batch_post_body, headers=headers) | ||
|
||
assert response.status_code == 403 | ||
assert response.get_json() == {"status": "failed", "error": "Invalid signature"} | ||
|
||
|
||
def test_message_batch_succeeds(setup, client, message_batch_post_body, message_batch_post_response): | ||
"""Test that valid request header values pass HMAC signature validation.""" | ||
signature = hmac_signature.create_digest(signature_secret(), json.dumps(message_batch_post_body, sort_keys=True)) | ||
|
||
headers = {API_KEY_HEADER_NAME: "api_key", SIGNATURE_HEADER_NAME: signature} | ||
|
||
with requests_mock.Mocker() as rm: | ||
rm.post( | ||
"http://example.com/comms/v1/message-batches", | ||
status_code=201, | ||
json=message_batch_post_response | ||
) | ||
|
||
response = client.post('/api/message/batch', json=message_batch_post_body, headers=headers) | ||
|
||
assert response.status_code == 201 | ||
assert response.get_json() == {"status": "success", "response": message_batch_post_response} | ||
|
||
|
||
def test_message_batch_fails_with_invalid_post_body(setup, client, message_batch_post_body): | ||
"""Test that invalid request body fails schema validation.""" | ||
message_batch_post_body["data"]["type"] = "invalid" | ||
signature = hmac_signature.create_digest(signature_secret(), json.dumps(message_batch_post_body, sort_keys=True)) | ||
|
||
headers = {API_KEY_HEADER_NAME: "api_key", SIGNATURE_HEADER_NAME: signature} | ||
|
||
response = client.post('/api/message/batch', json=message_batch_post_body, headers=headers) | ||
|
||
assert response.status_code == 422 | ||
assert response.get_json() == {"status": "failed", "error": "Invalid body: 'invalid'"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters