Пример #1
0
 def worker(self, sem, q):
     log.info('Entering chapmand worker thread')
     while not self._shutdown:
         try:
             msg, state = q.get(timeout=0.25)
         except Empty:
             continue
         try:
             log.info('Received %r', msg)
             task = Task.from_state(state)
             # task.handle(msg, 25)
             if task.path:
                 req = Request.blank(task.path, method='CHAPMAN')
             else:
                 req = Request.blank(self._chapman_path, method='CHAPMAN')
             req.registry = self._registry
             req.environ['chapmand.task'] = task
             req.environ['chapmand.message'] = msg
             for x in self._app(req.environ, lambda *a,**kw:None):
                 pass
         except Exception as err:
             exc_log.exception('Unexpected error in worker thread: %r', err)
             time.sleep(self._sleep)
         finally:
             self._num_active_messages -= 1
             sem.release()
     log.info('Exiting chapmand worker thread')
Пример #2
0
    def filemonitoring(self):
        log = logging.getLogger(__name__)
        if self._request.params:
            # TODO: add this information to the file
            md5_enabled = True if 'withmd5' in self._request.params and self._request.params['withmd5'] == '0' else False
            all_files = self._request.params.getall('file')

            complete_file, filenames, folders = self._get_monitored_files(self._request.params['folder'] + '/')

            with transaction.manager:
                for f in all_files:
                    if f in complete_file:
                        log.debug('Skipping file {0}, because it is already monitored'.format(f))
                        continue
                    (path, filename) = os.path.split(f)
                    dbobj = MonitoredFile(path, filename, f)
                    DBSession.add(dbobj)
                DBSession.commit()
            files_not_mentioned = [c for c in complete_file if c not in all_files]
            # TODO: decide on this
            log.info('TODO: Still have to decide whether files which are not selected should be deleted or not.'
                     'Affected files would be: {0}'.format(files_not_mentioned))
        else:
            log.info('Got an empty request, going to redirect to start page')
            subreq = Request.blank('/')
            return self._request.invoke_subrequest(subreq)

        subreq = Request.blank(self._request.route_path('filebrowser'),
                               POST=dict(folder=self._request.params['folder'],
                                         currentfolder=self._request.params['currentfolder'],
                                         pathdescription='abs'))
        return self._request.invoke_subrequest(subreq)
Пример #3
0
def batched_request_view(request):
    json_response = []
    response = request.response
    for rpc_request in request.batched_rpc_requests:
        body = json.dumps(rpc_request).encode(request.charset)
        subrequest = Request.blank(
            path=request.path,
            environ=request.environ,
            base_url=request.application_url,
            headers=request.headers,
            POST=body,
            charset=request.charset,
        )
        subresponse = request.invoke_subrequest(subrequest, use_tweens=True)
        if subresponse.json_body != "":
            json_response.append(subresponse.json_body)
    if json_response:
        # use charset and content-type from last subresponse
        response.charset = subresponse.charset
        response.content_type = subresponse.content_type
        # will automatically be encoded
        response.json_body = json_response
    else:
        # if we would send an empty list, instead send nothing
        # per JSON-RPC: http://www.jsonrpc.org/specification#batch
        response.content_type = "text/plain"
        response.body = b""
    return response
Пример #4
0
def get_env(config_uri, base_url):
    """
    Return a preconfigured paste environment object. Sets up the WSGI
    application and ensures that webassets knows to load files from
    ``h:static`` regardless of the ``webassets.base_dir`` setting.
    """
    request = Request.blank('', base_url=base_url)
    env = paster.bootstrap(config_uri, request)
    request.root = env['root']

    # Ensure that the webassets URL is absolute
    request.webassets_env.url = urlparse.urljoin(base_url,
                                                 request.webassets_env.url)

    # Disable webassets caching and manifest generation
    request.webassets_env.cache = False
    request.webassets_env.manifest = False

    # By default, webassets will use its base_dir setting as its search path.
    # When building extensions, we change base_dir so as to build assets
    # directly into the extension directories. As a result, we have to add
    # back the correct search path.
    request.webassets_env.append_path(resolve('h:static').abspath(),
                                      request.webassets_env.url)

    request.registry.notify(ContextFound(request))  # pyramid_layout attrs

    return env
Пример #5
0
def _render_view_on_slot_event(view_name, event, params):
    context = event.object
    request = event.request

    view_request = Request.blank(
        "{0}/{1}".format(request.path.rstrip('/'), view_name),
        base_url=request.application_url,
        POST=_encode(params),
        )

    post_items = request.POST.items()
    if post_items:
        view_request.POST.extend(post_items)

    # This is quite brittle:
    for name in REQUEST_ATTRS_TO_COPY:
        setattr(view_request, name, getattr(request, name))

    try:
        result = render_view(
            context,
            view_request,
            view_name,
            )
    except PredicateMismatch:
        return None
    else:
        return result.decode('utf-8')
