Skip to content

Commit

Permalink
Add tests for the new docs UI support
Browse files Browse the repository at this point in the history
  • Loading branch information
greyli committed Jun 28, 2022
1 parent 3482b64 commit 2d53838
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/apiflask/ui_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
</head>
<body>
<rapi-pdf
spec-url = "{{ url_for('openapi.spec') }}"
spec-url="{{ url_for('openapi.spec') }}"
{% if config.RAPIPDF_CONFIG %}
{% for key, value in config.RAPIPDF_CONFIG.items() %}
{{ key }}={{ value | tojson }}
Expand Down
17 changes: 17 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,20 @@ def bar():
else:
assert 'Foo' in spec['components']['schemas']
assert 'BarUpdate' in spec['components']['schemas']


@pytest.mark.parametrize(
'ui_name',
['swagger-ui', 'redoc', 'elements', 'rapidoc', 'rapipdf']
)
def test_docs_ui(ui_name):
app = APIFlask(__name__, docs_ui=ui_name)
client = app.test_client()

rv = client.get('/docs')
assert rv.status_code == 200


def test_bad_docs_ui():
with pytest.raises(ValueError):
APIFlask(__name__, docs_ui='bad')
33 changes: 26 additions & 7 deletions tests/test_openapi_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_openapi_blueprint(app):
bp_endpoints = [rule.endpoint for rule in rules if rule.endpoint.startswith('openapi')]
assert len(bp_endpoints) == 4
assert 'openapi.spec' in bp_endpoints
assert 'openapi.swagger_ui' in bp_endpoints
assert 'openapi.docs' in bp_endpoints
assert 'openapi.swagger_ui_oauth_redirect' in bp_endpoints
assert 'openapi.redoc' in bp_endpoints

Expand Down Expand Up @@ -50,7 +50,7 @@ def test_docs_path(app):
rules = list(app.url_map.iter_rules())
bp_endpoints = [rule.endpoint for rule in rules if rule.endpoint.startswith('openapi')]
assert len(bp_endpoints) == 2
assert 'openapi.swagger_ui' not in bp_endpoints
assert 'openapi.docs' not in bp_endpoints
assert 'openapi.swagger_ui_oauth_redirect' not in bp_endpoints


Expand All @@ -76,7 +76,7 @@ def test_docs_oauth2_redirect_path(client):
rules = list(app.url_map.iter_rules())
bp_endpoints = [rule.endpoint for rule in rules if rule.endpoint.startswith('openapi')]
assert len(bp_endpoints) == 3
assert 'openapi.swagger_ui' in bp_endpoints
assert 'openapi.docs' in bp_endpoints
assert 'openapi.swagger_ui_oauth_redirect' not in bp_endpoints
rv = app.test_client().get('/docs')
assert rv.status_code == 200
Expand Down Expand Up @@ -107,15 +107,34 @@ def test_disable_openapi_with_enable_openapi_arg(app):


def test_swagger_ui(client):
# default APIFlask(docs_ui) value is swagger-ui
rv = client.get('/docs')
assert rv.status_code == 200
assert b'swagger-ui-standalone-preset.js' in rv.data
assert b'Swagger UI' in rv.data

app = APIFlask(__name__, docs_ui='swagger-ui')
rv = app.test_client().get('/docs')
assert rv.status_code == 200
assert b'Swagger UI' in rv.data


@pytest.mark.parametrize(
'ui_name',
[
('swagger-ui', b'Swagger UI'),
('redoc', b'Redoc'),
('elements', b'Elements'),
('rapidoc', b'RapiDoc'),
('rapipdf', b'RapiPDF'),
]
)
def test_other_ui(ui_name):
app = APIFlask(__name__, docs_ui=ui_name[0])
client = app.test_client()

def test_redoc(client):
rv = client.get('/redoc')
rv = client.get('/docs')
assert rv.status_code == 200
assert b'redoc.standalone.js' in rv.data
assert ui_name[1] in rv.data


def test_openapi_blueprint_url_prefix(app):
Expand Down
91 changes: 91 additions & 0 deletions tests/test_settings_api_docs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest

from apiflask import APIFlask


def test_docs_favicon(app, client):
app.config['DOCS_FAVICON'] = '/my-favicon.png'
Expand Down Expand Up @@ -86,3 +88,92 @@ def test_swagger_ui_oauth_config(app, client):
assert b'ui.initOAuth(' in rv.data
assert b'"clientId": "foo"' in rv.data
assert b'"usePkceWithAuthorizationCodeGrant": true' in rv.data


def test_elements_config():
app = APIFlask(__name__, docs_ui='elements')
app.config['ELEMENTS_CONFIG'] = {
'hideTryIt': False,
'router': 'memory'
}

rv = app.test_client().get('/docs')
assert rv.status_code == 200
assert b'hideTryIt=false' in rv.data
assert b'router="memory"' in rv.data


def test_elements_layout():
app = APIFlask(__name__, docs_ui='elements')
app.config['ELEMENTS_LAYOUT'] = 'stacked'

rv = app.test_client().get('/docs')
assert rv.status_code == 200
assert b'layout="stacked"' in rv.data
assert b'layout="sidebar"' not in rv.data


def test_elements_resources():
app = APIFlask(__name__, docs_ui='elements')
app.config['ELEMENTS_CSS'] = 'https://cdn.example.com/elements.css'
app.config['ELEMENTS_JS'] = 'https://cdn.example.com/elements.js'

rv = app.test_client().get('/docs')
assert rv.status_code == 200
assert b'href="https://cdn.example.com/elements.css"' in rv.data
assert b'src="https://cdn.example.com/elements.js"' in rv.data


def test_rapidoc_config():
app = APIFlask(__name__, docs_ui='rapidoc')
app.config['RAPIDOC_CONFIG'] = {
'update-route': False,
'layout': 'row'
}

rv = app.test_client().get('/docs')
assert rv.status_code == 200
assert b'update-route=false' in rv.data
assert b'layout="row"' in rv.data


def test_rapidoc_theme():
app = APIFlask(__name__, docs_ui='rapidoc')
app.config['RAPIDOC_THEME'] = 'dark'

rv = app.test_client().get('/docs')
assert rv.status_code == 200
assert b'theme="dark"' in rv.data
assert b'theme="light"' not in rv.data


def test_rapidoc_resources():
app = APIFlask(__name__, docs_ui='rapidoc')
app.config['RAPIDOC_JS'] = 'https://cdn.example.com/rapidoc.js'

rv = app.test_client().get('/docs')
assert rv.status_code == 200
assert b'src="https://cdn.example.com/rapidoc.js"' in rv.data


def test_rapipdf_config():
app = APIFlask(__name__, docs_ui='rapipdf')
app.config['RAPIPDF_CONFIG'] = {
'include-example': True,
'button-label': 'Generate!'
}

rv = app.test_client().get('/docs')
assert rv.status_code == 200
assert b'include-example=true' in rv.data
assert b'button-label="Generate!"' in rv.data


def test_rapipdf_resources():
app = APIFlask(__name__, docs_ui='rapipdf')
client = app.test_client()
app.config['RAPIPDF_JS'] = 'https://cdn.example.com/rapipdf.js'

rv = client.get('/docs')
assert rv.status_code == 200
assert b'src="https://cdn.example.com/rapipdf.js"' in rv.data

0 comments on commit 2d53838

Please sign in to comment.