Files
main_backend/tests/integration/test_swagger_auth.py
T
Daniil 7f2ac4e358
dev / deploy (push) Successful in 3m8s
feat: add swagger auth
2026-04-30 01:00:04 +03:00

58 lines
1.6 KiB
Python

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