- Refactor v2 post_notification to use a single method for sms and email.

- Added the `simulate` notification logic to version 2. We have 3 email addresses and phone numbers that are used
to simulate a successful post to /notifications. This was missed out of the version 2 endpoint.
- Added a test to template_dao to check for the default value of normal for new templates
- in v2 get_notifications, casted the path param to a uuid, if not uuid abort(404)
This commit is contained in:
Rebecca Law
2017-01-17 12:08:24 +00:00
parent 31de10ae6f
commit a8cdabcecd
11 changed files with 193 additions and 104 deletions

View File

@@ -41,7 +41,8 @@ def persist_notification(template_id,
job_id=None,
job_row_number=None,
reference=None,
notification_id=None):
notification_id=None,
simulated=False):
notification = Notification(
id=notification_id,
template_id=template_id,
@@ -58,8 +59,9 @@ def persist_notification(template_id,
job_row_number=job_row_number,
client_reference=reference
)
dao_create_notification(notification)
redis_store.incr(redis.daily_limit_cache_key(service.id))
if not simulated:
dao_create_notification(notification)
redis_store.incr(redis.daily_limit_cache_key(service.id))
return notification
@@ -87,3 +89,9 @@ def send_notification_to_queue(notification, research_mode, queue=None):
current_app.logger.info(
"{} {} created at {}".format(notification.notification_type, notification.id, notification.created_at)
)
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'])

View File

@@ -8,7 +8,7 @@ from flask import (
json
)
from app import api_user, create_uuid, statsd_client
from app import api_user, statsd_client
from app.clients.email.aws_ses import get_aws_responses
from app.dao import (
templates_dao,
@@ -21,9 +21,12 @@ 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
from app.notifications.validators import check_service_message_limit, check_template_is_for_notification_type, \
check_template_is_active
from app.notifications.process_notifications import (persist_notification,
send_notification_to_queue,
simulated_recipient)
from app.notifications.validators import (check_service_message_limit,
check_template_is_for_notification_type,
check_template_is_active)
from app.schemas import (
email_notification_schema,
sms_template_notification_schema,
@@ -233,23 +236,26 @@ def send_notification(notification_type):
_service_allowed_to_send_to(notification, service)
saved_notification = None
if not _simulated_recipient(notification['to'], notification_type):
saved_notification = persist_notification(template_id=template.id,
template_version=template.version,
recipient=notification['to'],
service=service,
personalisation=notification.get('personalisation', None),
notification_type=notification_type,
api_key_id=api_user.id,
key_type=api_user.key_type)
# Do not persist or send notification to the queue if it is a simulated recipient
simulated = simulated_recipient(notification['to'], notification_type)
saved_notification = persist_notification(template_id=template.id,
template_version=template.version,
recipient=notification['to'],
service=service,
personalisation=notification.get('personalisation', None),
notification_type=notification_type,
api_key_id=api_user.id,
key_type=api_user.key_type,
simulated=simulated)
if not simulated:
send_notification_to_queue(saved_notification, service.research_mode)
notification_id = create_uuid() if saved_notification is None else saved_notification.id
else:
current_app.logger.info("POST simulated notification for id: {}".format(saved_notification.id))
notification.update({"template_version": template.version})
return jsonify(
data=get_notification_return_data(
notification_id,
saved_notification.id,
notification,
template_object)
), 201
@@ -268,12 +274,6 @@ def get_notification_return_data(notification_id, notification, template):
return output
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'])
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,7 +1,8 @@
from flask import current_app
from notifications_utils.recipients import validate_and_format_phone_number, validate_and_format_email_address
from app.dao import services_dao
from app.models import KEY_TYPE_TEST, KEY_TYPE_TEAM
from app.models import KEY_TYPE_TEST, KEY_TYPE_TEAM, SMS_TYPE
from app.service.utils import service_allowed_to_send_to
from app.v2.errors import TooManyRequestsError, BadRequestError
from app import redis_store
@@ -44,6 +45,14 @@ def service_can_send_to_recipient(send_to, key_type, service):
raise BadRequestError(message=message)
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)
else:
return validate_and_format_email_address(email_address=send_to)
def check_sms_content_char_count(content_count):
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
if (