Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn users when GitHub/GitLab environments are not checked during Trusted Publishing #17281

Merged
merged 11 commits into from
Jan 14, 2025
Merged
96 changes: 96 additions & 0 deletions tests/unit/email/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -6218,3 +6218,99 @@ def test_pep625_emails(
},
)
]

def test_environment_ignored_in_trusted_publisher_emails(
self, pyramid_request, pyramid_config, monkeypatch
):
template_name = "environment-ignored-in-trusted-publisher"
stub_user_owner = pretend.stub(
id="id_owner",
username="username_owner",
name="",
email="[email protected]",
primary_email=pretend.stub(email="[email protected]", verified=True),
)
subject_renderer = pyramid_config.testing_add_renderer(
f"email/{template_name}/subject.txt"
)
subject_renderer.string_response = "Email Subject"
body_renderer = pyramid_config.testing_add_renderer(
f"email/{template_name}/body.txt"
)
body_renderer.string_response = "Email Body"
html_renderer = pyramid_config.testing_add_renderer(
f"email/{template_name}/body.html"
)
html_renderer.string_response = "Email HTML Body"

send_email = pretend.stub(
delay=pretend.call_recorder(lambda *args, **kwargs: None)
)
pyramid_request.task = pretend.call_recorder(lambda *args, **kwargs: send_email)
monkeypatch.setattr(email, "send_email", send_email)

pyramid_request.db = pretend.stub(
query=lambda a: pretend.stub(
filter=lambda *a: pretend.stub(
one=lambda: pretend.stub(user_id=stub_user_owner.id)
)
),
)
fakepublisher = pretend.stub(
publisher_name="fakepublisher",
repository_owner="fakeowner",
repository_name="fakerepository",
environment="",
)
fakeenvironment = "fakeenvironment"
pyramid_request.user = stub_user_owner
pyramid_request.registry.settings = {"mail.sender": "[email protected]"}

project_name = "test_project"
result = email.send_environment_ignored_in_trusted_publisher_email(
pyramid_request,
[stub_user_owner],
project_name=project_name,
publisher=fakepublisher,
environment_name=fakeenvironment,
)

assert result == {
"project_name": project_name,
"publisher": fakepublisher,
"environment_name": fakeenvironment,
}
subject_renderer.assert_()
body_renderer.assert_()
html_renderer.assert_(
project_name=project_name,
publisher=fakepublisher,
environment_name=fakeenvironment,
)
assert pyramid_request.task.calls == [
pretend.call(send_email),
]
assert send_email.delay.calls == [
pretend.call(
f"{stub_user_owner.username} <{stub_user_owner.email}>",
{
"sender": None,
"subject": "Email Subject",
"body_text": "Email Body",
"body_html": (
"<html>\n<head></head>\n"
"<body><p>Email HTML Body</p></body>\n</html>\n"
),
},
{
"tag": "account:email:sent",
"user_id": stub_user_owner.id,
"additional": {
"from_": "[email protected]",
"to": stub_user_owner.email,
"subject": "Email Subject",
"redact_ip": False,
},
},
),
]
Loading