| |
@@ -0,0 +1,185 @@
|
| |
+ from unittest.mock import Mock, patch
|
| |
+
|
| |
+ from fasjson_client.errors import ClientError
|
| |
+ import pytest
|
| |
+
|
| |
+ import toddlers.utils.fedora_account
|
| |
+
|
| |
+
|
| |
+ class TestFedoraAccountFASJSON:
|
| |
+ def test_set_fas_no_fas_url(self):
|
| |
+ with pytest.raises(
|
| |
+ ValueError, match=r"No fas_url found in the configuration file"
|
| |
+ ):
|
| |
+ toddlers.utils.fedora_account.set_fasjson({})
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.Client")
|
| |
+ def test_set_fas_json(self, mock_fas):
|
| |
+ mock_fas.return_value = "fas_object"
|
| |
+ config = {
|
| |
+ "fas_url": "https:fas.example.com",
|
| |
+ "fasjson": True,
|
| |
+ }
|
| |
+ output = toddlers.utils.fedora_account.set_fasjson(config)
|
| |
+ mock_fas.assert_called_with(url="https:fas.example.com")
|
| |
+ assert output == "fas_object"
|
| |
+
|
| |
+ def test_set_fas_json_error(self):
|
| |
+ with patch(
|
| |
+ "toddlers.utils.fedora_account.Client",
|
| |
+ side_effect=ClientError(
|
| |
+ message="Error communicating with fasjson to instantiate the client",
|
| |
+ code="404",
|
| |
+ ),
|
| |
+ ):
|
| |
+ with pytest.raises(
|
| |
+ ClientError,
|
| |
+ match=r"Error communicating with fasjson to instantiate the client",
|
| |
+ ):
|
| |
+ config = {
|
| |
+ "fas_url": "https:fas.example.com",
|
| |
+ "fasjson": True,
|
| |
+ }
|
| |
+ toddlers.utils.fedora_account.set_fasjson(config)
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account._FASJSON", new=None)
|
| |
+ def test_get_fasjson_not_set(self):
|
| |
+ with pytest.raises(
|
| |
+ ValueError, match=r"No FASJSON client instantiated, call set_fas first"
|
| |
+ ):
|
| |
+ toddlers.utils.fedora_account.get_fasjson()
|
| |
+
|
| |
+ def test_get_fasjson(self):
|
| |
+ output = toddlers.utils.fedora_account.get_fasjson()
|
| |
+ assert output == "fas_object"
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_group_member(self, mock_fas):
|
| |
+ members = []
|
| |
+ for name in ["pingou", "ralph", "kevin", "nils"]:
|
| |
+ member = Mock()
|
| |
+ member.role_type = "administrator"
|
| |
+ member.username = name
|
| |
+ members.append(member)
|
| |
+ result = {"result": members}
|
| |
+ server = Mock()
|
| |
+ server.list_group_members.return_value = result
|
| |
+ mock_fas.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.fedora_account.get_group_member("sysadmin")
|
| |
+ assert output == {"kevin", "nils", "pingou", "ralph"}
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_group_member_error(self, mock_fas):
|
| |
+ server = Mock()
|
| |
+ server.list_group_members.side_effect = ClientError(
|
| |
+ message="Error getting list of members",
|
| |
+ code="500",
|
| |
+ )
|
| |
+ mock_fas.return_value = server
|
| |
+ output = toddlers.utils.fedora_account.get_group_member("sysadmin")
|
| |
+ assert output == set()
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_group_member_empty(self, mock_fas):
|
| |
+ members = []
|
| |
+ server = Mock()
|
| |
+ result = {"result": members}
|
| |
+ server.list_group_members.return_value = result
|
| |
+ mock_fas.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.fedora_account.get_group_member("sysadmin")
|
| |
+ assert output == set()
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_bz_email_user_no_bugzilla_email(self, mock_fas):
|
| |
+ server = Mock()
|
| |
+ server.get_user.return_value = {}
|
| |
+ mock_fas.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.fedora_account.get_bz_email_user("pingou", {})
|
| |
+ assert output is None
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_bz_email_user(self, mock_fas):
|
| |
+ server = Mock()
|
| |
+ server.get_user.return_value = {"result": {"emails": ["foo@bar.com"]}}
|
| |
+ mock_fas.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.fedora_account.get_bz_email_user("pingou", {})
|
| |
+ assert output == "foo@bar.com"
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_bz_email_user_error(self, mock_fas):
|
| |
+ server = Mock()
|
| |
+ server.get_user.side_effect = ClientError(
|
| |
+ message="Error getting bz_email",
|
| |
+ code="500",
|
| |
+ )
|
| |
+ mock_fas.return_value = server
|
| |
+ output = toddlers.utils.fedora_account.get_bz_email_user("pingou", {})
|
| |
+ assert output is None
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_bz_email_group_no_bugzilla_email(self, mock_fas):
|
| |
+ server = Mock()
|
| |
+ server.get_group.return_value = {}
|
| |
+ mock_fas.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.fedora_account.get_bz_email_group("toddlers-sig", {})
|
| |
+ assert output is None
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_bz_email_group(self, mock_fas):
|
| |
+ server = Mock()
|
| |
+ server.get_group.return_value = {
|
| |
+ "result": {"mailing_list": "foo@lists.bar.com"}
|
| |
+ }
|
| |
+ mock_fas.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.fedora_account.get_bz_email_group("toddlers-sig", {})
|
| |
+ assert output == "foo@lists.bar.com"
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_bz_email_group_error(self, mock_fas):
|
| |
+ server = Mock()
|
| |
+ server.get_group.side_effect = ClientError(
|
| |
+ message="Error getting bz_email for group",
|
| |
+ code="500",
|
| |
+ )
|
| |
+ mock_fas.return_value = server
|
| |
+ output = toddlers.utils.fedora_account.get_bz_email_group("sysadmin", {})
|
| |
+ assert output is None
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_user_by_email(self, mock_fas):
|
| |
+ user = [{"username": "scoady", "emails": ["scoady@fp.o"]}]
|
| |
+ result = {"result": user}
|
| |
+ server = Mock()
|
| |
+ server.search.return_value = result
|
| |
+ mock_fas.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.fedora_account.get_user_by_email("scoady@fp.o")
|
| |
+ assert output == {"username": "scoady", "emails": ["scoady@fp.o"]}
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_user_by_email_empty(self, mock_fas):
|
| |
+ user = []
|
| |
+ result = {"result": user}
|
| |
+ server = Mock()
|
| |
+ server.search.return_value = result
|
| |
+ mock_fas.return_value = server
|
| |
+
|
| |
+ output = toddlers.utils.fedora_account.get_user_by_email("scoady@fp.o")
|
| |
+ assert output is None
|
| |
+
|
| |
+ @patch("toddlers.utils.fedora_account.get_fasjson")
|
| |
+ def test_get_user_by_email_error(self, mock_fas):
|
| |
+ server = Mock()
|
| |
+ server.search.side_effect = ClientError(
|
| |
+ message="Error getting bz_email for group",
|
| |
+ code="500",
|
| |
+ )
|
| |
+ mock_fas.return_value = server
|
| |
+ output = toddlers.utils.fedora_account.get_user_by_email("scoady@fp.o")
|
| |
+ assert output is None
|
| |
Signed-off-by: Stephen Coady scoady@redhat.com