Skip to content

Commit

Permalink
Merge pull request RedHatInsights#1273 from EvanCasey13/principal_fil…
Browse files Browse the repository at this point in the history
…ter_error

RHCLOUD-31117 Added ternary operator to handle no username being pass…
  • Loading branch information
petracihalova authored Jan 9, 2025
2 parents fb9ef1d + e94ef44 commit 5ceb0fe
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
5 changes: 3 additions & 2 deletions rbac/management/principal/it_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
50 changes: 50 additions & 0 deletions tests/management/principal/test_it_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

0 comments on commit 5ceb0fe

Please sign in to comment.