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
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
def test_info_title_and_version(app): assert app.title == 'APIFlask' assert app.version == '0.1.0' app = APIFlask(__name__, title='Foo', version='1.0') assert app.spec['info']['title'] == 'Foo' assert app.spec['info']['version'] == '1.0'
def test_disable_openapi_with_enable_openapi_arg(app): assert app.enable_openapi app = APIFlask(__name__, enable_openapi=False) assert app.enable_openapi is False rules = list(app.url_map.iter_rules()) bp_endpoints = [ rule.endpoint for rule in rules if rule.endpoint.startswith('openapi') ] assert len(bp_endpoints) == 0
def test_redoc_path(app): assert app.redoc_path app = APIFlask(__name__, redoc_path=None) assert app.redoc_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.redoc' not in bp_endpoints
def test_docs_path(app): assert app.docs_path app = APIFlask(__name__, docs_path=None) assert app.docs_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) == 3 assert 'openapi.swagger_ui' not in bp_endpoints assert 'openapi.swagger_ui_oauth_redirect' not in bp_endpoints
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'
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
def test_openapi_blueprint(app): assert 'openapi' in app.blueprints rules = list(app.url_map.iter_rules()) bp_endpoints = [ rule.endpoint for rule in rules if rule.endpoint.startswith('openapi') ] assert len(bp_endpoints) == 5 assert 'openapi.spec' in bp_endpoints assert 'openapi.swagger_ui' in bp_endpoints assert 'openapi.swagger_ui_oauth_redirect' in bp_endpoints assert 'openapi.redoc' in bp_endpoints assert 'openapi.static' in bp_endpoints app = APIFlask(__name__, spec_path=None, docs_path=None, redoc_path=None) assert 'openapi' not in app.blueprints
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
def create_app(): '''Función principal de la aplicación''' # Crear el objeto app app = APIFlask(__name__) # Obtener la configuración de la aplicación a partir de settings.py app.config.from_pyfile("settings.py") # Se incializa la conexión entre SQLALchemy y la base de datos db.init_app(app) @app.before_first_request def crea_bases(): '''Función encargada de verificar que exista una base de datos con las tablas de alumnos y de usuarios pobladas correctamente. En caso de no existir, es creada''' # Verifica que exista la tabla alumno en la base de datos inspector = inspect(db.engine) if not inspector.has_table('alumno'): # Crea y llena la base de alumno. db.create_all() with open(app.config['PATH'] + "/../data/alumnos.txt", "rt") as f: alumnos = eval(f.read()) for alumno in alumnos: if Alumno.query.filter_by(cuenta=alumno["cuenta"]).first(): continue else: db.session.add(Alumno(**alumno)) db.session.commit() # Verifica que exista el usuario admin y lo crea si no es así if not User.query.filter_by(username="******").first(): user = User(username='******', email='*****@*****.**', password='******', active=True) db.session.add(user) db.session.commit() # Registra los blueprints con los endpoints app.register_blueprint(abc_alumnos, url_prefix='/api') app.register_blueprint(auth_bp, url_prefix='/auth') #Regresa la aplicación return app
from apiflask import APIFlask, Schema, input, output, abort_json from apiflask.fields import Integer, String from apiflask.validators import Length, OneOf from apiflask.schemas import EmptySchema app = APIFlask(__name__) pets = [ { 'id': 0, 'name': 'Kitty', 'category': 'cat' }, { 'id': 1, 'name': 'Coco', 'category': 'dog' }, { 'id': 2, 'name': 'Flash', 'category': 'cat' } ] class PetInSchema(Schema): name = String(required=True, validate=Length(0, 10)) category = String(required=True, validate=OneOf(['dog', 'cat']))
def app(): app = APIFlask(__name__) return app