Exemple #1
0
    def test_parse_message_bad_content_type_should_warn(self):
        # setup
        encoded_payload = json.dumps(self.payload).encode()
        properties = MessageProperties(content_type=self.bad_content_type)
        message = Message(
            body=encoded_payload,
            properties=properties,
            annotations={
                common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode()
            },
        )
        args = CommonParserArguments(content_type="application/json")
        parser = common_parser.CommonParser(message=message,
                                            common_parser_args=args)

        # act
        parsed_msg = parser.parse_message()

        # verify
        assert parsed_msg["event"]["payload"] == self.payload

        expected_details_1 = strings.invalid_encoding_none_found()
        expected_details_2 = strings.content_type_mismatch(
            self.bad_content_type, "application/json")
        _validate_issues(
            parser,
            Severity.warning,
            2,
            2,
            [expected_details_1, expected_details_2],
        )
Exemple #2
0
    def test_parse_bad_type_and_bad_payload_should_error(self):
        # setup
        encoded_payload = self.bad_payload.encode()
        properties = MessageProperties(content_type=self.bad_content_type,
                                       content_encoding=self.encoding)
        message = Message(
            body=encoded_payload,
            properties=properties,
            annotations={
                common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode()
            },
        )
        args = CommonParserArguments(content_type="application/json")
        parser = common_parser.CommonParser(message=message,
                                            common_parser_args=args)

        # act
        parsed_msg = parser.parse_message()

        # verify
        # since the content_encoding header is not present, just dump the raw payload
        payload = str(encoded_payload, "utf8")
        assert parsed_msg["event"]["payload"] == payload

        expected_details_1 = strings.content_type_mismatch(
            self.bad_content_type, "application/json")
        _validate_issues(parser, Severity.warning, 2, 1, [expected_details_1])

        expected_details_2 = strings.invalid_json()
        _validate_issues(parser, Severity.error, 2, 1, [expected_details_2])
Exemple #3
0
    def test_parse_message_should_succeed(
        self,
        device_id,
        encoding,
        content_type,
        interface_name,
        payload,
        properties,
        app_properties,
        module_id,
    ):
        # setup
        properties = MessageProperties(content_encoding=encoding,
                                       content_type=content_type)
        message = Message(
            body=json.dumps(payload).encode(),
            properties=properties,
            annotations={
                common_parser.DEVICE_ID_IDENTIFIER: device_id.encode(),
                common_parser.INTERFACE_NAME_IDENTIFIER:
                interface_name.encode(),
                common_parser.MODULE_ID_IDENTIFIER: module_id.encode(),
            },
            application_properties=_encode_app_props(app_properties),
        )
        args = CommonParserArguments(properties=["all"],
                                     content_type=content_type)
        parser = common_parser.CommonParser(message=message,
                                            common_parser_args=args)

        # act
        parsed_msg = parser.parse_message()

        # verify
        assert parsed_msg["event"]["payload"] == payload
        assert parsed_msg["event"]["origin"] == device_id
        device_identifier = str(common_parser.DEVICE_ID_IDENTIFIER, "utf8")
        assert parsed_msg["event"]["annotations"][
            device_identifier] == device_id
        module_identifier = str(common_parser.MODULE_ID_IDENTIFIER, "utf8")
        if module_id:
            assert parsed_msg["event"]["annotations"][
                module_identifier] == module_id
        else:
            assert not parsed_msg["event"]["annotations"].get(
                module_identifier)
        properties = parsed_msg["event"]["properties"]
        assert properties["system"]["content_encoding"] == encoding
        assert properties["system"]["content_type"] == content_type
        assert properties["application"] == app_properties

        assert len(parser.issues_handler.get_all_issues()) == 0
Exemple #4
0
    def test_parse_message_bad_encoding_should_warn(self):
        # setup
        properties = MessageProperties(content_encoding=self.bad_encoding,
                                       content_type=self.content_type)
        message = Message(
            body=json.dumps(self.payload).encode(self.bad_encoding),
            properties=properties,
            annotations={
                common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode()
            },
        )
        args = CommonParserArguments()
        parser = common_parser.CommonParser(message=message,
                                            common_parser_args=args)

        # act
        parser.parse_message()

        expected_details = strings.invalid_encoding(self.bad_encoding)
        _validate_issues(parser, Severity.warning, 1, 1, [expected_details])
Exemple #5
0
    def test_parse_message_bad_json_should_fail(self):
        # setup
        properties = MessageProperties(content_encoding=self.encoding,
                                       content_type=self.content_type)
        message = Message(
            body=self.bad_payload.encode(),
            properties=properties,
            annotations={
                common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode()
            },
        )
        args = CommonParserArguments()
        parser = common_parser.CommonParser(message=message,
                                            common_parser_args=args)

        # act
        parsed_msg = parser.parse_message()

        # verify
        # parsing should attempt to place raw payload into result even if parsing fails
        assert parsed_msg["event"]["payload"] == self.bad_payload

        expected_details = strings.invalid_json()
        _validate_issues(parser, Severity.error, 1, 1, [expected_details])