Skip to content

Commit

Permalink
test: Add tests for Swagger & RapiDoc with CSRF
Browse files Browse the repository at this point in the history
  • Loading branch information
floxay committed Sep 22, 2024
1 parent 393f4ed commit 59c219f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions litestar/openapi/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@


def _get_cookie_value_or_undefined(cookie_name: str) -> str:
"""Javascript code as a string to get the value of a cookie by name or undefined."""
return f"document.cookie.split('; ').find((row) => row.startsWith('{cookie_name}='))?.split('=')[1];"


Expand Down
39 changes: 39 additions & 0 deletions tests/unit/test_openapi/test_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from litestar import Litestar
from litestar.config.csrf import CSRFConfig
from litestar.openapi.config import OpenAPIConfig
from litestar.openapi.plugins import RapidocRenderPlugin, SwaggerRenderPlugin
from litestar.testing import TestClient


def test_rapidoc_csrf() -> None:
app = Litestar(
csrf_config=CSRFConfig(secret="litestar"),
openapi_config=OpenAPIConfig(
title="Litestar Example",
version="0.0.1",
render_plugins=[RapidocRenderPlugin()],
),
)

with TestClient(app=app) as client:
resp = client.get("/schema/rapidoc")
assert resp.status_code == 200
assert resp.headers["content-type"] == "text/html; charset=utf-8"
assert ".addEventListener('before-try'," in resp.text


def test_swagger_ui_csrf() -> None:
app = Litestar(
csrf_config=CSRFConfig(secret="litestar"),
openapi_config=OpenAPIConfig(
title="Litestar Example",
version="0.0.1",
render_plugins=[SwaggerRenderPlugin()],
),
)

with TestClient(app=app) as client:
resp = client.get("/schema/swagger")
assert resp.status_code == 200
assert resp.headers["content-type"] == "text/html; charset=utf-8"
assert "requestInterceptor:" in resp.text

0 comments on commit 59c219f

Please sign in to comment.