class Request(webob.Request): auth = descriptors.environ_getter('seeitservices.auth', None) auth_html = descriptors.environ_getter('seeitservices.auth_html', None) root = descriptors.environ_getter('seeitservices.root', None) sub_key = descriptors.environ_getter('subber.key', None) def add_sub(self, name, old, new, content_types=('text/html', ), replace=True): if 'subber.subs' not in self.environ: self.environ['subber.subs'] = {} self.environ['subber.subs'][name] = (old, new, content_types, replace) def get_sub(self, name): data = self.environ['subber.subs'].get(name) if not data: return None return data[1] @property def email(self): if self.auth: return self.auth['email'] return None
def test_environ_getter_rfc_section(): from webob.descriptors import environ_getter desc = environ_getter('HTTP_X_AKEY', rfc_section='14.3') eq_(desc.__doc__, "Gets and sets the ``X-Akey`` header " "(`HTTP spec section 14.3 " "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3>`_)." )
def test_environ_getter_rfc_section(): from webob.descriptors import environ_getter desc = environ_getter('HTTP_X_AKEY', rfc_section='14.3') eq_( desc.__doc__, "Gets and sets the ``X-Akey`` header " "(`HTTP spec section 14.3 " "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3>`_).")
def test_converter_date_docstring(): from webob.descriptors import converter_date from webob.descriptors import environ_getter desc = converter_date( environ_getter("HTTP_DATE", "Tue, 15 Nov 1994 08:12:31 GMT", "14.8")) assert 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.8' in desc.__doc__ assert '``Date`` header' in desc.__doc__
def test_environ_getter_default_fset_none(): from webob.descriptors import environ_getter req = Request.blank('/') desc = environ_getter('akey', default='the_default') desc.fset(req, 'baz') desc.fset(req, None) ok_('akey' not in req.environ)
def test_environ_getter_default_fset_none(): from webob.descriptors import environ_getter req = Request.blank('/') desc = environ_getter('akey', default='the_default') desc.fset(req, 'baz') desc.fset(req, None) assert 'akey' not in req.environ
def test_converter_date_docstring(): from webob.descriptors import converter_date from webob.descriptors import environ_getter desc = converter_date(environ_getter( "HTTP_DATE", "Tue, 15 Nov 1994 08:12:31 GMT", "14.8")) assert 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.8' in desc.__doc__ assert '``Date`` header' in desc.__doc__
def test_environ_getter_nodefault_fget(): from webob.descriptors import environ_getter req = Request.blank("/") desc = environ_getter("akey") desc.fset(req, "bar") assert req.environ["akey"] == "bar"
def test_environ_getter_rfc_section(): from webob.descriptors import environ_getter desc = environ_getter('akey', rfc_section='14.3') eq_( desc.__doc__, "Gets and sets the 'akey' key in the environment. For " "more information on akey see `section 14.3 " "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3>`_.")
def test_environ_getter_nodefault_keyerror(): from webob.descriptors import environ_getter req = Request.blank("/") desc = environ_getter("akey") with pytest.raises(KeyError): desc.fget(req)
def test_environ_getter_default_fset(): from webob.descriptors import environ_getter req = Request.blank("/") desc = environ_getter("akey", default="the_default") desc.fset(req, "bar") assert req.environ["akey"] == "bar"
def test_environ_getter_default_fset(): from webob.descriptors import environ_getter req = Request.blank("/") desc = environ_getter("akey", default="the_default") desc.fset(req, "bar") eq_(req.environ["akey"], "bar")
class Request(webob.Request): _context_dict = None def _get_context_dict(self): # allow middleware up the stack to provide context, params and headers. context = self.environ.get(CONTEXT_ENV, {}) try: context['query_string'] = dict(self.params.items()) except UnicodeDecodeError: # The webob package throws UnicodeError when a request cannot be # decoded. Raise ValidationError instead to avoid an UnknownError. msg = _('Query string is not UTF-8 encoded') raise exception.ValidationError(msg) context['headers'] = dict(self.headers.items()) context['path'] = self.environ['PATH_INFO'] scheme = self.environ.get(CONF.secure_proxy_ssl_header) if scheme: # NOTE(andrey-mp): "wsgi.url_scheme" contains the protocol used # before the proxy removed it ('https' usually). So if # the webob.Request instance is modified in order to use this # scheme instead of the one defined by API, the call to # webob.Request.relative_url() will return a URL with the correct # scheme. self.environ['wsgi.url_scheme'] = scheme context['host_url'] = self.host_url # authentication and authorization attributes are set as environment # values by the container and processed by the pipeline. The complete # set is not yet known. context['environment'] = self.environ context['accept_header'] = self.accept context.setdefault('is_admin', False) return context @property def context_dict(self): if not self._context_dict: self._context_dict = self._get_context_dict() return self._context_dict auth_type = environ_getter('AUTH_TYPE', None) remote_domain = environ_getter('REMOTE_DOMAIN', None)
def test_environ_getter_default_fset_none(): from webob.descriptors import environ_getter req = Request.blank("/") desc = environ_getter("akey", default="the_default") desc.fset(req, "baz") desc.fset(req, None) assert "akey" not in req.environ
def test_environ_getter_default_fset_none(): from webob.descriptors import environ_getter req = Request.blank("/") desc = environ_getter("akey", default="the_default") desc.fset(req, "baz") desc.fset(req, None) ok_("akey" not in req.environ)
def test_environ_getter_default_fdel(): from webob.descriptors import environ_getter req = Request.blank('/') desc = environ_getter('akey', default='the_default') desc.fset(req, 'baz') assert 'akey' in req.environ desc.fdel(req) ok_('akey' not in req.environ)
def test_converter_without_name_fget(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank("/") desc = converter(environ_getter("CONTENT_LENGTH", "666", "14.13"), parse_int_safe, serialize_int) eq_(desc.fget(req), 666)
def test_converter_date_docstring(): from webob.descriptors import converter_date from webob.descriptors import environ_getter desc = converter_date(environ_getter( "HTTP_DATE", "Tue, 15 Nov 1994 08:12:31 GMT", "14.8")) eq_(desc.__doc__, "Gets and sets the 'HTTP_DATE' key in the environment. " "For more information on Date see `section 14.8 " "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.8>`_. " "Converts it using HTTP date.")
def test_converter_date(): import datetime from webob.descriptors import converter_date from webob.descriptors import environ_getter req = Request.blank('/') UTC = GMT() desc = converter_date( environ_getter("HTTP_DATE", "Tue, 15 Nov 1994 08:12:31 GMT", "14.8")) eq_(desc.fget(req), datetime.datetime(1994, 11, 15, 8, 12, 31, tzinfo=UTC))
def test_converter_date(): import datetime from webob.descriptors import converter_date from webob.descriptors import environ_getter req = Request.blank('/') UTC = GMT() desc = converter_date(environ_getter( "HTTP_DATE", "Tue, 15 Nov 1994 08:12:31 GMT", "14.8")) assert desc.fget(req) == datetime.datetime(1994, 11, 15, 8, 12, 31, tzinfo=UTC)
def test_converter_without_name_fget(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank('/') desc = converter(environ_getter('CONTENT_LENGTH', '666', '14.13'), parse_int_safe, serialize_int) eq_(desc.fget(req), 666)
def test_environ_getter_default_fdel(): from webob.descriptors import environ_getter req = Request.blank("/") desc = environ_getter("akey", default="the_default") desc.fset(req, "baz") assert "akey" in req.environ desc.fdel(req) ok_("akey" not in req.environ)
def test_converter_with_name_docstring(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int desc = converter(environ_getter('CONTENT_LENGTH', '666', '14.13'), parse_int_safe, serialize_int, 'int') assert 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13' in desc.__doc__ assert '``Content-Length`` header' in desc.__doc__
def test_converter_with_name_fset(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank('/') desc = converter(environ_getter('CONTENT_LENGTH', '666', '14.13'), parse_int_safe, serialize_int, 'int') desc.fset(req, '999') assert desc.fget(req) == 999
def test_converter_none_for_wrong_type(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank('/') desc = converter( environ_getter('CONTENT_LENGTH', 'sixsixsix', '14.13'), parse_int_safe, serialize_int, 'int') assert desc.fget(req) is None
def test_converter_with_name_docstring(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int desc = converter(environ_getter("CONTENT_LENGTH", "666", "14.13"), parse_int_safe, serialize_int, "int") assert "http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13" in desc.__doc__ assert "``Content-Length`` header" in desc.__doc__
def test_converter_without_name_fget(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank('/') desc = converter( environ_getter('CONTENT_LENGTH', '666', '14.13'), parse_int_safe, serialize_int) assert desc.fget(req) == 666
def test_converter_date_docstring(): from webob.descriptors import converter_date from webob.descriptors import environ_getter desc = converter_date( environ_getter("HTTP_DATE", "Tue, 15 Nov 1994 08:12:31 GMT", "14.8")) eq_( desc.__doc__, "Gets and sets the 'HTTP_DATE' key in the environment. " "For more information on Date see `section 14.8 " "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.8>`_. " "Converts it using HTTP date.")
def test_converter_delete(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank('/') desc = converter( ## XXX: Should this fail if the type is wrong? environ_getter('CONTENT_LENGTH', '666', '14.13'), parse_int_safe, serialize_int, 'int') assert_raises(KeyError, desc.fdel, req)
def test_converter_none_for_wrong_type(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank('/') desc = converter( ## XXX: Should this fail if the type is wrong? environ_getter('CONTENT_LENGTH', 'sixsixsix', '14.13'), parse_int_safe, serialize_int, 'int') eq_(desc.fget(req), None)
def test_converter_without_name_fget(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank("/") desc = converter( environ_getter("CONTENT_LENGTH", "666", "14.13"), parse_int_safe, serialize_int ) assert desc.fget(req) == 666
def test_converter_with_name_fset(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank('/') desc = converter( environ_getter('CONTENT_LENGTH', '666', '14.13'), parse_int_safe, serialize_int, 'int') desc.fset(req, '999') eq_(desc.fget(req), 999)
def test_converter_delete(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank('/') desc = converter( environ_getter('CONTENT_LENGTH', '666', '14.13'), parse_int_safe, serialize_int, 'int') with pytest.raises(KeyError): desc.fdel(req)
def test_converter_with_name_docstring(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int desc = converter( environ_getter('CONTENT_LENGTH', '666', '14.13'), parse_int_safe, serialize_int, 'int') assert 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13' in desc.__doc__ assert '``Content-Length`` header' in desc.__doc__
def test_converter_with_name_docstring(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int desc = converter(environ_getter('CONTENT_LENGTH', '666', '14.13'), parse_int_safe, serialize_int, 'int') eq_( desc.__doc__, "Gets and sets the 'CONTENT_LENGTH' key in the " "environment. For more information on CONTENT_LENGTH see `section 14.13 " "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13>`_. " "Converts it using int.")
def test_converter_with_name_docstring(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int desc = converter( environ_getter('CONTENT_LENGTH', '666', '14.13'), parse_int_safe, serialize_int, 'int') eq_(desc.__doc__, "Gets and sets the 'CONTENT_LENGTH' key in the " "environment. For more information on CONTENT_LENGTH see `section 14.13 " "<http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13>`_. " "Converts it using int.")
def test_converter_without_name_fset(): from webob.descriptors import ( converter, environ_getter, parse_int_safe, serialize_int, ) req = Request.blank("/") desc = converter(environ_getter("CONTENT_LENGTH", "666", "14.13"), parse_int_safe, serialize_int) desc.fset(req, "999") assert desc.fget(req) == 999
def test_converter_none_for_wrong_type(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank("/") desc = converter( environ_getter("CONTENT_LENGTH", "sixsixsix", "14.13"), parse_int_safe, serialize_int, "int", ) assert desc.fget(req) is None
def test_converter_delete(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank("/") desc = converter( environ_getter("CONTENT_LENGTH", "666", "14.13"), parse_int_safe, serialize_int, "int", ) with pytest.raises(KeyError): desc.fdel(req)
def test_converter_none_for_wrong_type(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank("/") desc = converter( ## XXX: Should this fail if the type is wrong? environ_getter("CONTENT_LENGTH", "sixsixsix", "14.13"), parse_int_safe, serialize_int, "int", ) eq_(desc.fget(req), None)
def test_converter_delete(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank("/") desc = converter( ## XXX: Should this fail if the type is wrong? environ_getter("CONTENT_LENGTH", "666", "14.13"), parse_int_safe, serialize_int, "int", ) assert_raises(KeyError, desc.fdel, req)
def test_converter_with_name_fset(): from webob.descriptors import converter from webob.descriptors import environ_getter from webob.descriptors import parse_int_safe from webob.descriptors import serialize_int req = Request.blank("/") desc = converter( environ_getter("CONTENT_LENGTH", "666", "14.13"), parse_int_safe, serialize_int, "int", ) desc.fset(req, "999") assert desc.fget(req) == 999
def test_converter_with_name_docstring(): from webob.descriptors import ( converter, environ_getter, parse_int_safe, serialize_int, ) desc = converter( environ_getter("CONTENT_LENGTH", "666", "14.13"), parse_int_safe, serialize_int, "int", ) assert ("http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13" in desc.__doc__) assert "``Content-Length`` header" in desc.__doc__
def test_environ_getter_docstring(): from webob.descriptors import environ_getter desc = environ_getter("akey") assert desc.__doc__ == "Gets and sets the ``akey`` key in the environment."
def test_environ_getter_nodefault_fdel(): from webob.descriptors import environ_getter desc = environ_getter('akey') eq_(desc.fdel, None)
def test_environ_getter_nodefault_fdel(): from webob.descriptors import environ_getter desc = environ_getter('akey') assert desc.fdel == None
def test_environ_getter_docstring(): from webob.descriptors import environ_getter desc = environ_getter('akey') assert desc.__doc__ == "Gets and sets the ``akey`` key in the environment."
class Request(webob.Request): _context_dict = None def _get_context_dict(self): # allow middleware up the stack to provide context, params and headers. context = self.environ.get(CONTEXT_ENV, {}) # NOTE(jamielennox): The webob package throws UnicodeError when a # param cannot be decoded. If we make webob iterate them now we can # catch this and throw an error early rather than on access. try: self.params.items() except UnicodeDecodeError: msg = _('Query string is not UTF-8 encoded') raise exception.ValidationError(msg) context['path'] = self.environ['PATH_INFO'] scheme = self.environ.get(CONF.secure_proxy_ssl_header) if scheme: # NOTE(andrey-mp): "wsgi.url_scheme" contains the protocol used # before the proxy removed it ('https' usually). So if # the webob.Request instance is modified in order to use this # scheme instead of the one defined by API, the call to # webob.Request.relative_url() will return a URL with the correct # scheme. self.environ['wsgi.url_scheme'] = scheme context['host_url'] = self.host_url # authentication and authorization attributes are set as environment # values by the container and processed by the pipeline. The complete # set is not yet known. context['environment'] = self.environ context.setdefault('is_admin', False) return context @property def context_dict(self): if not self._context_dict: self._context_dict = self._get_context_dict() return self._context_dict @property def auth_context(self): return self.environ.get(authorization.AUTH_CONTEXT_ENV, {}) def assert_authenticated(self): """Ensure that the current request has been authenticated.""" if not self.context: LOG.warning( _LW('An authenticated call was made and there is ' 'no request.context. This means the ' 'auth_context middleware is not in place. You ' 'must have this middleware in your pipeline ' 'to perform authenticated calls')) raise exception.Unauthorized() if not self.context.authenticated: # auth_context didn't decode anything we can use raise exception.Unauthorized() @property def audit_initiator(self): """A pyCADF initiator describing the current authenticated context.""" pycadf_host = host.Host(address=self.remote_addr, agent=self.user_agent) initiator = resource.Resource(typeURI=taxonomy.ACCOUNT_USER, host=pycadf_host) if self.context.user_id: initiator.id = utils.resource_uuid(self.context.user_id) initiator.user_id = self.context.user_id if self.context.project_id: initiator.project_id = self.context.project_id if self.context.domain_id: initiator.domain_id = self.context.domain_id return initiator auth_type = environ_getter('AUTH_TYPE', None) remote_domain = environ_getter('REMOTE_DOMAIN', None) context = environ_getter(context.REQUEST_CONTEXT_ENV, None)
def test_environ_getter_nodefault_fdel(): from webob.descriptors import environ_getter desc = environ_getter("akey") assert desc.fdel is None
def test_environ_getter_default_fget(): from webob.descriptors import environ_getter req = Request.blank("/") desc = environ_getter("akey", default="the_default") assert desc.fget(req) == "the_default"
def test_environ_getter_default_fget(): from webob.descriptors import environ_getter req = Request.blank('/') desc = environ_getter('akey', default='the_default') assert desc.fget(req) == 'the_default'
def test_environ_getter_default_fset(): from webob.descriptors import environ_getter req = Request.blank('/') desc = environ_getter('akey', default='the_default') desc.fset(req, 'bar') assert req.environ['akey'] == 'bar'
def test_environ_getter_default_fset(): from webob.descriptors import environ_getter req = Request.blank('/') desc = environ_getter('akey', default='the_default') desc.fset(req, 'bar') eq_(req.environ['akey'], 'bar')