def send_one_off_notification(service_id, post_data): service = dao_fetch_service_by_id(service_id) template = dao_get_template_by_id_and_service_id( template_id=post_data['template_id'], service_id=service_id ) personalisation = post_data.get('personalisation', None) validate_template(template.id, personalisation, service, template.template_type) check_service_over_daily_message_limit(KEY_TYPE_NORMAL, service) validate_and_format_recipient( send_to=post_data['to'], key_type=KEY_TYPE_NORMAL, service=service, notification_type=template.template_type, allow_whitelisted_recipients=False, ) validate_created_by(service, post_data['created_by']) sender_id = post_data.get('sender_id', None) reply_to = get_reply_to_text( notification_type=template.template_type, sender_id=sender_id, service=service, template=template ) notification = persist_notification( template_id=template.id, template_version=template.version, template_postage=template.postage, recipient=post_data['to'], service=service, personalisation=personalisation, notification_type=template.template_type, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_by_id=post_data['created_by'], reply_to_text=reply_to, reference=create_one_off_reference(template.template_type), ) queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None if template.template_type == LETTER_TYPE and service.research_mode: _update_notification_status( notification, NOTIFICATION_DELIVERED, ) else: send_notification_to_queue( notification=notification, research_mode=service.research_mode, queue=queue_name, ) return {'id': str(notification.id)}
def send_one_off_notification(service_id, post_data): service = dao_fetch_service_by_id(service_id) template = dao_get_template_by_id_and_service_id(template_id=post_data["template_id"], service_id=service_id) personalisation = post_data.get("personalisation", None) validate_template(template.id, personalisation, service, template.template_type) check_service_over_daily_message_limit(KEY_TYPE_NORMAL, service) validate_and_format_recipient( send_to=post_data["to"], key_type=KEY_TYPE_NORMAL, service=service, notification_type=template.template_type, allow_safelisted_recipients=False, ) validate_created_by(service, post_data["created_by"]) sender_id = post_data.get("sender_id", None) reply_to = get_reply_to_text( notification_type=template.template_type, sender_id=sender_id, service=service, template=template, ) notification = persist_notification( template_id=template.id, template_version=template.version, template_postage=template.postage, recipient=post_data["to"], service=service, personalisation=personalisation, notification_type=template.template_type, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_by_id=post_data["created_by"], reply_to_text=reply_to, reference=create_one_off_reference(template.template_type), ) if template.template_type == LETTER_TYPE and service.research_mode: _update_notification_status( notification, NOTIFICATION_DELIVERED, ) else: send_notification_to_queue( notification=notification, research_mode=service.research_mode, queue=template.queue_to_use(), ) return {"id": str(notification.id)}
def test_validate_template(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_not_empty = mocker.patch( 'app.notifications.validators.check_notification_content_is_not_empty') validate_template(template.id, {}, sample_service, "email") mock_check_type.assert_called_once_with("email", "email") mock_check_if_active.assert_called_once_with(template) mock_create_conent.assert_called_once_with(template, {}) mock_check_not_empty.assert_called_once_with("content")
def send_notification_to_notify_support(template_id, personalisation=None): personalisation = personalisation or {} template = dao_get_template_by_id(template_id) notify_service = dao_fetch_service_by_id(current_app.config['NOTIFY_SERVICE_ID']) validate_template(template.id, personalisation, notify_service, template.template_type) notification = persist_notification( template_id=template.id, template_version=template.version, recipient=current_app.config['NOTIFY_SUPPORT_EMAIL'], service=notify_service, personalisation=personalisation, notification_type=template.template_type, api_key_id=None, key_type=KEY_TYPE_NORMAL, reply_to_text=notify_service.get_default_reply_to_email_address() ) send_notification_to_queue(notification, False, queue=QueueNames.NOTIFY)
def post_notification(notification_type): with POST_NOTIFICATION_JSON_PARSE_DURATION_SECONDS.time(): request_json = get_valid_json() if notification_type == EMAIL_TYPE: form = validate(request_json, post_email_request) elif notification_type == SMS_TYPE: form = validate(request_json, post_sms_request) elif notification_type == LETTER_TYPE: form = validate(request_json, post_letter_request) else: abort(404) check_service_has_permission(notification_type, authenticated_service.permissions) check_rate_limiting(authenticated_service, api_user) template, template_with_content = validate_template(form['template_id'], form.get( 'personalisation', {}), authenticated_service, notification_type, check_char_count=False) reply_to = get_reply_to_text(notification_type, form, template) if notification_type == LETTER_TYPE: notification = process_letter_notification( letter_data=form, api_key=api_user, service=authenticated_service, template=template, template_with_content=template_with_content, reply_to_text=reply_to) else: notification = process_sms_or_email_notification( form=form, notification_type=notification_type, template=template, template_with_content=template_with_content, template_process_type=template.process_type, service=authenticated_service, reply_to_text=reply_to) return jsonify(notification), 201
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_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) mock_create_conent.assert_called_once_with(template, {}) mock_check_not_empty.assert_called_once_with("content") assert not mock_check_message_is_too_long.called
def post_notification(notification_type): try: request_json = request.get_json() except werkzeug.exceptions.BadRequest as e: raise BadRequestError(message="Error decoding arguments: {}".format(e.description), status_code=400) if notification_type == EMAIL_TYPE: form = validate(request_json, post_email_request) elif notification_type == SMS_TYPE: form = validate(request_json, post_sms_request) elif notification_type == LETTER_TYPE: form = validate(request_json, post_letter_request) else: abort(404) check_service_has_permission(notification_type, authenticated_service.permissions) scheduled_for = form.get("scheduled_for", None) check_service_can_schedule_notification(authenticated_service.permissions, scheduled_for) check_rate_limiting(authenticated_service, api_user) template, template_with_content = validate_template( form['template_id'], form.get('personalisation', {}), authenticated_service, notification_type, ) reply_to = get_reply_to_text(notification_type, form, template) if notification_type == LETTER_TYPE: notification = process_letter_notification( letter_data=form, api_key=api_user, template=template, reply_to_text=reply_to ) else: notification = process_sms_or_email_notification( form=form, notification_type=notification_type, api_key=api_user, template=template, service=authenticated_service, reply_to_text=reply_to ) template_with_content.values = notification.personalisation if notification_type == SMS_TYPE: create_resp_partial = functools.partial( create_post_sms_response_from_notification, from_number=reply_to ) elif notification_type == EMAIL_TYPE: create_resp_partial = functools.partial( create_post_email_response_from_notification, subject=template_with_content.subject, email_from='{}@{}'.format(authenticated_service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']) ) elif notification_type == LETTER_TYPE: create_resp_partial = functools.partial( create_post_letter_response_from_notification, subject=template_with_content.subject, ) resp = create_resp_partial( notification=notification, content=str(template_with_content), url_root=request.url_root, scheduled_for=scheduled_for ) return jsonify(resp), 201
def send_notification(notification_type): if notification_type not in [SMS_TYPE, EMAIL_TYPE]: msg = "{} notification type is not supported".format(notification_type) msg = msg + ", please use the latest version of the client" if notification_type == LETTER_TYPE else msg raise InvalidRequest(msg, 400) 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(authenticated_service, api_user) template, template_with_content = validate_template( template_id=notification_form['template'], personalisation=notification_form.get('personalisation', {}), service=authenticated_service, notification_type=notification_type) _service_allowed_to_send_to(notification_form, authenticated_service) if not service_has_permission(notification_type, authenticated_service.permissions): raise InvalidRequest( { 'service': [ "Cannot send {}".format( get_public_notify_type_text(notification_type, plural=True)) ] }, status_code=400) if notification_type == SMS_TYPE: check_if_service_can_send_to_number(authenticated_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, postage=template.postage, recipient=request.get_json()['to'], service=authenticated_service, personalisation=notification_form.get('personalisation', None), notification_type=notification_type, api_key_id=api_user.id, key_type=api_user.key_type, simulated=simulated, reply_to_text=template.reply_to_text) if not simulated: queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None send_notification_to_queue( notification=notification_model, research_mode=authenticated_service.research_mode, queue=queue_name) else: current_app.logger.debug( "POST simulated notification for id: {}".format( notification_model.id)) notification_form.update({"template_version": template.version}) return jsonify(data=get_notification_return_data( notification_model.id, notification_form, template_with_content)), 201
def send_one_off_notification(service_id, post_data): service = dao_fetch_service_by_id(service_id) template = dao_get_template_by_id_and_service_id( template_id=post_data['template_id'], service_id=service_id ) personalisation = post_data.get('personalisation', None) validate_template(template.id, personalisation, service, template.template_type) check_service_over_daily_message_limit(KEY_TYPE_NORMAL, service) validate_and_format_recipient( send_to=post_data['to'], key_type=KEY_TYPE_NORMAL, service=service, notification_type=template.template_type, allow_guest_list_recipients=False, ) postage = None client_reference = None if template.template_type == LETTER_TYPE: # Validate address and set postage to europe|rest-of-world if international letter, # otherwise persist_notification with use template postage postage = validate_address(service, personalisation) if not postage: postage = template.postage from app.utils import get_reference_from_personalisation client_reference = get_reference_from_personalisation(personalisation) validate_created_by(service, post_data['created_by']) sender_id = post_data.get('sender_id', None) reply_to = get_reply_to_text( notification_type=template.template_type, sender_id=sender_id, service=service, template=template ) notification = persist_notification( template_id=template.id, template_version=template.version, recipient=post_data['to'], service=service, personalisation=personalisation, notification_type=template.template_type, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_by_id=post_data['created_by'], reply_to_text=reply_to, reference=create_one_off_reference(template.template_type), postage=postage, client_reference=client_reference ) queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None if template.template_type == LETTER_TYPE and service.research_mode: _update_notification_status( notification, NOTIFICATION_DELIVERED, ) else: send_notification_to_queue( notification=notification, research_mode=service.research_mode, queue=queue_name, ) return {'id': str(notification.id)}
def post_notification(notification_type): request_json = get_valid_json() if notification_type == EMAIL_TYPE: form = validate(request_json, post_email_request) elif notification_type == SMS_TYPE: form = validate(request_json, post_sms_request) elif notification_type == LETTER_TYPE: form = validate(request_json, post_letter_request) else: abort(404) check_service_has_permission(notification_type, authenticated_service.permissions) scheduled_for = form.get("scheduled_for", None) check_service_can_schedule_notification(authenticated_service.permissions, scheduled_for) check_rate_limiting(authenticated_service, api_user) template, template_with_content = validate_template( form['template_id'], form.get('personalisation', {}), authenticated_service, notification_type, ) reply_to = get_reply_to_text(notification_type, form, template) if notification_type == LETTER_TYPE: notification = process_letter_notification(letter_data=form, api_key=api_user, template=template, reply_to_text=reply_to) else: notification = process_sms_or_email_notification( form=form, notification_type=notification_type, api_key=api_user, template=template, service=authenticated_service, reply_to_text=reply_to) template_with_content.values = notification.personalisation if notification_type == SMS_TYPE: create_resp_partial = functools.partial( create_post_sms_response_from_notification, from_number=reply_to, ) elif notification_type == EMAIL_TYPE: create_resp_partial = functools.partial( create_post_email_response_from_notification, subject=template_with_content.subject, email_from='{}@{}'.format( authenticated_service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']), ) elif notification_type == LETTER_TYPE: create_resp_partial = functools.partial( create_post_letter_response_from_notification, subject=template_with_content.subject, ) resp = create_resp_partial( notification=notification, url_root=request.url_root, scheduled_for=scheduled_for, content=template_with_content.content_with_placeholders_filled_in, ) return jsonify(resp), 201
def test_validate_template(sample_service): template = create_template(sample_service, template_type="email") validate_template(template.id, {}, sample_service, "email")
def post_notification(notification_type): try: request_json = request.get_json() except werkzeug.exceptions.BadRequest as e: raise BadRequestError(message="Error decoding arguments: {}".format( e.description), status_code=400) if notification_type == EMAIL_TYPE: form = validate(request_json, post_email_request) elif notification_type == SMS_TYPE: form = validate(request_json, post_sms_request) elif notification_type == LETTER_TYPE: form = validate(request_json, post_letter_request) else: abort(404) check_service_has_permission(notification_type, authenticated_service.permissions) scheduled_for = form.get("scheduled_for", None) check_service_can_schedule_notification(authenticated_service.permissions, scheduled_for) check_rate_limiting(authenticated_service, api_user) template, template_with_content = validate_template( form['template_id'], form.get('personalisation', {}), authenticated_service, notification_type, ) reply_to = get_reply_to_text(notification_type, form, template) if notification_type == LETTER_TYPE: notification = process_letter_notification(letter_data=form, api_key=api_user, template=template, reply_to_text=reply_to) else: if 'email_address' in form or 'phone_number' in form: notification = process_sms_or_email_notification( form=form, notification_type=notification_type, api_key=api_user, template=template, service=authenticated_service, reply_to_text=reply_to) else: if accept_recipient_identifiers_enabled(): notification = process_notification_with_recipient_identifier( form=form, notification_type=notification_type, api_key=api_user, template=template, service=authenticated_service, reply_to_text=reply_to) else: current_app.logger.debug( "Sending a notification without contact information is not implemented." ) return jsonify(result='error', message="Not Implemented"), 501 template_with_content.values = notification.personalisation if notification_type == SMS_TYPE: create_resp_partial = functools.partial( create_post_sms_response_from_notification, from_number=reply_to) elif notification_type == EMAIL_TYPE: create_resp_partial = functools.partial( create_post_email_response_from_notification, subject=template_with_content.subject) elif notification_type == LETTER_TYPE: create_resp_partial = functools.partial( create_post_letter_response_from_notification, subject=template_with_content.subject, ) resp = create_resp_partial(notification=notification, content=str(template_with_content), url_root=request.url_root, scheduled_for=scheduled_for) return jsonify(resp), 201