remove letter preview tests

This commit is contained in:
Kenneth Kehl
2025-03-31 10:06:30 -07:00
parent 95d1d698ee
commit 98d10203a4

View File

@@ -1,6 +1,5 @@
import os import os
import sys import sys
from functools import partial
from time import process_time from time import process_time
from unittest import mock from unittest import mock
@@ -884,440 +883,440 @@ def test_phone_templates_normalise_whitespace(template_class):
) )
@freeze_time("2012-12-12 12:12:12") # @freeze_time("2012-12-12 12:12:12")
@mock.patch("notifications_utils.template.LetterPreviewTemplate.jinja_template.render") # @mock.patch("notifications_utils.template.LetterPreviewTemplate.jinja_template.render")
@mock.patch("notifications_utils.template.unlink_govuk_escaped") # @mock.patch("notifications_utils.template.unlink_govuk_escaped")
@mock.patch( # @mock.patch(
"notifications_utils.template.notify_letter_preview_markdown", return_value="Bar" # "notifications_utils.template.notify_letter_preview_markdown", return_value="Bar"
) # )
@pytest.mark.parametrize( # @pytest.mark.parametrize(
("values", "expected_address"), # ("values", "expected_address"),
[ # [
( # (
{}, # {},
[ # [
"<span class='placeholder-no-parenthesis'>address line 1</span>", # "<span class='placeholder-no-parenthesis'>address line 1</span>",
"<span class='placeholder-no-parenthesis'>address line 2</span>", # "<span class='placeholder-no-parenthesis'>address line 2</span>",
"<span class='placeholder-no-parenthesis'>address line 3</span>", # "<span class='placeholder-no-parenthesis'>address line 3</span>",
"<span class='placeholder-no-parenthesis'>address line 4</span>", # "<span class='placeholder-no-parenthesis'>address line 4</span>",
"<span class='placeholder-no-parenthesis'>address line 5</span>", # "<span class='placeholder-no-parenthesis'>address line 5</span>",
"<span class='placeholder-no-parenthesis'>address line 6</span>", # "<span class='placeholder-no-parenthesis'>address line 6</span>",
"<span class='placeholder-no-parenthesis'>address line 7</span>", # "<span class='placeholder-no-parenthesis'>address line 7</span>",
], # ],
), # ),
( # (
{ # {
"address line 1": "123 Fake Street", # "address line 1": "123 Fake Street",
"address line 6": "United Kingdom", # "address line 6": "United Kingdom",
}, # },
[ # [
"123 Fake Street", # "123 Fake Street",
"<span class='placeholder-no-parenthesis'>address line 2</span>", # "<span class='placeholder-no-parenthesis'>address line 2</span>",
"<span class='placeholder-no-parenthesis'>address line 3</span>", # "<span class='placeholder-no-parenthesis'>address line 3</span>",
"<span class='placeholder-no-parenthesis'>address line 4</span>", # "<span class='placeholder-no-parenthesis'>address line 4</span>",
"<span class='placeholder-no-parenthesis'>address line 5</span>", # "<span class='placeholder-no-parenthesis'>address line 5</span>",
"United Kingdom", # "United Kingdom",
"<span class='placeholder-no-parenthesis'>address line 7</span>", # "<span class='placeholder-no-parenthesis'>address line 7</span>",
], # ],
), # ),
( # (
{ # {
"address line 1": "123 Fake Street", # "address line 1": "123 Fake Street",
"address line 2": "City of Town", # "address line 2": "City of Town",
"postcode": "SW1A 1AA", # "postcode": "SW1A 1AA",
}, # },
[ # [
"123 Fake Street", # "123 Fake Street",
"City of Town", # "City of Town",
"SW1A 1AA", # "SW1A 1AA",
], # ],
), # ),
], # ],
) # )
@pytest.mark.parametrize( # @pytest.mark.parametrize(
("contact_block", "expected_rendered_contact_block"), # ("contact_block", "expected_rendered_contact_block"),
[ # [
(None, ""), # (None, ""),
("", ""), # ("", ""),
( # (
""" # """
The Pension Service # The Pension Service
Mail Handling Site A # Mail Handling Site A
Wolverhampton WV9 1LU # Wolverhampton WV9 1LU
Telephone: 0845 300 0168 # Telephone: 0845 300 0168
Email: fpc.customercare@dwp.gsi.gov.uk # Email: fpc.customercare@dwp.gsi.gov.uk
Monday - Friday 8am - 6pm # Monday - Friday 8am - 6pm
www.gov.uk # www.gov.uk
""", # """,
( # (
"The Pension Service<br>" # "The Pension Service<br>"
"Mail Handling Site A<br>" # "Mail Handling Site A<br>"
"Wolverhampton WV9 1LU<br>" # "Wolverhampton WV9 1LU<br>"
"<br>" # "<br>"
"Telephone: 0845 300 0168<br>" # "Telephone: 0845 300 0168<br>"
"Email: fpc.customercare@dwp.gsi.gov.uk<br>" # "Email: fpc.customercare@dwp.gsi.gov.uk<br>"
"Monday - Friday 8am - 6pm<br>" # "Monday - Friday 8am - 6pm<br>"
"www.gov.uk" # "www.gov.uk"
), # ),
), # ),
], # ],
) # )
@pytest.mark.parametrize( # @pytest.mark.parametrize(
("extra_args", "expected_logo_file_name", "expected_logo_class"), # ("extra_args", "expected_logo_file_name", "expected_logo_class"),
[ # [
({}, None, None), # ({}, None, None),
({"logo_file_name": "example.foo"}, "example.foo", "foo"), # ({"logo_file_name": "example.foo"}, "example.foo", "foo"),
], # ],
) # )
@pytest.mark.parametrize( # @pytest.mark.parametrize(
("additional_extra_args", "expected_date"), # ("additional_extra_args", "expected_date"),
[ # [
({}, "12 December 2012"), # ({}, "12 December 2012"),
({"date": None}, "12 December 2012"), # ({"date": None}, "12 December 2012"),
# ({'date': datetime.date.fromtimestamp(0)}, '1 January 1970'), # # ({'date': datetime.date.fromtimestamp(0)}, '1 January 1970'),
], # ],
) # )
def test_letter_preview_renderer( # def test_letter_preview_renderer(
letter_markdown, # letter_markdown,
unlink_govuk, # unlink_govuk,
jinja_template, # jinja_template,
values, # values,
expected_address, # expected_address,
contact_block, # contact_block,
expected_rendered_contact_block, # expected_rendered_contact_block,
extra_args, # extra_args,
expected_logo_file_name, # expected_logo_file_name,
expected_logo_class, # expected_logo_class,
additional_extra_args, # additional_extra_args,
expected_date, # expected_date,
): # ):
extra_args.update(additional_extra_args) # extra_args.update(additional_extra_args)
str( # str(
LetterPreviewTemplate( # LetterPreviewTemplate(
{"content": "Foo", "subject": "Subject", "template_type": "letter"}, # {"content": "Foo", "subject": "Subject", "template_type": "letter"},
values, # values,
contact_block=contact_block, # contact_block=contact_block,
**extra_args, # **extra_args,
) # )
) # )
jinja_template.assert_called_once_with( # jinja_template.assert_called_once_with(
{ # {
"address": expected_address, # "address": expected_address,
"subject": "Subject", # "subject": "Subject",
"message": "Bar", # "message": "Bar",
"date": expected_date, # "date": expected_date,
"contact_block": expected_rendered_contact_block, # "contact_block": expected_rendered_contact_block,
"admin_base_url": "http://localhost:6012", # "admin_base_url": "http://localhost:6012",
"logo_file_name": expected_logo_file_name, # "logo_file_name": expected_logo_file_name,
"logo_class": expected_logo_class, # "logo_class": expected_logo_class,
} # }
) # )
letter_markdown.assert_called_once_with(Markup("Foo\n")) # letter_markdown.assert_called_once_with(Markup("Foo\n"))
unlink_govuk.assert_not_called() # unlink_govuk.assert_not_called()
@freeze_time("2001-01-01 12:00:00.000000") # @freeze_time("2001-01-01 12:00:00.000000")
@mock.patch("notifications_utils.template.LetterPreviewTemplate.jinja_template.render") # @mock.patch("notifications_utils.template.LetterPreviewTemplate.jinja_template.render")
def test_letter_preview_renderer_without_mocks(jinja_template): # def test_letter_preview_renderer_without_mocks(jinja_template):
str( # str(
LetterPreviewTemplate( # LetterPreviewTemplate(
{"content": "Foo", "subject": "Subject", "template_type": "letter"}, # {"content": "Foo", "subject": "Subject", "template_type": "letter"},
{"addressline1": "name", "addressline2": "street", "postcode": "SW1 1AA"}, # {"addressline1": "name", "addressline2": "street", "postcode": "SW1 1AA"},
contact_block="", # contact_block="",
) # )
) # )
jinja_template_locals = jinja_template.call_args_list[0][0][0] # jinja_template_locals = jinja_template.call_args_list[0][0][0]
assert jinja_template_locals["address"] == [ # assert jinja_template_locals["address"] == [
"name", # "name",
"street", # "street",
"SW1 1AA", # "SW1 1AA",
] # ]
assert jinja_template_locals["subject"] == "Subject" # assert jinja_template_locals["subject"] == "Subject"
assert jinja_template_locals["message"] == "<p>Foo</p>" # assert jinja_template_locals["message"] == "<p>Foo</p>"
assert jinja_template_locals["date"] == "1 January 2001" # assert jinja_template_locals["date"] == "1 January 2001"
assert jinja_template_locals["contact_block"] == "" # assert jinja_template_locals["contact_block"] == ""
assert jinja_template_locals["admin_base_url"] == "http://localhost:6012" # assert jinja_template_locals["admin_base_url"] == "http://localhost:6012"
assert jinja_template_locals["logo_file_name"] is None # assert jinja_template_locals["logo_file_name"] is None
@freeze_time("2012-12-12 12:12:12") # @freeze_time("2012-12-12 12:12:12")
@mock.patch("notifications_utils.template.LetterImageTemplate.jinja_template.render") # @mock.patch("notifications_utils.template.LetterImageTemplate.jinja_template.render")
@pytest.mark.parametrize( # @pytest.mark.parametrize(
("page_count", "expected_oversized", "expected_page_numbers"), # ("page_count", "expected_oversized", "expected_page_numbers"),
[ # [
( # (
1, # 1,
False, # False,
[1], # [1],
), # ),
( # (
5, # 5,
False, # False,
[1, 2, 3, 4, 5], # [1, 2, 3, 4, 5],
), # ),
( # (
10, # 10,
False, # False,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
), # ),
( # (
11, # 11,
True, # True,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
), # ),
( # (
99, # 99,
True, # True,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
), # ),
], # ],
) # )
@pytest.mark.parametrize( # @pytest.mark.parametrize(
( # (
"postage_args", # "postage_args",
"expected_show_postage", # "expected_show_postage",
"expected_postage_class_value", # "expected_postage_class_value",
"expected_postage_description", # "expected_postage_description",
), # ),
[ # [
pytest.param({}, False, None, None), # pytest.param({}, False, None, None),
pytest.param({"postage": None}, False, None, None), # pytest.param({"postage": None}, False, None, None),
pytest.param({"postage": "first"}, True, "letter-postage-first", "first class"), # pytest.param({"postage": "first"}, True, "letter-postage-first", "first class"),
pytest.param( # pytest.param(
{"postage": "second"}, True, "letter-postage-second", "second class" # {"postage": "second"}, True, "letter-postage-second", "second class"
), # ),
pytest.param( # pytest.param(
{"postage": "europe"}, True, "letter-postage-international", "international" # {"postage": "europe"}, True, "letter-postage-international", "international"
), # ),
pytest.param( # pytest.param(
{"postage": "rest-of-world"}, # {"postage": "rest-of-world"},
True, # True,
"letter-postage-international", # "letter-postage-international",
"international", # "international",
), # ),
pytest.param( # pytest.param(
{"postage": "third"}, # {"postage": "third"},
True, # True,
"letter-postage-third", # "letter-postage-third",
"third class", # "third class",
marks=pytest.mark.xfail(raises=TypeError), # marks=pytest.mark.xfail(raises=TypeError),
), # ),
], # ],
) # )
def test_letter_image_renderer( # def test_letter_image_renderer(
jinja_template, # jinja_template,
page_count, # page_count,
expected_page_numbers, # expected_page_numbers,
expected_oversized, # expected_oversized,
postage_args, # postage_args,
expected_show_postage, # expected_show_postage,
expected_postage_class_value, # expected_postage_class_value,
expected_postage_description, # expected_postage_description,
): # ):
str( # str(
LetterImageTemplate( # LetterImageTemplate(
{"content": "Content", "subject": "Subject", "template_type": "letter"}, # {"content": "Content", "subject": "Subject", "template_type": "letter"},
image_url="http://example.com/endpoint.png", # image_url="http://example.com/endpoint.png",
page_count=page_count, # page_count=page_count,
contact_block="10 Downing Street", # contact_block="10 Downing Street",
**postage_args, # **postage_args,
) # )
) # )
jinja_template.assert_called_once_with( # jinja_template.assert_called_once_with(
{ # {
"image_url": "http://example.com/endpoint.png", # "image_url": "http://example.com/endpoint.png",
"page_numbers": expected_page_numbers, # "page_numbers": expected_page_numbers,
"address": [ # "address": [
"<span class='placeholder-no-parenthesis'>address line 1</span>", # "<span class='placeholder-no-parenthesis'>address line 1</span>",
"<span class='placeholder-no-parenthesis'>address line 2</span>", # "<span class='placeholder-no-parenthesis'>address line 2</span>",
"<span class='placeholder-no-parenthesis'>address line 3</span>", # "<span class='placeholder-no-parenthesis'>address line 3</span>",
"<span class='placeholder-no-parenthesis'>address line 4</span>", # "<span class='placeholder-no-parenthesis'>address line 4</span>",
"<span class='placeholder-no-parenthesis'>address line 5</span>", # "<span class='placeholder-no-parenthesis'>address line 5</span>",
"<span class='placeholder-no-parenthesis'>address line 6</span>", # "<span class='placeholder-no-parenthesis'>address line 6</span>",
"<span class='placeholder-no-parenthesis'>address line 7</span>", # "<span class='placeholder-no-parenthesis'>address line 7</span>",
], # ],
"contact_block": "10 Downing Street", # "contact_block": "10 Downing Street",
"date": "12 December 2012", # "date": "12 December 2012",
"subject": "Subject", # "subject": "Subject",
"message": "<p>Content</p>", # "message": "<p>Content</p>",
"show_postage": expected_show_postage, # "show_postage": expected_show_postage,
"postage_class_value": expected_postage_class_value, # "postage_class_value": expected_postage_class_value,
"postage_description": expected_postage_description, # "postage_description": expected_postage_description,
} # }
) # )
@freeze_time("2012-12-12 12:12:12") # @freeze_time("2012-12-12 12:12:12")
@mock.patch("notifications_utils.template.LetterImageTemplate.jinja_template.render") # @mock.patch("notifications_utils.template.LetterImageTemplate.jinja_template.render")
@pytest.mark.parametrize( # @pytest.mark.parametrize(
"postage_argument", # "postage_argument",
[ # [
None, # None,
"first", # "first",
"second", # "second",
"europe", # "europe",
"rest-of-world", # "rest-of-world",
], # ],
) # )
def test_letter_image_renderer_shows_international_post( # def test_letter_image_renderer_shows_international_post(
jinja_template, # jinja_template,
postage_argument, # postage_argument,
): # ):
str( # str(
LetterImageTemplate( # LetterImageTemplate(
{"content": "Content", "subject": "Subject", "template_type": "letter"}, # {"content": "Content", "subject": "Subject", "template_type": "letter"},
{ # {
"address line 1": "123 Example Street", # "address line 1": "123 Example Street",
"address line 2": "Lima", # "address line 2": "Lima",
"address line 3": "Peru", # "address line 3": "Peru",
}, # },
image_url="http://example.com/endpoint.png", # image_url="http://example.com/endpoint.png",
page_count=1, # page_count=1,
postage=postage_argument, # postage=postage_argument,
) # )
) # )
assert jinja_template.call_args_list[0][0][0]["postage_description"] == ( # assert jinja_template.call_args_list[0][0][0]["postage_description"] == (
"international" # "international"
) # )
def test_letter_image_template_renders_visually_hidden_address(): # def test_letter_image_template_renders_visually_hidden_address():
template = BeautifulSoup( # template = BeautifulSoup(
str( # str(
LetterImageTemplate( # LetterImageTemplate(
{"content": "", "subject": "", "template_type": "letter"}, # {"content": "", "subject": "", "template_type": "letter"},
{ # {
"address_line_1": "line 1", # "address_line_1": "line 1",
"address_line_2": "line 2", # "address_line_2": "line 2",
"postcode": "postcode", # "postcode": "postcode",
}, # },
image_url="http://example.com/endpoint.png", # image_url="http://example.com/endpoint.png",
page_count=1, # page_count=1,
) # )
), # ),
features="html.parser", # features="html.parser",
) # )
assert str(template.select_one(".govuk-visually-hidden ul")) == ( # assert str(template.select_one(".govuk-visually-hidden ul")) == (
"<ul>" "<li>line 1</li>" "<li>line 2</li>" "<li>postcode</li>" "</ul>" # "<ul>" "<li>line 1</li>" "<li>line 2</li>" "<li>postcode</li>" "</ul>"
) # )
@pytest.mark.parametrize( # @pytest.mark.parametrize(
"page_image_url", # "page_image_url",
[ # [
pytest.param("http://example.com/endpoint.png?page=0", marks=pytest.mark.xfail), # pytest.param("http://example.com/endpoint.png?page=0", marks=pytest.mark.xfail),
"http://example.com/endpoint.png?page=1", # "http://example.com/endpoint.png?page=1",
"http://example.com/endpoint.png?page=2", # "http://example.com/endpoint.png?page=2",
"http://example.com/endpoint.png?page=3", # "http://example.com/endpoint.png?page=3",
pytest.param("http://example.com/endpoint.png?page=4", marks=pytest.mark.xfail), # pytest.param("http://example.com/endpoint.png?page=4", marks=pytest.mark.xfail),
], # ],
) # )
def test_letter_image_renderer_pagination(page_image_url): # def test_letter_image_renderer_pagination(page_image_url):
assert page_image_url in str( # assert page_image_url in str(
LetterImageTemplate( # LetterImageTemplate(
{"content": "", "subject": "", "template_type": "letter"}, # {"content": "", "subject": "", "template_type": "letter"},
image_url="http://example.com/endpoint.png", # image_url="http://example.com/endpoint.png",
page_count=3, # page_count=3,
) # )
) # )
@pytest.mark.parametrize( # @pytest.mark.parametrize(
("partial_call", "expected_exception", "expected_message"), # ("partial_call", "expected_exception", "expected_message"),
[ # [
( # (
partial(LetterImageTemplate), # partial(LetterImageTemplate),
TypeError, # TypeError,
"image_url is required", # "image_url is required",
), # ),
( # (
partial(LetterImageTemplate, page_count=1), # partial(LetterImageTemplate, page_count=1),
TypeError, # TypeError,
"image_url is required", # "image_url is required",
), # ),
( # (
partial(LetterImageTemplate, image_url="foo"), # partial(LetterImageTemplate, image_url="foo"),
TypeError, # TypeError,
"page_count is required", # "page_count is required",
), # ),
( # (
partial(LetterImageTemplate, image_url="foo", page_count="foo"), # partial(LetterImageTemplate, image_url="foo", page_count="foo"),
ValueError, # ValueError,
"invalid literal for int() with base 10: 'foo'", # "invalid literal for int() with base 10: 'foo'",
), # ),
( # (
partial( # partial(
LetterImageTemplate, image_url="foo", page_count=1, postage="third" # LetterImageTemplate, image_url="foo", page_count=1, postage="third"
), # ),
TypeError, # TypeError,
"postage must be None, 'first', 'second', 'europe' or 'rest-of-world'", # "postage must be None, 'first', 'second', 'europe' or 'rest-of-world'",
), # ),
], # ],
) # )
def test_letter_image_renderer_requires_arguments( # def test_letter_image_renderer_requires_arguments(
partial_call, # partial_call,
expected_exception, # expected_exception,
expected_message, # expected_message,
): # ):
with pytest.raises(expected_exception) as exception: # with pytest.raises(expected_exception) as exception:
partial_call({"content": "", "subject": "", "template_type": "letter"}) # partial_call({"content": "", "subject": "", "template_type": "letter"})
assert str(exception.value) == expected_message # assert str(exception.value) == expected_message
@pytest.mark.parametrize( # @pytest.mark.parametrize(
("postage", "expected_attribute_value", "expected_postage_text"), # ("postage", "expected_attribute_value", "expected_postage_text"),
[ # [
(None, None, None), # (None, None, None),
( # (
"first", # "first",
["letter-postage", "letter-postage-first"], # ["letter-postage", "letter-postage-first"],
"Postage: first class", # "Postage: first class",
), # ),
( # (
"second", # "second",
["letter-postage", "letter-postage-second"], # ["letter-postage", "letter-postage-second"],
"Postage: second class", # "Postage: second class",
), # ),
( # (
"europe", # "europe",
["letter-postage", "letter-postage-international"], # ["letter-postage", "letter-postage-international"],
"Postage: international", # "Postage: international",
), # ),
( # (
"rest-of-world", # "rest-of-world",
["letter-postage", "letter-postage-international"], # ["letter-postage", "letter-postage-international"],
"Postage: international", # "Postage: international",
), # ),
], # ],
) # )
def test_letter_image_renderer_passes_postage_to_html_attribute( # def test_letter_image_renderer_passes_postage_to_html_attribute(
postage, # postage,
expected_attribute_value, # expected_attribute_value,
expected_postage_text, # expected_postage_text,
): # ):
template = BeautifulSoup( # template = BeautifulSoup(
str( # str(
LetterImageTemplate( # LetterImageTemplate(
{"content": "", "subject": "", "template_type": "letter"}, # {"content": "", "subject": "", "template_type": "letter"},
image_url="foo", # image_url="foo",
page_count=1, # page_count=1,
postage=postage, # postage=postage,
) # )
), # ),
features="html.parser", # features="html.parser",
) # )
if expected_attribute_value: # if expected_attribute_value:
assert ( # assert (
template.select_one(".letter-postage")["class"] == expected_attribute_value # template.select_one(".letter-postage")["class"] == expected_attribute_value
) # )
assert ( # assert (
template.select_one(".letter-postage").text.strip() == expected_postage_text # template.select_one(".letter-postage").text.strip() == expected_postage_text
) # )
else: # else:
assert not template.select(".letter-postage") # assert not template.select(".letter-postage")
@pytest.mark.parametrize( @pytest.mark.parametrize(