feat: add swagger auth
dev / deploy (push) Successful in 3m8s

This commit is contained in:
Daniil
2026-04-30 01:00:04 +03:00
parent acdd6c8e36
commit 7f2ac4e358
3 changed files with 114 additions and 5 deletions
+57
View File
@@ -0,0 +1,57 @@
from __future__ import annotations
import importlib
import pytest
from httpx import ASGITransport, AsyncClient, BasicAuth
from cpv3.infrastructure.settings import get_settings
@pytest.fixture
def swagger_app(monkeypatch):
monkeypatch.setenv("SWAGGER_AUTH_LOGIN", "docs-user")
monkeypatch.setenv("SWAGGER_AUTH_PASSWORD", "docs-password")
get_settings.cache_clear()
import cpv3.main
module = importlib.reload(cpv3.main)
yield module.app
get_settings.cache_clear()
importlib.reload(cpv3.main)
async def test_swagger_ui_requires_basic_auth(swagger_app):
async with AsyncClient(
transport=ASGITransport(app=swagger_app),
base_url="http://test",
) as client:
response = await client.get("/api/schema/swagger/")
assert response.status_code == 401
assert response.headers["www-authenticate"] == "Basic"
async def test_openapi_schema_requires_basic_auth(swagger_app):
async with AsyncClient(
transport=ASGITransport(app=swagger_app),
base_url="http://test",
) as client:
response = await client.get("/api/schema/")
assert response.status_code == 401
assert response.headers["www-authenticate"] == "Basic"
async def test_swagger_credentials_come_from_environment(swagger_app):
async with AsyncClient(
transport=ASGITransport(app=swagger_app),
base_url="http://test",
auth=BasicAuth("docs-user", "docs-password"),
) as client:
response = await client.get("/api/schema/swagger/")
assert response.status_code == 200
assert "Swagger UI" in response.text