Merge pull request #3095 from alphagov/allow-admin-to-create-broadcasts-without-templates

Let the admin app create broadcasts without templates
This commit is contained in:
Chris Hill-Scott
2021-01-19 16:57:33 +00:00
committed by GitHub
3 changed files with 142 additions and 17 deletions

View File

@@ -143,9 +143,9 @@ def test_create_broadcast_message(admin_request, sample_broadcast_service):
(
{},
[
{'error': 'ValidationError', 'message': 'template_id is a required property'},
{'error': 'ValidationError', 'message': 'service_id is a required property'},
{'error': 'ValidationError', 'message': 'created_by is a required property'}
{'error': 'ValidationError', 'message': 'created_by is a required property'},
{'error': 'ValidationError', 'message': '{} is not valid under any of the given schemas'},
]
),
(
@@ -178,12 +178,32 @@ def test_create_broadcast_message_400s_if_json_schema_fails_validation(
@freeze_time('2020-01-01')
def test_create_broadcast_message_400s_if_content_provided_but_no_template(admin_request, sample_broadcast_service):
# we don't currently support this, but might in the future
def test_create_broadcast_message_can_be_created_from_content(admin_request, sample_broadcast_service):
response = admin_request.post(
'broadcast_message.create_broadcast_message',
_data={
'template_id': None,
'content': 'Some tailor made broadcast content',
'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=201
)
assert response['content'] == 'Some tailor made broadcast content'
assert response['reference'] == 'abc123'
assert response['template_id'] is None
def test_create_broadcast_message_400s_if_content_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),
'content': 'Some tailor made broadcast content',
'service_id': str(sample_broadcast_service.id),
'created_by': str(sample_broadcast_service.created_by_id),
@@ -191,10 +211,93 @@ def test_create_broadcast_message_400s_if_content_provided_but_no_template(admin
service_id=sample_broadcast_service.id,
_expected_status=400
)
assert response['errors'] ==[
{'error': 'ValidationError', 'message': 'template_id is not a valid UUID'},
{'error': 'ValidationError', 'message': 'Additional properties are not allowed (content was unexpected)'},
]
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 lets just pick out the important bits
assert (
' is valid under each of '
) in response['errors'][0]['message']
assert (
'{required: [content]}'
) in response['errors'][0]['message']
assert (
'{required: [template_id]}'
) 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 lets 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'
)
def test_create_broadcast_message_400s_if_no_content_or_template(
admin_request,
sample_broadcast_service,
):
response = admin_request.post(
'broadcast_message.create_broadcast_message',
_data={
'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', [