Пример #6
0
 def makeRequest(self, path):
     from pyramid.request import Request
     request = Request.blank(path)
     request.registry = self.registry
     subpath = filter(None, path.split('/'))
     request.matchdict = {'subpath': subpath}
     return request
Пример #7
0
 def test_attributes_are_none_with_blank_requests(self):
     request = Request.blank(path='/')
     context = RouteFactory(request)
     self.assertIsNone(context.object_id)
     self.assertIsNone(context.required_permission)
     self.assertIsNone(context.resource_name)
     self.assertIsNone(context.check_permission)
Пример #8
0
 def getDiscussionUrl(self):
     from pyramid.request import Request
     req = Request.blank('/', base_url=self.discussion.get_base_url())
     #Celery didn't like this.  To revisit once we have virtual hosts
     #return req.route_url('home', discussion_slug=self.discussion.slug)
     return urljoin(
         self.discussion.get_base_url(), self.discussion.slug+'/')
Пример #9
0
def test_cors_adds_allow_origin_header_for_non_preflight():
    request = Request.blank("/")

    resp = request.get_response(wsgi_testapp)
    set_cors_headers(request, resp)

    assert resp.headers["Access-Control-Allow-Origin"] == "*"
 def test_hide_capabilities_set_get_capabilities_request(self):
     request = Request.blank("/test?REQUEST=GetCapabilities")
     request.registry = get_current_registry()
     request.registry.settings = {
         "hide_capabilities": True
     }
     self.assertFalse(self.predicate(None, request))
 def test_hide_capabilities_set_no_request_param(self):
     request = Request.blank("/test")
     request.registry = get_current_registry()
     request.registry.settings = {
         "hide_capabilities": True
     }
     self.assertTrue(self.predicate(None, request))
Пример #12
0
Файл: links.py Проект: gnott/h
    def __init__(self, base_url, registry):
        """
        Create a new links service.

        :param base_url: the base URL for link construction
        :param registry: the registry in which to look up routes
        :type registry: pyramid.registry.Registry
        """
        self.base_url = base_url
        self.registry = registry

        # It would be absolutely fair if at this point you asked yourself any
        # of the following questions:
        #
        # - Why are we constructing a fake request here?
        # - Didn't we have a request and then discard it in the service
        #   factory?
        # - This looks really janky!
        #
        # Well, apart from the fact that the last one there isn't a question,
        # those are good questions. The reason for doing this is that we need
        # to be able to generate links to annotations in situations where we
        # don't necessarily have a request object around, such as in the
        # WebSocket server, or in a CLI command.
        #
        # In these situations, it should suffice to have an application
        # registry (for the routing table) and a base URL. The reason we
        # generate a request object is that this is the simplest and least
        # error-prone way to get access to the route_url function, which can
        # be used by link generators.
        self._request = Request.blank('/', base_url=base_url)
        self._request.registry = registry
Пример #13
0
def easy_link(request):
    for x in DBSession.query(EasyLinks).filter_by(name=request.matchdict['link']):
        subreq = Request.blank(x.path)
        subreq.cookies = request.cookies #pass authentication data
        response = request.invoke_subrequest(subreq)
        return response
    return HTTPNotFound()
Пример #14
0
    def assert_cache_enabled(self, cache_factory):
        settings = {
            "boto3.sessions": "default",
            "boto3.clients": "s3",
            "boto3.client.s3.session": "default",
            "boto3.client.s3.service_name": "s3",
            "boto3.resources": "s3",
            "boto3.resource.s3.session": "default",
            "boto3.resource.s3.service_name": "s3",
            "boto3.cache_factory": cache_factory,
        }
        config = Configurator(settings=settings)
        config.include("pyramid_boto3")

        def aview(request):
            session = request.find_service(name="boto3.session.default")
            self.assertIsNotNone(session)
            s3cli = request.find_service(name="boto3.client.s3")
            self.assertIsNotNone(s3cli)
            s3res = request.find_service(name="boto3.resource.s3")
            self.assertIsNotNone(s3res)
            return "OK"

        config.add_view(aview, route_name="root", renderer="json")
        config.add_route("root", pattern="/")
        app = config.make_wsgi_app()
        request = Request.blank("/")
        response = request.get_response(app)
        self.assertEqual(response.json_body, "OK")
        del app
Пример #15
0
 def test_process_response_nonhtml(self):
     response = Response()
     response.content_type = 'text/plain'
     request = Request.blank('/')
     toolbar = self._makeOne(request, [DummyPanel])
     toolbar.process_response(response)
     self.assertTrue(response.processed)
Пример #16
0
def test_cors_sets_max_age_for_preflight_when_set(headers):
    request = Request.blank("/", method="OPTIONS", headers=headers)

    resp = request.get_response(wsgi_testapp)
    resp = set_cors_headers(request, resp, max_age=42)

    assert resp.headers["Access-Control-Max-Age"] == "42"
