def test_errorhandler_with_namespace(self, app, client): api = restplus.Api(app) ns = restplus.Namespace("ExceptionHandler", path="/") class CustomException(RuntimeError): pass @ns.route('/test/', endpoint='test') class TestResource(restplus.Resource): def get(self): raise CustomException('error') @ns.errorhandler(CustomException) def handle_custom_exception(error): return {'message': str(error), 'test': 'value'}, 400 api.add_namespace(ns) response = client.get('/test/') assert response.status_code == 400 assert response.content_type == 'application/json' data = json.loads(response.data.decode('utf8')) assert data == { 'message': 'error', 'test': 'value', }
def test_api_payload(self, app, client): api = restplus.Api(app, validate=True) ns = restplus.Namespace('apples') api.add_namespace(ns) fields = ns.model('Person', { 'name': restplus.fields.String(required=True), 'age': restplus.fields.Integer, 'birthdate': restplus.fields.DateTime, }) @ns.route('/validation/') class Payload(restplus.Resource): payload = None @ns.expect(fields) def post(self): Payload.payload = ns.payload return {} data = { 'name': 'John Doe', 'age': 15, } client.post_json('/apples/validation/', data) assert Payload.payload == data
def test_multiple_ns_with_authorizations(self, app): api = restplus.Api() a1 = {'apikey': {'type': 'apiKey', 'in': 'header', 'name': 'X-API'}} a2 = { 'oauth2': { 'type': 'oauth2', 'flow': 'accessCode', 'tokenUrl': 'https://somewhere.com/token', 'scopes': { 'read': 'Grant read-only access', 'write': 'Grant read-write access', } } } ns1 = restplus.Namespace('ns1', authorizations=a1) ns2 = restplus.Namespace('ns2', authorizations=a2) @ns1.route('/') class Ns1(restplus.Resource): @ns1.doc(security='apikey') def get(self): pass @ns2.route('/') class Ns2(restplus.Resource): @ns1.doc(security='oauth2') def post(self): pass api.add_namespace(ns1, path='/ns1') api.add_namespace(ns2, path='/ns2') api.init_app(app) assert { "apikey": [] } in api.__schema__["paths"]["/ns1/"]["get"]["security"] assert { "oauth2": [] } in api.__schema__["paths"]["/ns2/"]["post"]["security"] unified_auth = copy.copy(a1) unified_auth.update(a2) assert api.__schema__["securityDefinitions"] == unified_auth
def test_ns_path_prefixes(self, app): api = restplus.Api() ns = restplus.Namespace('test_ns', description='Test namespace') @ns.route('/test/', endpoint='test_resource') class TestResource(restplus.Resource): pass api.add_namespace(ns, '/api_test') api.init_app(app) with app.test_request_context(): assert url_for('test_resource') == '/api_test/test/'