Skip to content

Commit 76602f8

Browse files
committed
Add test and limit to json media type
1 parent 135d65e commit 76602f8

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

ansible_base/rbac/api/views.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,6 @@ class RoleTeamAssignmentViewSet(BaseAssignmentViewSet):
253253
]
254254
}
255255

256-
_ROLE_USER_ASSIGNMENT_REQUEST_SCHEMA = {
257-
'schema': {
258-
'allOf': [
259-
{'$ref': '#/components/schemas/RoleUserAssignment'},
260-
_USER_ACTOR_ONEOF,
261-
_OBJECT_ID_ONEOF,
262-
]
263-
}
264-
}
265-
266256

267257
class RoleUserAssignmentViewSet(BaseAssignmentViewSet):
268258

@@ -277,9 +267,13 @@ class RoleUserAssignmentViewSet(BaseAssignmentViewSet):
277267

278268
@extend_schema_if_available(
279269
request={
280-
'application/json': _ROLE_USER_ASSIGNMENT_REQUEST_SCHEMA,
281-
'application/x-www-form-urlencoded': _ROLE_USER_ASSIGNMENT_REQUEST_SCHEMA,
282-
'multipart/form-data': _ROLE_USER_ASSIGNMENT_REQUEST_SCHEMA,
270+
'application/json': {
271+
'allOf': [
272+
{'$ref': '#/components/schemas/RoleUserAssignment'},
273+
_USER_ACTOR_ONEOF,
274+
_OBJECT_ID_ONEOF,
275+
]
276+
},
283277
},
284278
description="Give a user permission to a resource, an organization, or globally (when allowed)."
285279
"Must specify 'role_definition' and exactly one of 'user' or 'user_ansible_id'."

test_app/tests/api_documentation/test_schema_integration.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,35 @@ def test_openapi_schema_unauthenticated_access(unauthenticated_api_client):
148148
if response.status_code == 200:
149149
schema = response.data
150150
assert 'openapi' in schema
151+
152+
153+
def test_role_user_assignment_create_schema(admin_api_client):
154+
"""
155+
Test that RoleUserAssignmentViewSet's create operation has proper schema constraints.
156+
157+
Generated by Claude Code (claude-sonnet-4-5@20250929)
158+
159+
Verifies that the request body schema properly enforces:
160+
- Exactly one of 'user' or 'user_ansible_id' is required
161+
- At most one of 'object_id' or 'object_ansible_id' can be specified
162+
"""
163+
url = '/api/v1/docs/schema/'
164+
response = admin_api_client.get(url)
165+
assert response.status_code == 200
166+
167+
# Navigate directly to the schema - will raise KeyError if path doesn't exist
168+
all_of = response.data['paths']['/api/v1/role_user_assignments/']['post']['requestBody']['content']['application/json']['schema']['allOf']
169+
170+
# Verify structure: [base_schema, user_constraint, object_constraint]
171+
assert len(all_of) == 3, "Should have 3 items: base schema + 2 constraint sets"
172+
assert all_of[0] == {'$ref': '#/components/schemas/RoleUserAssignment'}
173+
174+
# Verify user constraint: exactly one of 'user' or 'user_ansible_id'
175+
user_one_of = all_of[1]['oneOf']
176+
assert len(user_one_of) == 2
177+
assert user_one_of[0] == {'required': ['user'], 'not': {'required': ['user_ansible_id']}}
178+
assert user_one_of[1] == {'required': ['user_ansible_id'], 'not': {'required': ['user']}}
179+
180+
# Verify object constraint: at most one of 'object_id' or 'object_ansible_id'
181+
object_one_of = all_of[2]['oneOf']
182+
assert len(object_one_of) == 3

0 commit comments

Comments
 (0)