From ae69653cff521dd8ee77db2fb4b6c23b98741a50 Mon Sep 17 00:00:00 2001 From: Raphael Cohen Date: Fri, 3 Nov 2023 10:59:04 +0100 Subject: [PATCH] fix: Fixes batch url computation --- CHANGELOG.md | 4 ++++ sekoia_automation/connector/__init__.py | 4 ++-- tests/connectors/test_connector.py | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0789da..5bfbef2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fixes batch url computation + ## [1.6.0] - 2023-10-20 ### Added diff --git a/sekoia_automation/connector/__init__.py b/sekoia_automation/connector/__init__.py index ac5a3be..5fb55e5 100644 --- a/sekoia_automation/connector/__init__.py +++ b/sekoia_automation/connector/__init__.py @@ -6,8 +6,8 @@ from concurrent.futures import wait as wait_futures from datetime import datetime, time from functools import cached_property +from os.path import join as urljoin from typing import Any -from urllib.parse import urljoin import orjson import requests @@ -150,7 +150,7 @@ def push_events_to_intakes( self._error_count = 0 self._last_events_time = datetime.utcnow() intake_host = os.getenv("INTAKE_URL", self.configuration.intake_server) - batch_api = urljoin(intake_host, "/batch") + batch_api = urljoin(intake_host, "batch") # Dict to collect event_ids for the API collect_ids: dict[int, list] = {} diff --git a/tests/connectors/test_connector.py b/tests/connectors/test_connector.py index d027aa4..67c49bd 100644 --- a/tests/connectors/test_connector.py +++ b/tests/connectors/test_connector.py @@ -1,3 +1,4 @@ +import os from unittest.mock import Mock, PropertyMock, patch import pytest @@ -124,6 +125,27 @@ def test_push_event_to_intake_with_chunks(test_connector, mocked_trigger_logs): assert result == ["001", "002", "003", "004"] +def test_push_event_to_intake_custom_url(test_connector, mocked_trigger_logs): + url = "https://fra2.app.sekoia.io/v1/intake-http/batch" + batch_mock = mocked_trigger_logs.post( + url, json={"event_ids": ["001"]}, additional_matcher=match_events("foo") + ) + # With trailing slash + with patch.dict( + os.environ, {"INTAKE_URL": "https://fra2.app.sekoia.io/v1/intake-http/"} + ): + test_connector.push_events_to_intakes(["foo"]) + assert batch_mock.call_count == 1 + + # Without trailing slash + mocked_trigger_logs.reset_mock() + with patch.dict( + os.environ, {"INTAKE_URL": "https://fra2.app.sekoia.io/v1/intake-http"} + ): + test_connector.push_events_to_intakes(["foo"]) + assert batch_mock.call_count == 1 + + def test_push_event_to_intake_with_chunks_executor_stopped( test_connector, mocked_trigger_logs ):