Merge branch 'master' into rate-limit-api-calls

Conflicts:
	requirements.txt
	tests/app/notifications/rest/test_send_notification.py
	tests/app/notifications/test_validators.py
	tests/app/v2/notifications/test_post_notifications.py
This commit is contained in:
Martyn Inglis
2017-05-02 10:56:56 +01:00
49 changed files with 1279 additions and 203 deletions

View File

@@ -0,0 +1,39 @@
from datetime import datetime
from flask import (
Blueprint,
jsonify,
request,
current_app,
json
)
from app import statsd_client
from app.clients.email.aws_ses import get_aws_responses
from app.dao import (
notifications_dao
)
from app.notifications.process_client_response import validate_callback_data
letter_callback_blueprint = Blueprint('notifications_letter_callback', __name__)
from app.errors import (
register_errors,
InvalidRequest
)
register_errors(letter_callback_blueprint)
@letter_callback_blueprint.route('/notifications/letter/dvla', methods=['POST'])
def process_letter_response():
try:
dvla_request = json.loads(request.data)
current_app.logger.info(dvla_request)
return jsonify(
result="success", message="DVLA callback succeeded"
), 200
except ValueError:
error = "DVLA callback failed: invalid json"
raise InvalidRequest(error, status_code=400)

View File

@@ -2,6 +2,11 @@ from datetime import datetime
from flask import current_app
from notifications_utils.recipients import (
get_international_phone_info,
validate_and_format_phone_number
)
from app import redis_store
from app.celery import provider_tasks
from notifications_utils.clients import redis
@@ -24,22 +29,23 @@ def check_placeholders(template_object):
raise BadRequestError(fields=[{'template': message}], message=message)
def persist_notification(template_id,
template_version,
recipient,
service,
personalisation,
notification_type,
api_key_id,
key_type,
created_at=None,
job_id=None,
job_row_number=None,
reference=None,
client_reference=None,
notification_id=None,
simulated=False):
# if simulated create a Notification model to return but do not persist the Notification to the dB
def persist_notification(
template_id,
template_version,
recipient,
service,
personalisation,
notification_type,
api_key_id,
key_type,
created_at=None,
job_id=None,
job_row_number=None,
reference=None,
client_reference=None,
notification_id=None,
simulated=False
):
notification = Notification(
id=notification_id,
template_id=template_id,
@@ -57,6 +63,15 @@ def persist_notification(template_id,
client_reference=client_reference,
reference=reference
)
if notification_type == SMS_TYPE:
formatted_recipient = validate_and_format_phone_number(recipient, international=True)
recipient_info = get_international_phone_info(formatted_recipient)
notification.international = recipient_info.international
notification.phone_prefix = recipient_info.country_prefix
notification.rate_multiplier = recipient_info.billable_units
# if simulated create a Notification model to return but do not persist the Notification to the dB
if not simulated:
dao_create_notification(notification)
if key_type != KEY_TYPE_TEST:
@@ -98,6 +113,10 @@ def send_notification_to_queue(notification, research_mode, queue=None):
def simulated_recipient(to_address, notification_type):
return (to_address in current_app.config['SIMULATED_SMS_NUMBERS']
if notification_type == SMS_TYPE
else to_address in current_app.config['SIMULATED_EMAIL_ADDRESSES'])
if notification_type == SMS_TYPE:
formatted_simulated_numbers = [
validate_and_format_phone_number(number) for number in current_app.config['SIMULATED_SMS_NUMBERS']
]
return to_address in formatted_simulated_numbers
else:
return to_address in current_app.config['SIMULATED_EMAIL_ADDRESSES']

View File

@@ -2,8 +2,7 @@ from flask import (
Blueprint,
jsonify,
request,
current_app,
json
current_app
)
from app import api_user
@@ -14,10 +13,6 @@ from app.dao import (
)
from app.models import KEY_TYPE_TEAM, PRIORITY
from app.models import SMS_TYPE
from app.notifications.process_client_response import (
validate_callback_data,
process_sms_client_response
)
from app.notifications.process_notifications import (persist_notification,
send_notification_to_queue,
simulated_recipient)
@@ -35,6 +30,8 @@ from app.schemas import (
from app.service.utils import service_allowed_to_send_to
from app.utils import pagination_links, get_template_instance
from notifications_utils.recipients import get_international_phone_info
notifications = Blueprint('notifications', __name__)
from app.errors import (
@@ -104,13 +101,15 @@ def send_notification(notification_type):
notification_form, errors = (
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
).load(request.get_json())
if errors:
raise InvalidRequest(errors, status_code=400)
check_rate_limiting(service, api_user)
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=notification_form['template'],
service_id=service.id)
template = templates_dao.dao_get_template_by_id_and_service_id(
template_id=notification_form['template'],
service_id=service.id)
check_template_is_for_notification_type(notification_type, template.template_type)
check_template_is_active(template)
@@ -118,12 +117,14 @@ def send_notification(notification_type):
template_object = create_template_object_for_notification(template, notification_form.get('personalisation', {}))
_service_allowed_to_send_to(notification_form, service)
if notification_type == SMS_TYPE:
_service_can_send_internationally(service, notification_form['to'])
# Do not persist or send notification to the queue if it is a simulated recipient
simulated = simulated_recipient(notification_form['to'], notification_type)
notification_model = persist_notification(template_id=template.id,
template_version=template.version,
recipient=notification_form['to'],
recipient=request.get_json()['to'],
service=service,
personalisation=notification_form.get('personalisation', None),
notification_type=notification_type,
@@ -160,6 +161,16 @@ def get_notification_return_data(notification_id, notification, template):
return output
def _service_can_send_internationally(service, number):
international_phone_info = get_international_phone_info(number)
if international_phone_info.international and not service.can_send_international_sms:
raise InvalidRequest(
{'to': ["Cannot send to international mobile numbers"]},
status_code=400
)
def _service_allowed_to_send_to(notification, service):
if not service_allowed_to_send_to(notification['to'], service, api_user.key_type):
if api_user.key_type == KEY_TYPE_TEAM:

View File

@@ -1,5 +1,9 @@
from flask import current_app
from notifications_utils.recipients import validate_and_format_phone_number, validate_and_format_email_address
from notifications_utils.recipients import (
validate_and_format_phone_number,
validate_and_format_email_address,
get_international_phone_info
)
from app.dao import services_dao
from app.models import KEY_TYPE_TEST, KEY_TYPE_TEAM, SMS_TYPE
@@ -61,8 +65,17 @@ def service_can_send_to_recipient(send_to, key_type, service):
def validate_and_format_recipient(send_to, key_type, service, notification_type):
service_can_send_to_recipient(send_to, key_type, service)
if notification_type == SMS_TYPE:
return validate_and_format_phone_number(number=send_to)
international_phone_info = get_international_phone_info(send_to)
if international_phone_info.international and not service.can_send_international_sms:
raise BadRequestError(message="Cannot send to international mobile numbers")
return validate_and_format_phone_number(
number=send_to,
international=international_phone_info.international
)
else:
return validate_and_format_email_address(email_address=send_to)