Example #1
0
async def test_no_openapi():
    app = Pint('test',
               title='App Test',
               contact='foo',
               contact_email='*****@*****.**',
               no_openapi=True)
    client = app.test_client()
    rv = await client.get('/openapi.json')
    assert rv.status_code == HTTPStatus.NOT_FOUND
Example #2
0
async def test_custom_openapi():
    app = Pint('test',
               title='App Test',
               contact='foo',
               no_openapi=True,
               contact_email='*****@*****.**')

    @app.route('/hello')
    class Hello(Resource):
        async def get(self):
            return "OK"

    @app.route('/api.json', methods=['GET', 'OPTIONS'])
    async def api():
        return jsonify(app.__schema__)

    client = app.test_client()
    rv = await client.get('/api.json')
    assert rv.status_code == HTTPStatus.OK

    data = await rv.get_json()
    assert data == {
        'openapi': '3.0.0',
        'info': {
            'title': 'App Test',
            'version': '1.0',
            'contact': {
                'name': 'foo',
                'email': '*****@*****.**'
            }
        },
        'servers': [{
            'url': 'http://'
        }],
        'paths': {
            '/hello': {
                'get': {
                    'description': '',
                    'tags': [],
                    'responses': {
                        '200': {
                            'description': 'Success'
                        }
                    },
                    'operationId': 'get_hello'
                }
            }
        }
    }
Example #3
0
async def test_base_model_obj():
    app = Pint('test',
               title='App Test',
               contact='foo',
               contact_email='*****@*****.**',
               description='Sample Desc',
               base_model_schema=TEST_BASE_MODEL_SCHEMA)

    @app.route('/testque')
    @app.param('test_que', ref='#/components/parameters/test_query')
    class TestQue(Resource):
        async def get(self):
            return request.args['moo']

    test_que_ref = app.create_ref_validator('test_query', 'parameters')

    @app.route('/testref')
    @app.param('test_que_ref', ref=test_que_ref)
    class TestRef(Resource):
        async def get(self):
            return request.args['moo']

    @app.route('/testquename')
    @app.param('test_que_name', ref='test_query')
    class TestQueName(Resource):
        async def get(self):
            return request.args['moo']

    swag = app.__schema__
    assert swag['components']['parameters']['test_query']['name'] == 'moo'
    swag_testque = swag['paths']['/testque']['get']
    swag_testref = swag['paths']['/testref']['get']
    swag_testquename = swag['paths']['/testquename']['get']
    assert swag_testque['parameters'] == swag_testref['parameters']
    assert swag_testque['parameters'] == swag_testquename['parameters']

    client = app.test_client()

    rv = await client.get('/testque?moo=foo')
    assert rv.status_code == HTTPStatus.OK
    assert await rv.get_data() == b'foo'

    rv = await client.get('/testref?moo=bar')
    assert rv.status_code == HTTPStatus.OK
    assert await rv.get_data() == b'bar'

    rv = await client.get('/testquename?moo=baz')
    assert rv.status_code == HTTPStatus.OK
    assert await rv.get_data() == b'baz'
Example #4
0
def create_app(config_class=Development) -> Pint:
    app = Pint(__name__)
    app.test_client()
    DbWrapper.set_url(config_class.SQLALCHEMY_DATABASE_URI)
    db = DbWrapper.create_instance()

    @app.before_request
    async def connect_db() -> None:
        # todo: replace Exception with AlreadyConnectedToDbException
        try:
            await db.connect()
        except Exception:
            pass

    @app.after_request
    async def disconnect_db(response) -> None:
        await db.disconnect()
        return response

    app.config.from_object(Development)
    register_blueprints(app)

    app = cors(app, allow_credentials=True)
    return app
