|
| 1 | +""" |
| 2 | +Regression test for scoped_query: the `project` parameter must not be |
| 3 | +silently overridden by `group_id`. (BA-4280) |
| 4 | +""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import uuid |
| 9 | +from typing import Any |
| 10 | +from unittest.mock import MagicMock |
| 11 | + |
| 12 | +import graphene |
| 13 | +import pytest |
| 14 | + |
| 15 | +from ai.backend.manager.api.gql_legacy.base import scoped_query |
| 16 | +from ai.backend.manager.models.user import UserRole |
| 17 | + |
| 18 | + |
| 19 | +class TestScopedQuery: |
| 20 | + """Test fixes for scoped_query bugs BA-4280.""" |
| 21 | + |
| 22 | + @pytest.fixture |
| 23 | + def mock_graphene_info(self) -> MagicMock: |
| 24 | + """Mock GraphQL ResolveInfo with SUPERADMIN context.""" |
| 25 | + ctx = MagicMock() |
| 26 | + ctx.user = {"role": UserRole.SUPERADMIN, "domain_name": "default", "uuid": uuid.uuid4()} |
| 27 | + ctx.access_key = "test-key" |
| 28 | + info = MagicMock(spec=graphene.ResolveInfo) |
| 29 | + info.context = ctx |
| 30 | + return info |
| 31 | + |
| 32 | + @pytest.mark.asyncio |
| 33 | + async def test_project_param_preserved(self, mock_graphene_info: MagicMock) -> None: |
| 34 | + """Regression: project was silently overridden by group_id in scoped_query.""" |
| 35 | + project_id = uuid.uuid4() |
| 36 | + group_id = uuid.uuid4() |
| 37 | + received: dict[str, Any] = {} |
| 38 | + |
| 39 | + @scoped_query(autofill_user=False, user_key="user_uuid") |
| 40 | + async def _mock_resolver( |
| 41 | + _root: Any, |
| 42 | + _info: graphene.ResolveInfo, |
| 43 | + *, |
| 44 | + project: uuid.UUID | None = None, |
| 45 | + group_id: uuid.UUID | None = None, |
| 46 | + domain_name: str | None = None, |
| 47 | + user_uuid: uuid.UUID | None = None, |
| 48 | + ) -> None: |
| 49 | + received.update(project=project, group_id=group_id) |
| 50 | + |
| 51 | + await _mock_resolver(None, mock_graphene_info, project=project_id, group_id=group_id) |
| 52 | + |
| 53 | + assert received["project"] == project_id |
| 54 | + assert received["group_id"] == group_id |
0 commit comments