def test_can_get_value_from_dict_with_jwt_by_path(self):
     path = "/a/b[jwt]/sub"
     dictionary = {
         "a": {
             "b": TEST_JWT
         }
     }
     param = Parameter(path, 'event')
     response = param.extract_validated_value(dictionary)
     self.assertEqual("aadd1e0e-5807-4763-b1e8-5823bf631bb6", response)
 def test_can_get_value_from_dict_with_json_by_path(self):
     path = "/a/b[json]/c"
     dictionary = {
         "a": {
             "b": '{ "c": "hello" }',
             "c": "bye"
         }
     }
     param = Parameter(path, 'event')
     response = param.extract_validated_value(dictionary)
     self.assertEqual("hello", response)
 def test_can_get_dict_value_from_dict_by_path(self):
     path = "/a/b"
     dictionary = {
         "a": {
             "b": {
                 "c": "hello"
             }
         }
     }
     param = Parameter(path)
     response = param.extract_validated_value(dictionary)
     self.assertEqual({"c": "hello"}, response)
    def test_raises_decode_error_convert_json_string_to_dict(self):
        path = "/a/b[json]/c"
        dictionary = {
            "a": {
                "b": "{ 'c': 'hello' }",
                "c": "bye"
            }
        }
        param = Parameter(path)
        with self.assertRaises(JSONDecodeError) as context:
            param.extract_validated_value(dictionary)

        self.assertTrue("Expecting property name enclosed in double quotes" in context.exception.msg)
    def test_extract_returns_400_on_invalid_dictionary_schema(self):
        path = "/a"
        dictionary = {
            "a": {
                "b": {
                    "c": 3
                }
            }
        }

        schema = Schema(
            {
                "b": And(dict, {
                    "c": str
                })
            }
        )

        @extract([Parameter(path, 'event', validators=[SchemaValidator(schema)])])
        def handler(event, context, c=None):  # noqa
            return {}

        response = handler(dictionary, None)

        self.assertEqual(400, response["statusCode"])
        self.assertEqual('{"message": "Error extracting parameters"}', response["body"])
    def test_extract_valid_dictionary_schema(self):
        path = "/a"
        dictionary = {
            "a": {
                "b": {
                    "c": "d"
                }
            }
        }

        schema = Schema(
            {
                "b": And(dict, {
                    "c": str
                }),
                Optional("j"): str
            }
        )

        @extract([Parameter(path, 'event', validators=[SchemaValidator(schema)])])
        def handler(event, context, a=None):  # noqa
            return a

        response = handler(dictionary, None)

        expected = {
            "b": {
                "c": "d"
            }
        }
        self.assertEqual(expected, response)
Beispiel #7
0
    def test_extracts_from_list_by_index_annotation_successfully(self):
        path = "/a/b[1]/c"
        dictionary = {"a": {"b": [{"c": 2}, {"c": 3}]}}

        @extract([Parameter(path, 'event')])
        def handler(event, context, c=None):  # noqa
            return c

        response = handler(dictionary, None)

        self.assertEqual(3, response)
Beispiel #8
0
    def test_extract_multiple_parameters_from_json_hits_cache(self):
        dictionary = {"a": json.dumps({"b": 123, "c": 456})}

        initial_cache_info = decode_json.cache_info()

        @extract([
            Parameter("a[json]/b", "event", var_name="b"),
            Parameter("a[json]/c", "event", var_name="c")
        ])  # noqa: pylint - invalid-name
        def handler(event, b=None, c=None):  # noqa: pylint - unused-argument
            return {}

        handler(dictionary)

        self.assertEqual(decode_json.cache_info().hits,
                         initial_cache_info.hits + 1)
        self.assertEqual(decode_json.cache_info().misses,
                         initial_cache_info.misses + 1)
        self.assertEqual(decode_json.cache_info().currsize,
                         initial_cache_info.currsize + 1)