Пример #17
0
def test_cors_sets_allow_methods_OPTIONS_for_preflight(headers):
    request = Request.blank("/", method="OPTIONS", headers=headers)

    resp = request.get_response(wsgi_testapp)
    resp = set_cors_headers(request, resp)

    assert resp.headers["Access-Control-Allow-Methods"] == "OPTIONS"
Пример #18
0
 def test_call_view_exception_propagating(self):
     from zope.interface import Interface
     from zope.interface import directlyProvides
     class IContext(Interface):
         pass
     from pyramid.interfaces import IRequest
     from pyramid.interfaces import IViewClassifier
     from pyramid.interfaces import IRequestFactory
     def rfactory(environ):
         return request
     self.registry.registerUtility(rfactory, IRequestFactory)
     from pyramid.request import Request
     request = Request.blank('/')
     context = DummyContext()
     directlyProvides(context, IContext)
     self._registerTraverserFactory(context, subpath=[''])
     response = DummyResponse()
     view = DummyView(response, raise_exception=RuntimeError)
     environ = self._makeEnviron()
     self._registerView(view, '', IViewClassifier, IRequest, IContext)
     router = self._makeOne()
     start_response = DummyStartResponse()
     self.assertRaises(RuntimeError, router, environ, start_response)
     # ``exception`` must be attached to request even if a suitable
     # exception view cannot be found
     self.assertEqual(request.exception.__class__, RuntimeError)
Пример #19
0
def view_three(request):
    subreq = Request.blank('/view_four')
    try:
        return request.invoke_subrequest(subreq, use_tweens=True)
    except: # pragma: no cover
        request.response.body = b'Value error raised'
        return request.response
Пример #20
0
 def setupRequest(self, url='/'):
   request = Request.blank(url)
   self.request = request
   self.config = testing.setUp(request=request)
   self.registry = self.config.registry
   self.registry.settings = {}
   request.registry = self.registry
Пример #21
0
    def checkPermission(self, info):
        """ Does user have permission to author content in the given context?

        Uses ACL security policy to test.
        """
        users = find_users(self.context)
        for target in info['targets']:
            if 'error' in target:
                continue
            report_name = target.get('report')
            if report_name is not None:
                pd = find_peopledirectory(self.context)
                context = find_resource(pd, report_name.split('+'))
                permission = "email"
            else:
                communities = find_communities(self.context)
                community = communities[target['community']]
                context = community[target['tool']]
                permission = "create"   # XXX In theory could depend on target
            user = users.get_by_id(info['author'])
            if user is not None:
                user = dict(user)
                user['repoze.who.userid'] = info['author']

            # BFG Security API always assumes http request, so we fabricate a
            # fake request.
            request = Request.blank('/')
            request.environ['repoze.who.identity'] = user

            if not has_permission(permission, context, request):
                target['error'] = 'Permission Denied'
Пример #22
0
def test_cors_sets_allow_credentials_for_preflight_when_set(headers):
    request = Request.blank("/", method="OPTIONS", headers=headers)

    resp = request.get_response(wsgi_testapp)
    resp = set_cors_headers(request, resp, allow_credentials=True)

    assert resp.headers["Access-Control-Allow-Credentials"] == "true"
Пример #23
0
def getTopics(params):
    # Getting all topics
    subreq = Request.blank('/rest/services')
    topicresp = params.request.invoke_subrequest(subreq)
    if topicresp.status_int != 200:
        raise HTTPInternalServerError('Topics service did not return OK status')
    return json.loads(topicresp.body)['topics']
Пример #24
0
def test_cors_sets_allow_origin_for_preflight(headers):
    request = Request.blank("/", method="OPTIONS", headers=headers)

    resp = request.get_response(wsgi_testapp)
    resp = set_cors_headers(request, resp)

    assert resp.headers["Access-Control-Allow-Origin"] == "http://example.com"
Пример #25
0
    def run(self):
        if len(self.args) < 2:
            self.out('Command requires a config file arg and a url arg')
            return 2
        config_uri = self.args[0]
        url = self.args[1]

        if not url.startswith('/'):
            url = '/%s' % url
        request = Request.blank(url)
        env = self.bootstrap[0](config_uri, options=parse_vars(self.args[2:]),
                                request=request)
        view = self._find_view(request)
        self.out('')
        self.out("URL = %s" % url)
        self.out('')
        if view is not None:
            self.out("    context: %s" % view.__request_attrs__['context'])
            self.out("    view name: %s" % view.__request_attrs__['view_name'])
        if IMultiView.providedBy(view):
            for dummy, view_wrapper, dummy in view.views:
                self.output_view_info(view_wrapper)
                if IMultiView.providedBy(view_wrapper):
                    for dummy, mv_view_wrapper, dummy in view_wrapper.views:
                        self.output_view_info(mv_view_wrapper, level=2)
        else:
            if view is not None:
                self.output_view_info(view)
            else:
                self.out("    Not found.")
        self.out('')
        env['closer']()
        return 0
