def test_api_extra_spec_kwargs_init_app_update_init(self, app): """Test empty APISpec kwargs passed in init_app update init kwargs""" api = Api(spec_kwargs={'basePath': '/v1', 'host': 'example.com'}) api.init_app(app, spec_kwargs={'basePath': '/v2'}) spec = api.spec.to_dict() assert spec['host'] == 'example.com' assert spec['basePath'] == '/v2'
def test_api_register_field_before_and_after_init(self, app, openapi_version): app.config['OPENAPI_VERSION'] = openapi_version api = Api() class CustomField_1(ma.fields.Field): pass class CustomField_2(ma.fields.Field): pass api.register_field(CustomField_1, 'custom string', 'custom') api.init_app(app) api.register_field(CustomField_2, 'custom string', 'custom') class Schema_1(ma.Schema): int_1 = ma.fields.Int() custom_1 = CustomField_1() class Schema_2(ma.Schema): int_2 = ma.fields.Int() custom_2 = CustomField_2() api.spec.components.schema('Schema_1', schema=Schema_1) api.spec.components.schema('Schema_2', schema=Schema_2) schema_defs = get_schemas(api.spec) assert schema_defs['Schema_1']['properties']['custom_1'] == { 'type': 'custom string', 'format': 'custom' } assert schema_defs['Schema_2']['properties']['custom_2'] == { 'type': 'custom string', 'format': 'custom' }
def test_api_extra_spec_kwargs_init_app_update_init(self, app): """Test empty APISpec kwargs passed in init_app update init kwargs""" api = Api(spec_kwargs={"basePath": "/v1", "host": "example.com"}) api.init_app(app, spec_kwargs={"basePath": "/v2"}) spec = api.spec.to_dict() assert spec["host"] == "example.com" assert spec["basePath"] == "/v2"
def test_api_register_field_before_and_after_init(self, app, openapi_version): app.config["OPENAPI_VERSION"] = openapi_version api = Api() class CustomField_1(ma.fields.Field): pass class CustomField_2(ma.fields.Field): pass api.register_field(CustomField_1, "custom string", "custom") api.init_app(app) api.register_field(CustomField_2, "custom string", "custom") class Schema_1(ma.Schema): int_1 = ma.fields.Int() custom_1 = CustomField_1() class Schema_2(ma.Schema): int_2 = ma.fields.Int() custom_2 = CustomField_2() api.spec.components.schema("Schema_1", schema=Schema_1) api.spec.components.schema("Schema_2", schema=Schema_2) schema_defs = get_schemas(api.spec) assert schema_defs["Schema_1"]["properties"]["custom_1"] == { "type": "custom string", "format": "custom", } assert schema_defs["Schema_2"]["properties"]["custom_2"] == { "type": "custom string", "format": "custom", }
def test_api_register_converter_before_and_after_init( self, app, openapi_version): api = Api() blp = Blueprint('test', 'test', url_prefix='/test') class CustomConverter_1(BaseConverter): pass class CustomConverter_2(BaseConverter): pass app.url_map.converters['custom_str_1'] = CustomConverter_1 app.url_map.converters['custom_str_2'] = CustomConverter_2 api.register_converter(CustomConverter_1, 'custom string 1') api.init_app(app) api.register_converter(CustomConverter_2, 'custom string 2') @blp.route('/1/<custom_str_1:val>') def test_func_1(val): pass @blp.route('/2/<custom_str_2:val>') def test_func_2(val): pass api.register_blueprint(blp) spec = api.spec.to_dict() parameter_1 = spec['paths']['/test/1/{val}']['parameters'][0] parameter_2 = spec['paths']['/test/2/{val}']['parameters'][0] if 'openapi_version' == '2.0': assert parameter_1['type'] == 'custom string 1' assert parameter_2['type'] == 'custom string 2' else: assert parameter_1['schema']['type'] == 'custom string 1' assert parameter_2['schema']['type'] == 'custom string 2'
def create_app(config_class=Config): app = Flask(__name__) app.config.from_object(config_class) db.init_app(app) migrate.init_app(app, db) CORS(app) mail.init_app(app) api = Api() api.init_app(app) from app.errors import bp as errors_bp app.register_blueprint(errors_bp) from app.api import bp as api_bp api.register_blueprint(api_bp, url_prefix='/api') from app.main import bp as main_bp app.register_blueprint(main_bp) if not app.debug and not app.testing: if app.config['MAIL_SERVER']: auth = None if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']: auth = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD']) secure = None if app.config['MAIL_USE_TLS']: secure = () mail_handler = SMTPHandler( mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']), fromaddr='no-reply@' + app.config['MAIL_SERVER'], toaddrs=app.config['ADMINS'], subject='Starterflask Failure', credentials=auth, secure=secure) mail_handler.setLevel(logging.ERROR) app.logger.addHandler(mail_handler) if not os.path.exists('logs'): os.mkdir('logs') file_handler = RotatingFileHandler('logs/Starterflask.log', maxBytes=10240, backupCount=10) file_handler.setFormatter( logging.Formatter('%(asctime)s %(levelname)s: %(message)s ' '[in %(pathname)s:%(lineno)d]')) file_handler.setLevel(logging.INFO) app.logger.addHandler(file_handler) app.logger.setLevel(logging.INFO) app.logger.info('Starterflask startup') return app
def test_api_register_converter_before_or_after_init( self, app, openapi_version): app.config['OPENAPI_VERSION'] = openapi_version api = Api() blp = Blueprint('test', 'test', url_prefix='/test') class CustomConverter_1(BaseConverter): pass class CustomConverter_2(BaseConverter): pass def converter12paramschema(converter): return {'type': 'custom string 1'} def converter22paramschema(converter): return {'type': 'custom string 2'} app.url_map.converters['custom_str_1'] = CustomConverter_1 app.url_map.converters['custom_str_2'] = CustomConverter_2 api.register_converter(CustomConverter_1, converter12paramschema) api.init_app(app) api.register_converter(CustomConverter_2, converter22paramschema) @blp.route('/1/<custom_str_1:val>') def test_func_1(val): pass @blp.route('/2/<custom_str_2:val>') def test_func_2(val): pass api.register_blueprint(blp) spec = api.spec.to_dict() parameter_1 = spec['paths']['/test/1/{val}']['parameters'][0] parameter_2 = spec['paths']['/test/2/{val}']['parameters'][0] if openapi_version == '2.0': assert parameter_1['type'] == 'custom string 1' assert parameter_2['type'] == 'custom string 2' else: assert parameter_1['schema']['type'] == 'custom string 1' assert parameter_2['schema']['type'] == 'custom string 2'
def test_api_register_converter_before_or_after_init( self, app, openapi_version): app.config["OPENAPI_VERSION"] = openapi_version api = Api() blp = Blueprint("test", "test", url_prefix="/test") class CustomConverter_1(BaseConverter): pass class CustomConverter_2(BaseConverter): pass def converter12paramschema(converter): return {"type": "custom string 1"} def converter22paramschema(converter): return {"type": "custom string 2"} app.url_map.converters["custom_str_1"] = CustomConverter_1 app.url_map.converters["custom_str_2"] = CustomConverter_2 api.register_converter(CustomConverter_1, converter12paramschema) api.init_app(app) api.register_converter(CustomConverter_2, converter22paramschema) @blp.route("/1/<custom_str_1:val>") def test_func_1(val): pass @blp.route("/2/<custom_str_2:val>") def test_func_2(val): pass api.register_blueprint(blp) spec = api.spec.to_dict() parameter_1 = spec["paths"]["/test/1/{val}"]["parameters"][0] parameter_2 = spec["paths"]["/test/2/{val}"]["parameters"][0] if openapi_version == "2.0": assert parameter_1["type"] == "custom string 1" assert parameter_2["type"] == "custom string 2" else: assert parameter_1["schema"]["type"] == "custom string 1" assert parameter_2["schema"]["type"] == "custom string 2"
def create_app(cfg='default'): from config import config app = Flask(__name__) app.config.from_object(config[cfg]) app.url_map.strict_slashes = False config[cfg].init_app(app) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False from . import resources api = Api() api.init_app(app) db.init_app(app) ma.init_app(app) jwt.init_app(app) Migrate(app, db) resources.register_blueprints(api) return app
def test_api_extra_spec_kwargs(self, app, step): """Test APISpec kwargs can be passed in Api init or app config""" app.config['API_SPEC_OPTIONS'] = {'basePath': '/v2'} if step == 'at_once': api = Api(app, spec_kwargs={ 'basePath': '/v1', 'host': 'example.com' }) elif step == 'init': api = Api(spec_kwargs={'basePath': '/v1', 'host': 'example.com'}) api.init_app(app) elif step == 'init_app': api = Api() api.init_app(app, spec_kwargs={ 'basePath': '/v1', 'host': 'example.com' }) spec = api.spec.to_dict() assert spec['host'] == 'example.com' # app config overrides Api spec_kwargs parameters assert spec['basePath'] == '/v2'
def test_api_extra_spec_kwargs(self, app, step): """Test APISpec kwargs can be passed in Api init or app config""" app.config["API_SPEC_OPTIONS"] = {"basePath": "/v2"} if step == "at_once": api = Api(app, spec_kwargs={ "basePath": "/v1", "host": "example.com" }) elif step == "init": api = Api(spec_kwargs={"basePath": "/v1", "host": "example.com"}) api.init_app(app) elif step == "init_app": api = Api() api.init_app(app, spec_kwargs={ "basePath": "/v1", "host": "example.com" }) spec = api.spec.to_dict() assert spec["host"] == "example.com" # app config overrides Api spec_kwargs parameters assert spec["basePath"] == "/v2"