Validate recipient for restricted service w/ utils

Implements
https://github.com/alphagov/notifications-utils/pull/16

Once
https://github.com/alphagov/notifications-admin/pull/376
is merged it will no longer be possible for a user to upload a CSV file
containing recipients that they’re not allowed to send to.

So this commit also removes any restricted service checks in the task,
because any public phone numbers/email addresses no longer have any way
of reach this point if the service is restricted.
This commit is contained in:
Chris Hill-Scott
2016-04-04 15:02:25 +01:00
parent 30f0ca3e43
commit e56aee5d1d
7 changed files with 62 additions and 189 deletions

View File

@@ -1,5 +1,6 @@
from datetime import datetime
import uuid
import itertools
from flask import (
Blueprint,
@@ -11,6 +12,7 @@ from flask import (
)
from utils.template import Template
from utils.recipients import allowed_to_send_to, first_column_heading
from app.clients.sms.firetext import FiretextResponses
from app.clients.email.aws_ses import AwsSesResponses
from app import api_user, encryption, create_uuid, DATETIME_FORMAT, DATE_FORMAT
@@ -28,7 +30,6 @@ from app.schemas import (
notifications_filter_schema
)
from app.celery.tasks import send_sms, send_email
from app.validation import allowed_send_to_number, allowed_send_to_email
notifications = Blueprint('notifications', __name__)
@@ -356,12 +357,21 @@ def send_notification(notification_type):
}
), 400
if service.restricted and not allowed_to_send_to(
notification['to'],
itertools.chain.from_iterable(
[user.mobile_number, user.email_address] for user in service.users
)
):
return jsonify(
result="error", message={
'to': ['Invalid {} for restricted service'.format(first_column_heading[notification_type])]
}
), 400
notification_id = create_uuid()
if notification_type == 'sms':
if not allowed_send_to_number(service, notification['to']):
return jsonify(
result="error", message={'to': ['Invalid phone number for restricted service']}), 400
send_sms.apply_async((
service_id,
notification_id,
@@ -369,9 +379,6 @@ def send_notification(notification_type):
datetime.utcnow().strftime(DATETIME_FORMAT)
), queue='sms')
else:
if not allowed_send_to_email(service, notification['to']):
return jsonify(
result="error", message={'to': ['Email address not permitted for restricted service']}), 400
send_email.apply_async((
service_id,
notification_id,