Пример #26
0
 def test_it_reraises_on_no_match(self):
     from pyramid.request import Request
     def handler(request):
         raise ValueError
     tween = self._makeOne(handler)
     request = Request.blank('/')
     self.assertRaises(ValueError, lambda: tween(request))
Пример #27
0
 def test_it_intercept_redirect_nonredirect_code(self):
     request = Request.blank('/')
     request.remote_addr = '127.0.0.1'
     self.config.registry.settings['debugtoolbar.intercept_redirects'] = True
     request.registry = self.config.registry
     result = self._callFUT(request)
     self.assertTrue(result is self.response)
Пример #28
0
def blank_request():
    """Try to create a working request instance
    """
    request = Request.blank('/')
    request.to_api = partial(to_api, request)
    request.context = None
    return request
Пример #29
0
def get_request( user=None, path='/api/v1/', body=b'', request_method='GET' ):
    request = Request.blank( path )
    request.method = request_method
    request.user = user
    request.body = str(body)

    return request
Пример #30
0
def view_five(request):
    subreq = Request.blank('/view_four')
    try:
        return request.invoke_subrequest(subreq, use_tweens=False)
    except ValueError:
        request.response.body = b'Value error raised'
        return request.response
Пример #31
0
 def test_preflight_default_headers(self):
     origin = 'http://example.com'
     app = self.config.make_wsgi_app()
     request = Request.blank('/cors', base_url=origin)
     request.method = 'OPTIONS'
     request.headers['Origin'] = origin
     request.headers['Access-Control-Request-Method'] = 'GET'
     response = request.get_response(app)
     self.assertTrue(response.headers.get('Access-Control-Allow-Methods'))
     self.assertTrue(response.headers.get('Access-Control-Allow-Headers'))
     self.assertTrue(response.headers.get('Access-Control-Allow-Origin'))
     self.assertEqual(response.headers['Access-Control-Allow-Origin'], '*')
    def test_timing_contextmanager(self):
        def viewit(request):
            with metrics_timer("timer1"):
                time.sleep(0.01)

        request = Request.blank("/")
        initialize_request_metrics(request)
        with pyramid.testing.testConfig(request=request):
            viewit(request)

        ts = request.metrics["timer1"]
        self.assertTrue(0.01 < ts < 0.1)
 def test_it_intercept_redirect(self):
     from pyramid.httpexceptions import HTTPFound
     response = HTTPFound(location='http://other.com')
     def handler(request):
         return response
     request = Request.blank('/')
     request.registry = self.config.registry
     self.config.registry.settings['debugtoolbar.intercept_redirects'] = True
     result = self._callFUT(request, handler)
     self.assertTrue(result is response)
     self.assertEqual(result.status_int, 200)
     self.assertEqual(result.location, None)
Пример #34
0
    def test_it_remote_addr_proxies_list(self):
        request = Request.blank('/')
        request.remote_addr = '172.16.63.156, 64.119.211.105'

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")

            # test
            self._callFUT(request)

            self.assertEqual(len(w), 1)
            assert "REMOTE_ADDR" in str(w[-1].message)
 def test_it_raises_exception_intercept_exc(self):
     request = Request.blank('/')
     def handler(request):
         raise NotImplementedError
     self.config.registry.settings['debugtoolbar.intercept_exc'] = True
     self.config.registry.settings['debugtoolbar.secret'] = 'abc'
     self.config.add_route('debugtoolbar.exception', '/exception')
     request.registry = self.config.registry
     response = self._callFUT(request, handler)
     self.assertEqual(len(request.exc_history.tracebacks), 1)
     self.assertFalse(hasattr(request, 'debug_toolbar'))
     self.assertTrue(response.status_int, 500)
Пример #36
0
    def test_attributes_are_none_with_non_resource_requests(self):
        basic_service = object()
        request = Request.blank(path="/")
        request.prefixed_userid = property(utils.prefixed_userid)
        request.matched_route = mock.Mock(pattern="foo")
        request.registry = mock.Mock(cornice_services={"foo": basic_service})
        request.registry.settings = {}

        context = RouteFactory(request)
        self.assertIsNone(context.current_record)
        self.assertIsNone(context.required_permission)
        self.assertIsNone(context.resource_name)
Пример #37
0
def task_prerun_signal(task_id, task, args, kwargs, **kwaargs):
    if hasattr(celery, "pyramid"):
        env = celery.pyramid
        env = prepare(registry=env["request"].registry)
        proper_base_url = env["request"].registry.settings["mailing.app_url"]
        tmp_req = Request.blank("/", base_url=proper_base_url)
        # ensure tasks generate url for right domain from config
        env["request"].environ["HTTP_HOST"] = tmp_req.environ["HTTP_HOST"]
        env["request"].environ["SERVER_PORT"] = tmp_req.environ["SERVER_PORT"]
        env["request"].environ["SERVER_NAME"] = tmp_req.environ["SERVER_NAME"]
        env["request"].environ["wsgi.url_scheme"] = tmp_req.environ["wsgi.url_scheme"]
    get_current_request().tm.begin()
