From 8a37b8f7efc756da4ea2730ec89bc142df8fbcf6 Mon Sep 17 00:00:00 2001 From: Urvish Date: Fri, 4 Dec 2026 13:12:41 +0530 Subject: [PATCH] feat(module-builder): gate server repo sync behind ENABLE_MODULE_REPO_SYNC --- app/core/config.py | 1 + app/features/admin/api.py | 13 +++++ tests/test_module_builder_repo_sync_toggle.py | 51 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 tests/test_module_builder_repo_sync_toggle.py diff --git a/app/core/config.py b/app/core/config.py index 7e152dd..5b09f56 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -20,6 +20,7 @@ class Settings(BaseSettings): app_env: Literal["local", "dev", "test", "stage", "prod"] = "local" debug: bool = False content_preview_enabled: bool = False + enable_module_repo_sync: bool = False api_prefix: str = "/api/v1" cors_origins: str = "http://localhost:3000,http://localhost:8000" diff --git a/app/features/admin/api.py b/app/features/admin/api.py index 357211b..9cf2bd6 100644 --- a/app/features/admin/api.py +++ b/app/features/admin/api.py @@ -2,6 +2,7 @@ from __future__ import annotations import io from fastapi import APIRouter, Depends, HTTPException, Query, status from fastapi.responses import HTMLResponse, StreamingResponse +from app.core.config import get_settings from app.db.models import AppUser from app.features.payments import get_razorpay_service from .deps import get_admin_service, get_current_admin @@ -545,6 +546,13 @@ async def export_report_csv( # MODULE BUILDER ENDPOINTS # ============================================================================= +@router.get("/module-builder/config") +async def get_builder_config( + admin: AppUser = Depends(get_current_admin), +): + return {"sync_repo_enabled": get_settings().enable_module_repo_sync} + + @router.get("/module-builder/modules", response_model=list[BuilderModuleSummary]) async def list_builder_modules( admin: AppUser = Depends(get_current_admin), @@ -785,6 +793,11 @@ async def sync_builder_module_to_repo( admin: AppUser = Depends(get_current_admin), service: AdminService = Depends(get_admin_service), ): + if not get_settings().enable_module_repo_sync: + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail="Server repository sync is disabled in this environment.", + ) try: return await service.builder.sync_content_repo(module_code) except HTTPException: diff --git a/tests/test_module_builder_repo_sync_toggle.py b/tests/test_module_builder_repo_sync_toggle.py new file mode 100644 index 0000000..257f8f9 --- /dev/null +++ b/tests/test_module_builder_repo_sync_toggle.py @@ -0,0 +1,51 @@ +from __future__ import annotations + +from types import SimpleNamespace +from unittest.mock import AsyncMock, MagicMock + +import pytest +from fastapi import HTTPException + +from app.core.config import Settings +from app.features.admin import api as admin_api + + +def test_enable_module_repo_sync_defaults_to_false(monkeypatch): + monkeypatch.delenv("ENABLE_MODULE_REPO_SYNC", raising=False) + s = Settings(_env_file=None) + assert s.enable_module_repo_sync is False + + +@pytest.mark.asyncio +async def test_get_builder_config_reflects_setting(monkeypatch): + monkeypatch.setattr(admin_api, "get_settings", lambda: SimpleNamespace(enable_module_repo_sync=False)) + res_off = await admin_api.get_builder_config(admin=MagicMock()) + assert res_off == {"sync_repo_enabled": False} + + monkeypatch.setattr(admin_api, "get_settings", lambda: SimpleNamespace(enable_module_repo_sync=True)) + res_on = await admin_api.get_builder_config(admin=MagicMock()) + assert res_on == {"sync_repo_enabled": True} + + +@pytest.mark.asyncio +async def test_sync_builder_module_to_repo_blocked_when_disabled(monkeypatch): + monkeypatch.setattr(admin_api, "get_settings", lambda: SimpleNamespace(enable_module_repo_sync=False)) + service = SimpleNamespace(builder=SimpleNamespace(sync_content_repo=AsyncMock())) + + with pytest.raises(HTTPException) as exc_info: + await admin_api.sync_builder_module_to_repo("m0042", admin=MagicMock(), service=service) + + assert exc_info.value.status_code == 403 + service.builder.sync_content_repo.assert_not_awaited() + + +@pytest.mark.asyncio +async def test_sync_builder_module_to_repo_allowed_when_enabled(monkeypatch): + monkeypatch.setattr(admin_api, "get_settings", lambda: SimpleNamespace(enable_module_repo_sync=True)) + service = SimpleNamespace( + builder=SimpleNamespace(sync_content_repo=AsyncMock(return_value={"status": "synced"})) + ) + + res = await admin_api.sync_builder_module_to_repo("m0042", admin=MagicMock(), service=service) + assert res == {"status": "synced"} + service.builder.sync_content_repo.assert_awaited_once_with("m0042")