toddlers/tests/plugins/test_packager_bugzilla_sync.py
Nils Philippsen 7fe0937190 Using real objects is considered polite
Previously, ToddlerBase and derived classes were used directly, but none
of the methods is marked as a class or static method. Amazingly enough,
it worked regardless. It's useful to have a constructor, though, so do
things conventionally.

As we can't just monkey-patch the classes then, use the shared `toddler`
fixture which creates the object for the test methods that need it.

Signed-off-by: Nils Philippsen <nils@redhat.com>
2020-07-17 17:39:28 +02:00

145 lines
5.3 KiB
Python

from unittest.mock import Mock, patch
import pytest
from toddlers.plugins.packager_bugzilla_sync import main, PackagerBugzillaSync
class TestPackagerBugzillaSyncToddler:
toddler_cls = PackagerBugzillaSync
def test_accepts_topic_invalid(self, toddler):
assert toddler.accepts_topic("foo.bar") is False
@pytest.mark.parametrize(
"topic",
[
"org.fedoraproject.*.toddlers.trigger.packager_bugzilla_sync",
"org.fedoraproject.prod.toddlers.trigger.packager_bugzilla_sync",
"org.fedoraproject.stg.toddlers.trigger.packager_bugzilla_sync",
],
)
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, dry_run=True)
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,
dry_run=True,
)
out, err = capsys.readouterr()
assert out == "Failed to load the file containing the email-overrides\n"
assert err == ""
@patch("toddlers.utils.fedora_account.set_fas", new=Mock(return_value=True))
@patch("toddlers.utils.bugzilla_system.set_bz", new=Mock(return_value=True))
@patch("toddlers.utils.fedora_account.get_group_member")
@patch("toddlers.utils.fedora_account.get_bz_email_user")
@patch("toddlers.utils.bugzilla_system.get_group_member")
@patch("toddlers.utils.bugzilla_system.add_user_to_group")
@patch("toml.load")
def test_process(
self,
toml_load,
bz_user_grp,
get_bz_grp_mbr,
get_bz_email,
get_fas_grp_mbr,
toddler,
):
toml_load.return_value = {}
get_fas_grp_mbr.return_value = ["pingou", "nils"]
get_bz_email.side_effect = ["pingou@fp.o", "nils@fp.o"]
get_bz_grp_mbr.return_value = ["pingou@fp.o", "nphilipp@fp.o"]
toddler.process(
config={"email_overrides_file": "test", "bugzilla_group": "fedora_contrib"},
message=None,
username=False,
dry_run=False,
)
toml_load.assert_called_with("test")
get_fas_grp_mbr.assert_called_with("packager")
get_bz_email.assert_called_with("pingou", {})
get_bz_grp_mbr.assert_called_with("fedora_contrib")
bz_user_grp.assert_called_with(
user_email="nils@fp.o",
bz_group="fedora_contrib",
no_bz_account=[],
dry_run=False,
)
@patch("toddlers.utils.fedora_account.set_fas", new=Mock(return_value=True))
@patch("toddlers.utils.bugzilla_system.set_bz", new=Mock(return_value=True))
@patch("toddlers.utils.fedora_account.get_bz_email_user")
@patch("toddlers.utils.bugzilla_system.get_group_member")
@patch("toddlers.utils.bugzilla_system.add_user_to_group")
@patch("toml.load")
def test_process_username(
self, toml_load, bz_user_grp, get_bz_grp_mbr, get_bz_email, toddler
):
toml_load.return_value = {}
get_bz_email.side_effect = ["nils@fp.o"]
get_bz_grp_mbr.return_value = ["pingou@fp.o"]
toddler.process(
config={"email_overrides_file": "test", "bugzilla_group": "fedora_contrib"},
message=None,
username="nils",
dry_run=False,
)
toml_load.assert_called_with("test")
get_bz_email.assert_called_with("nils", {})
get_bz_grp_mbr.assert_called_with("fedora_contrib")
bz_user_grp.assert_called_with(
user_email="nils@fp.o",
bz_group="fedora_contrib",
no_bz_account=[],
dry_run=False,
)
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 == ""