Пример #38
0
    def test_it_reraises_on_no_match(self):
        from pyramid.request import Request

        def handler(request):
            raise ValueError

        tween = self._makeOne(handler)
        request = Request.blank('/')
        request.registry = self.config.registry
        self.assertRaises(ValueError, lambda: tween(request))
        self.assertIsNone(request.exception)
        self.assertIsNone(request.exc_info)
Пример #39
0
 def _check(self,
            error,
            status,
            json=True,
            content_type="application/json"):
     response = Request.blank("/").get_response(error())
     if content_type:
         assert response.content_type == content_type
     assert response.status_code == status
     if json:
         assert response.json == error.json_body()
     return response
Пример #40
0
 def test_unicode_user_id_with_non_utf_8_url(self):
     # On Python 2 we may get a unicode userid while QUERY_STRING is a "str"
     # object containing non-ascii bytes.
     from pyramid.request import Request
     with testing.testConfig() as config:
         config.testing_securitypolicy(
             userid=b'\xe6\xbc\xa2'.decode('utf-8'))
         request = Request.blank('/')
         request.environ['PATH_INFO'] = '/url'
         request.environ['QUERY_STRING'] = '\xfa=\xfa'
         msg = self._callFUT(request)
     self.assertTrue("could not decode params" in msg, msg)
Пример #41
0
def loadjs(request):
    mode = request.params.get('mode')
    vip = request.params.get('vip') == 'true'
    ignore_polyfill = request.params.get('ignore_polyfill')
    # Determined automatically in subscriber
    lang = request.lang
    public_bucket_host = request.registry.settings['public_bucket_host']
    if vip is True:
        public_bucket_host = public_bucket_host.replace('public', 'public-cdn')

    # If version not provided fallback to the first entry
    version_str = request.params.get('version', available_versions[0])
    # If provided make sure the version exists
    if version_str not in available_versions:
        raise HTTPNotFound(
            'Version %s you request is not available, available versions are %s.' % (version_str, ', '.join(available_versions)))

    path = '/rest/services/api/MapServer/layersConfig?lang=%s' % lang
    subRequest = Request.blank(path)
    resp = request.invoke_subrequest(subRequest)
    data = json.loads(resp.body)

    s3_resources_path = 'resources/api/%s' % version_str
    mode_str = '-debug' if mode is not None else ''

    def get_resource_url(filename, extension, mode_str=''):
        return 'https://%s/%s/%s%s.%s' % (
            public_bucket_host, s3_resources_path, filename, mode_str, extension)

    ga_css = get_resource_url('ga', 'css')
    if vip:
        ga_js = get_resource_url('ga-vip', 'js', mode_str)
    else:
        ga_js = get_resource_url('ga', 'js', mode_str)
    epsg_21781_js = get_resource_url('EPSG21781', 'js')
    epsg_2056_js = get_resource_url('EPSG2056', 'js')

    response = render_to_response(
        'chsdi:templates/loader.js',
        {
            'lang': lang,
            'ga_css': ga_css,
            'ga_js': ga_js,
            'epsg_21781_js': epsg_21781_js,
            'epsg_2056_js': epsg_2056_js,
            'api_url': request.path_url.replace('/loader.js', ''),
            'ignore_polyfill': ignore_polyfill,
            'data': json.dumps(data, separators=(',', ':'))
        },
        request=request
    )
    response.content_type = 'application/javascript'
    return response
Пример #42
0
def desk_signin(request):
    import requests
    req = request.json_body
    settings = request.registry.settings
    try:
        path = settings['desktop']['central_server'] + 'signin'
        session = requests.Session()
        session.headers.update({'Connection': 'Keep-Alive'})
        adapter = requests.adapters.HTTPAdapter(pool_connections=1,
                                                pool_maxsize=1,
                                                max_retries=10)
        session.mount('http://', adapter)
        status = session.post(path, json=req)
        client_id = status.json()['client_id']
        cookies = status.cookies.get_dict()
        with open('authentication_data.json', 'w') as f:
            f.write(json.dumps(cookies))
        if status.status_code == 200:
            path = request.route_url('basic_sync')
            subreq = Request.blank(path)
            subreq.method = 'POST'
            sub_headers = {'Cookie': request.headers['Cookie']}
            subreq.headers = sub_headers
            resp = request.invoke_subrequest(subreq)
            if resp.status_code == 200:
                headers = remember(request,
                                   principal=client_id,
                                   max_age=315360000)
                response = Response()
                response.headers = headers
                locale_id = cookies['locale_id']
                response.set_cookie(key='locale_id',
                                    value=str(locale_id),
                                    max_age=datetime.timedelta(days=3650))
                response.set_cookie(key='client_id',
                                    value=str(client_id),
                                    max_age=datetime.timedelta(days=3650))
                result = dict()
                result['client_id'] = client_id
                request.response.status = HTTPOk.code
                # request.response.headers = headers
                # return response
                return HTTPOk(headers=response.headers, json_body=result)
            # return result
    except HTTPUnauthorized:
        return HTTPUnauthorized(
            json_body={'error': 'Login or password is wrong, please retry'})
    except Exception:
        return HTTPServiceUnavailable(
            json_body={
                'error':
                'You have no internet connection or Lingvodoc server is unavailable; please retry later.'
            })
