def test_api_payload(self, app, client): api = Api(app, validate=True) ns = Namespace('apples') api.add_namespace(ns) f = ns.model( 'Person', { 'name': fields.String(required=True), 'age': fields.Integer, 'birthdate': fields.DateTime, }) @ns.route('/validation/') class Payload(Resource): payload = None @ns.expect(f) async def post(self, request): Payload.payload = ns.payload return {} data = { 'name': 'John Doe', 'age': 15, } client.post_json('/apples/validation/', data) assert Payload.payload == data
def test_inherit(self): api = Namespace('test') parent = api.model('Parent', {}) api.inherit('Child', parent, {}) assert 'Parent' in api.models assert 'Child' in api.models
def test_inherit(self): authorizations = { 'apikey': { 'type': 'apiKey', 'in': 'header', 'name': 'X-API-KEY' } } api = Namespace('test', authorizations=authorizations) parent = api.model('Parent', {}) api.inherit('Child', parent, {}) assert 'Parent' in api.models assert 'Child' in api.models assert api.authorizations == authorizations
def test_doc_decorator(self): api = Namespace('test') params = {'q': {'description': 'some description'}} @api.doc(params=params) class TestResource(restplus.Resource): pass assert hasattr(TestResource, '__apidoc__') assert TestResource.__apidoc__ == {'params': params}
def test_multiple_ns_with_authorizations(self, app): api = sanic_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 = Namespace('ns1', authorizations=a1) ns2 = Namespace('ns2', authorizations=a2) @ns1.route('/') class Ns1(sanic_restplus.Resource): @ns1.doc(security='apikey') async def get(self, request): pass @ns2.route('/') class Ns2(sanic_restplus.Resource): @ns1.doc(security='oauth2') async def post(self, request): 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 = sanic_restplus.Api() ns = Namespace('test_ns', description='Test namespace') @ns.route('/test/', endpoint='test_resource') class TestResource(sanic_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/'
def test_inherit_from_multiple_parents(self): api = Namespace('test') grand_parent = api.model('GrandParent', {}) parent = api.model('Parent', {}) api.inherit('Child', grand_parent, parent, {}) assert 'GrandParent' in api.models assert 'Parent' in api.models assert 'Child' in api.models
def test_clone_with_multiple_parents(self): api = Namespace('test') grand_parent = api.model('GrandParent', {}) parent = api.model('Parent', {}) api.clone('Child', grand_parent, parent, {}) assert 'Child' in api.models assert 'Parent' in api.models assert 'GrandParent' in api.models
def test_doc_with_inheritance(self): api = Namespace('test') base_params = {'q': {'description': 'some description', 'type': 'string', 'paramType': 'query'}} child_params = {'q': {'description': 'some new description'}, 'other': {'description': 'another param'}} @api.doc(params=base_params) class BaseResource(restplus.Resource): pass @api.doc(params=child_params) class TestResource(BaseResource): pass assert TestResource.__apidoc__ == {'params': { 'q': { 'description': 'some new description', 'type': 'string', 'paramType': 'query' }, 'other': {'description': 'another param'}, }}
from sanic_restplus import Namespace, Resource, fields from sanic.exceptions import SanicException, InvalidUsage api = Namespace('cats', description='Cats related operations') cat = api.model('Cat', { 'id': fields.String(required=True, description='The cat identifier'), 'name': fields.String(required=True, description='The cat name'), }) CATS = [ {'id': 'felix', 'name': 'Felix'}, ] @api.route('/') class CatList(Resource): @api.doc('list_cats') @api.marshal_list_with(cat) def get(self, request): '''List all cats''' return CATS @api.route('/<id>') @api.param('id', 'The cat identifier') @api.response(404, 'Cat not found') class Cat(Resource): @api.doc('get_cat') @api.marshal_with(cat) def get(self, request, id):
def test_parser(self): api = Namespace('test') assert isinstance(api.parser(), restplus.reqparse.RequestParser)
def test_schema_model(self): api = Namespace('test') api.schema_model('Person', {}) assert 'Person' in api.models
from sanic_restplus import Namespace, Resource, fields api = Namespace('dogs', description='Dogs related operations') dog = api.model('Dog', { 'id': fields.String(required=True, description='The dog identifier'), 'name': fields.String(required=True, description='The dog name'), }) DOGS = [ {'id': 'medor', 'name': 'Medor'}, ] @api.route('/') class DogList(Resource): @api.doc('list_dogs') @api.marshal_list_with(dog) def get(self, request): '''List all dogs''' return DOGS @api.route('/<id>') @api.param('id', 'The dog identifier') @api.response(404, 'Dog not found') class Dog(Resource): @api.doc('get_dog') @api.marshal_with(dog) def get(self, request, id): '''Fetch a dog given its identifier'''
def test_ordered_model(self): api = Namespace('test', ordered=True) api.model('Person', {}) assert 'Person' in api.models assert isinstance(api.models['Person'], OrderedModel)
def test_model(self): api = Namespace('test') api.model('Person', {}) assert 'Person' in api.models assert isinstance(api.models['Person'], Model)