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')
def _makeRequest(self, config, environ={}): from pyramid.request import Request from pyramid.interfaces import IRequestExtensions request = Request(environ) request.registry = config.registry request._set_extensions(config.registry.getUtility(IRequestExtensions)) return request
def test_create_update(self): from papyrus.protocol import Protocol from pyramid.request import Request from geojson import Feature, FeatureCollection from shapely.geometry import Point MappedClass = self._get_mapped_class() # a mock session specific to this test class MockSession(object): def query(self, mapped_class): return {'a': mapped_class(Feature(id='a')), 'b': mapped_class(Feature(id='b'))} def flush(self): pass proto = Protocol(MockSession, MappedClass, 'geom') # we need an actual Request object here, for body to do its job request = Request({}) request.method = 'POST' request.body = '{"type": "FeatureCollection", "features": [{"type": "Feature", "id": "a", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}, {"type": "Feature", "id": "b", "properties": {"text": "bar"}, "geometry": {"type": "Point", "coordinates": [46, 6]}}]}' features = proto.create(request) self.assertTrue(isinstance(features, FeatureCollection)) self.assertEqual(len(features.features), 2) self.assertEqual(features.features[0].id, 'a') self.assertEqual(features.features[0].text, 'foo') self.assertTrue(features.features[0].geom.shape.equals(Point(45, 5))) self.assertEqual(features.features[1].id, 'b') self.assertEqual(features.features[1].text, 'bar') self.assertTrue(features.features[1].geom.shape.equals(Point(46, 6)))
def _makeRequest(self, environ=None): from pyramid.request import Request if environ is None: environ = self._makeEnviron() request = Request(environ) request.registry = self.p_config.registry return request
def _makeRequest(self, **environ): from pyramid.request import Request from pyramid.registry import Registry environ = self._makeEnviron(**environ) request = Request(environ) request.registry = Registry() return request
def test_items_delete(self): """ Test deleting an item. """ # first create an item self._create_item_status() payload = {"name": "Macbook Air", "type": "TRADE", "quantity": "1", "price": "", "description": "Lightweight lappy.", "reason": "", "is_draft": "y", "uuid": str(uuid.uuid4())} request = Request({}, method='POST', body=json.dumps(payload)) request.registry = self.config.registry response = items(request) self.assertEqual(response.status_code, 200) self.assertEqual(DBSession.query(Item).count(), 1) # try retrieving the newly added item item = DBSession.query(Item).first() # now send a delete request request.method = 'DELETE' request.matchdict = {'id': item.id} request.body = None items(request) self.assertEqual(response.status_code, 200) self.assertEqual(DBSession.query(Item).count(), 0)
def _makeRequest(self, environ=None): if environ is None: environ = {} request = Request(environ) request.registry = self.config.registry return request
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)
def _makeOne(self): from pyramid.url import URLMethodsMixin class Request(URLMethodsMixin): application_url = 'http://example.com:5432' request = Request() request.registry = self.config.registry return request
def testGetWsdl(self): """Simple test for serving of WSDL by spyne through pyramid route""" application = PyramidApplication( Application([self.HelloWorldService], tns='spyne.examples.hello', in_protocol=Soap11(validator='lxml'), out_protocol=Soap11())) config = Configurator(settings={'debug_all': True}) config.add_route('home', '/') config.add_view(application, route_name='home') wsgi_app = validator(config.make_wsgi_app()) env = { 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/', 'QUERY_STRING': 'wsdl', } setup_testing_defaults(env) request = Request(env) resp = request.get_response(wsgi_app) self.assert_(resp.status.startswith("200 ")) node = etree.XML(resp.body) # will throw exception if non well formed
def request_fake(): """Create request with fake i18n subscribers on.""" config = testing.setUp() config.scan('pyramid_localize.subscribers.fake') request = Request({}) request.registry = config.registry return request
def _create_request(self): from pyramid.request import Request from c2cgeoportal import get_user_from_request request = Request({}) request.set_property(get_user_from_request, name="user", reify=True) return request
def _callFUT(self, environ): from pyramid.request import Request request = Request(environ) request.registry = self.config.registry from pyramid_zope_request import PyramidPublisherRequest rv = PyramidPublisherRequest(request) return rv
def __init__(self, *args, **kw): _Request.__init__(self, *args, **kw) self.features = self.DEFAULT_FEATURES.copy() # FIXME: Is there a cleaner place to put this? self.add_finished_callback(_teardown_session)
def test_directive_get_sockjs_manager(self): self.config.include('pyramid_sockjs') request = Request(self._environ) request.registry = self.registry self.registry.notify(NewRequest(request)) self.assertTrue(hasattr(request, 'get_sockjs_manager'))
def ware(environ, start_response): request = Request(environ) db = environ['db.session'] session = open_session(db, request) request.environ['gm_session'] = session response = request.get_response(app) save_session(db, session, request, response) return response(environ, start_response)
def test_request_properties(): root_request = Request({}, headers={"X-Some-Special-Header": "foobar"}) # this is a slightly baroque mechanism to make sure that the request is # internally consistent for all test environments root_request.body = '{"myKey": 42}'.encode() assert '{"myKey": 42}' == root_request.text request = PyramidSwaggerRequest(root_request, {}) assert {"myKey": 42} == request.body assert "foobar" == request.headers["X-Some-Special-Header"]
def _makeRequest(self, environ=None): if environ is None: environ = {} request = Request(environ) request.config = Mock() mock_configuration = { 'localize.locales.available': ['en', 'pl', 'de', 'cz']} request.config.configure_mock(**mock_configuration) return request
def test__make_response_with_real_request(self): # functional from pyramid.request import Request request = Request({}) request.registry = self.config.registry request.response.status = '406 You Lose' helper = self._makeOne('loo.foo') response = helper._make_response('abc', request) self.assertEqual(response.status, '406 You Lose') self.assertEqual(response.body, b'abc')
def _make_one(self, v=_no_header): from pyramid.request import Request r = Request(environ={}) class FakeRegistry(object): settings = {'pyramid.available_languages': 'en fr'} r.registry = FakeRegistry() if v is not _no_header: r.headers['Accept-Language'] = v return r
def test_get_session_manager_default(self): import pyramid_sockjs self.config.add_sockjs_route() request = Request(self._environ) request.registry = self.registry self.registry.notify(NewRequest(request)) sm = request.get_sockjs_manager() self.assertIs(self.registry.__sockjs_managers__[''], sm)
def request_factory(environ): request = Request(environ) request.response = Response() request.response.headerlist = [] request.response.headerlist.extend( ( ('Access-Control-Allow-Origin', '*'), ) ) return request
def test_item_image_delete_fail(self): """ Test deletion of non-existent image via DELETE request. """ # send DELETE request request = Request({}, method='DELETE') request.matchdict = {'id': 1} request.registry = self.config.registry self.assertRaises(HTTPBadRequest, item_images, None, request)
def test__make_response_with_real_request(self): # functional from pyramid.request import Request request = Request({}) request.registry = self.config.registry request.response.status = "406 You Lose" helper = self._makeOne("loo.foo") response = helper._make_response("abc", request) self.assertEqual(response.status, "406 You Lose") self.assertEqual(response.body, "abc")
def test_includeme(testconfig, icons_dir): testconfig.get_settings()["icons_path"] = str(icons_dir) testconfig.include(pylf.icons) testconfig.commit() req_exts = testconfig.registry.queryUtility(IRequestExtensions) req = Request({}) req._set_extensions(req_exts) assert hasattr(req, "icon_path") icon_path = req.icon_path assert icon_path((None, None)) == "/icons/unknown.png" assert icon_path(("text/plain", None)) == "/icons/text-plain.png"
def test_get_session_manager_unknown(self): request = Request(self._environ) request.registry = self.registry self.registry.notify(NewRequest(request)) self.assertRaises( KeyError, request.get_sockjs_manager, 'test') self.config.add_sockjs_route() self.assertRaises( KeyError, request.get_sockjs_manager, 'test')
def request_factory(environ): request = Request(environ) if request.is_xhr: request.response = Response() request.response.headerlist = [] request.response.headerlist.extend( ( ('Access-Control-Allow-Origin', '*'), #('Content-Type', 'application/json') ) ) return request
def _req_factory(environ): R = Request(environ) if R.is_xhr: R.response = Response() R.response.headerlist = [] R.response.headerlist.extend( ( ('Access-Control-Allow-Origin', '*'), ('Content-Type', 'application/json') ) ) return R
def _makeOne(self, environ=None): from pyramid.url import URLMethodsMixin if environ is None: environ = {} class Request(URLMethodsMixin): application_url = 'http://example.com:5432' script_name = '' def __init__(self, environ): self.environ = environ request = Request(environ) request.registry = self.config.registry return request
def test_items_post_failed(self): """ Test that when POSTing malformed payload, it'll raise HTTPBadRequest. """ payload = {"name": "", "type": "", "quantity": "", "price": "", "description": "", "reason": "", "is_draft": "", "uuid": ""} request = Request({}, method='POST', body=json.dumps(payload)) request.registry = self.config.registry self.assertRaises(HTTPBadRequest, items, request) self.assertEqual(DBSession.query(Item).count(), 0)
def run(self): if not self.args.config_uri or not self.args.path_info: self.out('You must provide at least two arguments') return 2 config_uri = self.args.config_uri config_vars = parse_vars(self.args.config_vars) path = self.args.path_info loader = self._get_config_loader(config_uri) loader.setup_logging(config_vars) app = loader.get_wsgi_app(self.args.app_name, config_vars) if not path.startswith('/'): path = '/' + path try: path, qs = path.split('?', 1) except ValueError: qs = '' path = url_unquote(path) headers = {} if self.args.login: enc = base64.b64encode(self.args.login.encode('ascii')) headers['Authorization'] = 'Basic ' + enc.decode('ascii') if self.args.headers: for item in self.args.headers: if ':' not in item: self.out( "Bad --header=%s option, value must be in the form " "'name:value'" % item ) return 2 name, value = item.split(':', 1) headers[name] = value.strip() request_method = (self.args.method or 'GET').upper() environ = { 'REQUEST_METHOD': request_method, 'SCRIPT_NAME': '', # may be empty if app is at the root 'PATH_INFO': path, 'SERVER_NAME': 'localhost', # always mandatory 'SERVER_PORT': '80', # always mandatory 'SERVER_PROTOCOL': 'HTTP/1.0', 'CONTENT_TYPE': 'text/plain', 'REMOTE_ADDR': '127.0.0.1', 'wsgi.run_once': True, 'wsgi.multithread': False, 'wsgi.multiprocess': False, 'wsgi.errors': sys.stderr, 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0), 'QUERY_STRING': qs, 'HTTP_ACCEPT': 'text/plain;q=1.0, */*;q=0.1', 'paste.command_request': True, } if request_method in ('POST', 'PUT', 'PATCH'): environ['wsgi.input'] = self.stdin environ['CONTENT_LENGTH'] = '-1' for name, value in headers.items(): if name.lower() == 'content-type': name = 'CONTENT_TYPE' else: name = 'HTTP_' + name.upper().replace('-', '_') environ[name] = value request = Request.blank(path, environ=environ) response = request.get_response(app) if self.args.display_headers: self.out(response.status) for name, value in response.headerlist: self.out('%s: %s' % (name, value)) if response.charset: self.out(response.ubody) else: self.out(response.body) return 0
def _get_user_locator(context: IResource, registry: Registry) -> IUserLocator: request = Request.blank('/dummy') request.registry = registry locator = registry.getMultiAdapter((context, request), IUserLocator) return locator
def read(self, key): url = self.request.route_url('api_real', subpath='annotations/%s' % key) subreq = Request.blank(url) return self._invoke_subrequest(subreq).json
def test_it_remote_addr_proxies_list(self): request = Request.blank('/') request.remote_addr = '172.16.63.156, 64.119.211.105' result = self._callFUT(request)
def sdi_mgmt_views(context, request, names=None): if not hasattr(context, '__name__'): # shortcut if the context doesn't have a name (usually happens if the # context is an exception); we do this because mgmt_path uses Pyramid's # resource_path_tuple, which wants every object in the lineage to have # a __name__. return [] registry = request.registry introspector = registry.introspector unordered = [] # create a dummy request signaling our intent req = Request(request.environ.copy()) req.script_name = request.script_name req.context = context req.matched_route = request.matched_route req.method = 'GET' req.registry = request.registry sro_enum = list(enumerate(providedBy(context).__sro__[:-1])) for data in introspector.get_category('sdi views'): related = data['related'] sdi_intr = data['introspectable'] tab_title = sdi_intr['tab_title'] tab_condition = sdi_intr['tab_condition'] tab_before = sdi_intr['tab_before'] tab_after = sdi_intr['tab_after'] def is_view(intr): return intr.category_name == 'views' for view_intr in filter(is_view, related): # NB: in reality, the above loop will execute exactly once because # each "sdi view" is associated with exactly one pyramid view view_name = view_intr['name'] req.path_info = request.sdiapi.mgmt_path(context, view_name) if names is not None and not view_name in names: continue # do a passable job at figuring out whether, if we visit the # url implied by this view, we'll be permitted to view it and # something reasonable will show up intr_context = view_intr['context'] sro_index = MAX_ORDER if intr_context is None: intr_context = Interface if IInterface.providedBy(intr_context): if not intr_context.providedBy(context): continue for i, spec in sro_enum: if spec is intr_context: sro_index = i break elif isinstance(context, intr_context): for i, spec in sro_enum: if spec.implementedBy(intr_context): sro_index = i break else: # pragma: no cover (coverage bug, this is reached) continue if tab_condition is not None and names is None: if callable(tab_condition): if not tab_condition(context, request): continue elif not tab_condition: continue derived = view_intr['derived_callable'] if hasattr(derived, '__predicated__'): if not derived.__predicated__(context, req): continue if hasattr(derived, '__permitted__'): if not derived.__permitted__(context, req): continue predicate_order = getattr(derived, '__order__', MAX_ORDER) if view_name == request.view_name: css_class = 'active' else: css_class = None unordered.append({ 'view_name': view_name, 'tab_before': tab_before, 'tab_after': tab_after, 'title': tab_title or view_name.capitalize(), 'class': css_class, 'predicate_order': predicate_order, 'sro_index': sro_index, 'url': request.sdiapi.mgmt_path(request.context, '@@%s' % view_name) }) # De-duplicate the unordered list of tabs with the same view_name. Prefer # the tab with the lowest (sro_index, predicate_order) tuple, because this # is the view that's most likely to be executed when visited and we'd # like to get its title right. unordered.sort(key=lambda s: (s['sro_index'], s['predicate_order'])) deduplicated = [] view_names = {} # use a sort-break to take only the first of each same-named view data. for view_data in unordered: vn = view_data['view_name'] if vn in view_names: continue view_names[vn] = True deduplicated.append(view_data) manually_ordered = [] tab_order = request.registry.content.metadata(context, 'tab_order') if tab_order is not None: ordered_names = [ y for y in tab_order if y in [x['view_name'] for x in deduplicated] ] for ordered_name in ordered_names: for view_data in unordered[:]: if view_data['view_name'] == ordered_name: deduplicated.remove(view_data) manually_ordered.append(view_data) # Sort non-manually-ordered views lexically by title. Reverse due to the # behavior of the toposorter; we'd like groups of things that share the # same before/after to be alpha sorted ascending relative to each other, # and reversing lexical ordering here gets us that behavior down the line. lexically_ordered = sorted( deduplicated, key=operator.itemgetter('title'), reverse=True, ) # Sort the lexically-presorted unordered views topologically based on any # tab_before and tab_after values in the view data. tsorter = TopologicalSorter(default_after=CENTER1, default_before=CENTER2) tsorter.add( CENTER1, None, after=FIRST, before=CENTER2, ) tsorter.add( CENTER2, None, after=CENTER1, before=LAST, ) for view_data in lexically_ordered: before = view_data.get('tab_before', None) after = view_data.get('tab_after', None) tsorter.add( view_data['view_name'], view_data, before=before, after=after, ) topo_ordered = [ x[1] for x in tsorter.sorted() if x[0] not in (CENTER1, CENTER2) ] return manually_ordered + topo_ordered
def test_valid(self): request = Request({"QUERY_STRING": ":action=browse", "PATH_INFO": "/pypi"}) sanity.junk_encoding(request)
def setUp(self): self.request = Request.blank('/') self.config = testing.setUp(request=self.request) panel = DummyPanel(self.request) self.request.debug_toolbar = DummyToolbar([panel])
def http_session_tween(request: Request) -> Response: log.debug("Starting http_session_tween") request.camcops_session = CamcopsHttpSession.get_http_session(request) response = handler(request) log.debug("Ending http_session_tween") return response
def test_invalid_path(self): request = Request({"PATH_INFO": "/projects/abouÃ…t"}) with pytest.raises(HTTPBadRequest, match="Invalid bytes in URL."): sanity.junk_encoding(request)
def test_google(self): request = Request.blank("/test?url=http://google.com/") self.assertTrue(self.predicate(None, request))
def home(context: Root, request: Request) -> Response: return HTTPFound(location=request.resource_url(context["lectures"]))
def test_invalid(self): request = Request.blank("/test?url=http://123456.com/") self.assertFalse(self.predicate(None, request))
def test_no_url(self): request = Request.blank("/test") self.assertFalse(self.predicate(None, request))
def route_url(self, *args, **kwargs): return Request.route_url(self, *args, **kwargs)
def get_root(app: Router): """Return the root of the application.""" request = Request.blank('/path-is-meaningless-here') request.registry = app.registry root = app.root_factory(request) return root
def test_it_startswith_excluded_prefix(self): request = Request.blank('/excluded') request.remote_addr = '127.0.0.1' result = self._callFUT(request) self.assertFalse(hasattr(request, 'debug_toolbar')) self.assertTrue(result is self.response)
def logout(request: Request): request.add_response_callback( lambda req, resp: __delete_cookie_callback(resp, auth_cookie_name))
def _create_request(self): from pyramid.request import Request from c2cgeoportal import get_user_from_request request = Request({}) request.set_property(get_user_from_request, name='user', reify=True) return request
def test_not_post(self): request = Request({"REQUEST_METHOD": "GET"}) sanity.invalid_forms(request)
def _makeRequest(self, url, registry): from pyramid.request import Request request = Request.blank('/a') request.registry = registry return request
def test_invalid_qsl(self): request = Request({"QUERY_STRING": "%Aaction=browse"}) with pytest.raises(HTTPBadRequest, match="Invalid bytes in query string."): sanity.junk_encoding(request)
def set_auth(request: Request, user_id: int): hash_val = __hash_text(str(user_id)) val = "{}:{}".format(user_id, hash_val) request.add_response_callback(lambda req, resp: __add_cookie_callback( req, resp, auth_cookie_name, val))
def search(self, **query): url = self.request.route_url('api_real', subpath='search', _query=query) subreq = Request.blank(url) return self._invoke_subrequest(subreq).json['rows']
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))
def copy_header_if_exists(self, header: str, request: Request): """Copy header if exists.""" value = self.request.headers.get(header, None) if value is not None: request.headers[header] = value
def test_it_remote_addr_is_None(self): request = Request.blank('/') request.remote_addr = None result = self._callFUT(request) self.assertFalse(hasattr(request, 'debug_toolbar')) self.assertTrue(result is self.response)
def can_add_resource(self, request: Request, meta: ResourceMetadata, context: IPool) -> bool: """Check that the resource type in `meta` is addable to `context`.""" permission = meta.permission_create allowed = request.has_permission(permission, context) return allowed
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 apply_rate_limit(request: Request, action_name: str) -> None: """Check the rate limit for an action, and raise HTTP 429 if it's exceeded.""" result = request.check_rate_limit(action_name) if not result.is_allowed: raise result.add_headers_to_response(HTTPTooManyRequests())
def blank_request(*args, **kwargs): return Request.blank("/blankpath", *args, **kwargs)