Пример #43
0
    def subrequest(self, url, params={}, method='GET'):
        req = Request.blank(url,
                            cookies=self.request.cookies,
                            content_type='application/json',
                            method=method)

        if req.method == 'GET' and params:
            req.body = urllib.urlencode(params)

        if req.method == 'POST':
            req.body = json.dumps(params)

        return self.request.invoke_subrequest(req)
 def test_inject_html(self):
     from pyramid_debugtoolbar.utils import STATIC_PATH
     self.config.add_static_view('_debugtoolbar/static', STATIC_PATH)
     self.config.add_route('debugtoolbar', '/_debugtoolbar/*subpath')
     response = Response('<body></body>')
     response.content_type = 'text/html'
     request = Request.blank('/')
     request.pdtb_id = 'abc'
     request.registry = self.config.registry
     toolbar = self._makeOne(request, [DummyPanel], [DummyPanel], [])
     toolbar.inject(request, response)
     self.assertTrue(bytes_('div id="pDebug"') in response.app_iter[0])
     self.assertEqual(response.content_length, len(response.app_iter[0]))
Пример #45
0
def task_prerun_signal(task_id, task, args, kwargs, **kwaargs):
    if hasattr(celery, 'pyramid'):
        env = celery.pyramid
        env = prepare(registry=env['request'].registry)
        proper_base_url = env['request'].registry.settings['mailing.app_url']
        tmp_req = Request.blank('/', base_url=proper_base_url)
        # ensure tasks generate url for right domain from config
        env['request'].environ['HTTP_HOST'] = tmp_req.environ['HTTP_HOST']
        env['request'].environ['SERVER_PORT'] = tmp_req.environ['SERVER_PORT']
        env['request'].environ['SERVER_NAME'] = tmp_req.environ['SERVER_NAME']
        env['request'].environ['wsgi.url_scheme'] = \
            tmp_req.environ['wsgi.url_scheme']
    get_current_request().tm.begin()
Пример #46
0
    def test_panel_injected(self):

        # make the app
        app = self.config.make_wsgi_app()
        # make a request
        req1 = Request.blank("/")
        req1.remote_addr = "127.0.0.1"
        resp1 = req1.get_response(app)
        self.assertEqual(resp1.status_code, 200)
        self.assertIn("http://localhost/_debug_toolbar/", resp1.text)

        # check the toolbar
        links = re_toolbar_link.findall(resp1.text)
        self.assertIsNotNone(links)
        self.assertIsInstance(links, list)
        self.assertEqual(len(links), 1)
        toolbar_link = links[0]

        req2 = Request.blank(toolbar_link)
        req2.remote_addr = "127.0.0.1"
        resp2 = req2.get_response(app)
        self.assertEqual(resp2.status_code, 200)

        self.assertIn('<li class="" id="pDebugPanel-performance-csv">',
                      resp2.text)
        self.assertIn(
            '<div id="pDebugPanel-performance-csv-content" class="panelContent" style="display: none;">',
            resp2.text,
        )
        self.assertIn(
            """<div class="pDebugPanelTitle">
              <h3>Performance CSV</h3>
            </div>""",
            resp2.text,
        )
        self.assertIn(
            "The profiler was not activated for this request. Activate the checkbox in the toolbar to use it.",
            resp2.text,
        )
Пример #47
0
 def view_match(url):
     pviews = PViewsCommand([None, os.path.join(here, '../', 'development.ini'), url], 
                            quiet=True)
     config_uri = pviews.args[0]
     url = pviews.args[1]
     request = Request.blank(url)
     env = bootstrap(config_uri, request=request)
     view = pviews._find_view(request)
     result = True 
     if view is None:
         result = False
     env['closer']()
     return result
Пример #48
0
 def test_collection_post(self):
     root = self._fixture()
     body = bytes(dumps({'title': 'Hello world!'}), 'utf-8')
     request = Request.blank('/',
                             method='POST',
                             body=body,
                             matchdict={'rid': 2})
     request.registry = self.config.registry
     apply_request_extensions(request)
     inst = self._cut(request, context=root)
     response = inst.collection_post()
     self.assertIn(response, root['wall'].values())
     self.assertEqual(len(root['wall']), 2)
