| |
@@ -0,0 +1,203 @@
|
| |
+ import logging
|
| |
+ import xmlrpc.client
|
| |
+ from unittest.mock import Mock, patch
|
| |
+
|
| |
+ import pytest
|
| |
+
|
| |
+ import toddlers.utils.bugzilla_system
|
| |
+
|
| |
+
|
| |
+ class TestBugzillaSystem:
|
| |
+ def test_set_bz_no_bugzilla_url(self):
|
| |
+ with pytest.raises(
|
| |
+ ValueError, match=r"No bugzilla_url found in the configuration file"
|
| |
+ ):
|
| |
+ toddlers.utils.bugzilla_system.set_bz({})
|
| |
+
|
| |
+ def test_set_bz_no_bugzilla_username(self):
|
| |
+ with pytest.raises(
|
| |
+ ValueError, match=r"No bugzilla_username found in the configuration file"
|
| |
+ ):
|
| |
+ config = {
|
| |
+ "bugzilla_url": "https:bz.example.com",
|
| |
+ }
|
| |
+ toddlers.utils.bugzilla_system.set_bz(config)
|
| |
+
|
| |
+ def test_set_bz_no_bugzilla_password(self):
|
| |
+ with pytest.raises(
|
| |
+ ValueError, match=r"No bugzilla_password found in the configuration file"
|
| |
+ ):
|
| |
+ config = {
|
| |
+ "bugzilla_url": "https:bz.example.com",
|
| |
+ "bugzilla_username": "bz_username",
|
| |
+ }
|
| |
+ toddlers.utils.bugzilla_system.set_bz(config)
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system.Bugzilla")
|
| |
+ def test_set_bz(self, mock_bz):
|
| |
+ mock_bz.return_value = "bugzilla_object"
|
| |
+ config = {
|
| |
+ "bugzilla_url": "https:bz.example.com",
|
| |
+ "bugzilla_username": "bz_username",
|
| |
+ "bugzilla_password": "bz_password",
|
| |
+ }
|
| |
+ output = toddlers.utils.bugzilla_system.set_bz(config)
|
| |
+ mock_bz.assert_called_with(
|
| |
+ url="https:bz.example.com/xmlrpc.cgi",
|
| |
+ user="bz_username",
|
| |
+ password="bz_password",
|
| |
+ cookiefile=None,
|
| |
+ tokenfile=None,
|
| |
+ )
|
| |
+ assert output == "bugzilla_object"
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system._BUGZILLA", new=None)
|
| |
+ def test_get_bz_not_set(self):
|
| |
+ with pytest.raises(
|
| |
+ ValueError, match=r"No bugzilla connection set, call set_bz first"
|
| |
+ ):
|
| |
+ toddlers.utils.bugzilla_system.get_bz()
|
| |
+
|
| |
+ def test_get_bz(self):
|
| |
+ output = toddlers.utils.bugzilla_system.get_bz()
|
| |
+ assert output == "bugzilla_object"
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system.get_bz")
|
| |
+ def test_get_group_member(self, mock_bz):
|
| |
+ groups = Mock()
|
| |
+ groups.member_emails = ["foo@bar.com", "foo@baz.com"]
|
| |
+ server = Mock()
|
| |
+ server.getgroup.return_value = groups
|
| |
+ mock_bz.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.bugzilla_system.get_group_member("test")
|
| |
+ mock_bz.assert_called_with()
|
| |
+ server.getgroup.assert_called_with("test", membership=True)
|
| |
+ assert output == ["foo@bar.com", "foo@baz.com"]
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system.get_bz")
|
| |
+ def test_remove_user_from_group(self, mock_bz):
|
| |
+ server = Mock()
|
| |
+ server.updateperms.return_value = True
|
| |
+ mock_bz.return_value = server
|
| |
+
|
| |
+ toddlers.utils.bugzilla_system.remove_user_from_group(
|
| |
+ "test@foo.com", "groupname"
|
| |
+ )
|
| |
+ mock_bz.assert_called_with()
|
| |
+ server.updateperms.assert_called_with("test@foo.com", "rem", "groupname")
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system.get_bz")
|
| |
+ def test_remove_user_from_group_failed(self, mock_bz):
|
| |
+ server = Mock()
|
| |
+ server.updateperms.side_effect = xmlrpc.client.Fault(55, "error string")
|
| |
+ mock_bz.return_value = server
|
| |
+
|
| |
+ with pytest.raises(xmlrpc.client.Fault, match=r"<Fault 55: 'error string'>"):
|
| |
+ toddlers.utils.bugzilla_system.remove_user_from_group(
|
| |
+ "test@foo.com", "groupname"
|
| |
+ )
|
| |
+ mock_bz.assert_called_with()
|
| |
+ server.updateperms.assert_called_with("test@foo.com", "rem", "groupname")
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system.get_bz")
|
| |
+ def test_remove_user_from_group_no_user(self, mock_bz):
|
| |
+ server = Mock()
|
| |
+ server.updateperms.side_effect = xmlrpc.client.Fault(51, "No such user")
|
| |
+ mock_bz.return_value = server
|
| |
+
|
| |
+ toddlers.utils.bugzilla_system.remove_user_from_group(
|
| |
+ "test@foo.com", "groupname"
|
| |
+ )
|
| |
+ mock_bz.assert_called_with()
|
| |
+ server.updateperms.assert_called_with("test@foo.com", "rem", "groupname")
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system.get_bz")
|
| |
+ def test_user_exists(self, mock_bz):
|
| |
+ server = Mock()
|
| |
+ server.getuser.return_value = True
|
| |
+ mock_bz.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.bugzilla_system.user_exists("test@foo.com")
|
| |
+ mock_bz.assert_called_with()
|
| |
+ server.getuser.assert_called_with("test@foo.com")
|
| |
+ assert output
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system.get_bz")
|
| |
+ def test_user_exists_no_user(self, mock_bz):
|
| |
+ server = Mock()
|
| |
+ server.getuser.side_effect = xmlrpc.client.Fault(51, "No such user")
|
| |
+ mock_bz.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.bugzilla_system.user_exists("test@foo.com")
|
| |
+ mock_bz.assert_called_with()
|
| |
+ server.getuser.assert_called_with("test@foo.com")
|
| |
+ assert output is False
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system.get_bz")
|
| |
+ def test_user_exists_failed(self, mock_bz):
|
| |
+ server = Mock()
|
| |
+ server.getuser.side_effect = xmlrpc.client.Fault(55, "error string")
|
| |
+ mock_bz.return_value = server
|
| |
+
|
| |
+ with pytest.raises(xmlrpc.client.Fault, match=r"<Fault 55: 'error string'>"):
|
| |
+ toddlers.utils.bugzilla_system.user_exists("test@foo.com")
|
| |
+ mock_bz.assert_called_with()
|
| |
+ server.getuser.assert_called_with("test@foo.com")
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system.user_exists", new=Mock(return_value=False))
|
| |
+ @patch("toddlers.utils.bugzilla_system.get_bz")
|
| |
+ def test_add_user_to_group_no_user(self, mock_bz):
|
| |
+ server = Mock()
|
| |
+ server.updateperms.return_value = True
|
| |
+ mock_bz.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.bugzilla_system.add_user_to_group(
|
| |
+ user_email="test@foo.com",
|
| |
+ bz_group="groupname",
|
| |
+ no_bz_account=[],
|
| |
+ dry_run=False,
|
| |
+ )
|
| |
+ mock_bz.assert_called_with()
|
| |
+ server.updateperms.assert_not_called()
|
| |
+ assert output == ["test@foo.com"]
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system.user_exists", new=Mock(return_value=True))
|
| |
+ @patch("toddlers.utils.bugzilla_system.get_bz")
|
| |
+ def test_add_user_to_group_dry_run(self, mock_bz, caplog):
|
| |
+ caplog.set_level(logging.INFO)
|
| |
+ server = Mock()
|
| |
+ server.updateperms.return_value = True
|
| |
+ mock_bz.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.bugzilla_system.add_user_to_group(
|
| |
+ user_email="test@foo.com",
|
| |
+ bz_group="groupname",
|
| |
+ no_bz_account=[],
|
| |
+ dry_run=True,
|
| |
+ )
|
| |
+ mock_bz.assert_called_with()
|
| |
+ server.updateperms.assert_not_called()
|
| |
+ assert output == []
|
| |
+ assert (
|
| |
+ caplog.records[-1].message
|
| |
+ == " Would add test@foo.com to the group groupname"
|
| |
+ )
|
| |
+
|
| |
+ @patch("toddlers.utils.bugzilla_system.user_exists", new=Mock(return_value=True))
|
| |
+ @patch("toddlers.utils.bugzilla_system.get_bz")
|
| |
+ def test_add_user_to_group(self, mock_bz, caplog):
|
| |
+ caplog.set_level(logging.INFO)
|
| |
+ server = Mock()
|
| |
+ server.updateperms.return_value = True
|
| |
+ mock_bz.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.bugzilla_system.add_user_to_group(
|
| |
+ user_email="test@foo.com",
|
| |
+ bz_group="groupname",
|
| |
+ no_bz_account=[],
|
| |
+ dry_run=False,
|
| |
+ )
|
| |
+ mock_bz.assert_called_with()
|
| |
+ server.updateperms.assert_called_with("test@foo.com", "add", "groupname")
|
| |
+ assert output == []
|
| |
This toddler is meant to be triggered by playtime at regular intervals
and will sync the packager accounts to bugzilla so they can edit flags
on Fedora bugs (for example).
Signed-off-by: Pierre-Yves Chibon pingou@pingoured.fr