from unittest.mock import Mock, patch import pytest import toddlers.plugins.packager_bugzilla_sync class TestPackagerBugzillaSyncToddler: def test_accepts_topic_invalid(self): assert ( toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.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, topic): assert toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.accepts_topic( topic ) def test_process_no_email_override(self, capsys): with pytest.raises(KeyError, match=r"'email_overrides_file'"): toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.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, capsys): with pytest.raises( FileNotFoundError, match=r"No such file or directory: 'test'" ): toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.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 ): 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"] toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.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 ): toml_load.return_value = {} get_bz_email.side_effect = ["nils@fp.o"] get_bz_grp_mbr.return_value = ["pingou@fp.o"] toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.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): toddlers.plugins.packager_bugzilla_sync.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'"): toddlers.plugins.packager_bugzilla_sync.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'"): toddlers.plugins.packager_bugzilla_sync.main(["test.cfg"]) out, err = capsys.readouterr() assert out == "Failed to load the file containing the email-overrides\n" assert err == ""