def get_requests(self): self.send(auth='', exception=HTTPUnauthorized()) self.send(auth='bad_user:'******'other_user:'******'total_count': True, 'limit': 2 }, result={ '_links': { 'self': { 'href': self.resource_url }, 'next': D(), }, '_embedded': { 'files': [ D({ 'name': 'readme0.txt', 'size': 12 }), D({ 'name': 'readme1.txt', 'size': 12 }), ] } }, result_headers=D({'X-Total-Count': '3'}))
def get_requests(self): self.send(result=D({ '_links': { 'self': { 'href': 'http://localhost/test_container/' } }, '_embedded': { 'items': [D()] * self.count_of_embedded } }))
def post_requests(self): params = {'title': 'New title', 'description': 'New description'} result = D(params) self.send(params=params, result=result, status=201) self.send(exception=self.ValidationError({'title': 'Required'})) self.send( params={'title': 'T' * 51}, exception=self.ValidationError( {'title': 'Longer than maximum length 50'}), )
def put_requests(self): self.send(auth='', exception=httpexceptions.HTTPUnauthorized) self.send(auth='other_user:123', exception=httpexceptions.HTTPForbidden) params = {'value': 10} self.send( params=params, result=D(params), ) self.send(params={'value': 'foo'}, exception=self.ValidationError( {'value': '"foo" is not a number'}))
def test_external_links(web_app, root, app_config, pyramid_request): # Get self resource res = web_app.get('container/resource').json_body assert res == { '_links': { 'self': { 'href': 'http://localhost/container/resource/' }, 'ext': { 'href': 'http://dummy.com/resource/{id}', 'templated': True }, } } resource = root['container']['resource'] view = get_resource_view(resource, pyramid_request) schema = view.options_for_get.output_schema().bind( request=pyramid_request, context=resource, ) ext_node = schema.get('_links').get('ext') assert ext_node.title == 'Link to extended resource' assert ext_node.description == 'Some description' assert ext_node.missing == colander.required # Get resource as embedded res = web_app.get('container') assert res.json_body == D({ '_embedded': { 'items': [{ '_links': { 'self': { 'href': 'http://localhost/container/resource/' }, # An external link is absent }, }] } }) # Get different api versions res = web_app.get('0/resource') assert res.json_body == { '_links': { 'self': { 'href': 'http://localhost/0/resource/' }, 'ext': { 'href': 'http://dummy.com/resource/{id}', 'templated': True }, 'ext2': { 'href': 'http://dummy.com/resource/lte_two' }, } } res = web_app.get('1/resource') assert res.json_body == { '_links': { 'self': { 'href': 'http://localhost/1/resource/' }, 'ext': { 'href': 'http://dummy.com/resource/{id}', 'templated': True }, 'ext1': { 'href': 'http://dummy.com/resource/gte_one' }, 'ext2': { 'href': 'http://dummy.com/resource/lte_two' }, } } resource = root['1']['resource'] view = get_resource_view(resource, pyramid_request) schema = view.options_for_get.output_schema().bind( request=pyramid_request, context=resource, ) ext_node = schema.get('_links').get('ext1') assert ext_node.missing == colander.drop res = web_app.get('2/resource') assert res.json_body == { '_links': { 'self': { 'href': 'http://localhost/2/resource/' }, 'ext': { 'href': 'http://dummy.com/resource/{id}', 'templated': True }, 'ext1': { 'href': 'http://dummy.com/resource/gte_one' }, 'ext2': { 'href': 'http://dummy.com/resource/lte_two' }, 'ext23': { 'href': 'http://dummy.com/resource/lte_two__gte_three' }, } } res = web_app.get('3/resource') assert res.json_body == { '_links': { 'self': { 'href': 'http://localhost/3/resource/' }, 'ext': { 'href': 'http://dummy.com/resource/{id}', 'templated': True }, 'ext1': { 'href': 'http://dummy.com/resource/gte_one' }, 'ext23': { 'href': 'http://dummy.com/resource/lte_two__gte_three' }, } }
def test_links_to_sub_resource(web_app, root, app_config): # Get self resource res = web_app.get('container/resource') assert res.json_body == { '_links': { 'self': {'href': 'http://localhost/container/resource/'}, 'sub': {'href': 'http://localhost/container/resource/sub/'}, 'static_sub': {'href': 'http://localhost/container/resource/static_sub/'}, } } # Get resource as embedded res = web_app.get('container') assert res.json_body == D({ '_embedded': { 'items': [ { '_links': { 'self': {'href': 'http://localhost/container/resource/'}, # Only static link to sub-resource has added 'static_sub': {'href': 'http://localhost/container/resource/static_sub/'}, }, } ] } }) # Get different api versions res = web_app.get('0/resource') assert res.json_body == { '_links': { 'self': {'href': 'http://localhost/0/resource/'}, 'sub': {'href': 'http://localhost/0/resource/sub/'}, 'sub2': {'href': 'http://localhost/0/resource/sub2/'}, 'static_sub': {'href': 'http://localhost/0/resource/static_sub/'}, } } res = web_app.get('1/resource') assert res.json_body == { '_links': { 'self': {'href': 'http://localhost/1/resource/'}, 'sub': {'href': 'http://localhost/1/resource/sub/'}, 'sub1': {'href': 'http://localhost/1/resource/sub1/'}, 'sub2': {'href': 'http://localhost/1/resource/sub2/'}, 'static_sub': {'href': 'http://localhost/1/resource/static_sub/'}, } } res = web_app.get('2/resource') assert res.json_body == { '_links': { 'self': {'href': 'http://localhost/2/resource/'}, 'sub': {'href': 'http://localhost/2/resource/sub/'}, 'sub1': {'href': 'http://localhost/2/resource/sub1/'}, 'sub2': {'href': 'http://localhost/2/resource/sub2/'}, 'sub23': {'href': 'http://localhost/2/resource/sub23/'}, 'static_sub': {'href': 'http://localhost/2/resource/static_sub/'}, } } res = web_app.get('3/resource') assert res.json_body == { '_links': { 'self': {'href': 'http://localhost/3/resource/'}, 'sub': {'href': 'http://localhost/3/resource/sub/'}, 'sub1': {'href': 'http://localhost/3/resource/sub1/'}, 'sub23': {'href': 'http://localhost/3/resource/sub23/'}, 'static_sub': {'href': 'http://localhost/3/resource/static_sub/'}, } }