def _get_app(self): self.config.include('cornice') failing_service = Service(name='failing', path='/fail') failing_service.add_view('GET', lambda r: 1 / 0) self.config.add_cornice_service(failing_service) return TestApp(CatchErrors(self.config.make_wsgi_app()))
def _get_app(self): self.config.include('cornice') failing_service = Service(name='failing', path='/fail') failing_service.add_view('GET', lambda r: 1 / 0) self.config.add_cornice_service(failing_service) return TestApp(CatchErrors(self.config.make_wsgi_app()))
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_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_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)
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)
def includeme(config): # FIXME this should also work in includeme user_info = Service(name='users', path='/{username}/info') user_info.add_view('get', get_info) config.add_cornice_service(user_info) resource.add_view(ThingImp.collection_get, permission='read') thing_resource = resource.add_resource( ThingImp, collection_path='/thing', path='/thing/{id}', name='thing_service') config.add_cornice_resource(thing_resource)
def includeme(config): # FIXME this should also work in includeme user_info = Service(name='users', path='/{username}/info') user_info.add_view('get', get_info) config.add_cornice_service(user_info) resource.add_view(ThingImp.collection_get, permission='read') thing_resource = resource.add_resource(ThingImp, collection_path='/thing', path='/thing/{id}', name='thing_service') config.add_cornice_resource(thing_resource)
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()))
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()))
def create_cornice_services(path_prefix=''): path = lambda original_path: path_prefix + original_path # noqa resolver = DottedNameResolver() Service.default_filters = [] # /average average = Service('average', path('/average'), renderer='json') average.add_view('get', resolver.resolve('.views.get_averages'), accept='application/json', decorator=multiple('.decorators.pretty', ), schema=resolver.resolve('.schemas.AverageQuerySchema'), permission=None, renderer='jsonp') return [average]
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)
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)
def get_fresh_air(self): resp = Response() resp.text = u'air with ' + repr(self.context) return resp def make_it_fresh(self, response): response.text = u'fresh ' + response.text return response def check_temperature(self, request): if not 'X-Temperature' in request.headers: request.errors.add('header', 'X-Temperature') tc = Service(name="TemperatureCooler", path="/fresh-air", klass=TemperatureCooler, factory=dummy_factory) tc.add_view("GET", "get_fresh_air", filters=('make_it_fresh',), validators=('check_temperature',)) class TestService(TestCase): def setUp(self): self.config = testing.setUp() self.config.include("cornice") self.config.scan("cornice.tests.test_service") self.config.scan("cornice.tests.test_pyramidhook") self.app = TestApp(CatchErrors(self.config.make_wsgi_app())) def tearDown(self): testing.tearDown() def test_404(self):
def get_fresh_air(self): resp = Response() resp.body = "air" return resp def make_it_fresh(self, response): response.body = "fresh " + response.body return response def check_temperature(self, request): if not "X-Temperature" in request.headers: request.errors.add("header", "X-Temperature") tc = Service(name="TemperatureCooler", path="/fresh-air", klass=TemperatureCooler) tc.add_view("GET", "get_fresh_air", filters=("make_it_fresh",), validators=("check_temperature",)) class TestService(TestCase): def setUp(self): self.config = testing.setUp() self.config.include("cornice") self.config.scan("cornice.tests.test_service") self.config.scan("cornice.tests.test_pyramidhook") self.app = TestApp(CatchErrors(self.config.make_wsgi_app())) def tearDown(self): testing.tearDown() def test_404(self): # a get on a resource that explicitely return a 404 should return
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 get_fresh_air(self): resp = Response() resp.text = u'air with ' + repr(self.context) return resp def make_it_fresh(self, response): response.text = u'fresh ' + response.text return response def check_temperature(self, request, **kw): if not 'X-Temperature' in request.headers: request.errors.add('header', 'X-Temperature') tc = Service(name="TemperatureCooler", path="/fresh-air", klass=TemperatureCooler, factory=dummy_factory) tc.add_view("GET", "get_fresh_air", filters=('make_it_fresh',), validators=('check_temperature',)) class TestService(TestCase): def setUp(self): self.config = testing.setUp( settings={'pyramid.debug_authorization': True}) # Set up debug_authorization logging to console logging.basicConfig(level=logging.DEBUG) debug_logger = logging.getLogger() self.config.registry.registerUtility(debug_logger, IDebugLogger) self.config.include("cornice")
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 make_it_fresh(self, response): response.text = u'fresh ' + response.text return response def check_temperature(self, request, **kw): if not 'X-Temperature' in request.headers: request.errors.add('header', 'X-Temperature') tc = Service(name="TemperatureCooler", path="/fresh-air", klass=TemperatureCooler, factory=dummy_factory) tc.add_view("GET", "get_fresh_air", filters=('make_it_fresh', ), validators=('check_temperature', )) class TestService(TestCase): def setUp(self): self.config = testing.setUp( settings={'pyramid.debug_authorization': True}) # Set up debug_authorization logging to console logging.basicConfig(level=logging.DEBUG) debug_logger = logging.getLogger() self.config.registry.registerUtility(debug_logger, IDebugLogger) self.config.include("cornice")
def get_fresh_air(self): resp = Response() resp.body = 'air' return resp def make_it_fresh(self, response): response.body = 'fresh ' + response.body return response def check_temperature(self, request): if not 'X-Temperature' in request.headers: request.errors.add('header', 'X-Temperature') tc = Service(name="TemperatureCooler", path="/fresh-air", klass=TemperatureCooler) tc.add_view("GET", "get_fresh_air", filters=('make_it_fresh',), validators=('check_temperature',)) class TestService(TestCase): def setUp(self): self.config = testing.setUp() self.config.include("cornice") self.config.scan("cornice.tests.test_service") self.config.scan("cornice.tests.test_pyramidhook") self.app = TestApp(CatchErrors(self.config.make_wsgi_app())) def tearDown(self): testing.tearDown() def test_404(self):
def make_it_fresh(self, response): response.text = u'fresh ' + response.text return response def check_temperature(self, request): if not 'X-Temperature' in request.headers: request.errors.add('header', 'X-Temperature') tc = Service(name="TemperatureCooler", path="/fresh-air", klass=TemperatureCooler, factory=dummy_factory) tc.add_view("GET", "get_fresh_air", filters=('make_it_fresh', ), validators=('check_temperature', )) class TestService(TestCase): def setUp(self): self.config = testing.setUp() self.config.include("cornice") self.config.scan("cornice.tests.test_service") self.config.scan("cornice.tests.test_pyramidhook") self.app = TestApp(CatchErrors(self.config.make_wsgi_app())) def tearDown(self): testing.tearDown() def test_404(self):
return resp def make_it_fresh(self, response): response.body = 'fresh ' + response.body return response def check_temperature(self, request): if not 'X-Temperature' in request.headers: request.errors.add('header', 'X-Temperature') tc = Service(name="TemperatureCooler", path="/fresh-air", klass=TemperatureCooler) tc.add_view("GET", "get_fresh_air", filters=('make_it_fresh', ), validators=('check_temperature', )) class TestService(TestCase): def setUp(self): self.config = testing.setUp() self.config.include("cornice") self.config.scan("cornice.tests.test_service") self.config.scan("cornice.tests.test_pyramidhook") self.app = TestApp(CatchErrors(self.config.make_wsgi_app())) def tearDown(self): testing.tearDown() def test_404(self):
def test(self): service = Service(name="test", path="/", schema=NonpickableSchema()) service.add_view('GET', lambda _: _) register_service_views(self.config, service)
def test(self): service = Service(name="test", path="/", schema=NonpickableSchema()) service.add_view('GET', lambda _:_) register_service_views(self.config, service)