From 109aed58983df8e407beb9ad26b13c64a56bbb98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kone=C4=8Dn=C3=BD?= Date: Thu, 11 Aug 2022 15:06:29 +0200 Subject: [PATCH] When looking for FAS user try bugzilla mail as well MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently only emails field in fasjson is checked for corresponding e-mail, but in case the bugzilla e-mail is set we don't find the user. Let's check the bugzilla mail as second option. Signed-off-by: Michal Konečný --- tests/utils/test_fedora_account_fasjson.py | 24 +++++++++++++++++++++- toddlers/utils/fedora_account.py | 3 +++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/utils/test_fedora_account_fasjson.py b/tests/utils/test_fedora_account_fasjson.py index a6b2870..59ec812 100644 --- a/tests/utils/test_fedora_account_fasjson.py +++ b/tests/utils/test_fedora_account_fasjson.py @@ -1,4 +1,4 @@ -from unittest.mock import Mock, patch +from unittest.mock import call, Mock, patch from fasjson_client.errors import ClientError import pytest @@ -183,6 +183,28 @@ class TestFedoraAccountFASJSON: 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_bzemail(self, mock_fas): + """ + Assert that bugzilla e-mail is searched for when there is no result in + primary e-mail. + """ + user = [{"username": "scoady", "rhbzemail": "scoady@fp.o"}] + empty_result = Mock() + empty_result.result = [] + result = Mock() + result.result = user + server = Mock() + server.search.side_effect = [empty_result, result] + mock_fas.return_value = server + + output = toddlers.utils.fedora_account.get_user_by_email("scoady@fp.o") + assert output == {"username": "scoady", "rhbzemail": "scoady@fp.o"} + + server.search.assert_has_calls( + [call(email="scoady@fp.o"), call(rhbzemail="scoady@fp.o")] + ) + @patch("toddlers.utils.fedora_account.get_fasjson") def test_get_user_by_email_empty(self, mock_fas): user = [] diff --git a/toddlers/utils/fedora_account.py b/toddlers/utils/fedora_account.py index 1c9d958..f3d784a 100644 --- a/toddlers/utils/fedora_account.py +++ b/toddlers/utils/fedora_account.py @@ -99,6 +99,9 @@ def get_user_by_email(email: str) -> Optional[dict]: user = None try: result = fasjson.search(email=email).result or [] + if not result: + # Try the bugzilla email as well + result = fasjson.search(rhbzemail=email).result or [] if result: user = result[0] except ClientError: