toddlers/tests/plugins/test_check_email_overrides.py
Michal Konečný d9c03e08ce Fix formatting for for black > 23
Signed-off-by: Michal Konečný <mkonecny@redhat.com>
2023-02-03 14:33:39 +01:00

200 lines
7.9 KiB
Python

from unittest.mock import MagicMock, Mock, patch
import fedora_messaging.api
import pytest
import toddlers.plugins.check_email_overrides
class TestCheckEmailOverridesToddler:
toddler_cls = toddlers.plugins.check_email_overrides.CheckEmailOverrides
def test_accepts_topic_invalid(self, toddler):
assert toddler.accepts_topic("foo.bar") is False
@pytest.mark.parametrize(
"topic",
[
"org.fedoraproject.*.toddlers.trigger.check_email_overrides",
"org.fedoraproject.prod.toddlers.trigger.check_email_overrides",
"org.fedoraproject.stg.toddlers.trigger.check_email_overrides",
],
)
def test_accepts_topic_valid(self, topic, toddler):
assert toddler.accepts_topic(topic)
def test_process_config_missing_url(self, toddler):
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = (
"org.fedoraproject.prod.toddlers.trigger.check_email_overrideschange"
)
msg.body = {"foo": "bar"}
with pytest.raises(
Exception,
match=r"Invalid toddler configuration, no `email_overrides_url` defined",
):
assert toddler.process(config={}, message=msg) is None
def test_process_invalid_download_email_overrides(self, toddler):
toddler.requests_session.get.return_value = MagicMock(
ok=False, status_code=404, text="File not fond", url="http://localhost/foo"
)
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = (
"org.fedoraproject.prod.toddlers.trigger.check_email_overrideschange"
)
msg.body = {"foo": "bar"}
config = {"email_overrides_url": "http://localhost/file"}
with pytest.raises(
Exception,
match=r"('Could not retrieve the email_overrides.toml file at: %s - returned %s', "
"'http://localhost/foo', 404)",
):
assert toddler.process(config=config, message=msg) is None
@patch("toddlers.utils.fedora_account.set_fasjson", new=Mock(return_value=True))
@patch("toddlers.utils.bugzilla_system.set_bz", new=Mock(return_value=True))
@patch("toddlers.utils.notify.send_email")
@patch("toddlers.utils.fedora_account.get_user_by_email")
@patch("toddlers.utils.bugzilla_system.get_user")
def test_process_all_good(self, get_user, get_user_by_email, send_email, toddler):
toddler.requests_session.get.return_value = MagicMock(
ok=True, status_code=200, text="'foo@bar.com' = 'bar@bar.org'"
)
get_user_by_email.return_value = True
get_user.return_value = True
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = (
"org.fedoraproject.prod.toddlers.trigger.check_email_overrideschange"
)
msg.body = {"foo": "bar"}
config = {"email_overrides_url": "http://localhost/file"}
assert toddler.process(config=config, message=msg) is None
send_email.assert_not_called()
@patch("toddlers.utils.fedora_account.set_fasjson", new=Mock(return_value=True))
@patch("toddlers.utils.bugzilla_system.set_bz", new=Mock(return_value=True))
@patch("toddlers.utils.notify.send_email")
@patch("toddlers.utils.fedora_account.get_user_by_email")
@patch("toddlers.utils.bugzilla_system.get_user")
def test_process_no_fas(self, get_user, get_user_by_email, send_email, toddler):
toddler.requests_session.get.return_value = MagicMock(
ok=True, status_code=200, text="'foo@bar.com' = 'bar@bar.org'"
)
get_user_by_email.return_value = False
get_user.return_value = True
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = (
"org.fedoraproject.prod.toddlers.trigger.check_email_overrideschange"
)
msg.body = {"foo": "bar"}
config = {
"email_overrides_url": "http://localhost/file",
"admin_email": "admin@fp.o",
"mail_server": "mail_server",
}
assert toddler.process(config=config, message=msg) is None
send_email.assert_called_with(
to_addresses=["admin@fp.o"],
from_address="admin@fp.o",
subject="Invalid entries in email_overrides.toml",
content="Dear Admin,\n\nIn the email_overrides.toml file located at:\n"
"http://localhost/file\n\nWe have identified the following issues:\n"
"- The mapping foo@bar.com (fas) -> bar@bar.org (bz) did not map to a FAS account\n\n"
"Overrides having no FAS account can be simply removed.\n\nOverrides having "
"no bugzilla account needs to be notified to the person.\n\n\n"
"Have a wonderful day and see you (maybe?) at the next run!\n\n",
mail_server="mail_server",
)
@patch("toddlers.utils.fedora_account.set_fasjson", new=Mock(return_value=True))
@patch("toddlers.utils.bugzilla_system.set_bz", new=Mock(return_value=True))
@patch("toddlers.utils.notify.send_email")
@patch("toddlers.utils.fedora_account.get_user_by_email")
@patch("toddlers.utils.bugzilla_system.get_user")
def test_process_no_bz(self, get_user, get_user_by_email, send_email, toddler):
toddler.requests_session.get.return_value = MagicMock(
ok=True, status_code=200, text="'foo@bar.com' = 'bar@bar.org'"
)
get_user_by_email.return_value = True
get_user.return_value = False
msg = fedora_messaging.api.Message()
msg.id = 123
msg.topic = (
"org.fedoraproject.prod.toddlers.trigger.check_email_overrideschange"
)
msg.body = {"foo": "bar"}
config = {
"email_overrides_url": "http://localhost/file",
"admin_email": "admin@fp.o",
"mail_server": "mail_server",
}
assert toddler.process(config=config, message=msg) is None
send_email.assert_called_with(
to_addresses=["admin@fp.o"],
from_address="admin@fp.o",
subject="Invalid entries in email_overrides.toml",
content="Dear Admin,\n\nIn the email_overrides.toml file located at:\n"
"http://localhost/file\n\nWe have identified the following issues:\n"
"- The mapping foo@bar.com (fas) -> bar@bar.org (bz) did not map to a bugzilla "
"account\n\n"
"Overrides having no FAS account can be simply removed.\n\n"
"Overrides having no bugzilla account needs to be notified to the person.\n\n\n"
"Have a wonderful day and see you (maybe?) at the next run!\n\n",
mail_server="mail_server",
)
def test_main_no_args(self, capsys):
with pytest.raises(SystemExit):
toddlers.plugins.check_email_overrides.main([])
out, err = capsys.readouterr()
assert out == ""
# Expecting something along these lines, but don't make the test too tight:
#
# usage: pytest [-h] [--dry-run] [-q | --debug] conf [username]
# pytest: error: the following arguments are required: conf
assert err.startswith("usage:")
assert "error: the following arguments are required:" in err
@patch("toml.load", new=Mock(return_value={}))
def test_main_debug(self, capsys):
with pytest.raises(
Exception,
match=r"Invalid toddler configuration, no `email_overrides_url` defined",
):
toddlers.plugins.check_email_overrides.main(["test.cfg", "--debug"])
out, err = capsys.readouterr()
assert out == ""
assert err == ""
@patch("toml.load", new=Mock(return_value={}))
def test_main(self, capsys):
with pytest.raises(
Exception,
match=r"Invalid toddler configuration, no `email_overrides_url` defined",
):
toddlers.plugins.check_email_overrides.main(["test.cfg"])
out, err = capsys.readouterr()
assert out == ""
assert err == ""