Ejemplo n.º 1
0
def test_docs_oauth2_redirect_path(client):
    rv = client.get('/docs/oauth2-redirect')
    assert rv.status_code == 200
    assert b'<title>Swagger UI: OAuth2 Redirect</title>' in rv.data
    rv = client.get('/docs')
    assert rv.status_code == 200
    assert b'oauth2RedirectUrl: "/docs/oauth2-redirect"' in rv.data

    app = APIFlask(__name__, docs_oauth2_redirect_path='/docs/oauth2/redirect')
    rv = app.test_client().get('/docs/oauth2/redirect')
    assert rv.status_code == 200
    assert b'<title>Swagger UI: OAuth2 Redirect</title>' in rv.data
    rv = app.test_client().get('/docs')
    assert rv.status_code == 200
    assert b'oauth2RedirectUrl: "/docs/oauth2/redirect"' in rv.data

    app = APIFlask(__name__, docs_oauth2_redirect_path=None)
    assert app.docs_oauth2_redirect_path is None

    rules = list(app.url_map.iter_rules())
    bp_endpoints = [
        rule.endpoint for rule in rules if rule.endpoint.startswith('openapi')
    ]
    assert len(bp_endpoints) == 4
    assert 'openapi.swagger_ui' in bp_endpoints
    assert 'openapi.swagger_ui_oauth_redirect' not in bp_endpoints
    rv = app.test_client().get('/docs')
    assert rv.status_code == 200
    assert b'oauth2RedirectUrl' not in rv.data
Ejemplo n.º 2
0
def test_yaml_spec():
    app = APIFlask(__name__, spec_path='/spec.yaml')
    client = app.test_client()

    rv = client.get('/spec.yaml')
    assert rv.status_code == 200
    assert rv.headers['Content-Type'] == 'text/vnd.yaml'
    assert b'title: APIFlask' in rv.data

    app = APIFlask(__name__, spec_path='/spec.yml')
    client = app.test_client()

    rv = client.get('/spec.yml')
    assert rv.status_code == 200
    assert rv.headers['Content-Type'] == 'text/vnd.yaml'
    assert b'title: APIFlask' in rv.data
Ejemplo n.º 3
0
def test_yaml_spec_mimetype():
    app = APIFlask(__name__, spec_path='/openapi.yaml')
    client = app.test_client()

    rv = client.get('/openapi.yaml')
    assert rv.status_code == 200
    assert rv.mimetype == 'text/vnd.yaml'

    app.config['YAML_SPEC_MIMETYPE'] = 'text/custom.yaml'

    rv = client.get('/openapi.yaml')
    assert rv.status_code == 200
    assert rv.mimetype == 'text/custom.yaml'
Ejemplo n.º 4
0
def test_bypasss_default_auth_error_handler():
    app = APIFlask(__name__, json_errors=False)
    auth = HTTPTokenAuth()

    @app.route('/foo')
    @auth_required(auth)
    def foo():
        pass

    rv = app.test_client().get('/foo')
    assert rv.status_code == 401
    assert rv.headers['Content-Type'] == 'text/html; charset=utf-8'
    assert b'Unauthorized Access' in rv.data
    assert 'WWW-Authenticate' in rv.headers
Ejemplo n.º 5
0
def test_json_errors(app, client):
    assert app.json_errors is True

    rv = client.get('/not-exist')
    assert rv.status_code == 404
    assert rv.headers['Content-Type'] == 'application/json'
    assert 'message' in rv.json
    assert 'detail' in rv.json
    assert rv.json['status_code'] == 404

    app = APIFlask(__name__, json_errors=False)
    assert app.json_errors is False
    rv = app.test_client().get('/not-exist')
    assert rv.status_code == 404
    assert rv.headers['Content-Type'] == 'text/html; charset=utf-8'
    assert b'!DOCTYPE' in rv.data