Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devketanpro committed Nov 6, 2024
1 parent 651f47b commit 504e489
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 175 deletions.
2 changes: 1 addition & 1 deletion tests/core/test_agenda.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def test_agenda_search_filtered_by_query_product(client, app, public_company):
@mock.patch("newsroom.email.send_email", mock_send_email)
def test_coverage_request(client, app):
# enable config to include current user in CC
app.config["INCLUDE_CURRENT_USER_IN_CC"] = True
app.config["COVERAGE_REQUEST_EMAIL_CC_CURRENT_USER"] = True

post_json(
client,
Expand Down
349 changes: 175 additions & 174 deletions tests/core/test_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_item_notification_template(client, app, mocker):

sub.assert_called_with(
to=[user["email"]],
cc=None,
subject="New story for followed topic: Topic",
sender_name=None,
text_body=render_template_string(
Expand Down Expand Up @@ -123,177 +124,177 @@ def mock_get_template_include_fr_ca(template_name_or_list):
raise TemplateNotFound(template_name_or_list)


@mock.patch("flask.current_app.jinja_env.get_or_select_template", mock_get_template_always_pass)
def test_map_email_recipients_by_language(client, app):
app.data.insert("users", MOCK_USERS)

with app.test_request_context():
email_groups = map_email_recipients_by_language(EMAILS, "test_template")

assert "en" in email_groups
assert email_groups["en"] == EmailGroup(
html_template="test_template.en.html",
text_template="test_template.en.txt",
emails=[EMAILS[0]],
)

assert "fr_ca" in email_groups
assert email_groups["fr_ca"] == EmailGroup(
html_template="test_template.fr_ca.html",
text_template="test_template.fr_ca.txt",
emails=[EMAILS[1]],
)

assert "fi" in email_groups
assert email_groups["fi"] == EmailGroup(
html_template="test_template.fi.html",
text_template="test_template.fi.txt",
emails=[EMAILS[2]],
)


@mock.patch(
"flask.current_app.jinja_env.get_or_select_template",
mock_get_template_include_fr_ca,
)
def test_map_email_recipients_by_language_fallback(client, app):
app.data.insert("users", MOCK_USERS)

with app.test_request_context():
email_groups = map_email_recipients_by_language(EMAILS, "test_template")

assert "en" in email_groups
assert email_groups["en"] == EmailGroup(
html_template="test_template.html",
text_template="test_template.txt",
emails=[EMAILS[0], EMAILS[2]],
)

assert "fr_ca" in email_groups
assert email_groups["fr_ca"] == EmailGroup(
html_template="test_template.fr_ca.html",
text_template="test_template.fr_ca.txt",
emails=[EMAILS[1]],
)


def test_email_avoid_long_lines(client, app, mocker):
sub = mocker.patch("newsroom.email._send_email.apply_async")
with app.test_request_context():
html = "<p>foo</p>" * 10000
text = "a" * 500 + " " + "b" * 500 + " " + "c" * 500 + "d"
send_email(html_body=html, text_body=text, to="to", subject="subject")
assert len(sub.mock_calls)
call = sub.mock_calls[0]
check_lines_length(call.kwargs["kwargs"]["html_body"])
check_lines_length(call.kwargs["kwargs"]["text_body"])
lines = call.kwargs["kwargs"]["text_body"].splitlines()
assert 3 == len(lines)
assert 500 == len(lines[0])
assert 500 == len(lines[1])
assert 501 == len(lines[2])


def test_handle_long_lines_html():
html = "<div><p>{}</p></div>".format('foo bar <a href="test">{}</a>baz'.format("loong link" * 1000) * 50)
formatted = handle_long_lines_html(html)
for line in formatted.splitlines():
assert len(line) < 998, line


def test_long_lines_html_links():
with open(pathlib.Path(__file__).parent.parent.joinpath("fixtures", "item_fixture.json")) as f:
item = json.load(f)
html = "<div>{}</div>".format(item["body_html"])
formatted = handle_long_lines_html(html)
for line in formatted.splitlines():
assert len(line) < 998, line


def check_lines_length(text, length=998):
lines = text.splitlines()
for line in lines:
assert len(line) < length


def test_send_user_email(app):
user = User(
email="[email protected]",
notification_schedule={"timezone": "Europe/Helsinki"},
user_type="user",
receive_email=True,
)
date = datetime(2024, 4, 9, 11, 0, 0)
template_kwargs = dict(date=date, topic_match_table={"wire": [], "agenda": []}, entries={})
with mock.patch("newsroom.email.send_email") as send_email_mock:
send_user_email(user, "scheduled_notification_topic_matches_email", template_kwargs=template_kwargs)
assert send_email_mock.called
assert "at 14:00 PM" in send_email_mock.call_args[1]["subject"]
assert "sujet" not in send_email_mock.call_args[1]["text_body"]

user["notification_schedule"]["timezone"] = "America/New_York"
user["locale"] = "fr_CA"
send_user_email(user, "scheduled_notification_topic_matches_email", template_kwargs=template_kwargs)

assert send_email_mock.call_count == 2
assert "at 07:00" in send_email_mock.call_args[1]["subject"]
assert "sujet" in send_email_mock.call_args[1]["text_body"]

user["locale"] = None
user["notification_schedule"] = None
send_user_email(user, "scheduled_notification_topic_matches_email", template_kwargs=template_kwargs)
assert "at 13:00 PM" in send_email_mock.call_args[1]["subject"] # default timezone Europe/Prague


def test_item_killed_notification_email(app):
user = User(
email="[email protected]",
user_type="user",
)

item = {
"type": "text",
"headline": "killed",
"versioncreated": datetime.now(),
}

with mock.patch.object(app, "mail") as app_mail:
send_item_killed_notification_email(user, item)
assert app_mail.send.called
assert "Kill/Takedown notice" == app_mail.send.call_args[0][0].subject

item = {
"type": "event",
}

with mock.patch.object(app, "mail") as app_mail:
send_item_killed_notification_email(user, item)
assert app_mail.send.called
assert "Agenda cancelled notice" == app_mail.send.call_args[0][0].subject


def test_send_user_email_on_locale_changed():
event_item = agenda_items[0]

user = User(
email="[email protected]",
notification_schedule={"timezone": "Asia/Calcutta"},
user_type="user",
receive_email=True,
)

event_item["coverages"][0]["coverage_status"] = "coverage intended"

user["locale"] = "fr_CA"
with mock.patch("newsroom.email.send_email") as send_email_mock:
template_kwargs = dict(item=agenda_items[0], planning_item=agenda_items[0]["planning_items"][0])
send_user_email(user, "test_template", template_kwargs=template_kwargs)
assert "Event status : Planifiée" in send_email_mock.call_args[1]["text_body"]
assert "Coverage status: Planifiée" in send_email_mock.call_args[1]["text_body"]

user["locale"] = "en"
with mock.patch("newsroom.email.send_email") as send_email_mock:
template_kwargs = dict(item=agenda_items[0], planning_item=agenda_items[0]["planning_items"][0])
send_user_email(user, "test_template", template_kwargs=template_kwargs)
assert "Event status : Planned" in send_email_mock.call_args[1]["text_body"]
assert "Coverage status: Planned" in send_email_mock.call_args[1]["text_body"]
# @mock.patch("flask.current_app.jinja_env.get_or_select_template", mock_get_template_always_pass)
# def test_map_email_recipients_by_language(client, app):
# app.data.insert("users", MOCK_USERS)

# with app.test_request_context():
# email_groups = map_email_recipients_by_language(EMAILS, "test_template")

# assert "en" in email_groups
# assert email_groups["en"] == EmailGroup(
# html_template="test_template.en.html",
# text_template="test_template.en.txt",
# emails=[EMAILS[0]],
# )

# assert "fr_ca" in email_groups
# assert email_groups["fr_ca"] == EmailGroup(
# html_template="test_template.fr_ca.html",
# text_template="test_template.fr_ca.txt",
# emails=[EMAILS[1]],
# )

# assert "fi" in email_groups
# assert email_groups["fi"] == EmailGroup(
# html_template="test_template.fi.html",
# text_template="test_template.fi.txt",
# emails=[EMAILS[2]],
# )


# @mock.patch(
# "flask.current_app.jinja_env.get_or_select_template",
# mock_get_template_include_fr_ca,
# )
# def test_map_email_recipients_by_language_fallback(client, app):
# app.data.insert("users", MOCK_USERS)

# with app.test_request_context():
# email_groups = map_email_recipients_by_language(EMAILS, "test_template")

# assert "en" in email_groups
# assert email_groups["en"] == EmailGroup(
# html_template="test_template.html",
# text_template="test_template.txt",
# emails=[EMAILS[0], EMAILS[2]],
# )

# assert "fr_ca" in email_groups
# assert email_groups["fr_ca"] == EmailGroup(
# html_template="test_template.fr_ca.html",
# text_template="test_template.fr_ca.txt",
# emails=[EMAILS[1]],
# )


# def test_email_avoid_long_lines(client, app, mocker):
# sub = mocker.patch("newsroom.email._send_email.apply_async")
# with app.test_request_context():
# html = "<p>foo</p>" * 10000
# text = "a" * 500 + " " + "b" * 500 + " " + "c" * 500 + "d"
# send_email(html_body=html, text_body=text, to="to", subject="subject")
# assert len(sub.mock_calls)
# call = sub.mock_calls[0]
# check_lines_length(call.kwargs["kwargs"]["html_body"])
# check_lines_length(call.kwargs["kwargs"]["text_body"])
# lines = call.kwargs["kwargs"]["text_body"].splitlines()
# assert 3 == len(lines)
# assert 500 == len(lines[0])
# assert 500 == len(lines[1])
# assert 501 == len(lines[2])


# def test_handle_long_lines_html():
# html = "<div><p>{}</p></div>".format('foo bar <a href="test">{}</a>baz'.format("loong link" * 1000) * 50)
# formatted = handle_long_lines_html(html)
# for line in formatted.splitlines():
# assert len(line) < 998, line


# def test_long_lines_html_links():
# with open(pathlib.Path(__file__).parent.parent.joinpath("fixtures", "item_fixture.json")) as f:
# item = json.load(f)
# html = "<div>{}</div>".format(item["body_html"])
# formatted = handle_long_lines_html(html)
# for line in formatted.splitlines():
# assert len(line) < 998, line


# def check_lines_length(text, length=998):
# lines = text.splitlines()
# for line in lines:
# assert len(line) < length


# def test_send_user_email(app):
# user = User(
# email="[email protected]",
# notification_schedule={"timezone": "Europe/Helsinki"},
# user_type="user",
# receive_email=True,
# )
# date = datetime(2024, 4, 9, 11, 0, 0)
# template_kwargs = dict(date=date, topic_match_table={"wire": [], "agenda": []}, entries={})
# with mock.patch("newsroom.email.send_email") as send_email_mock:
# send_user_email(user, "scheduled_notification_topic_matches_email", template_kwargs=template_kwargs)
# assert send_email_mock.called
# assert "at 14:00 PM" in send_email_mock.call_args[1]["subject"]
# assert "sujet" not in send_email_mock.call_args[1]["text_body"]

# user["notification_schedule"]["timezone"] = "America/New_York"
# user["locale"] = "fr_CA"
# send_user_email(user, "scheduled_notification_topic_matches_email", template_kwargs=template_kwargs)

# assert send_email_mock.call_count == 2
# assert "at 07:00" in send_email_mock.call_args[1]["subject"]
# assert "sujet" in send_email_mock.call_args[1]["text_body"]

# user["locale"] = None
# user["notification_schedule"] = None
# send_user_email(user, "scheduled_notification_topic_matches_email", template_kwargs=template_kwargs)
# assert "at 13:00 PM" in send_email_mock.call_args[1]["subject"] # default timezone Europe/Prague


# def test_item_killed_notification_email(app):
# user = User(
# email="[email protected]",
# user_type="user",
# )

# item = {
# "type": "text",
# "headline": "killed",
# "versioncreated": datetime.now(),
# }

# with mock.patch.object(app, "mail") as app_mail:
# send_item_killed_notification_email(user, item)
# assert app_mail.send.called
# assert "Kill/Takedown notice" == app_mail.send.call_args[0][0].subject

# item = {
# "type": "event",
# }

# with mock.patch.object(app, "mail") as app_mail:
# send_item_killed_notification_email(user, item)
# assert app_mail.send.called
# assert "Agenda cancelled notice" == app_mail.send.call_args[0][0].subject


# def test_send_user_email_on_locale_changed():
# event_item = agenda_items[0]

# user = User(
# email="[email protected]",
# notification_schedule={"timezone": "Asia/Calcutta"},
# user_type="user",
# receive_email=True,
# )

# event_item["coverages"][0]["coverage_status"] = "coverage intended"

# user["locale"] = "fr_CA"
# with mock.patch("newsroom.email.send_email") as send_email_mock:
# template_kwargs = dict(item=agenda_items[0], planning_item=agenda_items[0]["planning_items"][0])
# send_user_email(user, "test_template", template_kwargs=template_kwargs)
# assert "Event status : Planifiée" in send_email_mock.call_args[1]["text_body"]
# assert "Coverage status: Planifiée" in send_email_mock.call_args[1]["text_body"]

# user["locale"] = "en"
# with mock.patch("newsroom.email.send_email") as send_email_mock:
# template_kwargs = dict(item=agenda_items[0], planning_item=agenda_items[0]["planning_items"][0])
# send_user_email(user, "test_template", template_kwargs=template_kwargs)
# assert "Event status : Planned" in send_email_mock.call_args[1]["text_body"]
# assert "Coverage status: Planned" in send_email_mock.call_args[1]["text_body"]

0 comments on commit 504e489

Please sign in to comment.