diff --git a/rbac/management/principal/it_service.py b/rbac/management/principal/it_service.py index ac05c2291..c7788e0e7 100644 --- a/rbac/management/principal/it_service.py +++ b/rbac/management/principal/it_service.py @@ -250,9 +250,10 @@ def get_service_accounts(self, user: User, options: dict[str, Any] = {}) -> Tupl if specified_usernames: usernames = specified_usernames.split(",") - # If "match_criteria" is specified, only the first username is taken into account. + # If "match_criteria" is specified and the usernames list is not empty, + # only the first username is taken into account match_criteria = options.get("match_criteria") - if match_criteria: + if match_criteria and usernames: username = usernames[0] if match_criteria == "partial": diff --git a/tests/management/principal/test_it_service.py b/tests/management/principal/test_it_service.py index 0a8d94c65..3064a3c0e 100644 --- a/tests/management/principal/test_it_service.py +++ b/tests/management/principal/test_it_service.py @@ -1570,3 +1570,53 @@ def test_merge_principals_it_service_accounts(self) -> None: f' "{expected_first_username}" or "{expected_second_username}", got the following service account:' f" {filtered_result}", ) + + @override_settings(IT_BYPASS_IT_CALLS=True) + def test_principal_filtering_without_username_match_criteria_exact_skipped(self): + """Test the function under test skips the expected service account when filtering by exact match criteria without usernames""" + user = User() + user.account = self.tenant.account_id + user.org_id = self.tenant.org_id + + # Set the options for the filter. + options = { + "limit": 10, + "offset": 0, + "match_criteria": "exact", + "usernames": None, + } + + # Call the function under test. + result, count = self.it_service.get_service_accounts(user=user, options=options) + print(result) + print(count) + self.assertEqual( + 0, + count, + "unexpected number of service accounts fetched for the tenant", + ) + + @override_settings(IT_BYPASS_IT_CALLS=True) + def test_principal_filtering_without_username_match_criteria_partial_skipped(self): + """Test the function under test skips the expected service account when match_criteria is partial without usernames""" + user = User() + user.account = self.tenant.account_id + user.org_id = self.tenant.org_id + + # Set the options for the filter. + options = { + "limit": 10, + "offset": 0, + "match_criteria": "partial", + "usernames": None, + } + + # Call the function under test. + result, count = self.it_service.get_service_accounts(user=user, options=options) + print(result) + print(count) + self.assertEqual( + 0, + count, + "unexpected number of service accounts fetched for the tenant", + )