Exemple #1
0
def construct_dummy_request(json_body: dict = None,
                            match_dict: dict = None,
                            validated: dict = None,
                            params: dict = None) -> DummyRequest:
    """
    Creates a Dummy-Request. Optionally takes a json_body etc, which can directly be injected into the request.

    :param json_body: dict
    :param match_dict: dict
    :param validated: dict
    :param params: dict
    :return: DummyRequest
    :rtype: DummyRequest
    """
    json_body = json_body if json_body else {}
    match_dict = match_dict if match_dict else {}
    validated = validated if validated else {}
    params = params if params else {}

    d_request = DummyRequest(json_body=json_body,
                             matchdict=match_dict,
                             validated=validated,
                             params=params,
                             errors=Errors(),
                             mailer=DummyMailer,
                             cookies={'_LOCALE_': 'en'},
                             decorated={'extras': {}})

    if d_request.registry.settings:
        d_request.registry.settings.update(get_key_pair())
    else:
        d_request.registry.settings = get_key_pair()

    return d_request
Exemple #2
0
 def test_get_json_appstruct_bad_payload(self):
     request = Request.blank('/', body=b'Hello world!')
     request.errors = Errors()
     request.registry = self.config.registry
     apply_request_extensions(request)
     view = self._cut(request)
     view.get_json_appstruct()
     self.assertEqual(len(request.errors), 1)
Exemple #3
0
 def test_get_json_appstruct_no_payload(self):
     request = testing.DummyRequest()
     request.errors = Errors()
     request.registry = self.config.registry
     apply_request_extensions(request)
     view = self._cut(request)
     view.get_json_appstruct()
     self.assertEqual(len(request.errors), 1)
Exemple #4
0
def catch_all_error_handler(exc, request):
    log.exception('Unexpected error: {}'.format(exc))

    show_debugger_for_errors = \
        request.registry.settings.get('show_debugger_for_errors', '')
    if show_debugger_for_errors == 'true':
        raise exc

    request.errors = Errors(500)
    request.errors.add('body', 'unexpected error',
                       'please consult the server logs')

    return json_error(request)
Exemple #5
0
def http_error_handler(exc, request):
    """In case of a HTTP error, return the error details as JSON, e.g.:

        {
            "status": "error",
            "errors": [
                {
                    "location": "request",
                    "name": "Not Found",
                    "description": "document not found"
                }
            ]
        }
    """
    if isinstance(exc, _JSONError):
        # if it is an error from Cornice, just return it
        return exc

    errors = Errors(request, exc.code)
    errors.add('request', exc.title, exc.detail)

    return json_error(errors)
Exemple #6
0
def http_error_handler(exc, request):
    """In case of a HTTP error, return the error details as JSON, e.g.:

        {
            "status": "error",
            "errors": [
                {
                    "location": "request",
                    "name": "Not Found",
                    "description": "document not found"
                }
            ]
        }
    """
    if isinstance(exc, _JSONError):
        # if it is an error from Cornice, just return it
        return exc

    errors = Errors(request, exc.code)
    errors.add('request', exc.title, exc.detail)

    return json_error(errors)
Exemple #7
0
        def inner(request):
            if not hasattr(request, 'validated'):
                setattr(request, 'validated', {})
            if not hasattr(request, 'errors'):
                setattr(request, 'errors', Errors())
            if not hasattr(request, 'info'):
                setattr(request, 'info', {})

            for validator in self.validators:
                validator(request=request)

            if len(request.errors) > 0:
                return json_error(request)
            return func(request)
Exemple #8
0
    def test_valid_user_as_author_of_argument(self):
        request = construct_dummy_request()
        response = user.valid_user_as_author_of_argument(request)
        self.assertFalse(response)

        for id in ['', 'hello', 'anonymous']:
            self.config.testing_securitypolicy(userid=id, permissive=True)
            for el in ['', 'a', '0', '1', 1]:
                request = testing.DummyRequest(json_body={'uid': el})
                request.validated = {}
                setattr(request, 'errors', Errors())
                response = user.valid_user_as_author_of_argument(request)
                self.assertIsInstance(response, bool)
                if id == 'anonymous' and el in ['1', 1]:
                    self.assertTrue(response)
                else:
                    self.assertFalse(response)
Exemple #9
0
def construct_dummy_request(json_body: dict = None,
                            match_dict: dict = None) -> DummyRequest:
    """
    Creates a Dummy-Request. Optionally takes a json_body, which can directly be injected into the request.

    :param json_body: dict
    :param match_dict: dict
    :return: DummyRequest
    :rtype: DummyRequest
    """
    if json_body is None:
        json_body = dict()
    if match_dict is None:
        match_dict = dict()
    return DummyRequest(json_body=json_body,
                        matchdict=match_dict,
                        validated={},
                        errors=Errors(),
                        mailer=DummyMailer,
                        cookies={'_LOCALE_': 'en'})
Exemple #10
0
def construct_dummy_request(validated: Dict = None, json_body: Dict = None, **kwargs) -> DummyRequest:
    """
    Creates a Dummy-Request prepared with everything needed to run D-BAS tests.
    Optionally takes the same parameters as DummyRequest, which are directly passed to the DummyRequest.

    :return: DummyRequest
    """
    # environ, headers, cookies, params and path can be None. See
    # https://docs.pylonsproject.org/projects/pyramid/en/latest/_modules/pyramid/testing.html#DummyRequest for details.
    # Only pass parameters that are explicitly given.
    validated = validated if validated is not None else {}
    json_body = json_body if json_body is not None else {}

    dummy_request = DummyRequest(errors=Errors(), mailer=DummyMailer, cookies={'_LOCALE_': 'en'}, validated=validated,
                                 decorated={'extras': {}}, json_body=json_body, **kwargs)

    if dummy_request.registry.settings:
        dummy_request.registry.settings.update(_settings_dict_for_tests())
    else:
        dummy_request.registry.settings = _settings_dict_for_tests()

    return dummy_request
Exemple #11
0
def account_error_handler(exc, request):
    request.errors = Errors(400)
    request.errors.add('body', 'Error', exc.args)

    return json_error(request)
Exemple #12
0
def account_error_handler(exc, request):
    errors = Errors(request, 400)
    errors.add('request', 'Error', exc.args)

    return json_error(errors)