Пример #49
0
    def _makeOne(self, is_active=None):
        """
        Makes a request to the main application
        * which invokes `self._session_view`
        * make a request to the toolbar
        * return the toolbar ``Response``

        :param is_active:
            Default ``None``
            If ``True``, a ``pdbt_active`` cookie will be sent to activate
            additional features in the "Session" panel.
        """
        # make a request
        req1 = Request.blank("/session-view")
        req1.remote_addr = "127.0.0.1"
        _cookies = []
        if is_active:
            _cookies.append("pdtb_active=session")
        if _cookies:
            _cookies = "; ".join(_cookies)
            if not PY3:
                _cookies = _cookies.encode()
            req1.headers["Cookie"] = _cookies
        resp_app = req1.get_response(self.app)
        self.assertEqual(resp_app.status_code, 200)
        self.assertIn("http://localhost/_debug_toolbar/", resp_app.text)

        # check the toolbar
        links = self.re_toolbar_link.findall(resp_app.text)
        self.assertIsNotNone(links)
        self.assertIsInstance(links, list)
        self.assertEqual(len(links), 1)
        toolbar_link = links[0]

        req2 = Request.blank(toolbar_link)
        req2.remote_addr = "127.0.0.1"
        resp_toolbar = req2.get_response(self.app)

        return (resp_app, resp_toolbar)
Пример #50
0
    def test_transform_failure(self):
        """Test MathML2SVG post with content that won't transform,
        but contains valid xml and MathML elements.
        """
        request = Request.blank('/', POST={'MathML': INVALID_MATHML})

        from cnxmathml2svg import convert
        exception_cls = httpexceptions.HTTPInternalServerError
        with self.assertRaises(exception_cls) as caught_exc:
            convert(request)

        exception = caught_exc.exception
        self.assertIn(b'Error reported by XML parser: ', exception.comment)
Пример #51
0
 def get_answers(self, user_id: str, skip: int = 0, take: int = 50000):
     request = Request.blank('/api/form/v1/user/%s/answer?skip=%d&take=%d' %
                             (user_id, skip, take))
     request.authorization = self.auth.get_session_id()
     response = request.get_response()
     data = json.loads(response.body.decode())
     if response.status_code != HTTPStatus.OK and response.status_code != HTTPStatus.NOT_FOUND:
         logging.warning('Fail to load answers for user ' + user_id + ': ' +
                         data.get('error_message', ''))
         return OperationResult.fail(
             FailResult(code=response.status_code, **data))
     data['items'] = list(map(lambda o: Answer(**o), data.get('items', [])))
     return OperationResult.success(ItemsResult(**data))
Пример #52
0
    def test_lang_is_not_available(self):
        from pyramid.request import Request
        from pyramid.threadlocal import get_current_registry

        from c2cgeoportal_geoportal import locale_negotiator

        request = Request.blank("/")
        request.registry = get_current_registry()
        request.registry.settings = {"default_locale_name": "de", "available_locale_names": ["de", "es"]}

        request.headers["accept-language"] = "en-us,en;q=0.3,fr;q=0.7"
        lang = locale_negotiator(request)
        self.assertEqual(lang, "de")
Пример #53
0
def view_item_service_func2(request):
    view_id = request.matchdict['view_id']
    container_id = request.matchdict['container_id']
    item_id = request.matchdict['item_id']
    ci = DBSession.query(ContainerItem, Container)\
        .join(Container, Container.id_container == ContainerItem.id_container)\
        .filter(Container.id_view == view_id)\
        .filter(sqlalchemy.and_(ContainerItem.id_container == container_id, ContainerItem.id_item == item_id))\
        .one()

    DBSession.delete(ci[0])
    req = Request.blank('/api/v1/view/' + view_id + '/container/' + container_id)
    return request.invoke_subrequest(req)
Пример #54
0
Файл: store.py Проект: shepazu/h
    def search_raw(self, query):
        url = self.request.route_url('api_real', subpath='search_raw')
        subreq = Request.blank(url, method='POST')
        subreq.json = query
        result = self._invoke_subrequest(subreq)
        payload = json.loads(result.body)

        hits = []
        for res in payload['hits']['hits']:
            # Add id
            res["_source"]["id"] = res["_id"]
            hits.append(res["_source"])
        return hits
Пример #55
0
 def test_process_response_html(self):
     from pyramid_debugtoolbar.utils import ROOT_ROUTE_NAME
     from pyramid_debugtoolbar.utils import STATIC_PATH
     self.config.add_static_view('_debugtoolbar/static', STATIC_PATH)
     self.config.add_route(ROOT_ROUTE_NAME, '/_debugtoolbar')
     response = Response('<body></body>')
     response.content_type = 'text/html'
     request = Request.blank('/')
     request.registry = self.config.registry
     toolbar = self._makeOne(request, [DummyPanel])
     toolbar.process_response(response)
     self.assertTrue(response.processed)
     self.assertTrue(bytes_('div id="pDebug"') in response.app_iter[0])
