def test_letter_preview_uses_non_breaking_hyphens(): assert 'non\u2011breaking' in str(LetterPreviewTemplate( {'content': 'non-breaking', 'subject': 'foo'} )) assert '–' in str(LetterPreviewTemplate( {'content': 'en dash - not hyphen - when set with spaces', 'subject': 'foo'} ))
def get_preview_of_content(notification): if notification['template'].get('redact_personalisation'): notification['personalisation'] = {} if notification['template']['is_precompiled_letter']: return notification['client_reference'] if notification['template']['template_type'] == 'sms': return str( SMSBodyPreviewTemplate( notification['template'], notification['personalisation'], )) if notification['template']['template_type'] == 'email': return Markup( EmailPreviewTemplate( notification['template'], notification['personalisation'], redact_missing_personalisation=True, ).subject) if notification['template']['template_type'] == 'letter': return Markup( LetterPreviewTemplate( notification['template'], notification['personalisation'], ).subject)
def test_multiple_newlines_in_letters( content, expected_preview_markup, ): assert expected_preview_markup in str(LetterPreviewTemplate( {'content': content, 'subject': 'foo'} ))
def test_letter_address_format(address, expected): template = LetterPreviewTemplate( {'content': '', 'subject': ''}, address, ) assert template.values_with_default_optional_address_lines == expected # check that we can actually build a valid letter from this data assert str(template)
def get_template( template, service, show_recipient=False, letter_preview_url=None, page_count=1, redact_missing_personalisation=False, email_reply_to=None, sms_sender=None, ): # Local Jinja support - add USE_LOCAL_JINJA_TEMPLATES=True to .env # Add a folder to the project root called 'jinja_templates' with copies from notification-utls repo of: # 'email_preview_template.jinja2' # 'sms_preview_template.jinja2' debug_template_path = path.dirname( path.abspath(__file__)) if os.environ.get( "USE_LOCAL_JINJA_TEMPLATES") == "True" else None if "email" == template["template_type"]: return EmailPreviewTemplate( template, from_name=service.name, from_address="{}@notifications.service.gov.uk".format( service.email_from), show_recipient=show_recipient, redact_missing_personalisation=redact_missing_personalisation, reply_to=email_reply_to, jinja_path=debug_template_path, **get_email_logo_options(service), ) if "sms" == template["template_type"]: return SMSPreviewTemplate( template, prefix=service.name, show_prefix=service.prefix_sms, sender=sms_sender, show_sender=bool(sms_sender), show_recipient=show_recipient, redact_missing_personalisation=redact_missing_personalisation, jinja_path=debug_template_path, ) if "letter" == template["template_type"]: if letter_preview_url: return LetterImageTemplate( template, image_url=letter_preview_url, page_count=int(page_count), contact_block=template["reply_to_text"], postage=template["postage"], ) else: return LetterPreviewTemplate( template, contact_block=template["reply_to_text"], admin_base_url=current_app.config["ADMIN_BASE_URL"], redact_missing_personalisation=redact_missing_personalisation, )
def test_letter_preview_strips_dvla_markup(mock_strip_dvla_markup): assert 'FOOBARBAZ' in str( LetterPreviewTemplate({ "content": 'content', 'subject': 'subject', }, )) assert mock_strip_dvla_markup.call_args_list == [ mock.call(Markup('subject')), mock.call('content'), ]
def get_html(json): filename = f'{json["filename"]}.svg' if json['filename'] else None return str(LetterPreviewTemplate( json['template'], values=json['values'] or None, contact_block=json['letter_contact_block'], # letter assets are hosted on s3 admin_base_url=current_app.config['LETTER_LOGO_URL'], logo_file_name=filename, date=dateutil.parser.parse(json['date']) if json.get('date') else None, ))
def test_from_utils_template_calls_through( mocker, mock_get_service_letter_template, partial_call, expected_page_argument, ): mock_from_db = mocker.patch('app.template_previews.TemplatePreview.from_database_object') template = LetterPreviewTemplate(mock_get_service_letter_template(None, None)['data']) ret = partial_call(template, 'foo') assert ret == mock_from_db.return_value mock_from_db.assert_called_once_with(template._template, 'foo', template.values, page=expected_page_argument)
def get_template( template, service, show_recipient=False, letter_preview_url=None, page_count=1, redact_missing_personalisation=False, email_reply_to=None, sms_sender=None, ): if 'email' == template['template_type']: return EmailPreviewTemplate( template, from_name=service.name, from_address='{}@notifications.service.gov.uk'.format(service.email_from), show_recipient=show_recipient, redact_missing_personalisation=redact_missing_personalisation, reply_to=email_reply_to, ) if 'sms' == template['template_type']: return SMSPreviewTemplate( template, prefix=service.name, show_prefix=service.prefix_sms, sender=sms_sender, show_sender=bool(sms_sender), show_recipient=show_recipient, redact_missing_personalisation=redact_missing_personalisation, ) if 'letter' == template['template_type']: if letter_preview_url: return LetterImageTemplate( template, image_url=letter_preview_url, page_count=int(page_count), contact_block=template['reply_to_text'], postage=template['postage'], ) else: return LetterPreviewTemplate( template, contact_block=template['reply_to_text'], admin_base_url=current_app.config['ADMIN_BASE_URL'], redact_missing_personalisation=redact_missing_personalisation, ) if 'broadcast' == template['template_type']: return BroadcastPreviewTemplate( template, )
def view_letter_template(filetype): """ POST /preview.pdf with the following json blob { "letter_contact_block": "contact block for service, if any", "template": { "template data, as it comes out of the database" } "values": {"dict of placeholder values"} } """ try: if filetype not in ('pdf', 'png'): abort(404) if filetype == 'pdf' and request.args.get('page') is not None: abort(400) json = request.get_json() validate_preview_request(json) try: logo_file_name = current_app.config['LOGO_FILENAMES'][ json['dvla_org_id']] except KeyError: abort(400) template = LetterPreviewTemplate( json['template'], values=json['values'] or None, contact_block=json['letter_contact_block'], # we get the images of our local server to keep network topography clean, # which is just http://localhost:6013 admin_base_url='http://localhost:6013', logo_file_name=logo_file_name, ) string = str(template) html = HTML(string=string) pdf = render_pdf(html) if filetype == 'pdf': return pdf elif filetype == 'png': return send_file(**png_from_pdf( pdf, page_number=int(request.args.get('page', 1)))) except Exception as e: current_app.logger.error(str(e)) raise e
def test_letter_preview_renderer( strip_pipes, prepare_newlines, letter_markdown, unlink_govuk, remove_empty_lines, jinja_template, values, expected_address, contact_block, expected_rendered_contact_block, extra_args, expected_logo_file_name, ): str( LetterPreviewTemplate({ 'content': 'Foo', 'subject': 'Subject' }, values, contact_block=contact_block, **extra_args)) remove_empty_lines.assert_called_once_with(expected_address) jinja_template.assert_called_once_with({ 'address': '123 Street', 'subject': 'Subject', 'message': 'Bar', 'date': '1 January 2001', 'contact_block': expected_rendered_contact_block, 'admin_base_url': 'http://localhost:6012', 'logo_file_name': expected_logo_file_name, }) prepare_newlines.assert_called_once_with('Foo') letter_markdown.assert_called_once_with('Baz') unlink_govuk.assert_not_called() assert strip_pipes.call_args_list == [ mock.call('Subject'), mock.call('Foo'), mock.call(expected_address), mock.call(expected_rendered_contact_block), ]
def test_letter_preview_renderer_without_mocks(jinja_template): str(LetterPreviewTemplate( {'content': 'Foo', 'subject': 'Subject'}, {'addressline1': 'name', 'addressline2': 'street', 'postcode': 'SW1 1AA'}, contact_block='', )) jinja_template_locals = jinja_template.call_args_list[0][0][0] assert jinja_template_locals['address'] == ( '<ul>' '<li>name</li>' '<li>street</li>' '<li>SW1 1AA</li>' '</ul>' ) assert jinja_template_locals['subject'] == 'Subject' assert jinja_template_locals['message'] == "Foo" assert jinja_template_locals['date'] == '1 January 2001' assert jinja_template_locals['contact_block'] == '' assert jinja_template_locals['admin_base_url'] == 'http://localhost:6012' assert jinja_template_locals['logo_file_name'] == 'hm-government.svg'
def test_nested_lists_in_lettr_markup(): template_content = str(LetterPreviewTemplate({ 'content': ( 'nested list:\n' '\n' '1. one\n' '2. two\n' '3. three\n' ' - three one\n' ' - three two\n' ' - three three\n' ), 'subject': 'foo', })) assert ( ' <p>\n' ' 1 January 2001\n' ' </p>\n' ' <h1>\n' ' foo\n' ' </h1>\n' ' nested list:<div class=\'linebreak‑block\'> </div><ol>\n' '<li>one</li>\n' '<li>two</li>\n' '<li>three<ul>\n' '<li>three one</li>\n' '<li>three two</li>\n' '<li>three three</li>\n' '</ul></li>\n' '</ol>\n' '\n' ' </div>\n' ' </body>\n' '</html>' ) in template_content
def test_lists_in_combination_with_other_elements_in_letters(markdown, expected): assert expected in str(LetterPreviewTemplate( {'content': markdown, 'subject': 'Hello'}, {}, ))
), ( HTMLEmailTemplate( {"content": "((content))", "subject": "((subject))"}, ), ['content', 'subject'], ), ( EmailPreviewTemplate( {"content": "((content))", "subject": "((subject))"}, ), ['content', 'subject'], ), ( LetterPreviewTemplate( {"content": "((content))", "subject": "((subject))"}, contact_block='((contact_block))', ), ['content', 'subject', 'contact_block'], ), ( LetterImageTemplate( {"content": "((content))", "subject": "((subject))"}, contact_block='((contact_block))', image_url='http://example.com', page_count=99, ), ['content', 'subject', 'contact_block'], ), ]) def test_templates_extract_placeholders( template_instance,