async def test_required_query():
    app = Pint('test', title='App Test', contact='foo',
                  contact_email='*****@*****.**', description='Sample Desc',
                  base_model_schema=TEST_BASE_MODEL_SCHEMA)

    test_que_ref = app.create_ref_validator('test_query', 'parameters')

    @app.param('test_que_ref', _in='query', schema=test_que_ref)
    @app.route('/testref')
    def testref():
        return request.args['moo']

    client = app.test_client()
    rv = await client.get('/testref')
    # will add validation in the future for required query args
    assert rv.status_code == HTTPStatus.BAD_REQUEST
async def test_openapi_blueprint_noprefix(app: Pint) -> None:
    blueprint = PintBlueprint('blueprint', __name__)

    @blueprint.route('/')
    class Testing(Resource):
        async def get(self):
            return 'GET'

    app.register_blueprint(blueprint)

    client = app.test_client()
    response = await client.get('/openapi.json')
    assert response.status_code == HTTPStatus.OK

    openapi = await response.get_json()
    assert openapi['paths'].get('/', None) is not None
    assert openapi['paths']['/'].get('get', None) is not None
async def test_blueprint_method_view(app: Pint) -> None:
    blueprint = Blueprint('blueprint', __name__)

    class Views(MethodView):
        async def get(self) -> ResponseReturnValue:
            return 'GET'

        async def post(self) -> ResponseReturnValue:
            return 'POST'

    blueprint.add_url_rule('/', view_func=Views.as_view('simple'))
    app.register_blueprint(blueprint)

    test_client = app.test_client()
    response = await test_client.get('/')
    assert 'GET' == (await response.get_data(raw=False))
    response = await test_client.post('/')
    assert 'POST' == (await response.get_data(raw=False))
async def test_blueprint_resource(app: Pint) -> None:
    blueprint = PintBlueprint('blueprint', __name__)

    @blueprint.route('/testing')
    class Testing(Resource):
        async def get(self):
            return 'GET'

        async def post(self):
            return 'POST'

    app.register_blueprint(blueprint)

    client = app.test_client()
    response = await client.post('/testing')
    assert 'POST' == (await response.get_data(raw=False))
    response = await client.get('/testing')
    assert 'GET' == (await response.get_data(raw=False))
async def test_openapi_with_blueprint(app: Pint) -> None:
    blueprint = PintBlueprint('blueprint', __name__, url_prefix='/blueprint')

    @app.route('/testing')
    @blueprint.route('/testing')
    class Testing(Resource):
        async def get(self):
            return 'GET'

        async def post(self):
            return 'POST'

    app.register_blueprint(blueprint)

    client = app.test_client()
    response = await client.get('/openapi.json')
    assert response.status_code == HTTPStatus.OK

    openapi = await response.get_json()
    assert len(openapi['paths'].keys()) == 2
Example #10
0
async def test_post_validation():
    app = Pint('test', base_model_schema=TEST_BASE_MODEL_SCHEMA)

    test_ref = app.create_ref_validator('User', 'schemas')

    @app.route('/testroute')
    class TestReq(Resource):
        @app.expect(test_ref)
        async def post(self):
            return jsonify(await request.get_json())

    client = app.test_client()

    # fail validation, missing required props
    rv = await client.post('/testroute', json={})
    assert rv.status_code == HTTPStatus.BAD_REQUEST
    assert rv.headers['content-type'] == 'application/json'
    data = await rv.get_json()
    assert data['message'] == 'Request Body failed validation'
    assert 'msg' in data['error'] and data['error']['msg']
    assert 'value' in data['error']
    assert 'schema' in data['error']

    # fail validation, have required props, but age is wrong type
    rv = await client.post('/testroute', json={'name': 'foobar', 'age': 'baz'})
    assert rv.status_code == HTTPStatus.BAD_REQUEST
    assert rv.headers['content-type'] == 'application/json'
    data = await rv.get_json()
    assert data['message'] == 'Request Body failed validation'
    assert 'msg' in data['error'] and data['error']['msg']
    assert 'value' in data['error']
    assert 'schema' in data['error']

    # succeed validation
    rv = await client.post('/testroute', json={'name': 'foobar', 'age': 10})
    assert rv.status_code == HTTPStatus.OK