Merge pull request #3456 from alphagov/error-if-cap-missing-references

Return 400 if references missing from cancel broadcast
This commit is contained in:
Chris Hill-Scott
2022-02-14 13:15:46 +00:00
committed by GitHub
4 changed files with 47 additions and 7 deletions

View File

@@ -7,7 +7,10 @@ def cap_xml_to_dict(cap_xml):
return {
"msgType": cap.alert.msgType.text,
"reference": cap.alert.identifier.text,
"references": cap.alert.references.text, # references to previous events belonging to the same alert
"references": (
# references to previous events belonging to the same alert
cap.alert.references.text if cap.alert.references else None
),
"cap_event": cap.alert.info.event.text,
"category": cap.alert.info.category.text,
"expires": cap.alert.info.expires.text,

View File

@@ -48,6 +48,11 @@ def create_broadcast():
validate(broadcast_json, post_broadcast_schema)
if broadcast_json["msgType"] == "Cancel":
if broadcast_json["references"] is None:
raise BadRequestError(
message='Missing <references>',
status_code=400,
)
broadcast_message = _cancel_or_reject_broadcast(
broadcast_json["references"].split(","),
authenticated_service.id

View File

@@ -45,7 +45,7 @@ WAINFLEET_CANCEL = """
<source>Flood warning service</source>
<scope>Public</scope>
<code></code>
<references>www.gov.uk/environment-agency,50385fcb0ab7aa447bbd46d848ce8466E,2020-02-16T23:01:13-00:00</references>
{}
<info>
<language>en-GB</language>
<category>Met</category>
@@ -70,6 +70,16 @@ WAINFLEET_CANCEL = """
</alert>
"""
WAINFLEET_CANCEL_WITH_REFERENCES = WAINFLEET_CANCEL.format(
"<references>www.gov.uk/environment-agency,50385fcb0ab7aa447bbd46d848ce8466E,2020-02-16T23:01:13-00:00</references>"
)
WAINFLEET_CANCEL_WITH_MISSING_REFERENCES = WAINFLEET_CANCEL.format(
""
)
WAINFLEET_CANCEL_WITH_EMPTY_REFERENCES = WAINFLEET_CANCEL.format(
"<references></references>"
)
UPDATE = """
<alert xmlns="urn:oasis:names:tc:emergency:cap:1.2">
<identifier>PAAQ-4-mg5a94</identifier>

View File

@@ -156,7 +156,7 @@ def test_valid_cancel_broadcast_request_calls_validate_and_update_broadcast_mess
# cancel broadcast
response_for_cancel = client.post(
path='/v2/broadcast',
data=sample_cap_xml_documents.WAINFLEET_CANCEL,
data=sample_cap_xml_documents.WAINFLEET_CANCEL_WITH_REFERENCES,
headers=[('Content-Type', 'application/cap+xml'), auth_header],
)
assert response_for_cancel.status_code == 201
@@ -167,9 +167,29 @@ def test_valid_cancel_broadcast_request_calls_validate_and_update_broadcast_mess
)
@pytest.mark.parametrize('cap_xml_document, expected_status, expected_error', (
(
sample_cap_xml_documents.WAINFLEET_CANCEL_WITH_REFERENCES,
404,
[{'error': 'NoResultFound', 'message': 'No result found'}],
),
(
sample_cap_xml_documents.WAINFLEET_CANCEL_WITH_EMPTY_REFERENCES,
404,
[{'error': 'NoResultFound', 'message': 'No result found'}],
),
(
sample_cap_xml_documents.WAINFLEET_CANCEL_WITH_MISSING_REFERENCES,
400,
[{'error': 'BadRequestError', 'message': 'Missing <references>'}],
),
))
def test_cancel_request_does_not_cancel_broadcast_if_reference_does_not_match(
client,
sample_broadcast_service
sample_broadcast_service,
cap_xml_document,
expected_status,
expected_error,
):
auth_header = create_service_authorization_header(service_id=sample_broadcast_service.id)
@@ -191,11 +211,13 @@ def test_cancel_request_does_not_cancel_broadcast_if_reference_does_not_match(
# try to cancel broadcast, but reference doesn't match
response_for_cancel = client.post(
path='/v2/broadcast',
data=sample_cap_xml_documents.WAINFLEET_CANCEL,
data=cap_xml_document,
headers=[('Content-Type', 'application/cap+xml'), auth_header],
)
response_for_cancel_json = json.loads(response_for_cancel.get_data(as_text=True))
assert response_for_cancel.status_code == 404
assert response_for_cancel.status_code == expected_status
assert response_for_cancel_json["errors"] == expected_error
def test_cancel_request_does_not_cancel_broadcast_if_service_id_does_not_match(
@@ -224,7 +246,7 @@ def test_cancel_request_does_not_cancel_broadcast_if_service_id_does_not_match(
auth_header_2 = create_service_authorization_header(service_id=sample_broadcast_service_2.id)
response_for_cancel = client.post(
path='/v2/broadcast',
data=sample_cap_xml_documents.WAINFLEET_CANCEL,
data=sample_cap_xml_documents.WAINFLEET_CANCEL_WITH_REFERENCES,
headers=[('Content-Type', 'application/cap+xml'), auth_header_2],
)