Пример #56
0
def test_request_logger_tween_factory_call(
    mock_datetime,
    mock_handler,
    mock_factory,
    status_code,
    exc,
    expected_lvl,
    extra_expected_response,
):
    req = Request.blank("/path/to/something")
    mock_handler.return_value = Response(
        body="a_body",
        status=status_code,
    )
    if exc is not None:
        mock_handler.side_effect = exc
    mock_factory._log = mock.Mock()
    mock_datetime.now = mock.Mock(
        side_effect=[datetime.fromtimestamp(0),
                     datetime.fromtimestamp(57)], )

    try:
        mock_factory(req)
    except Exception as e:
        if exc is None:
            pytest.fail(f"Got unexpected exception: {e}")

    expected_response = {
        "status_code": status_code,
        "response_time_ms": 57000.0,
    }
    expected_response.update(extra_expected_response)
    assert mock_factory._log.call_args_list == [
        mock.call(
            timestamp=datetime.fromtimestamp(0),
            level=expected_lvl,
            additional_fields={
                # most of these are default for a blank request
                "request": {
                    "path": "/path/to/something",
                    "params": {},
                    "client_addr": None,
                    "http_method": "GET",
                    "headers": {
                        "Host": "localhost:80"
                    },
                },
                "response": expected_response,
            },
        ),
    ]
Пример #57
0
def login_verify(context, request):
    ######################################################################################
    #                                                                                    #
    # Let's clarify the difference between "provider" and "method" in this function:     #
    #                                                                                    #
    # * Conceptually, [authentication] methods can be understood pretty much like        #
    #   protocols or transports. So, methods would be for example: OpenID, OAuth2 and    #
    #   other authentication protocols supported by Velruse.                             #
    #                                                                                    #
    # * A provider is simply an entity, like Google, Yahoo, Twitter, Facebook, Verisign, #
    #   Github, Launchpad and hundreds of other entities which employ authentication     #
    #   methods like OpenID, OAuth2 and others supported by Velruse.                     #
    #                                                                                    #
    # * In particular, certain entities implement their own authentication methods or    #
    #   they eventually offer several authentication methods. For this reason, there are #
    #   specific methods for "yahoo", "tweeter", "google_hybrid", "google_oauth2", etc.  #
    #                                                                                    #
    ######################################################################################

    log.debug(sys._getframe().f_code.co_name)

    ####################################################################################
    #TODO: should pass "came_from" to view "logged_in" so that we can redirect
    #      to the previous page. Sorry... I failed to make it work :(
    #-- came_from = request.params.get('came_from', request.resource_url(context))
    ####################################################################################

    provider = request.params['provider']
    method = request.params['method']

    settings = request.registry.settings
    if not method in find_providers(settings):
        raise HTTPNotFound('Provider/method {}/{} is not configured'.format(
            provider, method)).exception

    payload = dict(request.params)
    payload['format'] = 'json'
    if 'yahoo' == method: payload['oauth'] = 'true'
    if 'openid' == method: payload['use_popup'] = 'false'
    del payload['provider']
    del payload['method']

    try:
        url = login_url(request, method)
        response = request.invoke_subrequest(Request.blank(url, POST=payload))
        return response
    except Exception as e:
        message = _(u'Provider/method: {}/{} :: {}.').format(
            provider, method, e.message)
        log.exception(_(u'{}\nStacktrace follows:\n{}').format(message, e))
        raise HTTPNotFound(message).exception
 def test_it_remote_addr_mask(self):
     self.config.registry.settings['debugtoolbar.hosts'] = ['127.0.0.0/24']
     request = Request.blank('/')
     self.config.registry.settings['debugtoolbar.panels'] = [DummyPanel]
     request.registry = self.config.registry
     request.remote_addr = '127.0.0.254'
     result = self._callFUT(request)
     self.assertTrue(getattr(result, 'processed', False))
     request.remote_addr = '127.0.0.1'
     result = self._callFUT(request)
     self.assertTrue(getattr(result, 'processed', False))
     request.remote_addr = '127.0.1.1'
     result = self._callFUT(request)
     self.assertFalse(getattr(result, 'processed', False))
    def test_it_raises_exception_no_intercept_exc(self):
        request = Request.blank('/')
        request.remote_addr = '127.0.0.1'

        def handler(request):
            raise NotImplementedError

        request.registry = self.config.registry
        logger = DummyLogger()
        self.assertRaises(NotImplementedError,
                          self._callFUT,
                          request,
                          handler,
                          _logger=logger)
 def test_passing_of_button_style(self):
     from pyramid_debugtoolbar.utils import STATIC_PATH
     self.config.add_static_view('_debugtoolbar/static', STATIC_PATH)
     self.config.add_route('debugtoolbar', '/_debugtoolbar/*subpath')
     self.config.registry.settings['debugtoolbar.button_style'] = \
         'top:120px;zoom:50%'
     response = Response('<body></body>')
     response.content_type = 'text/html'
     request = Request.blank('/')
     request.pdtb_id = 'abc'
     request.registry = self.config.registry
     toolbar = self._makeOne(request, [DummyPanel], [DummyPanel], [])
     toolbar.inject(request, response)
     self.assertTrue(bytes_('top:120px;zoom:50%') in response.app_iter[0])