diff --git a/backend/btrixcloud/main.py b/backend/btrixcloud/main.py index 863569c5..d161d75d 100644 --- a/backend/btrixcloud/main.py +++ b/backend/btrixcloud/main.py @@ -13,6 +13,7 @@ from fastapi.routing import APIRouter from fastapi.openapi.utils import get_openapi from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html +from pydantic import BaseModel from .db import init_db, await_db_and_migrations, update_and_prepare_db @@ -98,9 +99,29 @@ See [https://docs.browsertrix.com/](https://docs.browsertrix.com/) for more info return schema +# ============================================================================ +class SettingsResponse(BaseModel): + """/api/settings response model""" + + registrationEnabled: bool + + jwtTokenLifetime: int + + defaultBehaviorTimeSeconds: int + defaultPageLoadTimeSeconds: int + + maxPagesPerCrawl: int + maxScale: int + + billingEnabled: bool + + salesEmail: str = "" + supportEmail: str = "" + + # ============================================================================ # pylint: disable=too-many-locals, duplicate-code -def main(): +def main() -> None: """init browsertrix api""" app = APIRouter() @@ -110,20 +131,21 @@ def main(): dbclient, mdb = init_db() - settings = { - "registrationEnabled": is_bool(os.environ.get("REGISTRATION_ENABLED")), - "jwtTokenLifetime": JWT_TOKEN_LIFETIME, - "defaultBehaviorTimeSeconds": int( + settings = SettingsResponse( + registrationEnabled=is_bool(os.environ.get("REGISTRATION_ENABLED")), + jwtTokenLifetime=JWT_TOKEN_LIFETIME, + defaultBehaviorTimeSeconds=int( os.environ.get("DEFAULT_BEHAVIOR_TIME_SECONDS", 300) ), - "defaultPageLoadTimeSeconds": int( + defaultPageLoadTimeSeconds=int( os.environ.get("DEFAULT_PAGE_LOAD_TIME_SECONDS", 120) ), - "maxPagesPerCrawl": int(os.environ.get("MAX_PAGES_PER_CRAWL", 0)), - "maxScale": int(os.environ.get("MAX_CRAWL_SCALE", 3)), - "billingEnabled": is_bool(os.environ.get("BILLING_ENABLED")), - "salesEmail": os.environ.get("SALES_EMAIL"), - } + maxPagesPerCrawl=int(os.environ.get("MAX_PAGES_PER_CRAWL", 0)), + maxScale=int(os.environ.get("MAX_CRAWL_SCALE", 3)), + billingEnabled=is_bool(os.environ.get("BILLING_ENABLED")), + salesEmail=os.environ.get("SALES_EMAIL", ""), + supportEmail=os.environ.get("EMAIL_SUPPORT", ""), + ) invites = init_invites(mdb, email) @@ -245,8 +267,8 @@ def main(): app.include_router(org_ops.router) - @app.get("/settings", tags=["settings"]) - async def get_settings(): + @app.get("/settings", tags=["settings"], response_model=SettingsResponse) + async def get_settings() -> SettingsResponse: if not db_inited.get("inited"): raise HTTPException(status_code=503, detail="not_ready_yet") return settings diff --git a/backend/test/test_api.py b/backend/test/test_api.py index ef4d7c11..5cc0fd74 100644 --- a/backend/test/test_api.py +++ b/backend/test/test_api.py @@ -30,3 +30,22 @@ def test_api_openapi(): json = r.json() assert json["info"]["title"] == "Browsertrix" assert json["info"]["x-logo"]["url"] == "/docs-logo.svg" + + +def test_api_settings(): + r = requests.get(f"{API_PREFIX}/settings") + assert r.status_code == 200 + + data = r.json() + + assert data == { + "registrationEnabled": False, + "jwtTokenLifetime": 86400, + "defaultBehaviorTimeSeconds": 300, + "maxPagesPerCrawl": 4, + "maxScale": 3, + "defaultPageLoadTimeSeconds": 120, + "billingEnabled": True, + "salesEmail": "", + "supportEmail": "", + } diff --git a/backend/test/test_settings.py b/backend/test/test_settings.py deleted file mode 100644 index a40eeeac..00000000 --- a/backend/test/test_settings.py +++ /dev/null @@ -1,21 +0,0 @@ -import requests - -from .conftest import API_PREFIX - - -def test_settings(): - r = requests.get(f"{API_PREFIX}/settings") - assert r.status_code == 200 - - data = r.json() - - assert data == { - "registrationEnabled": False, - "jwtTokenLifetime": 86400, - "defaultBehaviorTimeSeconds": 300, - "maxPagesPerCrawl": 4, - "maxScale": 3, - "defaultPageLoadTimeSeconds": 120, - "billingEnabled": True, - "salesEmail": "", - }