Beispiel #9
0
    def test_extract_multiple_parameters_from_jwt_hits_cache(self):
        dictionary = {"a": TEST_JWT}

        initial_cache_info = decode_jwt.cache_info()

        @extract([
            Parameter("a[jwt]/sub", "event", var_name="sub"),
            Parameter("a[jwt]/aud", "event", var_name="aud")
        ])
        def handler(event,
                    sub=None,
                    aud=None):  # noqa: pylint - unused-argument
            return {}

        handler(dictionary)

        self.assertEqual(decode_jwt.cache_info().hits,
                         initial_cache_info.hits + 1)
        self.assertEqual(decode_jwt.cache_info().misses,
                         initial_cache_info.misses + 1)
        self.assertEqual(decode_jwt.cache_info().currsize,
                         initial_cache_info.currsize + 1)
    def test_can_add_name_to_parameter(self):
        path = "/a/b"
        dictionary = {
            "a": {
                "b": "hello"
            }
        }

        @extract([Parameter(path, 'event', validators=[Mandatory()], var_name='custom')])
        def handler(event, context, custom=None):  # noqa
            return custom

        response = handler(dictionary, None)

        self.assertEqual("hello", response)
    def test_extract_from_event_calls_function_with_extra_kwargs(self):
        path = "/a/b/c"
        dictionary = {
            "a": {
                "b": {
                    "c": "hello"
                }
            }
        }

        @extract_from_event([Parameter(path)])
        def handler(event, context, c=None):  # noqa
            return c

        self.assertEqual(handler(dictionary, None), "hello")
    def test_extract_from_event_calls_function_with_extra_kwargs_bool_false(self):
        path = "/a/b/c"
        dictionary = {
            "a": {
                "b": {
                    "c": False
                }
            }
        }

        @extract_from_event([Parameter(path)])
        def handler(event, context, c=None):  # noqa
            return c

        self.assertFalse(handler(dictionary, None) is None)
        self.assertEqual(False, handler(dictionary, None))
    def test_extract_does_not_raise_an_error_on_missing_optional_key(self):
        path = "/a/b/c"
        dictionary = {
            "a": {
                "b": {
                }
            }
        }

        @extract([Parameter(path, 'event')])
        def handler(event, context, c=None):  # noqa
            return {}

        response = handler(dictionary, None)

        self.assertEqual({}, response)
    def test_extract_returns_400_on_empty_path(self):
        path = None
        dictionary = {
            "a": {
                "b": {
                }
            }
        }

        @extract([Parameter(path, 'event')])
        def handler(event, context, c=None):  # noqa
            return {}

        response = handler(dictionary, None)

        self.assertEqual(400, response["statusCode"])
        self.assertEqual('{"message": "Error extracting parameters"}', response["body"])
Beispiel #15
0
    def test_extract_returns_400_on_jwt_decode_error(self, mock_logger):
        path = "/a/b[jwt]/c"
        dictionary = {"a": {"b": "wrong.jwt"}}

        @extract([Parameter(path, "event")])
        def handler(event, context, c=None):  # noqa
            return {}

        response = handler(dictionary, None)

        self.assertEqual(400, response["statusCode"])
        self.assertTrue("{\"message\": \"Error extracting parameters\"}" in
                        response["body"])

        mock_logger.error.assert_called_once_with(
            "%s: %s in argument %s for path %s", "jwt.exceptions.DecodeError",
            "Not enough segments", "event", "/a/b[jwt]/c")
    def test_extract_returns_400_on_missing_mandatory_key(self):
        path = "/a/b/c"
        dictionary = {
            "a": {
                "b": {
                }
            }
        }

        @extract([Parameter(path, 'event', validators=[Mandatory()])])
        def handler(event, context, c=None):  # noqa
            return {}

        response = handler(dictionary, None)

        self.assertEqual(400, response["statusCode"])
        self.assertEqual('{"message": "Error extracting parameters"}', response["body"])
