Exemple #1
0
    def test_path_prefix(self):
        auth = ChannelAuth(self.url, self.username, self.password)

        with patch("requests.Session") as session:
            session.return_value = MagicMock()  # self._session
            session.return_value.request = MagicMock()

            create_mock = MagicMock()
            create_mock.status_code = 200
            create_mock.json = MagicMock(
                return_value={"consumerInstanceId": 1234})

            produce_mock = MagicMock()
            produce_mock.status_code = 204

            session.return_value.request.side_effect = [
                create_mock, produce_mock
            ]

            channel = Channel(self.url,
                              auth=auth,
                              consumer_group=self.consumer_group,
                              path_prefix="/base-path",
                              retry_on_fail=False)

            channel.create()

            session.return_value.request.assert_called_with(
                "post",
                "http://localhost/base-path/consumers",
                json={
                    "consumerGroup": self.consumer_group,
                    "configs": {
                        "auto.offset.reset": "latest",
                        "enable.auto.commit": "false"
                    }
                })

            channel.produce({})

            session.return_value.request.assert_called_with(
                "post",
                "http://localhost/base-path/produce",
                json={},
                headers={"Content-Type": _PRODUCE_CONTENT_TYPE})

            session.return_value.request.reset_mock()
            session.return_value.request.side_effect = [
                create_mock, produce_mock
            ]

            channel = Channel(self.url,
                              auth=auth,
                              consumer_group=self.consumer_group,
                              consumer_path_prefix="/custom-consumer-path",
                              producer_path_prefix="/custom-producer-path",
                              retry_on_fail=False)

            channel.create()

            session.return_value.request.assert_called_with(
                "post",
                "http://localhost/custom-consumer-path/consumers",
                json={
                    "consumerGroup": self.consumer_group,
                    "configs": {
                        "auto.offset.reset": "latest",
                        "enable.auto.commit": "false"
                    }
                })

            channel.produce({})

            session.return_value.request.assert_called_with(
                "post",
                "http://localhost/custom-producer-path/produce",
                json={},
                headers={"Content-Type": _PRODUCE_CONTENT_TYPE})
Exemple #2
0
    def test_main(self):
        auth = ChannelAuth(self.url, self.username, self.password)

        case_event = {
            "id": "a45a03de-5c3d-452a-8a37-f68be954e784",
            "entity": "case",
            "type": "creation",
            "tenant-id": "7af4746a-63be-45d8-9fb5-5f58bf909c25",
            "user": "******",
            "origin": "",
            "nature": "",
            "timestamp": "",
            "transaction-id": "",
            "case": {
                "id": "c00547df-6d74-4833-95ad-3a377c7274a6",
                "name": "A great case full of malware",
                "url": "https://mycaseserver.com/#/cases"
                "/4e8e23f4-9fe9-4215-92c9-12c9672be9f1",
                "priority": "Low"
            }
        }

        with patch("requests.Session") as session:
            session.return_value = MagicMock()  # self._session
            session.return_value.request = MagicMock()

            create_mock = MagicMock()
            create_mock.status_code = 200
            create_mock.json = MagicMock(
                return_value={"consumerInstanceId": 1234})

            subscr_mock = MagicMock()
            subscr_mock.status_code = 204

            consum_mock = MagicMock()
            consum_mock.status_code = 200
            consum_mock.json = MagicMock(return_value=create_records([
                create_record("foo-topic", case_event, partition=1, offset=1)
            ]))

            commit_consumer_error_mock = MagicMock()
            commit_consumer_error_mock.status_code = 404
            commit_error_mock = MagicMock()
            commit_error_mock.status_code = 500
            commit_mock = MagicMock()
            commit_mock.status_code = 204

            produce_mock = MagicMock()
            produce_mock.status_code = 204

            delete_mock = MagicMock()
            delete_mock.status_code = 204
            delete_404_mock = MagicMock()
            delete_404_mock.status_code = 404
            delete_500_mock = MagicMock()
            delete_500_mock.status_code = 500

            session.return_value.request.side_effect = [
                create_mock, subscr_mock, consum_mock,
                commit_consumer_error_mock, commit_error_mock, commit_mock,
                produce_mock, delete_500_mock, delete_404_mock, delete_mock
            ]

            channel = Channel(self.url,
                              auth=auth,
                              consumer_group=self.consumer_group,
                              retry_on_fail=False,
                              verify_cert_bundle="cabundle.crt",
                              request_timeout=70,
                              session_timeout=60,
                              offset="earliest",
                              extra_configs={
                                  "enable.auto.commit": "true",
                                  "one.extra.setting": "one extra value",
                                  "another.extra.setting": 42
                              })

            self.assertEqual(channel._session.verify, "cabundle.crt")

            channel.commit()  # forcing early exit due to no records to commit

            channel.create()
            session.return_value.request.assert_called_with(
                "post",
                "http://localhost/databus/consumer-service/v1/consumers",
                json={
                    "consumerGroup": self.consumer_group,
                    "configs": {
                        "request.timeout.ms": "70000",
                        "session.timeout.ms": "60000",
                        "enable.auto.commit": "true",
                        "auto.offset.reset": "earliest",
                        "one.extra.setting": "one extra value",
                        "another.extra.setting": 42
                    }
                })

            channel.subscribe(["topic1", "topic2"])
            session.return_value.request.assert_called_with(
                "post",
                "http://localhost/databus/consumer-service/v1/consumers/1234/subscription",
                json={"topics": ["topic1", "topic2"]})

            records = channel.consume()
            self.assertEqual(records[0]["id"],
                             "a45a03de-5c3d-452a-8a37-f68be954e784")

            with self.assertRaises(ConsumerError):
                channel.commit()
            with self.assertRaises(TemporaryError):
                channel.commit()

            channel.commit()

            message_payload = {"detail": "Hello from OpenDXL"}

            produce_payload = {
                "records": [{
                    "routingData": {
                        "topic": "topic1",
                        "shardingKey": ""
                    },
                    "message": {
                        "headers": {},
                        "payload":
                        base64.b64encode(
                            json.dumps(message_payload).encode()).decode()
                    }
                }]
            }

            channel.produce(produce_payload)

            session.return_value.request.assert_called_with(
                "post",
                "http://localhost/databus/cloudproxy/v1/produce",
                json=produce_payload,
                headers={"Content-Type": _PRODUCE_CONTENT_TYPE})

            with self.assertRaises(TemporaryError):
                channel.delete()  # trigger 500
                session.return_value.request.assert_called_with(
                    "delete",
                    "http://localhost/databus/consumer-service/v1/consumers/1234"
                )
                session.return_value.request.reset_mock()

            channel.delete()  # trigger silent 404
            session.return_value.request.assert_called_with(
                "delete",
                "http://localhost/databus/consumer-service/v1/consumers/1234")
            session.return_value.request.reset_mock()

            channel._consumer_id = "1234"  # resetting consumer
            channel.delete()  # Proper deletion
            session.return_value.request.assert_called_with(
                "delete",
                "http://localhost/databus/consumer-service/v1/consumers/1234")
            session.return_value.request.reset_mock()

            channel.delete()  # trigger early exit