mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-15 23:21:41 -05:00
Merge pull request #705 from alphagov/team-key-sends-to-whitelist
Let team key send to whitelist
This commit is contained in:
@@ -34,16 +34,16 @@ def service_allowed_to_send_to(recipient, service, key_type):
|
||||
return True
|
||||
|
||||
team_members = itertools.chain.from_iterable(
|
||||
[user.mobile_number, user.email_address] for user in service.users)
|
||||
[user.mobile_number, user.email_address] for user in service.users
|
||||
)
|
||||
whitelist_members = [
|
||||
member.recipient for member in service.whitelist
|
||||
]
|
||||
|
||||
if key_type == KEY_TYPE_TEAM:
|
||||
return allowed_to_send_to(
|
||||
recipient,
|
||||
team_members
|
||||
)
|
||||
|
||||
if key_type == KEY_TYPE_NORMAL and service.restricted:
|
||||
whitelist_members = [member.recipient for member in service.whitelist]
|
||||
if (
|
||||
(key_type == KEY_TYPE_NORMAL and service.restricted) or
|
||||
(key_type == KEY_TYPE_TEAM)
|
||||
):
|
||||
return allowed_to_send_to(
|
||||
recipient,
|
||||
itertools.chain(
|
||||
|
||||
@@ -768,27 +768,28 @@ def test_should_not_persist_notification_or_send_sms_if_simulated_number(
|
||||
assert Notification.query.count() == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize('notification_type,to, key_type', [
|
||||
('sms', '07827992635', KEY_TYPE_NORMAL),
|
||||
('email', 'non_whitelist_recipient@mail.com', KEY_TYPE_NORMAL),
|
||||
('sms', '07827992635', KEY_TYPE_TEAM),
|
||||
('email', 'non_whitelist_recipient@mail.com', KEY_TYPE_TEAM)])
|
||||
def test_should_not_send_notification_to_non_whitelist_recipient_in_trial_mode(client,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
notification_type,
|
||||
to,
|
||||
key_type,
|
||||
mocker):
|
||||
@pytest.mark.parametrize('key_type', [
|
||||
KEY_TYPE_NORMAL, KEY_TYPE_TEAM
|
||||
])
|
||||
@pytest.mark.parametrize('notification_type, to, _create_sample_template', [
|
||||
('sms', '07827992635', create_sample_template),
|
||||
('email', 'non_whitelist_recipient@mail.com', create_sample_email_template)]
|
||||
)
|
||||
def test_should_not_send_notification_to_non_whitelist_recipient_in_trial_mode(
|
||||
client,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
notification_type,
|
||||
to,
|
||||
_create_sample_template,
|
||||
key_type,
|
||||
mocker
|
||||
):
|
||||
service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=True)
|
||||
service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, service=service)
|
||||
|
||||
apply_async = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type))
|
||||
if notification_type == 'sms':
|
||||
template = create_sample_template(notify_db, notify_db_session, service=service)
|
||||
elif notification_type == 'email':
|
||||
template = create_sample_email_template(notify_db, notify_db_session, service=service)
|
||||
|
||||
template = _create_sample_template(notify_db, notify_db_session, service=service)
|
||||
assert service_whitelist.service_id == service.id
|
||||
assert to not in [member.recipient for member in service.whitelist]
|
||||
|
||||
@@ -819,23 +820,34 @@ def test_should_not_send_notification_to_non_whitelist_recipient_in_trial_mode(c
|
||||
apply_async.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('notification_type,to', [
|
||||
('sms', '07123123123'),
|
||||
('email', 'whitelist_recipient@mail.com')])
|
||||
def test_should_send_notification_to_whitelist_recipient_in_trial_mode_with_live_key(client,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
notification_type,
|
||||
to,
|
||||
mocker):
|
||||
service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=True)
|
||||
@pytest.mark.parametrize('service_restricted', [
|
||||
True, False
|
||||
])
|
||||
@pytest.mark.parametrize('key_type', [
|
||||
KEY_TYPE_NORMAL, KEY_TYPE_TEAM
|
||||
])
|
||||
@pytest.mark.parametrize('notification_type, to, _create_sample_template', [
|
||||
('sms', '07123123123', create_sample_template),
|
||||
('email', 'whitelist_recipient@mail.com', create_sample_email_template)]
|
||||
)
|
||||
def test_should_send_notification_to_whitelist_recipient(
|
||||
client,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
notification_type,
|
||||
to,
|
||||
_create_sample_template,
|
||||
key_type,
|
||||
service_restricted,
|
||||
mocker
|
||||
):
|
||||
service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=service_restricted)
|
||||
apply_async = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type))
|
||||
template = _create_sample_template(notify_db, notify_db_session, service=service)
|
||||
if notification_type == 'sms':
|
||||
template = create_sample_template(notify_db, notify_db_session, service=service)
|
||||
service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session,
|
||||
service=service, mobile_number=to)
|
||||
elif notification_type == 'email':
|
||||
template = create_sample_email_template(notify_db, notify_db_session, service=service)
|
||||
service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session,
|
||||
service=service, email_address=to)
|
||||
|
||||
@@ -849,8 +861,8 @@ def test_should_send_notification_to_whitelist_recipient_in_trial_mode_with_live
|
||||
'template': str(template.id)
|
||||
}
|
||||
|
||||
sample_live_key = create_sample_api_key(notify_db, notify_db_session, service)
|
||||
auth_header = create_jwt_token(secret=sample_live_key.unsigned_secret, client_id=str(sample_live_key.service_id))
|
||||
sample_key = create_sample_api_key(notify_db, notify_db_session, service, key_type=key_type)
|
||||
auth_header = create_jwt_token(secret=sample_key.unsigned_secret, client_id=str(sample_key.service_id))
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/{}'.format(notification_type),
|
||||
|
||||
Reference in New Issue
Block a user