Beispiel #17
0
    def test_extracts_from_list_by_index_out_of_range_fails_with_400(
            self, mock_logger):
        path = "/a/b[4]/c"
        dictionary = {"a": {"b": [{"c": 2}, {"c": 3}]}}

        @extract([Parameter(path, 'event')])
        def handler(event, context, c=None):  # noqa
            return c

        response = handler(dictionary, None)

        self.assertEqual(400, response["statusCode"])  # noqa
        self.assertTrue('{"message": "Error extracting parameters"}'
                        in response["body"])  # noqa

        mock_logger.error.assert_called_once_with(
            "%s: '%s' in argument %s for path %s", 'IndexError',
            'list index out of range', 'event', '/a/b[4]/c')
    def test_extract_returns_400_on_type_error(self):
        path = "/a/b[json]/c"
        dictionary = {
            "a": {
                "b": {
                    "c": "hello"
                }
            }
        }

        @extract([Parameter(path)])
        def handler(event, context, c=None):  # noqa
            return {}

        response = handler(dictionary, None)

        self.assertEqual(400, response["statusCode"])
        self.assertEqual('{"message": "Error extracting parameters"}', response["body"])
    def test_extract_does_not_raise_an_error_on_valid_regex_key(self):
        path = "/a/b/c"
        dictionary = {
            "a": {
                "b": {
                    "c": "2019"
                }
            }
        }

        #  Expect a number
        @extract([Parameter(path, 'event', [RegexValidator(r'\d+')])])
        def handler(event, context, c=None):  # noqa
            return {}

        response = handler(dictionary, None)

        self.assertEqual({}, response)
    def test_extract_returns_400_on_invalid_regex_key(self):
        path = "/a/b/c"
        dictionary = {
            "a": {
                "b": {
                    "c": "hello"
                }
            }
        }

        #  Expect a number
        @extract([Parameter(path, 'event', [RegexValidator(r'\d+')])])
        def handler(event, context, c=None):  # noqa
            return {}

        response = handler(dictionary, None)
        self.assertEqual(400, response["statusCode"])
        self.assertEqual('{"message": "Error extracting parameters"}', response["body"])
Beispiel #21
0
    def test_extract_returns_400_on_json_decode_error(self, mock_logger):
        path = "/a/b[json]/c"
        dictionary = {"a": {"b": "{'c'}"}}

        @extract([Parameter(path, 'event')])
        def handler(event, context, c=None):  # noqa
            return {}

        response = handler(dictionary, None)

        self.assertEqual(400, response["statusCode"])
        self.assertEqual('{"message": "Error extracting parameters"}',
                         response["body"])

        mock_logger.error.assert_called_once_with(
            "%s: '%s' in argument %s for path %s",
            'json.decoder.JSONDecodeError',
            'Expecting property name enclosed in double quotes: line 1 column 2 '
            '(char 1)', 'event', '/a/b[json]/c')
    def test_can_not_add_pythonic_keyword_as_name_to_parameter(self, mock_logger):
        path = "/a/b"
        dictionary = {
            "a": {
                "b": "hello"
            }
        }

        @extract_from_event([Parameter(path, validators=[Mandatory()], var_name='class')])
        def handler(event, context):  # noqa
            return {}

        response = handler(dictionary, None)

        self.assertEqual(400, response["statusCode"])
        self.assertEqual('{"message": "Error extracting parameters"}', response["body"])

        mock_logger.error.assert_called_once_with("%s: '%s' in argument %s for path %s",
                                                  'SyntaxError',
                                                  'class',
                                                  'event',
                                                  '/a/b')
Beispiel #23
0
 def test_annotations_from_key_returns_annotation(self):
     key = 'simple[annotation]'
     response = Parameter.get_annotations_from_key(key)
     self.assertTrue(response[0] == 'simple')
     self.assertTrue(response[1] == 'annotation')
 def test_annotations_from_key_returns_annotation(self):
     key = "simple[annotation]"
     response = Parameter.get_annotations_from_key(key)
     self.assertTrue(response[0] == "simple")
     self.assertTrue(response[1] == "annotation")
 def test_annotations_from_key_returns_none_when_no_annotations(self):
     key = 'simple'
     response = Parameter.get_annotations_from_key(key)
     self.assertTrue(response[0] == 'simple')
     self.assertTrue(response[1] is None)