Debian Codegraph @

tests/test_openapi_docs.py

raw symbols SCIP
1 from django.conf import settings
2 from django.test import override_settings
3
4 from ninja import NinjaAPI, Redoc, Swagger
5 from ninja.testing import TestClient
6
7 NO_NINJA_INSTALLED_APPS = [i for i in settings.INSTALLED_APPS if i != "ninja"]
8
9
10 def test_swagger():
11 "Default engine is swagger"
12 api = NinjaAPI()
13
14 assert isinstance(api.docs, Swagger)
15
16 client = TestClient(api)
17
18 response = client.get("/docs")
19 assert response.status_code == 200
20 assert b"swagger-ui-init.js" in response.content
21
22 # Testing without ninja in INSTALLED_APPS
23 @override_settings(INSTALLED_APPS=NO_NINJA_INSTALLED_APPS)
24 def call_docs():
25 response = client.get("/docs")
26 assert response.status_code == 200
27 assert b"https://cdn.jsdelivr.net/npm/swagger-ui-dist" in response.content
28
29 call_docs()
30
31
32 def test_swagger_settings():
33 api = NinjaAPI(docs=Swagger(settings={"persistAuthorization": True}))
34 client = TestClient(api)
35 response = client.get("/docs")
36 assert response.status_code == 200
37 assert b'"persistAuthorization": true' in response.content
38
39
40 def test_redoc():
41 api = NinjaAPI(docs=Redoc())
42 client = TestClient(api)
43
44 response = client.get("/docs")
45 assert response.status_code == 200
46 assert b"redoc.standalone.js" in response.content
47
48 # Testing without ninja in INSTALLED_APPS
49 @override_settings(INSTALLED_APPS=NO_NINJA_INSTALLED_APPS)
50 def call_docs():
51 response = client.get("/docs")
52 assert response.status_code == 200
53 assert (
54 b"https://cdn.jsdelivr.net/npm/redoc@2/bundles/redoc.standalone.js"
55 in response.content
56 )
57
58 call_docs()
59
60
61 def test_redoc_settings():
62 api = NinjaAPI(docs=Redoc(settings={"disableSearch": True}))
63 client = TestClient(api)
64 response = client.get("/docs")
65 assert response.status_code == 200
66 assert b'"disableSearch": true' in response.content