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(self):
# Compiled regexs are, apparently, non-pickleable
service = Service(name="test", path="/", schema={'a': re.compile('')})
service.add_view('GET', lambda _:_)
register_service_views(self.config, service)
def test_fallback_no_predicate(self):
service = Service(name='fallback-test', path='/',
effective_principals=('group:admins',))
service.add_view('GET', lambda _:_)
register_service_views(self.config, service)
self.config.include('cornice')
app = self.config.make_wsgi_app()
testapp = TestApp(app)
testapp.get('/', status=404)
#self.assertRaises(PredicateMismatch, testapp.get, '/')
def test_route_construction(self):
config = mock.MagicMock()
config.add_route = mock.MagicMock()
register_service_views(config, test_service)
config.add_route.assert_called_with('jardinet', '/jardinet')
def setUp(self):
self.config = testing.setUp()
self.config.include("cornice")
self.config.add_route('proute', '/from_pyramid')
self.config.scan("tests.test_pyramidhook")
def handle_response(request):
return {'service': request.current_service.name,
'route': request.matched_route.name}
rserv = Service(name="ServiceWPyramidRoute", pyramid_route="proute")
rserv.add_view('GET', handle_response)
register_service_views(self.config, rserv)
self.app = TestApp(CatchErrors(self.config.make_wsgi_app()))
logging.basicConfig(level=logging.DEBUG)
debug_logger = logging.getLogger()
self.config.registry.registerUtility(debug_logger, IDebugLogger)
self.config.include("cornice")
self.authz_policy = ACLAuthorizationPolicy()
self.config.set_authorization_policy(self.authz_policy)
self.authn_policy = AuthTktAuthenticationPolicy('$3kr1t')
self.config.set_authentication_policy(self.authn_policy)
self.config.scan("tests.test_service")
self.config.scan("tests.test_pyramidhook")
self.app = TestApp(CatchErrors(self.config.make_wsgi_app()))
register_service_views(self.config, service)
def test_fallback_no_required_csrf(self):
service = Service(name='fallback-csrf', path='/', content_type='application/json')
service.add_view('POST', lambda _:'', require_csrf=False)
register_service_views(self.config, service)
self.config.include('cornice')
app = self.config.make_wsgi_app()
testapp = TestApp(app)
testapp.post('/', status=415, headers={'Content-Type': 'application/xml'})
def test_fallback_permission(self):
"""
Fallback view should be registered with NO_PERMISSION_REQUIRED
Fixes: https://github.com/mozilla-services/cornice/issues/245
"""
service = Service(name='fallback-test', path='/')
service.add_view('GET', lambda _:_)
register_service_views(self.config, service)
# This is a bit baroque
introspector = self.config.introspector
views = introspector.get_category('views')
fallback_views = [i for i in views
if i['introspectable']['route_name']=='fallback-test']
for v in fallback_views:
if v['introspectable'].title == u'function cornice.pyramidhook._fallback_view':
permissions = [p['value'] for p in v['related'] if p.type_name == 'permission']
self.assertIn(NO_PERMISSION_REQUIRED, permissions)
@classmethod
def register_service(cls, config):
for service in cls._services.values():
cornice.pyramidhook.register_service_views(config, service)
def includeme(config):
"""Include the Cornice definitions
"""
# attributes required to maintain services
config.registry.cornice_services = {}
settings = config.get_settings()
# localization request subscriber must be set before first call
# for request.localizer (in wrap_request)
if settings.get('available_languages'):
setup_localization(config)
config.add_directive('add_cornice_service', register_service_views)
config.add_directive('add_cornice_resource', register_resource_views)
config.add_subscriber(wrap_request, NewRequest)
config.add_renderer('simplejson', util.json_renderer)
config.add_view_predicate('content_type', ContentTypePredicate)
config.add_request_method(current_service, reify=True)
if asbool(settings.get('handle_exceptions', True)):
config.add_view(handle_exceptions, context=Exception,
permission=NO_PERMISSION_REQUIRED)
config.add_view(handle_exceptions, context=HTTPNotFound,
permission=NO_PERMISSION_REQUIRED)
config.add_view(handle_exceptions, context=HTTPForbidden,
permission=NO_PERMISSION_REQUIRED)