def test_create_letter_notification_creates_notification( sample_letter_template, sample_api_key): data = { 'personalisation': { 'address_line_1': 'The Queen', 'address_line_2': 'Buckingham Palace', 'postcode': 'SW1 1AA', } } template = SerialisedTemplate.from_id_and_service_id( sample_letter_template.id, sample_letter_template.service_id) notification = create_letter_notification( data, template, sample_letter_template.service, sample_api_key, NOTIFICATION_CREATED, ) assert notification == Notification.query.one() assert notification.job is None assert notification.status == NOTIFICATION_CREATED assert notification.template_id == sample_letter_template.id assert notification.template_version == sample_letter_template.version assert notification.api_key == sample_api_key assert notification.notification_type == LETTER_TYPE assert notification.key_type == sample_api_key.key_type assert notification.reference is not None assert notification.client_reference is None assert notification.postage == 'second'
def validate_template(template_id, personalisation, service, notification_type, check_char_count=True): try: template = SerialisedTemplate.from_id_and_service_id( template_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, personalisation) check_notification_content_is_not_empty(template_with_content) # validating the template in post_notifications happens before the file is uploaded for doc download, # which means the length of the message can be exceeded because it's including the file. # The document download feature is only available through the api. if check_char_count: check_is_message_too_long(template_with_content) return template, template_with_content
def test_create_content_for_notification_fails_with_missing_personalisation( sample_template_with_placeholders): template = SerialisedTemplate.from_id_and_service_id( sample_template_with_placeholders.id, sample_template_with_placeholders.service_id) with pytest.raises(BadRequestError): create_content_for_notification(template, None)
def test_create_content_for_notification_with_placeholders_passes(sample_template_with_placeholders): template = SerialisedTemplate.from_id_and_service_id( sample_template_with_placeholders.id, sample_template_with_placeholders.service_id ) content = create_content_for_notification(template, {'name': 'Bobby'}) assert content.content == template.content assert 'Bobby' in str(content)
def save_letter( self, service_id, notification_id, encrypted_notification, ): notification = encryption.decrypt(encrypted_notification) postal_address = PostalAddress.from_personalisation( Columns(notification['personalisation'])) service = SerialisedService.from_id(service_id) template = SerialisedTemplate.from_id_and_service_id( notification['template'], service_id=service.id, version=notification['template_version'], ) try: # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up status = NOTIFICATION_CREATED if not service.research_mode else NOTIFICATION_SENDING saved_notification = persist_notification( template_id=notification['template'], template_version=notification['template_version'], postage=postal_address.postage if postal_address.international else template.postage, recipient=postal_address.normalised, service=service, personalisation=notification['personalisation'], notification_type=LETTER_TYPE, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_at=datetime.utcnow(), job_id=notification['job'], job_row_number=notification['row_number'], notification_id=notification_id, reference=create_random_identifier(), reply_to_text=template.reply_to_text, status=status) if not service.research_mode: letters_pdf_tasks.get_pdf_for_templated_letter.apply_async( [str(saved_notification.id)], queue=QueueNames.CREATE_LETTERS_PDF) elif current_app.config['NOTIFY_ENVIRONMENT'] in [ 'preview', 'development' ]: research_mode_tasks.create_fake_letter_response_file.apply_async( (saved_notification.reference, ), queue=QueueNames.RESEARCH_MODE) else: update_notification_status_by_reference( saved_notification.reference, 'delivered') current_app.logger.debug("Letter {} created at {}".format( saved_notification.id, saved_notification.created_at)) except SQLAlchemyError as e: handle_exception(self, notification, notification_id, e)
def test_check_notification_content_is_not_empty_passes(notify_api, mocker, sample_service): template_id = create_template(sample_service, content="Content is not empty").id template = SerialisedTemplate.from_id_and_service_id( template_id=template_id, service_id=sample_service.id ) template_with_content = create_content_for_notification(template, {}) assert check_notification_content_is_not_empty(template_with_content) is None
def test_create_content_for_notification_allows_additional_personalisation( sample_template_with_placeholders): template = SerialisedTemplate.from_id_and_service_id( sample_template_with_placeholders.id, sample_template_with_placeholders.service_id) create_content_for_notification(template, { 'name': 'Bobby', 'Additional placeholder': 'Data' })
def save_sms(self, service_id, notification_id, encrypted_notification, sender_id=None): notification = encryption.decrypt(encrypted_notification) service = SerialisedService.from_id(service_id) template = SerialisedTemplate.from_id_and_service_id( notification['template'], service_id=service.id, version=notification['template_version'], ) if sender_id: reply_to_text = dao_get_service_sms_senders_by_id(service_id, sender_id).sms_sender else: reply_to_text = template.reply_to_text if not service_allowed_to_send_to(notification['to'], service, KEY_TYPE_NORMAL): current_app.logger.debug( "SMS {} failed as restricted service".format(notification_id) ) return try: saved_notification = persist_notification( template_id=notification['template'], template_version=notification['template_version'], recipient=notification['to'], service=service, personalisation=notification.get('personalisation'), notification_type=SMS_TYPE, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_at=datetime.utcnow(), job_id=notification.get('job', None), job_row_number=notification.get('row_number', None), notification_id=notification_id, reply_to_text=reply_to_text ) provider_tasks.deliver_sms.apply_async( [str(saved_notification.id)], queue=QueueNames.SEND_SMS if not service.research_mode else QueueNames.RESEARCH_MODE ) current_app.logger.debug( "SMS {} created at {} for job {}".format( saved_notification.id, saved_notification.created_at, notification.get('job', None)) ) except SQLAlchemyError as e: handle_exception(self, notification, notification_id, e)
def test_check_notification_content_is_not_empty_fails( notify_api, mocker, sample_service, template_content, notification_values ): template_id = create_template(sample_service, content=template_content).id template = SerialisedTemplate.from_id_and_service_id( template_id=template_id, service_id=sample_service.id ) template_with_content = create_content_for_notification(template, notification_values) with pytest.raises(BadRequestError) as e: check_notification_content_is_not_empty(template_with_content) assert e.value.status_code == 400 assert e.value.message == 'Your message is empty.' assert e.value.fields == []
def validate_template(template_id, personalisation, service, notification_type): try: template = SerialisedTemplate.from_id_and_service_id(template_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, personalisation) check_notification_content_is_not_empty(template_with_content) check_content_char_count(template_with_content) return template, template_with_content
def test_create_letter_notification_sets_billable_units( sample_letter_template, sample_api_key): data = { 'personalisation': { 'address_line_1': 'The Queen', 'address_line_2': 'Buckingham Palace', 'postcode': 'SW1 1AA', }, } template = SerialisedTemplate.from_id_and_service_id( sample_letter_template.id, sample_letter_template.service_id) notification = create_letter_notification( data, template, sample_letter_template.service, sample_api_key, NOTIFICATION_CREATED, billable_units=3, ) assert notification.billable_units == 3
def test_create_content_for_notification_passes(sample_email_template): template = SerialisedTemplate.from_id_and_service_id( sample_email_template.id, sample_email_template.service_id) content = create_content_for_notification(template, None) assert str(content) == template.content + '\n'