mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 16:31:15 -05:00
Require reference if template not provided
In the admin app we need something to use in show in lieu of template name when a template isn’t used. Let’s store this in the reference field for now.
This commit is contained in:
@@ -16,11 +16,18 @@ create_broadcast_message_schema = {
|
|||||||
'areas': {"type": "array", "items": {"type": "string"}},
|
'areas': {"type": "array", "items": {"type": "string"}},
|
||||||
'simple_polygons': {"type": "array", "items": {"type": "array"}},
|
'simple_polygons': {"type": "array", "items": {"type": "array"}},
|
||||||
'content': {'type': 'string', 'minLength': 1, 'maxLength': 1395},
|
'content': {'type': 'string', 'minLength': 1, 'maxLength': 1395},
|
||||||
|
'reference': {'type': 'string', 'minLength': 1, 'maxLength': 255},
|
||||||
},
|
},
|
||||||
'required': ['service_id', 'created_by'],
|
'required': ['service_id', 'created_by'],
|
||||||
'oneOf': [
|
'allOf': [
|
||||||
|
{'oneOf': [
|
||||||
{'required': ['template_id']},
|
{'required': ['template_id']},
|
||||||
{'required': ['content']},
|
{'required': ['content']},
|
||||||
|
]},
|
||||||
|
{'oneOf': [
|
||||||
|
{'required': ['template_id']},
|
||||||
|
{'required': ['reference']},
|
||||||
|
]},
|
||||||
],
|
],
|
||||||
'additionalProperties': False
|
'additionalProperties': False
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,8 +109,9 @@ def create_broadcast_message(service_id):
|
|||||||
content = template._as_utils_template_with_personalisation(
|
content = template._as_utils_template_with_personalisation(
|
||||||
personalisation
|
personalisation
|
||||||
).content_with_placeholders_filled_in
|
).content_with_placeholders_filled_in
|
||||||
|
reference = None
|
||||||
else:
|
else:
|
||||||
template, content = None, data['content']
|
template, content, reference = None, data['content'], data['reference']
|
||||||
|
|
||||||
broadcast_message = BroadcastMessage(
|
broadcast_message = BroadcastMessage(
|
||||||
service_id=service.id,
|
service_id=service.id,
|
||||||
@@ -123,6 +124,7 @@ def create_broadcast_message(service_id):
|
|||||||
finishes_at=_parse_nullable_datetime(data.get('finishes_at')),
|
finishes_at=_parse_nullable_datetime(data.get('finishes_at')),
|
||||||
created_by_id=user.id,
|
created_by_id=user.id,
|
||||||
content=content,
|
content=content,
|
||||||
|
reference=reference,
|
||||||
)
|
)
|
||||||
|
|
||||||
dao_save_object(broadcast_message)
|
dao_save_object(broadcast_message)
|
||||||
|
|||||||
@@ -183,6 +183,7 @@ def test_create_broadcast_message_can_be_created_from_content(admin_request, sam
|
|||||||
'broadcast_message.create_broadcast_message',
|
'broadcast_message.create_broadcast_message',
|
||||||
_data={
|
_data={
|
||||||
'content': 'Some tailor made broadcast content',
|
'content': 'Some tailor made broadcast content',
|
||||||
|
'reference': 'abc123',
|
||||||
'service_id': str(sample_broadcast_service.id),
|
'service_id': str(sample_broadcast_service.id),
|
||||||
'created_by': str(sample_broadcast_service.created_by_id),
|
'created_by': str(sample_broadcast_service.created_by_id),
|
||||||
},
|
},
|
||||||
@@ -190,6 +191,7 @@ def test_create_broadcast_message_can_be_created_from_content(admin_request, sam
|
|||||||
_expected_status=201
|
_expected_status=201
|
||||||
)
|
)
|
||||||
assert response['content'] == 'Some tailor made broadcast content'
|
assert response['content'] == 'Some tailor made broadcast content'
|
||||||
|
assert response['reference'] == 'abc123'
|
||||||
assert response['template_id'] is None
|
assert response['template_id'] is None
|
||||||
|
|
||||||
|
|
||||||
@@ -224,6 +226,60 @@ def test_create_broadcast_message_400s_if_content_and_template_provided(
|
|||||||
'{required: [template_id]}'
|
'{required: [template_id]}'
|
||||||
) in response['errors'][0]['message']
|
) in response['errors'][0]['message']
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_broadcast_message_400s_if_reference_and_template_provided(
|
||||||
|
admin_request,
|
||||||
|
sample_broadcast_service,
|
||||||
|
):
|
||||||
|
template = create_template(sample_broadcast_service, BROADCAST_TYPE)
|
||||||
|
response = admin_request.post(
|
||||||
|
'broadcast_message.create_broadcast_message',
|
||||||
|
_data={
|
||||||
|
'template_id': str(template.id),
|
||||||
|
'reference': 'abc123',
|
||||||
|
'service_id': str(sample_broadcast_service.id),
|
||||||
|
'created_by': str(sample_broadcast_service.created_by_id),
|
||||||
|
},
|
||||||
|
service_id=sample_broadcast_service.id,
|
||||||
|
_expected_status=400
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(response['errors']) == 1
|
||||||
|
assert response['errors'][0]['error'] == 'ValidationError'
|
||||||
|
# The error message for oneOf is ugly, non-deterministic in ordering
|
||||||
|
# and contains some UUID, so let’s just pick out the important bits
|
||||||
|
assert (
|
||||||
|
' is valid under each of '
|
||||||
|
) in response['errors'][0]['message']
|
||||||
|
assert (
|
||||||
|
'{required: [reference]}'
|
||||||
|
) in response['errors'][0]['message']
|
||||||
|
assert (
|
||||||
|
'{required: [template_id]}'
|
||||||
|
) in response['errors'][0]['message']
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_broadcast_message_400s_if_reference_not_provided_with_content(
|
||||||
|
admin_request,
|
||||||
|
sample_broadcast_service,
|
||||||
|
):
|
||||||
|
response = admin_request.post(
|
||||||
|
'broadcast_message.create_broadcast_message',
|
||||||
|
_data={
|
||||||
|
'content': 'Some tailor made broadcast content',
|
||||||
|
'service_id': str(sample_broadcast_service.id),
|
||||||
|
'created_by': str(sample_broadcast_service.created_by_id),
|
||||||
|
},
|
||||||
|
service_id=sample_broadcast_service.id,
|
||||||
|
_expected_status=400
|
||||||
|
)
|
||||||
|
assert len(response['errors']) == 1
|
||||||
|
assert response['errors'][0]['error'] == 'ValidationError'
|
||||||
|
assert response['errors'][0]['message'].endswith(
|
||||||
|
'is not valid under any of the given schemas'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('status', [
|
@pytest.mark.parametrize('status', [
|
||||||
BroadcastStatusType.DRAFT,
|
BroadcastStatusType.DRAFT,
|
||||||
BroadcastStatusType.PENDING_APPROVAL,
|
BroadcastStatusType.PENDING_APPROVAL,
|
||||||
|
|||||||
Reference in New Issue
Block a user