Skip to content

Commit

Permalink
add the 'user' as default value for the 'type' query parameter and 'G…
Browse files Browse the repository at this point in the history
…ET /principals/' (RedHatInsights#1503)
  • Loading branch information
petracihalova authored Feb 11, 2025
1 parent 55bae0f commit ca5763f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/source/specs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@
"in": "query",
"name": "type",
"required": false,
"description": "Parameter for selecting the type of principal to be returned.",
"description": "Parameter for selecting the type of principal to be returned. Defaults to 'user'.",
"schema": {
"type": "string",
"enum": [
Expand Down
10 changes: 6 additions & 4 deletions rbac/management/principal/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
VALID_BOOLEAN_VALUE = ["true", "false"]
USERNAME_ONLY_KEY = "username_only"
PRINCIPAL_TYPE_KEY = "type"
VALID_PRINCIPAL_TYPE_VALUE = ["service-account", "user"]
USER_KEY = "user"
SA_KEY = "service-account"
VALID_PRINCIPAL_TYPE_VALUE = [SA_KEY, USER_KEY]


class PrincipalView(APIView):
Expand Down Expand Up @@ -126,12 +128,12 @@ def get(self, request):
# Attempt validating and obtaining the "principal type" query
# parameter.
principal_type = validate_and_get_key(
query_params, PRINCIPAL_TYPE_KEY, VALID_PRINCIPAL_TYPE_VALUE, required=False
query_params, PRINCIPAL_TYPE_KEY, VALID_PRINCIPAL_TYPE_VALUE, default_value=USER_KEY, required=False
)
options["principal_type"] = principal_type

# Get either service accounts or user principals, depending on what the user specified.
if principal_type == "service-account":
if principal_type == SA_KEY:
options["email"] = query_params.get(EMAIL_KEY)
options["match_criteria"] = validate_and_get_key(
query_params, MATCH_CRITERIA_KEY, VALID_MATCH_VALUE, required=False
Expand Down Expand Up @@ -173,7 +175,7 @@ def get(self, request):
response_data = {}
if status_code == status.HTTP_200_OK:
data = resp.get("data", [])
if principal_type == "service-account":
if principal_type == SA_KEY:
count = sa_count
elif isinstance(data, dict):
count = data.get("userCount")
Expand Down
28 changes: 14 additions & 14 deletions tests/management/principal/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_non_admin_can_read_principal_list_with_permissions(self, mock_request):
"status": "enabled",
"admin_only": "false",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
org_id=self.customer["org_id"],
)
Expand Down Expand Up @@ -163,7 +163,7 @@ def test_read_principal_list_success(self, mock_request):
"status": "enabled",
"admin_only": "false",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
org_id=self.customer_data["org_id"],
)
Expand Down Expand Up @@ -258,7 +258,7 @@ def test_check_principal_admin(self, mock_request):
"status": "enabled",
"admin_only": "false",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
org_id=self.customer_data["org_id"],
)
Expand Down Expand Up @@ -301,7 +301,7 @@ def test_read_principal_filtered_list_success_without_cross_account_user(self, m
"sort_order": "asc",
"status": "enabled",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
)
# Cross account user won't be returned.
Expand Down Expand Up @@ -339,7 +339,7 @@ def test_read_principal_filtered_list_success(self, mock_request):
"sort_order": "asc",
"status": "enabled",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down Expand Up @@ -373,7 +373,7 @@ def test_read_principal_partial_matching(self, mock_request):
"sort_order": "asc",
"status": "enabled",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
org_id=self.customer_data["org_id"],
)
Expand Down Expand Up @@ -408,7 +408,7 @@ def test_read_principal_multi_filter(self, mock_request):
"sort_order": "asc",
"status": "enabled",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
org_id=self.customer_data["org_id"],
)
Expand Down Expand Up @@ -478,7 +478,7 @@ def test_read_principal_list_account(self, mock_request):
"sort_order": "desc",
"status": "enabled",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down Expand Up @@ -571,7 +571,7 @@ def test_read_principal_list_by_email(self, mock_request):
"sort_order": "asc",
"status": "enabled",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
org_id=self.customer_data["org_id"],
)
Expand Down Expand Up @@ -606,7 +606,7 @@ def test_read_users_of_desired_status(self, mock_request):
"status": "disabled",
"admin_only": "false",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
org_id=self.customer_data["org_id"],
)
Expand Down Expand Up @@ -639,7 +639,7 @@ def test_principal_default_status_enabled(self, mock_request):
"admin_only": "false",
"status": "enabled",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
org_id=self.customer_data["org_id"],
)
Expand Down Expand Up @@ -672,7 +672,7 @@ def test_read_list_of_admins(self, mock_request):
"status": "enabled",
"admin_only": "true",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
org_id=self.customer_data["org_id"],
)
Expand Down Expand Up @@ -731,7 +731,7 @@ def test_read_principal_list_by_email_partial_matching(self, mock_request):
"sort_order": "asc",
"status": "enabled",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
org_id=self.customer_data["org_id"],
)
Expand Down Expand Up @@ -780,7 +780,7 @@ def test_read_principal_users(self, mock_request):
"status": "enabled",
"admin_only": "false",
"username_only": "false",
"principal_type": None,
"principal_type": "user",
},
org_id=self.customer_data["org_id"],
)
Expand Down

0 comments on commit ca5763f

Please sign in to comment.