From ab03a0721fc045ae715fac54fa57b50cbde84ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E7=8E=AE=20=28Jade=20Lin=29?= Date: Wed, 30 Sep 2026 16:56:58 +0800 Subject: [PATCH] fix(test): update network access admission fixtures --- .../workspace/test_network_access_group.py | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/api/tests/unit_tests/controllers/console/workspace/test_network_access_group.py b/api/tests/unit_tests/controllers/console/workspace/test_network_access_group.py index e8eea2a553a..0e46f1a9ddc 100644 --- a/api/tests/unit_tests/controllers/console/workspace/test_network_access_group.py +++ b/api/tests/unit_tests/controllers/console/workspace/test_network_access_group.py @@ -14,6 +14,7 @@ from pydantic import ValidationError from werkzeug.exceptions import BadGateway, BadRequest, Conflict, Forbidden, HTTPException, NotFound, ServiceUnavailable from werkzeug.test import TestResponse +from controllers.console import flask_admission, wraps from controllers.console.app.error import AppNotFoundError from controllers.console.workspace.network_access_group import ( AppNetworkAccessGroupApi, @@ -32,7 +33,10 @@ from controllers.console.workspace.network_access_group import ( ) from core.network_access.client_ip import NetworkAccessClientIPUnavailableError from enums import DeploymentEdition +from libs import login as login_adapter +from libs.login import AccountWithTenant from machinery.context import RequestContext +from models.account import Account, AccountStatus from services.entities.network_access_group_entities import ( NetworkAccessAppConfig, NetworkAccessAppStatus, @@ -64,6 +68,19 @@ GROUP_ID = "44444444-4444-4444-8444-444444444444" BINDING_ID = "55555555-5555-4555-8555-555555555555" +@pytest.fixture +def authenticated_console_account(monkeypatch: pytest.MonkeyPatch) -> AccountWithTenant: + account = Account(name="Admin", email="admin@example.com", status=AccountStatus.ACTIVE) + account.id = ACCOUNT_ID + identity = AccountWithTenant(account, TENANT_ID) + monkeypatch.setattr(login_adapter, "_resolve_current_user", lambda: account) + monkeypatch.setattr(login_adapter, "check_csrf_token", MagicMock()) + monkeypatch.setattr(wraps, "_is_setup_completed", lambda: True) + monkeypatch.setattr(wraps, "current_account_with_tenant", lambda: identity) + monkeypatch.setattr(flask_admission, "current_account_with_tenant", lambda: identity) + return identity + + def _request_context() -> RequestContext: return RequestContext( request_id="request-id", @@ -339,26 +356,26 @@ def test_application_error_mapping( AppNetworkAccessGroupApi.put, ], ) -def test_mutation_paid_plan_admission_precedes_payload_validation(mutation: Callable[..., object]) -> None: +def test_mutation_paid_plan_admission_precedes_payload_validation( + mutation: Callable[..., object], + authenticated_console_account: AccountWithTenant, + config_overrides: Callable[..., None], +) -> None: app = Flask(__name__) - paid_plan_wrapper = unwrap(mutation, stop=lambda candidate: candidate is not mutation) + config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.CLOUD, LOGIN_DISABLED=False) with ( app.test_request_context(json={"name": "", "description": "", "allowed_cidrs": []}), - patch( - "controllers.console.wraps.current_account_with_tenant", - return_value=(object(), TENANT_ID), - ), patch( "controllers.console.wraps.is_cloud_edition_billing_paid_plan", return_value=False, ) as is_paid_plan, pytest.raises(HTTPException) as exc_info, ): - paid_plan_wrapper(object()) + mutation(object()) assert exc_info.value.code == 403 - is_paid_plan.assert_called_once_with(TENANT_ID) + is_paid_plan.assert_called_once_with(authenticated_console_account.tenant_id) def test_controller_maps_service_error_before_serialization() -> None: @@ -633,7 +650,11 @@ def test_current_ip_read_maps_role_denial_without_resolving_ip() -> None: @pytest.mark.parametrize("edition", [DeploymentEdition.COMMUNITY, DeploymentEdition.ENTERPRISE]) -def test_current_ip_read_is_cloud_only(config_overrides: Callable[..., None], edition: DeploymentEdition) -> None: +@pytest.mark.usefixtures("authenticated_console_account") +def test_current_ip_read_is_cloud_only( + config_overrides: Callable[..., None], + edition: DeploymentEdition, +) -> None: app = Flask(__name__) service = MagicMock() config_overrides(DEPLOYMENT_EDITION=edition)