Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_service_instanciation(self):
service = Service("coconuts", "/migrate")
self.assertEqual(service.name, "coconuts")
self.assertEqual(service.path, "/migrate")
self.assertEqual(service.renderer, Service.renderer)
service = Service("coconuts", "/migrate", renderer="html")
self.assertEqual(service.renderer, "html")
# test that lists are also set
validators = [lambda x: True, ]
service = Service("coconuts", "/migrate", validators=validators)
self.assertEqual(service.validators, validators)
def test_callable_default_security(self):
def get_security(service, method):
definitions = service.definitions
for definition in definitions:
met, view, args = definition
if met == method:
break
if 'security' in args:
return [{'basicAuth': []}]
else:
return []
service = Service("IceCream", "/icecream/{flavour}")
@service.get()
def view_get(self, request):
return service
@service.post(security='foo')
def view_post(self, request):
return service
swagger = CorniceSwagger([service])
swagger.swagger = {'securityDefinitions': {'basicAuth': {'type': 'basic'}}}
swagger.default_security = get_security
spec = swagger.generate()
validate(spec)
security = spec['paths']['/icecream/{flavour}']['post']['security']
self.assertEquals(security, [{'basicAuth': []}])
def test_get_contenttypes(self):
# defining a service with different "content_type" headers, we should
# be able to retrieve this information easily
service = Service("color", "/favorite-color")
service.add_view("GET", lambda x: "blue", content_type="text/plain")
self.assertEquals(service.get_contenttypes("GET"), ['text/plain'])
service.add_view("GET", lambda x: "blue",
content_type="application/json")
self.assertEquals(service.get_contenttypes("GET"),
['text/plain', 'application/json'])
# adding a view for the POST method should not break everything :-)
service.add_view("POST", lambda x: "ok", content_type=('foo/bar'))
self.assertEquals(service.get_contenttypes("GET"),
['text/plain', 'application/json'])
# and of course the list of supported ingress content-types should be
# available for the "POST" as well.
self.assertEquals(service.get_contenttypes("POST"),
['foo/bar'])
def test_max_age_is_none_if_undefined(self):
foo = Service(name='foo', path='/foo')
foo.add_view('POST', _stub)
self.assertIsNone(foo.cors_max_age_for('POST'))
def test_callable_default_tags(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.get()
def view_get(self, request):
return service
def default_tag_callable(service, method):
return ['cold']
swagger = CorniceSwagger([service])
swagger.default_tags = default_tag_callable
spec = swagger.generate()
validate(spec)
tags = spec['paths']['/icecream/{flavour}']['get']['tags']
self.assertEquals(tags, ['cold'])
def test_credential_support_is_disabled_by_default(self):
foo = Service(name='foo', path='/foo')
foo.add_view('POST', _stub)
self.assertFalse(foo.cors_support_credentials_for())
bacon = Service(path='/bacon/{type}', name='bacon', cors_origins=('*',))
class Klass(object):
"""
Class implementation of a service
"""
def __init__(self, request):
self.request = request
def post(self):
return "moar squirels (take care)"
cors_policy = {'origins': ('*',), 'enabled': True, 'credentials': True}
cors_klass = Service(name='cors_klass',
path='/cors_klass',
klass=Klass,
cors_policy=cors_policy)
cors_klass.add_view('post', 'post')
@squirel.get(cors_origins=('notmyidea.org',), cors_headers=('X-My-Header',))
def get_squirel(request):
return "squirels"
@squirel.post(cors_enabled=False, cors_headers=('X-Another-Header'))
def post_squirel(request):
return "moar squirels (take care)"
def test_max_age_can_be_defined(self):
foo = Service(name='foo', path='/foo', cors_max_age=42)
foo.add_view('POST', _stub)
self.assertEqual(foo.cors_max_age_for(), 42)
def test_provided_tags_override_sorting(self):
service = Service("IceCream", "/icecream/{flavour}")
class IceCream(object):
@service.get(tags=['cold', 'foo'])
def view_get(self, request):
return service
swagger = CorniceSwagger([service])
tags = [{'name': 'foo', 'description': 'bar'}]
swagger.swagger = {'tags': tags}
spec = swagger.generate()
validate(spec)
self.assertListEqual([{'name': 'foo', 'description': 'bar'},
{'name': 'cold'}], spec['tags'])