feat(module-builder): gate server repo sync behind ENABLE_MODULE_REPO_SYNC

This commit is contained in:
Urvish Prajapati 2026-12-04 13:12:41 +05:30
parent 06e6d91927
commit 8a37b8f7ef
3 changed files with 65 additions and 0 deletions

View File

@ -20,6 +20,7 @@ class Settings(BaseSettings):
app_env: Literal["local", "dev", "test", "stage", "prod"] = "local" app_env: Literal["local", "dev", "test", "stage", "prod"] = "local"
debug: bool = False debug: bool = False
content_preview_enabled: bool = False content_preview_enabled: bool = False
enable_module_repo_sync: bool = False
api_prefix: str = "/api/v1" api_prefix: str = "/api/v1"
cors_origins: str = "http://localhost:3000,http://localhost:8000" cors_origins: str = "http://localhost:3000,http://localhost:8000"

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import io import io
from fastapi import APIRouter, Depends, HTTPException, Query, status from fastapi import APIRouter, Depends, HTTPException, Query, status
from fastapi.responses import HTMLResponse, StreamingResponse from fastapi.responses import HTMLResponse, StreamingResponse
from app.core.config import get_settings
from app.db.models import AppUser from app.db.models import AppUser
from app.features.payments import get_razorpay_service from app.features.payments import get_razorpay_service
from .deps import get_admin_service, get_current_admin from .deps import get_admin_service, get_current_admin
@ -545,6 +546,13 @@ async def export_report_csv(
# MODULE BUILDER ENDPOINTS # 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]) @router.get("/module-builder/modules", response_model=list[BuilderModuleSummary])
async def list_builder_modules( async def list_builder_modules(
admin: AppUser = Depends(get_current_admin), admin: AppUser = Depends(get_current_admin),
@ -785,6 +793,11 @@ async def sync_builder_module_to_repo(
admin: AppUser = Depends(get_current_admin), admin: AppUser = Depends(get_current_admin),
service: AdminService = Depends(get_admin_service), 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: try:
return await service.builder.sync_content_repo(module_code) return await service.builder.sync_content_repo(module_code)
except HTTPException: except HTTPException:

View File

@ -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")