mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 17:01:35 -05:00
notify-api-412 use black to enforce python style standards
This commit is contained in:
@@ -49,44 +49,54 @@ from tests.conftest import set_config
|
||||
|
||||
|
||||
# all of these tests should have redis enabled (except where we specifically disable it)
|
||||
@pytest.fixture(scope='module', autouse=True)
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def enable_redis(notify_api):
|
||||
with set_config(notify_api, 'REDIS_ENABLED', True):
|
||||
with set_config(notify_api, "REDIS_ENABLED", True):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type', ['team', 'normal'])
|
||||
def test_check_service_message_limit_over_total_limit_fails(key_type, mocker, notify_db_session):
|
||||
@pytest.mark.parametrize("key_type", ["team", "normal"])
|
||||
def test_check_service_message_limit_over_total_limit_fails(
|
||||
key_type, mocker, notify_db_session
|
||||
):
|
||||
service = create_service()
|
||||
mocker.patch('app.redis_store.get', return_value="5001")
|
||||
mocker.patch("app.redis_store.get", return_value="5001")
|
||||
|
||||
with pytest.raises(TotalRequestsError) as e:
|
||||
check_application_over_retention_limit(key_type, service)
|
||||
assert e.value.status_code == 429
|
||||
assert e.value.message == 'Exceeded total application limits (5000) for today'
|
||||
assert e.value.message == "Exceeded total application limits (5000) for today"
|
||||
assert e.value.fields == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('template_type, notification_type',
|
||||
[(EMAIL_TYPE, EMAIL_TYPE),
|
||||
(SMS_TYPE, SMS_TYPE)])
|
||||
@pytest.mark.parametrize(
|
||||
"template_type, notification_type", [(EMAIL_TYPE, EMAIL_TYPE), (SMS_TYPE, SMS_TYPE)]
|
||||
)
|
||||
def test_check_template_is_for_notification_type_pass(template_type, notification_type):
|
||||
assert check_template_is_for_notification_type(notification_type=notification_type,
|
||||
template_type=template_type) is None
|
||||
assert (
|
||||
check_template_is_for_notification_type(
|
||||
notification_type=notification_type, template_type=template_type
|
||||
)
|
||||
is None
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('template_type, notification_type',
|
||||
[(SMS_TYPE, EMAIL_TYPE),
|
||||
(EMAIL_TYPE, SMS_TYPE)])
|
||||
@pytest.mark.parametrize(
|
||||
"template_type, notification_type", [(SMS_TYPE, EMAIL_TYPE), (EMAIL_TYPE, SMS_TYPE)]
|
||||
)
|
||||
def test_check_template_is_for_notification_type_fails_when_template_type_does_not_match_notification_type(
|
||||
template_type, notification_type):
|
||||
template_type, notification_type
|
||||
):
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
check_template_is_for_notification_type(notification_type=notification_type,
|
||||
template_type=template_type)
|
||||
check_template_is_for_notification_type(
|
||||
notification_type=notification_type, template_type=template_type
|
||||
)
|
||||
assert e.value.status_code == 400
|
||||
error_message = '{0} template is not suitable for {1} notification'.format(template_type, notification_type)
|
||||
error_message = "{0} template is not suitable for {1} notification".format(
|
||||
template_type, notification_type
|
||||
)
|
||||
assert e.value.message == error_message
|
||||
assert e.value.fields == [{'template': error_message}]
|
||||
assert e.value.fields == [{"template": error_message}]
|
||||
|
||||
|
||||
def test_check_template_is_active_passes(sample_template):
|
||||
@@ -96,77 +106,110 @@ def test_check_template_is_active_passes(sample_template):
|
||||
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)
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
check_template_is_active(sample_template)
|
||||
assert e.value.status_code == 400
|
||||
assert e.value.message == 'Template has been deleted'
|
||||
assert e.value.fields == [{'template': 'Template has been deleted'}]
|
||||
assert e.value.message == "Template has been deleted"
|
||||
assert e.value.fields == [{"template": "Template has been deleted"}]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type',
|
||||
['test', 'normal'])
|
||||
@pytest.mark.parametrize("key_type", ["test", "normal"])
|
||||
def test_service_can_send_to_recipient_passes(key_type, notify_db_session):
|
||||
trial_mode_service = create_service(service_name='trial mode', restricted=True)
|
||||
trial_mode_service = create_service(service_name="trial mode", restricted=True)
|
||||
serialised_service = SerialisedService.from_id(trial_mode_service.id)
|
||||
assert service_can_send_to_recipient(trial_mode_service.users[0].email_address,
|
||||
key_type,
|
||||
serialised_service) is None
|
||||
assert service_can_send_to_recipient(trial_mode_service.users[0].mobile_number,
|
||||
key_type,
|
||||
serialised_service) is None
|
||||
assert (
|
||||
service_can_send_to_recipient(
|
||||
trial_mode_service.users[0].email_address, key_type, serialised_service
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert (
|
||||
service_can_send_to_recipient(
|
||||
trial_mode_service.users[0].mobile_number, key_type, serialised_service
|
||||
)
|
||||
is None
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('user_number, recipient_number', [
|
||||
['+12028675309', '202-867-5309'],
|
||||
['+447513332413', '+44 (07513) 332413'],
|
||||
])
|
||||
def test_service_can_send_to_recipient_passes_with_non_normalized_number(sample_service, user_number, recipient_number):
|
||||
@pytest.mark.parametrize(
|
||||
"user_number, recipient_number",
|
||||
[
|
||||
["+12028675309", "202-867-5309"],
|
||||
["+447513332413", "+44 (07513) 332413"],
|
||||
],
|
||||
)
|
||||
def test_service_can_send_to_recipient_passes_with_non_normalized_number(
|
||||
sample_service, user_number, recipient_number
|
||||
):
|
||||
sample_service.users[0].mobile_number = user_number
|
||||
|
||||
serialised_service = SerialisedService.from_id(sample_service.id)
|
||||
|
||||
assert service_can_send_to_recipient(recipient_number, 'team', serialised_service) is None
|
||||
assert (
|
||||
service_can_send_to_recipient(recipient_number, "team", serialised_service)
|
||||
is None
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('user_email, recipient_email', [
|
||||
['test@example.com', 'TeSt@EXAMPLE.com'],
|
||||
])
|
||||
def test_service_can_send_to_recipient_passes_with_non_normalized_email(sample_service, user_email, recipient_email):
|
||||
@pytest.mark.parametrize(
|
||||
"user_email, recipient_email",
|
||||
[
|
||||
["test@example.com", "TeSt@EXAMPLE.com"],
|
||||
],
|
||||
)
|
||||
def test_service_can_send_to_recipient_passes_with_non_normalized_email(
|
||||
sample_service, user_email, recipient_email
|
||||
):
|
||||
sample_service.users[0].email_address = user_email
|
||||
|
||||
serialised_service = SerialisedService.from_id(sample_service.id)
|
||||
|
||||
assert service_can_send_to_recipient(recipient_email, 'team', serialised_service) is None
|
||||
assert (
|
||||
service_can_send_to_recipient(recipient_email, "team", serialised_service)
|
||||
is None
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type',
|
||||
['test', 'normal'])
|
||||
def test_service_can_send_to_recipient_passes_for_live_service_non_team_member(key_type, sample_service):
|
||||
@pytest.mark.parametrize("key_type", ["test", "normal"])
|
||||
def test_service_can_send_to_recipient_passes_for_live_service_non_team_member(
|
||||
key_type, sample_service
|
||||
):
|
||||
serialised_service = SerialisedService.from_id(sample_service.id)
|
||||
assert service_can_send_to_recipient("some_other_email@test.com",
|
||||
key_type,
|
||||
serialised_service) is None
|
||||
assert service_can_send_to_recipient('07513332413',
|
||||
key_type,
|
||||
serialised_service) is None
|
||||
assert (
|
||||
service_can_send_to_recipient(
|
||||
"some_other_email@test.com", key_type, serialised_service
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert (
|
||||
service_can_send_to_recipient("07513332413", key_type, serialised_service)
|
||||
is None
|
||||
)
|
||||
|
||||
|
||||
def test_service_can_send_to_recipient_passes_for_guest_list_recipient_passes(sample_service):
|
||||
def test_service_can_send_to_recipient_passes_for_guest_list_recipient_passes(
|
||||
sample_service,
|
||||
):
|
||||
create_service_guest_list(sample_service, email_address="some_other_email@test.com")
|
||||
assert service_can_send_to_recipient("some_other_email@test.com",
|
||||
'team',
|
||||
sample_service) is None
|
||||
create_service_guest_list(sample_service, mobile_number='2028675309')
|
||||
assert service_can_send_to_recipient('2028675309',
|
||||
'team',
|
||||
sample_service) is None
|
||||
assert (
|
||||
service_can_send_to_recipient(
|
||||
"some_other_email@test.com", "team", sample_service
|
||||
)
|
||||
is None
|
||||
)
|
||||
create_service_guest_list(sample_service, mobile_number="2028675309")
|
||||
assert service_can_send_to_recipient("2028675309", "team", sample_service) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('recipient', [
|
||||
{"email_address": "some_other_email@test.com"},
|
||||
{"mobile_number": "2028675300"},
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"recipient",
|
||||
[
|
||||
{"email_address": "some_other_email@test.com"},
|
||||
{"mobile_number": "2028675300"},
|
||||
],
|
||||
)
|
||||
def test_service_can_send_to_recipient_fails_when_ignoring_guest_list(
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
@@ -176,120 +219,148 @@ def test_service_can_send_to_recipient_fails_when_ignoring_guest_list(
|
||||
with pytest.raises(BadRequestError) as exec_info:
|
||||
service_can_send_to_recipient(
|
||||
next(iter(recipient.values())),
|
||||
'team',
|
||||
"team",
|
||||
sample_service,
|
||||
allow_guest_list_recipients=False,
|
||||
)
|
||||
assert exec_info.value.status_code == 400
|
||||
assert exec_info.value.message == 'Can’t send to this recipient using a team-only API key'
|
||||
assert (
|
||||
exec_info.value.message
|
||||
== "Can’t send to this recipient using a team-only API key"
|
||||
)
|
||||
assert exec_info.value.fields == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('recipient', ['2028675300', 'some_other_email@test.com'])
|
||||
@pytest.mark.parametrize('key_type, error_message',
|
||||
[('team', 'Can’t send to this recipient using a team-only API key'),
|
||||
('normal',
|
||||
"Can’t send to this recipient when service is in trial mode – see https://www.notifications.service.gov.uk/trial-mode")]) # noqa
|
||||
@pytest.mark.parametrize("recipient", ["2028675300", "some_other_email@test.com"])
|
||||
@pytest.mark.parametrize(
|
||||
"key_type, error_message",
|
||||
[
|
||||
("team", "Can’t send to this recipient using a team-only API key"),
|
||||
(
|
||||
"normal",
|
||||
"Can’t send to this recipient when service is in trial mode – see https://www.notifications.service.gov.uk/trial-mode", # noqa
|
||||
),
|
||||
],
|
||||
) # noqa
|
||||
def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(
|
||||
recipient,
|
||||
key_type,
|
||||
error_message,
|
||||
notify_db_session,
|
||||
):
|
||||
trial_mode_service = create_service(service_name='trial mode', restricted=True)
|
||||
trial_mode_service = create_service(service_name="trial mode", restricted=True)
|
||||
with pytest.raises(BadRequestError) as exec_info:
|
||||
service_can_send_to_recipient(recipient,
|
||||
key_type,
|
||||
trial_mode_service)
|
||||
service_can_send_to_recipient(recipient, key_type, trial_mode_service)
|
||||
assert exec_info.value.status_code == 400
|
||||
assert exec_info.value.message == error_message
|
||||
assert exec_info.value.fields == []
|
||||
|
||||
|
||||
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(sample_service):
|
||||
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(
|
||||
sample_service,
|
||||
):
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
service_can_send_to_recipient("0758964221",
|
||||
'team',
|
||||
sample_service)
|
||||
service_can_send_to_recipient("0758964221", "team", sample_service)
|
||||
assert e.value.status_code == 400
|
||||
assert e.value.message == 'Can’t send to this recipient using a team-only API key'
|
||||
assert e.value.message == "Can’t send to this recipient using a team-only API key"
|
||||
assert e.value.fields == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('char_count', [612, 0, 494, 200, 918])
|
||||
@pytest.mark.parametrize('show_prefix', [True, False])
|
||||
@pytest.mark.parametrize('template_type', ['sms', 'email'])
|
||||
def test_check_is_message_too_long_passes(notify_db_session, show_prefix, char_count, template_type):
|
||||
@pytest.mark.parametrize("char_count", [612, 0, 494, 200, 918])
|
||||
@pytest.mark.parametrize("show_prefix", [True, False])
|
||||
@pytest.mark.parametrize("template_type", ["sms", "email"])
|
||||
def test_check_is_message_too_long_passes(
|
||||
notify_db_session, show_prefix, char_count, template_type
|
||||
):
|
||||
service = create_service(prefix_sms=show_prefix)
|
||||
t = create_template(service=service, content='a' * char_count, template_type=template_type)
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=t.id, service_id=service.id)
|
||||
t = create_template(
|
||||
service=service, content="a" * char_count, template_type=template_type
|
||||
)
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||
template_id=t.id, service_id=service.id
|
||||
)
|
||||
template_with_content = get_template_instance(template=template.__dict__, values={})
|
||||
assert check_is_message_too_long(template_with_content) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('char_count', [919, 6000])
|
||||
@pytest.mark.parametrize('show_prefix', [True, False])
|
||||
@pytest.mark.parametrize("char_count", [919, 6000])
|
||||
@pytest.mark.parametrize("show_prefix", [True, False])
|
||||
def test_check_is_message_too_long_fails(notify_db_session, show_prefix, char_count):
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
service = create_service(prefix_sms=show_prefix)
|
||||
t = create_template(service=service, content='a' * char_count, template_type='sms')
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=t.id, service_id=service.id)
|
||||
template_with_content = get_template_instance(template=template.__dict__, values={})
|
||||
t = create_template(
|
||||
service=service, content="a" * char_count, template_type="sms"
|
||||
)
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||
template_id=t.id, service_id=service.id
|
||||
)
|
||||
template_with_content = get_template_instance(
|
||||
template=template.__dict__, values={}
|
||||
)
|
||||
check_is_message_too_long(template_with_content)
|
||||
assert e.value.status_code == 400
|
||||
expected_message = f'Your message is too long. '\
|
||||
f'Text messages cannot be longer than {SMS_CHAR_COUNT_LIMIT} characters. '\
|
||||
f'Your message is {char_count} characters long.'
|
||||
expected_message = (
|
||||
f"Your message is too long. "
|
||||
f"Text messages cannot be longer than {SMS_CHAR_COUNT_LIMIT} characters. "
|
||||
f"Your message is {char_count} characters long."
|
||||
)
|
||||
assert e.value.message == expected_message
|
||||
assert e.value.fields == []
|
||||
|
||||
|
||||
def test_check_is_message_too_long_passes_for_long_email(sample_service):
|
||||
email_character_count = 2_000_001
|
||||
t = create_template(service=sample_service, content='a' * email_character_count, template_type='email')
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=t.id,
|
||||
service_id=t.service_id)
|
||||
t = create_template(
|
||||
service=sample_service,
|
||||
content="a" * email_character_count,
|
||||
template_type="email",
|
||||
)
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||
template_id=t.id, service_id=t.service_id
|
||||
)
|
||||
template_with_content = get_template_instance(template=template.__dict__, values={})
|
||||
template_with_content.values
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
check_is_message_too_long(template_with_content)
|
||||
assert e.value.status_code == 400
|
||||
expected_message = (
|
||||
'Your message is too long. ' +
|
||||
'Emails cannot be longer than 2000000 bytes. ' +
|
||||
'Your message is 2000001 bytes.'
|
||||
"Your message is too long. "
|
||||
+ "Emails cannot be longer than 2000000 bytes. "
|
||||
+ "Your message is 2000001 bytes."
|
||||
)
|
||||
assert e.value.message == expected_message
|
||||
assert e.value.fields == []
|
||||
|
||||
|
||||
def test_check_notification_content_is_not_empty_passes(notify_api, mocker, sample_service):
|
||||
def test_check_notification_content_is_not_empty_passes(
|
||||
notify_api, mocker, sample_service
|
||||
):
|
||||
template_id = create_template(sample_service, content="Content is not empty").id
|
||||
template = SerialisedTemplate.from_id_and_service_id(
|
||||
template_id=template_id,
|
||||
service_id=sample_service.id
|
||||
template_id=template_id, service_id=sample_service.id
|
||||
)
|
||||
template_with_content = create_content_for_notification(template, {})
|
||||
assert check_notification_content_is_not_empty(template_with_content) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('template_content,notification_values', [
|
||||
("", {}),
|
||||
("((placeholder))", {"placeholder": ""})
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"template_content,notification_values",
|
||||
[("", {}), ("((placeholder))", {"placeholder": ""})],
|
||||
)
|
||||
def test_check_notification_content_is_not_empty_fails(
|
||||
notify_api, mocker, sample_service, template_content, notification_values
|
||||
):
|
||||
template_id = create_template(sample_service, content=template_content).id
|
||||
template = SerialisedTemplate.from_id_and_service_id(
|
||||
template_id=template_id,
|
||||
service_id=sample_service.id
|
||||
template_id=template_id, service_id=sample_service.id
|
||||
)
|
||||
template_with_content = create_content_for_notification(
|
||||
template, notification_values
|
||||
)
|
||||
template_with_content = create_content_for_notification(template, notification_values)
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
check_notification_content_is_not_empty(template_with_content)
|
||||
assert e.value.status_code == 400
|
||||
assert e.value.message == 'Your message is empty.'
|
||||
assert e.value.message == "Your message is empty."
|
||||
assert e.value.fields == []
|
||||
|
||||
|
||||
@@ -299,18 +370,29 @@ def test_validate_template(sample_service):
|
||||
|
||||
|
||||
@pytest.mark.parametrize("check_char_count", [True, False])
|
||||
def test_validate_template_calls_all_validators(mocker, fake_uuid, sample_service, check_char_count):
|
||||
def test_validate_template_calls_all_validators(
|
||||
mocker, fake_uuid, sample_service, check_char_count
|
||||
):
|
||||
template = create_template(sample_service, template_type="email")
|
||||
mock_check_type = mocker.patch('app.notifications.validators.check_template_is_for_notification_type')
|
||||
mock_check_if_active = mocker.patch('app.notifications.validators.check_template_is_active')
|
||||
mock_create_conent = mocker.patch(
|
||||
'app.notifications.validators.create_content_for_notification', return_value="content"
|
||||
mock_check_type = mocker.patch(
|
||||
"app.notifications.validators.check_template_is_for_notification_type"
|
||||
)
|
||||
mock_check_if_active = mocker.patch(
|
||||
"app.notifications.validators.check_template_is_active"
|
||||
)
|
||||
mock_create_conent = mocker.patch(
|
||||
"app.notifications.validators.create_content_for_notification",
|
||||
return_value="content",
|
||||
)
|
||||
mock_check_not_empty = mocker.patch(
|
||||
"app.notifications.validators.check_notification_content_is_not_empty"
|
||||
)
|
||||
mock_check_message_is_too_long = mocker.patch(
|
||||
"app.notifications.validators.check_is_message_too_long"
|
||||
)
|
||||
template, template_with_content = validate_template(
|
||||
template.id, {}, sample_service, "email", check_char_count=check_char_count
|
||||
)
|
||||
mock_check_not_empty = mocker.patch('app.notifications.validators.check_notification_content_is_not_empty')
|
||||
mock_check_message_is_too_long = mocker.patch('app.notifications.validators.check_is_message_too_long')
|
||||
template, template_with_content = validate_template(template.id, {}, sample_service, "email",
|
||||
check_char_count=check_char_count
|
||||
)
|
||||
|
||||
mock_check_type.assert_called_once_with("email", "email")
|
||||
mock_check_if_active.assert_called_once_with(template)
|
||||
@@ -322,17 +404,29 @@ def test_validate_template_calls_all_validators(mocker, fake_uuid, sample_servic
|
||||
assert not mock_check_message_is_too_long.called
|
||||
|
||||
|
||||
def test_validate_template_calls_all_validators_exception_message_too_long(mocker, fake_uuid, sample_service):
|
||||
def test_validate_template_calls_all_validators_exception_message_too_long(
|
||||
mocker, fake_uuid, sample_service
|
||||
):
|
||||
template = create_template(sample_service, template_type="email")
|
||||
mock_check_type = mocker.patch('app.notifications.validators.check_template_is_for_notification_type')
|
||||
mock_check_if_active = mocker.patch('app.notifications.validators.check_template_is_active')
|
||||
mock_create_conent = mocker.patch(
|
||||
'app.notifications.validators.create_content_for_notification', return_value="content"
|
||||
mock_check_type = mocker.patch(
|
||||
"app.notifications.validators.check_template_is_for_notification_type"
|
||||
)
|
||||
mock_check_if_active = mocker.patch(
|
||||
"app.notifications.validators.check_template_is_active"
|
||||
)
|
||||
mock_create_conent = mocker.patch(
|
||||
"app.notifications.validators.create_content_for_notification",
|
||||
return_value="content",
|
||||
)
|
||||
mock_check_not_empty = mocker.patch(
|
||||
"app.notifications.validators.check_notification_content_is_not_empty"
|
||||
)
|
||||
mock_check_message_is_too_long = mocker.patch(
|
||||
"app.notifications.validators.check_is_message_too_long"
|
||||
)
|
||||
template, template_with_content = validate_template(
|
||||
template.id, {}, sample_service, "email", check_char_count=False
|
||||
)
|
||||
mock_check_not_empty = mocker.patch('app.notifications.validators.check_notification_content_is_not_empty')
|
||||
mock_check_message_is_too_long = mocker.patch('app.notifications.validators.check_is_message_too_long')
|
||||
template, template_with_content = validate_template(template.id, {}, sample_service, "email",
|
||||
check_char_count=False)
|
||||
|
||||
mock_check_type.assert_called_once_with("email", "email")
|
||||
mock_check_if_active.assert_called_once_with(template)
|
||||
@@ -341,24 +435,24 @@ def test_validate_template_calls_all_validators_exception_message_too_long(mocke
|
||||
assert not mock_check_message_is_too_long.called
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type', ['team', 'live', 'test'])
|
||||
@pytest.mark.parametrize("key_type", ["team", "live", "test"])
|
||||
def test_check_service_over_api_rate_limit_when_exceed_rate_limit_request_fails_raises_error(
|
||||
key_type,
|
||||
sample_service,
|
||||
mocker):
|
||||
key_type, sample_service, mocker
|
||||
):
|
||||
with freeze_time("2016-01-01 12:00:00.000000"):
|
||||
|
||||
if key_type == 'live':
|
||||
api_key_type = 'normal'
|
||||
if key_type == "live":
|
||||
api_key_type = "normal"
|
||||
else:
|
||||
api_key_type = key_type
|
||||
|
||||
mocker.patch('app.redis_store.exceeded_rate_limit', return_value=True)
|
||||
mocker.patch("app.redis_store.exceeded_rate_limit", return_value=True)
|
||||
|
||||
sample_service.restricted = True
|
||||
api_key = create_api_key(sample_service, key_type=api_key_type)
|
||||
serialised_service = SerialisedService.from_id(sample_service.id)
|
||||
serialised_api_key = SerialisedAPIKeyCollection.from_service_id(serialised_service.id)[0]
|
||||
serialised_api_key = SerialisedAPIKeyCollection.from_service_id(
|
||||
serialised_service.id
|
||||
)[0]
|
||||
|
||||
with pytest.raises(RateLimitError) as e:
|
||||
check_service_over_api_rate_limit(serialised_service, serialised_api_key)
|
||||
@@ -366,46 +460,51 @@ def test_check_service_over_api_rate_limit_when_exceed_rate_limit_request_fails_
|
||||
assert app.redis_store.exceeded_rate_limit.called_with(
|
||||
"{}-{}".format(str(sample_service.id), api_key.key_type),
|
||||
sample_service.rate_limit,
|
||||
60
|
||||
60,
|
||||
)
|
||||
assert e.value.status_code == 429
|
||||
assert e.value.message == 'Exceeded rate limit for key type {} of {} requests per {} seconds'.format(
|
||||
key_type.upper(), sample_service.rate_limit, 60
|
||||
assert (
|
||||
e.value.message
|
||||
== "Exceeded rate limit for key type {} of {} requests per {} seconds".format(
|
||||
key_type.upper(), sample_service.rate_limit, 60
|
||||
)
|
||||
)
|
||||
assert e.value.fields == []
|
||||
|
||||
|
||||
def test_check_service_over_api_rate_limit_when_rate_limit_has_not_exceeded_limit_succeeds(
|
||||
sample_service,
|
||||
mocker):
|
||||
sample_service, mocker
|
||||
):
|
||||
with freeze_time("2016-01-01 12:00:00.000000"):
|
||||
mocker.patch('app.redis_store.exceeded_rate_limit', return_value=False)
|
||||
mocker.patch("app.redis_store.exceeded_rate_limit", return_value=False)
|
||||
|
||||
sample_service.restricted = True
|
||||
api_key = create_api_key(sample_service)
|
||||
serialised_service = SerialisedService.from_id(sample_service.id)
|
||||
serialised_api_key = SerialisedAPIKeyCollection.from_service_id(serialised_service.id)[0]
|
||||
serialised_api_key = SerialisedAPIKeyCollection.from_service_id(
|
||||
serialised_service.id
|
||||
)[0]
|
||||
|
||||
check_service_over_api_rate_limit(serialised_service, serialised_api_key)
|
||||
assert app.redis_store.exceeded_rate_limit.called_with(
|
||||
"{}-{}".format(str(sample_service.id), api_key.key_type),
|
||||
3000,
|
||||
60
|
||||
"{}-{}".format(str(sample_service.id), api_key.key_type), 3000, 60
|
||||
)
|
||||
|
||||
|
||||
def test_check_service_over_api_rate_limit_should_do_nothing_if_limiting_is_disabled(
|
||||
sample_service,
|
||||
mocker):
|
||||
sample_service, mocker
|
||||
):
|
||||
with freeze_time("2016-01-01 12:00:00.000000"):
|
||||
current_app.config['API_RATE_LIMIT_ENABLED'] = False
|
||||
current_app.config["API_RATE_LIMIT_ENABLED"] = False
|
||||
|
||||
mocker.patch('app.redis_store.exceeded_rate_limit', return_value=False)
|
||||
mocker.patch("app.redis_store.exceeded_rate_limit", return_value=False)
|
||||
|
||||
sample_service.restricted = True
|
||||
create_api_key(sample_service)
|
||||
serialised_service = SerialisedService.from_id(sample_service.id)
|
||||
serialised_api_key = SerialisedAPIKeyCollection.from_service_id(serialised_service.id)[0]
|
||||
serialised_api_key = SerialisedAPIKeyCollection.from_service_id(
|
||||
serialised_service.id
|
||||
)[0]
|
||||
|
||||
check_service_over_api_rate_limit(serialised_service, serialised_api_key)
|
||||
app.redis_store.exceeded_rate_limit.assert_not_called()
|
||||
@@ -414,7 +513,9 @@ def test_check_service_over_api_rate_limit_should_do_nothing_if_limiting_is_disa
|
||||
def test_check_rate_limiting_validates_api_rate_limit_and_daily_limit(
|
||||
notify_db_session, mocker
|
||||
):
|
||||
mock_rate_limit = mocker.patch('app.notifications.validators.check_service_over_api_rate_limit')
|
||||
mock_rate_limit = mocker.patch(
|
||||
"app.notifications.validators.check_service_over_api_rate_limit"
|
||||
)
|
||||
service = create_service()
|
||||
api_key = create_api_key(service=service)
|
||||
|
||||
@@ -423,122 +524,167 @@ def test_check_rate_limiting_validates_api_rate_limit_and_daily_limit(
|
||||
mock_rate_limit.assert_called_once_with(service, api_key)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type', ['test', 'normal'])
|
||||
@pytest.mark.parametrize("key_type", ["test", "normal"])
|
||||
def test_validate_and_format_recipient_fails_when_international_number_and_service_does_not_allow_int_sms(
|
||||
key_type,
|
||||
notify_db_session,
|
||||
key_type,
|
||||
notify_db_session,
|
||||
):
|
||||
service = create_service(service_permissions=[SMS_TYPE])
|
||||
service_model = SerialisedService.from_id(service.id)
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
validate_and_format_recipient('+20-12-1234-1234', key_type, service_model, SMS_TYPE)
|
||||
validate_and_format_recipient(
|
||||
"+20-12-1234-1234", key_type, service_model, SMS_TYPE
|
||||
)
|
||||
assert e.value.status_code == 400
|
||||
assert e.value.message == 'Cannot send to international mobile numbers'
|
||||
assert e.value.message == "Cannot send to international mobile numbers"
|
||||
assert e.value.fields == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type', ['test', 'normal'])
|
||||
@pytest.mark.parametrize("key_type", ["test", "normal"])
|
||||
def test_validate_and_format_recipient_succeeds_with_international_numbers_if_service_does_allow_int_sms(
|
||||
key_type, sample_service_full_permissions):
|
||||
key_type, sample_service_full_permissions
|
||||
):
|
||||
service_model = SerialisedService.from_id(sample_service_full_permissions.id)
|
||||
result = validate_and_format_recipient('+4407513332413', key_type, service_model, SMS_TYPE)
|
||||
assert result == '+447513332413'
|
||||
result = validate_and_format_recipient(
|
||||
"+4407513332413", key_type, service_model, SMS_TYPE
|
||||
)
|
||||
assert result == "+447513332413"
|
||||
|
||||
|
||||
def test_validate_and_format_recipient_fails_when_no_recipient():
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
validate_and_format_recipient(None, 'key_type', 'service', 'SMS_TYPE')
|
||||
validate_and_format_recipient(None, "key_type", "service", "SMS_TYPE")
|
||||
assert e.value.status_code == 400
|
||||
assert e.value.message == "Recipient can't be empty"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('notification_type', ['sms', 'email'])
|
||||
@pytest.mark.parametrize("notification_type", ["sms", "email"])
|
||||
def test_check_service_email_reply_to_id_where_reply_to_id_is_none(notification_type):
|
||||
assert check_service_email_reply_to_id(None, None, notification_type) is None
|
||||
|
||||
|
||||
def test_check_service_email_reply_to_where_email_reply_to_is_found(sample_service):
|
||||
reply_to_address = create_reply_to_email(sample_service, "test@test.com")
|
||||
assert check_service_email_reply_to_id(sample_service.id, reply_to_address.id, EMAIL_TYPE) == "test@test.com"
|
||||
assert (
|
||||
check_service_email_reply_to_id(
|
||||
sample_service.id, reply_to_address.id, EMAIL_TYPE
|
||||
)
|
||||
== "test@test.com"
|
||||
)
|
||||
|
||||
|
||||
def test_check_service_email_reply_to_id_where_service_id_is_not_found(sample_service, fake_uuid):
|
||||
def test_check_service_email_reply_to_id_where_service_id_is_not_found(
|
||||
sample_service, fake_uuid
|
||||
):
|
||||
reply_to_address = create_reply_to_email(sample_service, "test@test.com")
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
check_service_email_reply_to_id(fake_uuid, reply_to_address.id, EMAIL_TYPE)
|
||||
assert e.value.status_code == 400
|
||||
assert e.value.message == 'email_reply_to_id {} does not exist in database for service id {}' \
|
||||
.format(reply_to_address.id, fake_uuid)
|
||||
assert (
|
||||
e.value.message
|
||||
== "email_reply_to_id {} does not exist in database for service id {}".format(
|
||||
reply_to_address.id, fake_uuid
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_check_service_email_reply_to_id_where_reply_to_id_is_not_found(sample_service, fake_uuid):
|
||||
def test_check_service_email_reply_to_id_where_reply_to_id_is_not_found(
|
||||
sample_service, fake_uuid
|
||||
):
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
check_service_email_reply_to_id(sample_service.id, fake_uuid, EMAIL_TYPE)
|
||||
assert e.value.status_code == 400
|
||||
assert e.value.message == 'email_reply_to_id {} does not exist in database for service id {}' \
|
||||
.format(fake_uuid, sample_service.id)
|
||||
assert (
|
||||
e.value.message
|
||||
== "email_reply_to_id {} does not exist in database for service id {}".format(
|
||||
fake_uuid, sample_service.id
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('notification_type', ['sms', 'email'])
|
||||
@pytest.mark.parametrize("notification_type", ["sms", "email"])
|
||||
def test_check_service_sms_sender_id_where_sms_sender_id_is_none(notification_type):
|
||||
assert check_service_sms_sender_id(None, None, notification_type) is None
|
||||
|
||||
|
||||
def test_check_service_sms_sender_id_where_sms_sender_id_is_found(sample_service):
|
||||
sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456')
|
||||
assert check_service_sms_sender_id(sample_service.id, sms_sender.id, SMS_TYPE) == '123456'
|
||||
sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456")
|
||||
assert (
|
||||
check_service_sms_sender_id(sample_service.id, sms_sender.id, SMS_TYPE)
|
||||
== "123456"
|
||||
)
|
||||
|
||||
|
||||
def test_check_service_sms_sender_id_where_service_id_is_not_found(sample_service, fake_uuid):
|
||||
sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456')
|
||||
def test_check_service_sms_sender_id_where_service_id_is_not_found(
|
||||
sample_service, fake_uuid
|
||||
):
|
||||
sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456")
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
check_service_sms_sender_id(fake_uuid, sms_sender.id, SMS_TYPE)
|
||||
assert e.value.status_code == 400
|
||||
assert e.value.message == 'sms_sender_id {} does not exist in database for service id {}' \
|
||||
.format(sms_sender.id, fake_uuid)
|
||||
assert (
|
||||
e.value.message
|
||||
== "sms_sender_id {} does not exist in database for service id {}".format(
|
||||
sms_sender.id, fake_uuid
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_check_service_sms_sender_id_where_sms_sender_is_not_found(sample_service, fake_uuid):
|
||||
def test_check_service_sms_sender_id_where_sms_sender_is_not_found(
|
||||
sample_service, fake_uuid
|
||||
):
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
check_service_sms_sender_id(sample_service.id, fake_uuid, SMS_TYPE)
|
||||
assert e.value.status_code == 400
|
||||
assert e.value.message == 'sms_sender_id {} does not exist in database for service id {}' \
|
||||
.format(fake_uuid, sample_service.id)
|
||||
assert (
|
||||
e.value.message
|
||||
== "sms_sender_id {} does not exist in database for service id {}".format(
|
||||
fake_uuid, sample_service.id
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('notification_type', ['sms', 'email'])
|
||||
@pytest.mark.parametrize("notification_type", ["sms", "email"])
|
||||
def test_check_reply_to_with_empty_reply_to(sample_service, notification_type):
|
||||
assert check_reply_to(sample_service.id, None, notification_type) is None
|
||||
|
||||
|
||||
def test_check_reply_to_email_type(sample_service):
|
||||
reply_to_address = create_reply_to_email(sample_service, "test@test.com")
|
||||
assert check_reply_to(sample_service.id, reply_to_address.id, EMAIL_TYPE) == 'test@test.com'
|
||||
assert (
|
||||
check_reply_to(sample_service.id, reply_to_address.id, EMAIL_TYPE)
|
||||
== "test@test.com"
|
||||
)
|
||||
|
||||
|
||||
def test_check_reply_to_sms_type(sample_service):
|
||||
sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456')
|
||||
assert check_reply_to(sample_service.id, sms_sender.id, SMS_TYPE) == '123456'
|
||||
sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456")
|
||||
assert check_reply_to(sample_service.id, sms_sender.id, SMS_TYPE) == "123456"
|
||||
|
||||
|
||||
def test_check_if_service_can_send_files_by_email_raises_if_no_contact_link_set(sample_service):
|
||||
def test_check_if_service_can_send_files_by_email_raises_if_no_contact_link_set(
|
||||
sample_service,
|
||||
):
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
check_if_service_can_send_files_by_email(
|
||||
service_contact_link=sample_service.contact_link,
|
||||
service_id=sample_service.id
|
||||
service_id=sample_service.id,
|
||||
)
|
||||
|
||||
message = f"Send files by email has not been set up - add contact details for your service at " \
|
||||
f"http://localhost:6012/services/{sample_service.id}/service-settings/send-files-by-email"
|
||||
message = (
|
||||
f"Send files by email has not been set up - add contact details for your service at "
|
||||
f"http://localhost:6012/services/{sample_service.id}/service-settings/send-files-by-email"
|
||||
)
|
||||
assert e.value.status_code == 400
|
||||
assert e.value.message == message
|
||||
|
||||
|
||||
def test_check_if_service_can_send_files_by_email_passes_if_contact_link_set(sample_service):
|
||||
sample_service.contact_link = 'contact.me@gov.uk'
|
||||
def test_check_if_service_can_send_files_by_email_passes_if_contact_link_set(
|
||||
sample_service,
|
||||
):
|
||||
sample_service.contact_link = "contact.me@gov.uk"
|
||||
check_if_service_can_send_files_by_email(
|
||||
service_contact_link=sample_service.contact_link,
|
||||
service_id=sample_service.id
|
||||
service_contact_link=sample_service.contact_link, service_id=sample_service.id
|
||||
)
|
||||
|
||||
|
||||
@@ -548,25 +694,37 @@ def test_get_string_to_sign():
|
||||
"Type": "Notification",
|
||||
"MessageId": "ccccccccc-cccc-cccc-cccc-ccccccccccccc",
|
||||
"TopicArn": "arn:aws:sns:us-west-2:009969138378:connector-svc-test",
|
||||
"Message": "{\"AbsoluteTime\":\"2021-09-08T13:28:24.656Z\",\"Content\":\"help\",\"ContentType\":\"text/plain\",\"Id\":\"333333333-be0d-4a44-889d-d2a86fc06f0c\",\"Type\":\"MESSAGE\",\"ParticipantId\":\"bbbbbbbb-c562-4d95-b76c-dcbca8b4b5f7\",\"DisplayName\":\"Jane\",\"ParticipantRole\":\"CUSTOMER\",\"InitialContactId\":\"33333333-abc5-46db-9ad5-d772559ab556\",\"ContactId\":\"33333333-abc5-46db-9ad5-d772559ab556\"}", # noqa
|
||||
"Message": '{"AbsoluteTime":"2021-09-08T13:28:24.656Z","Content":"help","ContentType":"text/plain","Id":"333333333-be0d-4a44-889d-d2a86fc06f0c","Type":"MESSAGE","ParticipantId":"bbbbbbbb-c562-4d95-b76c-dcbca8b4b5f7","DisplayName":"Jane","ParticipantRole":"CUSTOMER","InitialContactId":"33333333-abc5-46db-9ad5-d772559ab556","ContactId":"33333333-abc5-46db-9ad5-d772559ab556"}', # noqa
|
||||
"Timestamp": "2021-09-08T13:28:24.860Z",
|
||||
"SignatureVersion": "1",
|
||||
"Signature": "examplegggggg/1tEBYdiVDgJgBoJUniUFcArLFGfg5JCvpOr/v6LPCHiD7A0BWy8+ZOnGTmOjBMn80U9jSzYhKbHDbQHaNYTo9sRyQA31JtHHiIseQeMfTDpcaAXqfs8hdIXq4XZaJYqDFqosfbvh56VPh5QgmeHTltTc7eOZBUwnt/177eOTLTt2yB0ItMV3NAYuE1Tdxya1lLYZQUIMxETTVcRAZkDIu8TbRZC9a00q2RQVjXhDaU3k+tL+kk85syW/2ryjjkDYoUb+dyRGkqMy4aKA22UpfidOtdAZ/GGtXaXSKBqazZTEUuSEzt0duLtFntQiYJanU05gtDig==", # noqa
|
||||
"SigningCertURL": "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-11111111111111111111111111111111.pem", # noqa
|
||||
"UnsubscribeURL": "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:000000000000:connector-svc-test:22222222-aaaa-bbbb-cccc-333333333333", # noqa
|
||||
"Signature": "examplegggggg/1tEBYdiVDgJgBoJUniUFcArLFGfg5JCvpOr/v6LPCHiD7A0BWy8+ZOnGTmOjBMn80U9jSzYhKbHDbQHaNYTo9sRyQA31JtHHiIseQeMfTDpcaAXqfs8hdIXq4XZaJYqDFqosfbvh56VPh5QgmeHTltTc7eOZBUwnt/177eOTLTt2yB0ItMV3NAYuE1Tdxya1lLYZQUIMxETTVcRAZkDIu8TbRZC9a00q2RQVjXhDaU3k+tL+kk85syW/2ryjjkDYoUb+dyRGkqMy4aKA22UpfidOtdAZ/GGtXaXSKBqazZTEUuSEzt0duLtFntQiYJanU05gtDig==", # noqa
|
||||
"SigningCertURL": "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-11111111111111111111111111111111.pem", # noqa
|
||||
"UnsubscribeURL": "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:000000000000:connector-svc-test:22222222-aaaa-bbbb-cccc-333333333333", # noqa
|
||||
"MessageAttributes": {
|
||||
"InitialContactId": {"Type": "String", "Value": "33333333-abc5-46db-9ad5-d772559ab556"},
|
||||
"InitialContactId": {
|
||||
"Type": "String",
|
||||
"Value": "33333333-abc5-46db-9ad5-d772559ab556",
|
||||
},
|
||||
"MessageVisibility": {"Type": "String", "Value": "ALL"},
|
||||
"Type": {"Type": "String", "Value": "MESSAGE"},
|
||||
"AccountId": {"Type": "String", "Value": "999999999999"},
|
||||
"ContentType": {"Type": "String", "Value": "text/plain"},
|
||||
"InstanceId": {"Type": "String", "Value": "dddddddd-b64e-40c5-921b-109fd92499ae"},
|
||||
"ContactId": {"Type": "String", "Value": "33333333-abc5-46db-9ad5-d772559ab556"},
|
||||
"ParticipantRole": {"Type": "String", "Value": "CUSTOMER"}
|
||||
}
|
||||
"InstanceId": {
|
||||
"Type": "String",
|
||||
"Value": "dddddddd-b64e-40c5-921b-109fd92499ae",
|
||||
},
|
||||
"ContactId": {
|
||||
"Type": "String",
|
||||
"Value": "33333333-abc5-46db-9ad5-d772559ab556",
|
||||
},
|
||||
"ParticipantRole": {"Type": "String", "Value": "CUSTOMER"},
|
||||
},
|
||||
}
|
||||
str = get_string_to_sign(sns_payload)
|
||||
assert str == b'Message\n{"AbsoluteTime":"2021-09-08T13:28:24.656Z","Content":"help","ContentType":"text/plain","Id":"333333333-be0d-4a44-889d-d2a86fc06f0c","Type":"MESSAGE","ParticipantId":"bbbbbbbb-c562-4d95-b76c-dcbca8b4b5f7","DisplayName":"Jane","ParticipantRole":"CUSTOMER","InitialContactId":"33333333-abc5-46db-9ad5-d772559ab556","ContactId":"33333333-abc5-46db-9ad5-d772559ab556"}\nMessageId\nccccccccc-cccc-cccc-cccc-ccccccccccccc\nTimestamp\n2021-09-08T13:28:24.860Z\nTopicArn\narn:aws:sns:us-west-2:009969138378:connector-svc-test\nType\nNotification\n' # noqa
|
||||
assert (
|
||||
str
|
||||
== b'Message\n{"AbsoluteTime":"2021-09-08T13:28:24.656Z","Content":"help","ContentType":"text/plain","Id":"333333333-be0d-4a44-889d-d2a86fc06f0c","Type":"MESSAGE","ParticipantId":"bbbbbbbb-c562-4d95-b76c-dcbca8b4b5f7","DisplayName":"Jane","ParticipantRole":"CUSTOMER","InitialContactId":"33333333-abc5-46db-9ad5-d772559ab556","ContactId":"33333333-abc5-46db-9ad5-d772559ab556"}\nMessageId\nccccccccc-cccc-cccc-cccc-ccccccccccccc\nTimestamp\n2021-09-08T13:28:24.860Z\nTopicArn\narn:aws:sns:us-west-2:009969138378:connector-svc-test\nType\nNotification\n' # noqa
|
||||
)
|
||||
|
||||
# This is a test payload with no valid cert, so it should raise a ValueError
|
||||
with pytest.raises(ValueError):
|
||||
|
||||
Reference in New Issue
Block a user