toddlers/tests/plugins/test_packagers_without_bugzilla.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

454 lines
19 KiB
Python

from unittest.mock import call, Mock, patch
import pytest
from toddlers.plugins.packagers_without_bugzilla import main, PackagersWithoutBugzilla
class TestPackagersWithoutBugzillaToddler:
toddler_cls = PackagersWithoutBugzilla
def test_accepts_topic_invalid(self, toddler):
assert toddler.accepts_topic("foo.bar") is False
@pytest.mark.parametrize(
"topic",
[
"org.fedoraproject.*.toddlers.trigger.packagers_without_bugzilla",
"org.fedoraproject.prod.toddlers.trigger.packagers_without_bugzilla",
"org.fedoraproject.stg.toddlers.trigger.packagers_without_bugzilla",
],
)
def test_accepts_topic_valid(self, toddler, topic):
assert toddler.accepts_topic(topic)
def test_process_no_email_override(self, toddler, capsys):
with pytest.raises(KeyError, match=r"'email_overrides_file'"):
toddler.process(config={}, message=None, username=False)
out, err = capsys.readouterr()
assert out == "Failed to load the file containing the email-overrides\n"
assert err == ""
def test_process_no_email_override_file(self, toddler, capsys):
with pytest.raises(
FileNotFoundError, match=r"No such file or directory: 'test'"
):
toddler.process(
config={"email_overrides_file": "test"},
message=None,
username=False,
)
out, err = capsys.readouterr()
assert out == "Failed to load the file containing the email-overrides\n"
assert err == ""
@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.bugzilla_system.get_user")
@patch("toddlers.utils.bugzilla_system.get_group_member")
@patch("toddlers.utils.fedora_account.get_bz_email_group")
@patch("toddlers.utils.fedora_account.get_bz_email_user")
@patch("toml.load")
def test_process_ignorable_namespaces(
self,
toml_load,
get_bz_email_user,
get_bz_email_group,
bz_get_group_member,
bz_get_user,
send_email,
toddler,
):
toml_load.return_value = {}
get_bz_email_user.side_effect = [
"besser82@fp.o",
"churchyard@fp.o",
"dwmw2@fp.o",
]
get_bz_email_group.side_effect = ["python-sig@lists.fp.o"]
bz_get_group_member.return_value = ["dwmw2@fp.o", "besser82@fp.o"]
bz_get_user.side_effect = [Exception("ahah"), False, False, False]
req = Mock()
req.ok = True
req.json.return_value = {
"rpms": {
"0xFFFF": ["dwmw2"],
"2048-cli": ["besser82"],
"CuraEngine": ["@python-sig", "churchyard"],
}
}
toddler.requests_session.get.return_value = req
toddler.process(
config={
"email_overrides_file": "test",
"bugzilla_group": "fedora_contrib",
"dist_git_url": "https://src.fp.o",
"admin_email": "admin@fp.o",
"mail_server": "mail_server",
"ignorable_namespaces": ["rpms"],
},
message=None,
username=False,
)
toml_load.assert_called_with("test")
get_bz_email_user.assert_not_called()
get_bz_email_group.assert_not_called()
bz_get_group_member.assert_called_with("fedora_contrib")
bz_get_user.assert_not_called()
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.bugzilla_system.get_user")
@patch("toddlers.utils.bugzilla_system.get_group_member")
@patch("toddlers.utils.fedora_account.get_bz_email_group")
@patch("toddlers.utils.fedora_account.get_bz_email_user")
@patch("toml.load")
def test_process(
self,
toml_load,
get_bz_email_user,
get_bz_email_group,
bz_get_group_member,
bz_get_user,
send_email,
toddler,
):
toml_load.return_value = {}
get_bz_email_user.side_effect = [
"besser82@fp.o",
"churchyard@fp.o",
"dwmw2@fp.o",
]
get_bz_email_group.side_effect = ["python-sig@lists.fp.o"]
bz_get_group_member.return_value = ["dwmw2@fp.o", "besser82@fp.o"]
bz_get_user.side_effect = [Exception("ahah"), False, False, False]
req = Mock()
req.ok = True
req.json.return_value = {
"rpms": {
"0xFFFF": ["dwmw2"],
"2048-cli": ["besser82"],
"CuraEngine": ["@python-sig", "churchyard"],
}
}
toddler.requests_session.get.return_value = req
toddler.process(
config={
"email_overrides_file": "test",
"bugzilla_group": "fedora_contrib",
"dist_git_url": "https://src.fp.o",
"admin_email": "admin@fp.o",
"mail_server": "mail_server",
},
message=None,
username=False,
)
toml_load.assert_called_with("test")
get_bz_email_user.assert_called()
get_bz_email_user.assert_has_calls(
calls=[call("besser82", {}), call("churchyard", {}), call("dwmw2", {})]
)
get_bz_email_group.assert_called()
get_bz_email_group.assert_has_calls(calls=[call("python-sig", {})])
bz_get_group_member.assert_called_with("fedora_contrib")
bz_get_user.assert_called()
bz_get_user.assert_has_calls(
calls=[
call(user_email="churchyard@fp.o"),
call(user_email="python-sig@lists.fp.o"),
]
)
send_email.assert_called()
send_email.assert_has_calls(
calls=[
call(
to_addresses=["churchyard@fp.o"],
from_address="admin@fp.o",
subject="Fedora Account System and Bugzilla Mismatch",
content="Hello churchyard,\n\n"
"We have identified you[1] as either a Fedora packager or someone who has "
"asked to\n"
"be included in the CC list of tickets created for one or more component on\n"
"bugzilla. Fedora packagers are granted special permissions on the Fedora bugs"
" in\n"
"bugzilla.\n"
"However, to enable these functionalities (granting you these permissions or\n"
"including you to the CC list of your packages of interest), we need to have "
"your\n"
"bugzilla email address stored in the Fedora Account System[2].\n"
"At the moment you have:\n\n"
"churchyard@fp.o\n\n"
"which bugzilla is telling us is not an account in bugzilla. If you could\n"
"please set up an account in bugzilla with this address or change your email\n"
"address (either Red Hat Bugzilla email or Fedora Account email) on your "
"Fedora\n"
"Account to match an existing bugzilla account this "
"would let us go forward.\n\n"
"Note #1: this message is being generated by an automated script. You'll\n"
"continue getting this message until the problem is resolved. Sorry for the\n"
"inconvenience.\n\nThank you,\nThe Fedora Account System\nadmin@fp.o\n\n\n"
"[1] the source of this information is the following JSON file:\n"
" https://src.fedoraproject.org/extras/pagure_bz.json\n"
" We are happy to tell you exactly which packages are linked to your "
"account\n"
" if you wish.\n[2] https://accounts.fedoraproject.org/\n",
mail_server="mail_server",
),
call(
to_addresses=["python-sig@lists.fp.o"],
from_address="admin@fp.o",
subject="Fedora Account System and Bugzilla Mismatch",
content="Hello @python-sig,\n\n"
"We have identified you[1] as either a Fedora packager or someone who has "
"asked to\n"
"be included in the CC list of tickets created for one or more component on\n"
"bugzilla. Fedora packagers are granted special permissions on the Fedora bugs "
"in\n"
"bugzilla.\n"
"However, to enable these functionalities (granting you these permissions or\n"
"including you to the CC list of your packages of interest), we need to have "
"your\n"
"bugzilla email address stored in the Fedora Account System[2].\n"
"At the moment you have:\n\n"
"python-sig@lists.fp.o\n\n"
"which bugzilla is telling us is not an account in bugzilla. If you could\n"
"please set up an account in bugzilla with this address or change your email\n"
"address (either Red Hat Bugzilla email or Fedora Account email) on your "
"Fedora\n"
"Account to match an existing bugzilla account this "
"would let us go forward.\n\n"
"Note #1: this message is being generated by an automated script. You'll\n"
"continue getting this message until the problem is resolved. Sorry for the\n"
"inconvenience.\n\nThank you,\nThe Fedora Account System\nadmin@fp.o\n\n\n"
"[1] the source of this information is the following JSON file:\n"
" https://src.fedoraproject.org/extras/pagure_bz.json\n"
" We are happy to tell you exactly which packages are linked to your "
"account\n"
" if you wish.\n[2] https://accounts.fedoraproject.org/\n",
mail_server="mail_server",
),
call(
to_addresses=["admin@fp.o"],
from_address="admin@fp.o",
subject="Toddlers found some packagers without bugzilla account",
content="Dear Admin,\n\n"
"The packagers_without_bugzilla toddler just ran and noticed some packagers "
"without\n"
"valid bugzilla account. The list of them is here:\n"
"- churchyard (email: churchyard@fp.o) has no corresponding bugzilla account\n"
"- @python-sig (email: python-sig@lists.fp.o) has no corresponding bugzilla "
"account\n\n"
"Each person on this list has been notified and, hopefully will fix the "
"situation.\n"
"Failing to do so, this notification may be used a record for a "
"potential\n"
"non-responsive packager procedure.\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.bugzilla_system.get_user")
@patch("toddlers.utils.bugzilla_system.get_group_member")
@patch("toddlers.utils.fedora_account.get_bz_email_group")
@patch("toddlers.utils.fedora_account.get_bz_email_user")
@patch("toml.load")
def test_process_username_no_bz_email(
self,
toml_load,
get_bz_email_user,
get_bz_email_group,
bz_get_group_member,
bz_get_user,
send_email,
toddler,
):
toml_load.return_value = {}
get_bz_email_user.side_effect = [None]
get_bz_email_group.side_effect = ["python-sig@lists.fp.o"]
bz_get_group_member.return_value = ["dwmw2@fp.o", "besser82@fp.o"]
bz_get_user.return_value = False
req = Mock()
req.ok = True
req.json.return_value = {
"rpms": {
"0xFFFF": ["dwmw2"],
"2048-cli": ["besser82"],
"CuraEngine": ["@python-sig", "churchyard"],
}
}
toddler.requests_session.get.return_value = req
toddler.process(
config={
"email_overrides_file": "test",
"bugzilla_group": "fedora_contrib",
"dist_git_url": "https://src.fp.o",
"admin_email": "admin@fp.o",
"mail_server": "mail_server",
},
message=None,
username="nils",
)
toml_load.assert_called_with("test")
bz_get_group_member.assert_called_with("fedora_contrib")
get_bz_email_user.assert_called()
get_bz_email_user.assert_has_calls(calls=[call("nils", {})])
get_bz_email_group.assert_not_called()
bz_get_group_member.assert_called_with("fedora_contrib")
bz_get_user.assert_not_called()
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.bugzilla_system.get_user")
@patch("toddlers.utils.bugzilla_system.get_group_member")
@patch("toddlers.utils.fedora_account.get_bz_email_group")
@patch("toddlers.utils.fedora_account.get_bz_email_user")
@patch("toml.load")
def test_process_username_ignored(
self,
toml_load,
get_bz_email_user,
get_bz_email_group,
bz_get_group_member,
bz_get_user,
send_email,
toddler,
):
toml_load.return_value = {}
get_bz_email_user.side_effect = [
"dwmw2@fp.o",
]
get_bz_email_group.side_effect = ["python-sig@lists.fp.o"]
bz_get_group_member.return_value = ["churchyard@fp.o"]
bz_get_user.return_value = False
req = Mock()
req.ok = True
req.json.return_value = {"rpms": {"0xFFFF": ["dwmw2"]}}
toddler.requests_session.get.return_value = req
toddler.process(
config={
"email_overrides_file": "test",
"bugzilla_group": "fedora_contrib",
"dist_git_url": "https://src.fp.o",
"admin_email": "admin@fp.o",
"mail_server": "mail_server",
"ignorable_accounts": ["dwmw2"],
},
message=None,
)
toml_load.assert_called_with("test")
bz_get_group_member.assert_called_with("fedora_contrib")
get_bz_email_user.assert_called()
get_bz_email_user.assert_has_calls(calls=[call("dwmw2", {})])
get_bz_email_group.assert_not_called()
bz_get_user.assert_called()
bz_get_user.assert_has_calls(calls=[call(user_email="dwmw2@fp.o")])
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.bugzilla_system.get_user")
@patch("toddlers.utils.bugzilla_system.get_group_member")
@patch("toddlers.utils.fedora_account.get_bz_email_group")
@patch("toddlers.utils.fedora_account.get_bz_email_user")
@patch("toml.load")
def test_process_username_group_no_bz_email(
self,
toml_load,
get_bz_email_user,
get_bz_email_group,
bz_get_group_member,
bz_get_user,
send_email,
toddler,
):
toml_load.return_value = {}
get_bz_email_group.side_effect = [None]
bz_get_group_member.return_value = ["dwmw2@fp.o", "besser82@fp.o"]
bz_get_user.return_value = False
req = Mock()
req.ok = True
req.json.return_value = {
"rpms": {
"0xFFFF": ["dwmw2"],
"2048-cli": ["besser82"],
"CuraEngine": ["@python-sig", "churchyard"],
}
}
toddler.requests_session.get.return_value = req
toddler.process(
config={
"email_overrides_file": "test",
"bugzilla_group": "fedora_contrib",
"dist_git_url": "https://src.fp.o",
"admin_email": "admin@fp.o",
"mail_server": "mail_server",
},
message=None,
username="@python-sig",
)
toml_load.assert_called_with("test")
bz_get_group_member.assert_called_with("fedora_contrib")
get_bz_email_user.assert_not_called()
get_bz_email_group.assert_called()
get_bz_email_group.assert_has_calls(calls=[call("python-sig", {})])
bz_get_group_member.assert_called_with("fedora_contrib")
bz_get_user.assert_not_called()
send_email.assert_not_called()
def test_main_no_args(self, capsys):
with pytest.raises(SystemExit):
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(KeyError, match=r"'email_overrides_file'"):
main(["test.cfg", "--debug"])
out, err = capsys.readouterr()
assert out == "Failed to load the file containing the email-overrides\n"
assert err == ""
@patch("toml.load", new=Mock(return_value={}))
def test_main(self, capsys):
with pytest.raises(KeyError, match=r"'email_overrides_file'"):
main(["test.cfg"])
out, err = capsys.readouterr()
assert out == "Failed to load the file containing the email-overrides\n"
assert err == ""