Ejemplo n.º 1
0
 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'})
Ejemplo n.º 2
0
    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',
                                            traverse='/')
Ejemplo n.º 3
0
    def test_route_construction(self):
        config = MagicMock()
        config.add_route = MagicMock()

        register_service_views(config, test_service)
        self.assertTrue(
            ('traverse', '/jardinet'),
            config.add_route.called_args,
        )
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
    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()))
Ejemplo n.º 6
0
    def test_that_service_metrics_include_correct_response_codes(self):
        stub_service = Service(name="stub", path="/{what}")

        @stub_service.get()
        def stub_view(request):
            what = request.matchdict["what"]
            if what == "ok":
                return Response(status=200)
            if what == "notfound":
                return Response(status=404)
            if what == "forbidden":
                return Response(status=403)
            if what == "exc_forbidden":
                raise HTTPForbidden
            if what == "impl_forbidden":
                request.response.status_code = 403
                return ""
            raise HTTPNotFound

        with pyramid.testing.testConfig() as config:
            config.include("cornice")
            config.include("mozsvc")
            register_service_views(config, stub_service)
            app = TestApp(config.make_wsgi_app())

            app.get("/ok", status=200)
            r = self.logs.records[-1]
            self.assertEqual(r.code, 200)

            app.get("/notfound", status=404)
            r = self.logs.records[-1]
            self.assertEqual(r.code, 404)
            app.get("/forbidden", status=403)
            r = self.logs.records[-1]
            self.assertEqual(r.code, 403)

            app.get("/exc_notfound", status=404)
            r = self.logs.records[-1]
            self.assertEqual(r.code, 404)
            app.get("/exc_forbidden", status=403)
            r = self.logs.records[-1]
            self.assertEqual(r.code, 403)

            app.get("/impl_forbidden", status=403)
            r = self.logs.records[-1]
            self.assertEqual(r.code, 403)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    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")

        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)
Ejemplo n.º 9
0
    def test_service_metrics(self):
        stub_service = Service(name="stub", path="/stub")

        @stub_service.get()
        @metrics_timer("view_time")
        def stub_view(request):
            request.metrics["stub"] = "stub-a-dub-dub"
            return {}

        with pyramid.testing.testConfig() as config:
            config.include("cornice")
            config.include("mozsvc")
            register_service_views(config, stub_service)
            app = TestApp(config.make_wsgi_app())
            res = app.get("/stub")
            self.assertEqual(res.body, "{}")

        self.assertTrue(len(self.logs.records), 1)
        r = self.logs.records[0]
        self.assertEqual(r.stub, "stub-a-dub-dub")
        self.assertTrue(0 < r.request_time < 0.1)
        self.assertTrue(0 < r.view_time <= r.request_time)
Ejemplo n.º 10
0
 def test(self):
     service = Service(name="test", path="/", schema=NonpickableSchema())
     service.add_view('GET', lambda _: _)
     register_service_views(self.config, service)
Ejemplo n.º 11
0
 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)