File: test_openapi_docs.py

package info (click to toggle)
django-ninja 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,280 kB
  • sloc: python: 16,041; javascript: 1,689; makefile: 40; sh: 25
file content (66 lines) | stat: -rw-r--r-- 1,928 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from django.conf import settings
from django.test import override_settings

from ninja import NinjaAPI, Redoc, Swagger
from ninja.testing import TestClient

NO_NINJA_INSTALLED_APPS = [i for i in settings.INSTALLED_APPS if i != "ninja"]


def test_swagger():
    "Default engine is swagger"
    api = NinjaAPI()

    assert isinstance(api.docs, Swagger)

    client = TestClient(api)

    response = client.get("/docs")
    assert response.status_code == 200
    assert b"swagger-ui-init.js" in response.content

    # Testing without ninja in INSTALLED_APPS
    @override_settings(INSTALLED_APPS=NO_NINJA_INSTALLED_APPS)
    def call_docs():
        response = client.get("/docs")
        assert response.status_code == 200
        assert b"https://cdn.jsdelivr.net/npm/swagger-ui-dist" in response.content

    call_docs()


def test_swagger_settings():
    api = NinjaAPI(docs=Swagger(settings={"persistAuthorization": True}))
    client = TestClient(api)
    response = client.get("/docs")
    assert response.status_code == 200
    assert b'"persistAuthorization": true' in response.content


def test_redoc():
    api = NinjaAPI(docs=Redoc())
    client = TestClient(api)

    response = client.get("/docs")
    assert response.status_code == 200
    assert b"redoc.standalone.js" in response.content

    # Testing without ninja in INSTALLED_APPS
    @override_settings(INSTALLED_APPS=NO_NINJA_INSTALLED_APPS)
    def call_docs():
        response = client.get("/docs")
        assert response.status_code == 200
        assert (
            b"https://cdn.jsdelivr.net/npm/redoc@2/bundles/redoc.standalone.js"
            in response.content
        )

    call_docs()


def test_redoc_settings():
    api = NinjaAPI(docs=Redoc(settings={"disableSearch": True}))
    client = TestClient(api)
    response = client.get("/docs")
    assert response.status_code == 200
    assert b'"disableSearch": true' in response.content