def test_check_template_is_active_fails(sample_template): sample_template.archived = True from app.dao.templates_dao import dao_update_template dao_update_template(sample_template) with pytest.raises(BadRequestError) as e: check_template_is_active(sample_template) assert e.value.status_code == 400 assert e.value.message == 'Template has been deleted' assert e.value.fields == [{'template': 'Template has been deleted'}]
def __validate_template(form, service, notification_type): try: template = templates_dao.dao_get_template_by_id_and_service_id( template_id=form["template_id"], service_id=service.id ) except NoResultFound: message = "Template not found" raise BadRequestError(message=message, fields=[{"template": message}]) check_template_is_for_notification_type(notification_type, template.template_type) check_template_is_active(template) template_with_content = create_content_for_notification(template, form.get("personalisation", {})) if template.template_type == SMS_TYPE: check_sms_content_char_count(template_with_content.content_count) return template, template_with_content
def test_check_template_is_active_passes(sample_template): assert check_template_is_active(sample_template) is None
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 = templates_dao.dao_get_template_by_id_and_service_id( template_id=notification_form['template'], service_id=authenticated_service.id) check_template_is_for_notification_type(notification_type, template.template_type) check_template_is_active(template) template_object = create_template_object_for_notification( template, notification_form.get('personalisation', {})) _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: _service_can_send_internationally(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, 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.get_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_object)), 201