Updated test_validators to test the contents of the error messages.

Added some tests to the test_post_notifications.
Added a errorhandler for AuthErrors.

This endpoint is not being used anywhere, however there is some common code being used in the v1 post endpoint. The only thing that may be affected is the error response, hopefully they are the same.
This commit is contained in:
Rebecca Law
2016-10-31 12:22:26 +00:00
parent 8cf2fc72a8
commit fc298367c5
7 changed files with 120 additions and 42 deletions

View File

@@ -1,5 +1,4 @@
import pytest
from app.notifications.validators import check_service_message_limit, check_template_is_for_notification_type, \
check_template_is_active, service_can_send_to_recipient, check_sms_content_char_count
from app.v2.errors import BadRequestError, TooManyRequestsError
@@ -26,8 +25,12 @@ def test_check_service_message_limit_over_message_limit_fails(key_type, notify_d
service = create_service(notify_db, notify_db_session, restricted=True, limit=4)
for x in range(5):
create_notification(notify_db, notify_db_session, service=service)
with pytest.raises(TooManyRequestsError):
with pytest.raises(TooManyRequestsError) as e:
check_service_message_limit(key_type, service)
assert e.value.status_code == 429
assert e.value.code == '10429'
assert e.value.message == 'Exceeded send limits (4) for today'
assert e.value.fields == []
@pytest.mark.parametrize('template_type, notification_type',
@@ -43,9 +46,14 @@ def test_check_template_is_for_notification_type_pass(template_type, notificatio
('email', 'sms')])
def test_check_template_is_for_notification_type_fails_when_template_type_does_not_match_notification_type(
template_type, notification_type):
with pytest.raises(BadRequestError):
with pytest.raises(BadRequestError) as e:
check_template_is_for_notification_type(notification_type=notification_type,
template_type=template_type)
assert e.value.code == 10400
error_message = '{0} template is not suitable for {1} notification'.format(template_type, notification_type)
assert e.value.message == error_message
assert e.value.link == 'link to documentation'
assert e.value.fields == [{'template': error_message}]
def test_check_template_is_active_passes(sample_template):
@@ -56,13 +64,13 @@ def test_check_template_is_active_fails(sample_template):
sample_template.archived = True
from app.dao.templates_dao import dao_update_template
dao_update_template(sample_template)
try:
with pytest.raises(BadRequestError) as e:
check_template_is_active(sample_template)
except BadRequestError as e:
assert e.status_code == 400
assert e.code == '10400'
assert e.message == 'Template has been deleted'
assert e.link == "link to documentation"
assert e.value.status_code == 400
assert e.value.code == 10400
assert e.value.message == 'Template has been deleted'
assert e.value.link == "link to documentation"
assert e.value.fields == [{'template': 'Template has been deleted'}]
@pytest.mark.parametrize('key_type',
@@ -103,26 +111,36 @@ def test_service_can_send_to_recipient_passes_for_whitelisted_recipient_passes(k
sample_service) is None
@pytest.mark.parametrize('key_type',
['team', 'normal'])
def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(key_type, notify_db, notify_db_session):
@pytest.mark.parametrize('recipient', ['07513332413', 'some_other_email@test.com'])
@pytest.mark.parametrize('key_type, error_message',
[('team', 'Cant send to this recipient using a team-only API key'),
('normal',
"Cant send to this recipient when service is in trial mode see https://www.notifications.service.gov.uk/trial-mode")]) # noqa
def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(recipient, key_type, error_message,
notify_db, notify_db_session):
trial_mode_service = create_service(notify_db, notify_db_session, service_name='trial mode', restricted=True)
with pytest.raises(BadRequestError):
assert service_can_send_to_recipient("some_other_email@test.com",
key_type,
trial_mode_service) is None
with pytest.raises(BadRequestError):
assert service_can_send_to_recipient('07513332413',
with pytest.raises(BadRequestError) as exec_info:
assert service_can_send_to_recipient(recipient,
key_type,
trial_mode_service) is None
assert exec_info.value.status_code == 400
assert exec_info.value.code == 10400
assert exec_info.value.message == error_message
assert exec_info.value.link == 'link to documentation'
assert exec_info.value.fields == []
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(notify_db, notify_db_session):
live_service = create_service(notify_db, notify_db_session, service_name='live mode', restricted=False)
with pytest.raises(BadRequestError):
with pytest.raises(BadRequestError) as e:
assert service_can_send_to_recipient("0758964221",
'team',
live_service) is None
assert e.value.status_code == 400
assert e.value.code == 10400
assert e.value.message == 'Cant send to this recipient using a team-only API key'
assert e.value.link == 'link to documentation'
assert e.value.fields == []
@pytest.mark.parametrize('char_count', [495, 0, 494, 200])
@@ -132,5 +150,11 @@ def test_check_sms_content_char_count_passes(char_count, notify_api):
@pytest.mark.parametrize('char_count', [496, 500, 6000])
def test_check_sms_content_char_count_fails(char_count, notify_api):
with pytest.raises(BadRequestError):
with pytest.raises(BadRequestError) as e:
check_sms_content_char_count(char_count)
assert e.value.status_code == 400
assert e.value.code == 10400
assert e.value.message == 'Content for template has a character count greater than the limit of {}'.format(
notify_api.config['SMS_CHAR_COUNT_LIMIT'])
assert e.value.link == 'link to documentation'
assert e.value.fields == []