✨ Allow disabling docs UIs by disabling OpenAPI (#1421)

* ✨ Allow disabling docs UIs by disabling openapi_url

* 📝 Add docs for disabling OpenAPI and docs in prod or other environments

* ✅ Add tests for disabling OpenAPI and docs
This commit is contained in:
Sebastián Ramírez authored and GitHub committed 2020-05-16 17:45:12 +02:00
1 parent cfb72eec5a
commit 409264960e
7 files changed
+122 -4

No files matched your search

Whitespace-only changes.
@@ -0,0 +1,46 @@
import importlib
from fastapi.testclient import TestClient
from conditional_openapi import tutorial001
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/": {
"get": {
"summary": "Root",
"operationId": "root__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}
},
}
}
},
}
def test_default_openapi():
client = TestClient(tutorial001.app)
response = client.get("/openapi.json")
assert response.json() == openapi_schema
response = client.get("/docs")
assert response.status_code == 200, response.text
response = client.get("/redoc")
assert response.status_code == 200, response.text
def test_disable_openapi(monkeypatch):
monkeypatch.setenv("OPENAPI_URL", "")
importlib.reload(tutorial001)
client = TestClient(tutorial001.app)
response = client.get("/openapi.json")
assert response.status_code == 404, response.text
response = client.get("/docs")
assert response.status_code == 404, response.text
response = client.get("/redoc")
assert response.status_code == 404, response.text