From eeb78ca1bf031e6f9cffcdbe448f40f70e5a760b Mon Sep 17 00:00:00 2001 From: Raphael Cohen Date: Mon, 29 Jan 2024 17:45:48 +0100 Subject: [PATCH] chore: Apply black --- sekoia_automation/aio/connector.py | 1 + .../helpers/http/test_http_client_session.py | 1 + .../helpers/http/test_http_token_refresher.py | 1 + tests/aio/helpers/http/test_http_utils.py | 1 + tests/aio/test_connector.py | 11 ++-- tests/conftest.py | 37 ++++++------ tests/connectors/test_connector.py | 7 ++- tests/loguru/test_loguru_formatters.py | 1 + tests/loguru/test_loguru_handler.py | 1 + tests/test_action.py | 35 +++++++----- tests/test_module.py | 57 +++++++++++-------- tests/test_storage.py | 5 +- tests/test_trigger.py | 51 ++++++++++------- 13 files changed, 123 insertions(+), 86 deletions(-) diff --git a/sekoia_automation/aio/connector.py b/sekoia_automation/aio/connector.py index 05dee51..a8a3152 100644 --- a/sekoia_automation/aio/connector.py +++ b/sekoia_automation/aio/connector.py @@ -1,4 +1,5 @@ """Contains connector with async version.""" + from abc import ABC from asyncio import AbstractEventLoop, get_event_loop from collections.abc import AsyncGenerator diff --git a/tests/aio/helpers/http/test_http_client_session.py b/tests/aio/helpers/http/test_http_client_session.py index b2dc26f..8436dfc 100644 --- a/tests/aio/helpers/http/test_http_client_session.py +++ b/tests/aio/helpers/http/test_http_client_session.py @@ -1,4 +1,5 @@ """Tests for sekoia_automation.helpers.aio.http.http_client.""" + import time import pytest diff --git a/tests/aio/helpers/http/test_http_token_refresher.py b/tests/aio/helpers/http/test_http_token_refresher.py index 7289ec7..fbb3fd2 100644 --- a/tests/aio/helpers/http/test_http_token_refresher.py +++ b/tests/aio/helpers/http/test_http_token_refresher.py @@ -1,4 +1,5 @@ """Tests for `sekoia_automation.aio.helpers.http.token_refresher`.""" + import asyncio import time from asyncio import Lock diff --git a/tests/aio/helpers/http/test_http_utils.py b/tests/aio/helpers/http/test_http_utils.py index 3f811c7..b890a66 100644 --- a/tests/aio/helpers/http/test_http_utils.py +++ b/tests/aio/helpers/http/test_http_utils.py @@ -1,4 +1,5 @@ """Tests for `sekoia_automation.aio.helpers.http.utils`.""" + import os import aiofiles diff --git a/tests/aio/test_connector.py b/tests/aio/test_connector.py index 1fdcd61..14c9e2d 100644 --- a/tests/aio/test_connector.py +++ b/tests/aio/test_connector.py @@ -1,4 +1,5 @@ """Test async connector.""" + from unittest.mock import Mock, patch from urllib.parse import urljoin @@ -156,8 +157,9 @@ async def test_async_connector_push_multiple_events( request_url = urljoin(async_connector.configuration.intake_server, "batch") - with aioresponses() as mocked_responses, patch( - "sekoia_automation.connector.CHUNK_BYTES_MAX_SIZE", 128 + with ( + aioresponses() as mocked_responses, + patch("sekoia_automation.connector.CHUNK_BYTES_MAX_SIZE", 128), ): for _ in range(100): mocked_responses.post( @@ -200,8 +202,9 @@ async def test_async_connector_raise_error( request_url = urljoin(async_connector.configuration.intake_server, "batch") - with aioresponses() as mocked_responses, patch( - "sekoia_automation.connector.CHUNK_BYTES_MAX_SIZE", 128 + with ( + aioresponses() as mocked_responses, + patch("sekoia_automation.connector.CHUNK_BYTES_MAX_SIZE", 128), ): for _ in range(2): mocked_responses.post( diff --git a/tests/conftest.py b/tests/conftest.py index 2f548fd..8897329 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -76,23 +76,26 @@ def tls_storage(): @pytest.fixture def mocked_trigger_logs(): - with patch.object( - Trigger, - "logs_url", - new_callable=PropertyMock, - return_value="http://sekoia-playbooks/logs", - ), patch.object( - Trigger, - "secrets_url", - new_callable=PropertyMock, - return_value="http://sekoia-playbooks/secrets", - ), patch.object( - Trigger, - "callback_url", - new_callable=PropertyMock, - return_value="http://sekoia-playbooks/callback", - ), patch.object( - Trigger, "token", return_value="secure_token" + with ( + patch.object( + Trigger, + "logs_url", + new_callable=PropertyMock, + return_value="http://sekoia-playbooks/logs", + ), + patch.object( + Trigger, + "secrets_url", + new_callable=PropertyMock, + return_value="http://sekoia-playbooks/secrets", + ), + patch.object( + Trigger, + "callback_url", + new_callable=PropertyMock, + return_value="http://sekoia-playbooks/callback", + ), + patch.object(Trigger, "token", return_value="secure_token"), ): with requests_mock.Mocker() as mock: mock.post("http://sekoia-playbooks/logs") diff --git a/tests/connectors/test_connector.py b/tests/connectors/test_connector.py index 7e85f1a..5ed868e 100644 --- a/tests/connectors/test_connector.py +++ b/tests/connectors/test_connector.py @@ -234,9 +234,10 @@ def test_query_exception_api(test_connector, requests_mock): def test_connector_configuration(test_connector): test_connector._configuration = None config = DefaultConnectorConfiguration(intake_key="foo") - with patch.object( - Module, "load_config", return_value=config - ) as mock_load_config, patch("sentry_sdk.set_context") as mock_set_sentry_context: + with ( + patch.object(Module, "load_config", return_value=config) as mock_load_config, + patch("sentry_sdk.set_context") as mock_set_sentry_context, + ): assert test_connector.configuration == config mock_set_sentry_context.assert_called_with( "connector_configuration", config.dict() diff --git a/tests/loguru/test_loguru_formatters.py b/tests/loguru/test_loguru_formatters.py index 7447419..ac38cb1 100644 --- a/tests/loguru/test_loguru_formatters.py +++ b/tests/loguru/test_loguru_formatters.py @@ -1,4 +1,5 @@ """Formatters tests.""" + import pytest from sekoia_automation.loguru.formatters import format_record diff --git a/tests/loguru/test_loguru_handler.py b/tests/loguru/test_loguru_handler.py index 3972959..996cd2b 100644 --- a/tests/loguru/test_loguru_handler.py +++ b/tests/loguru/test_loguru_handler.py @@ -1,4 +1,5 @@ """Tests related to logging.""" + import logging from logging import LogRecord diff --git a/tests/test_action.py b/tests/test_action.py index f14572b..4d47409 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -79,12 +79,10 @@ def run(self, arguments): with requests_mock.Mocker() as rmock: rmock.patch(FAKE_URL, json={"module_configuration": {"value": secret}}) - with patch.object( - Module, "manifest_secrets", return_value=["a_key"] - ), patch.object( - Module, "manifest_required_properties", return_value=[] - ), patch.object( - Module, "has_secrets", return_value=True + with ( + patch.object(Module, "manifest_secrets", return_value=["a_key"]), + patch.object(Module, "manifest_required_properties", return_value=[]), + patch.object(Module, "has_secrets", return_value=True), ): action.execute() @@ -128,9 +126,10 @@ class TestAction(Action): def run(self, arguments): raise NotImplementedError - with requests_mock.Mocker() as rmock, patch( - "sentry_sdk.capture_exception" - ) as sentry_patch: + with ( + requests_mock.Mocker() as rmock, + patch("sentry_sdk.capture_exception") as sentry_patch, + ): rmock.patch(FAKE_URL) action = TestAction() @@ -457,9 +456,12 @@ def __init__(self): action.module = DummyModule() - with patch.object( - Action, "callback_url", return_value=callback_url, new_callable=PropertyMock - ), patch.object(Module, "manifest_secrets", return_value=secret_key): + with ( + patch.object( + Action, "callback_url", return_value=callback_url, new_callable=PropertyMock + ), + patch.object(Module, "manifest_secrets", return_value=secret_key), + ): action.set_task_as_running() assert action.module.configuration == secrets @@ -489,8 +491,11 @@ def __init__(self): action.module = DummyModule() - with patch.object( - Action, "callback_url", return_value=callback_url, new_callable=PropertyMock - ), patch.object(Module, "manifest_secrets", return_value=secret_key): + with ( + patch.object( + Action, "callback_url", return_value=callback_url, new_callable=PropertyMock + ), + patch.object(Module, "manifest_secrets", return_value=secret_key), + ): action.set_task_as_running() assert getattr(action.module.configuration, secret_key) == secrets[secret_key] diff --git a/tests/test_module.py b/tests/test_module.py index fdac6e2..e1ed113 100644 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -140,14 +140,17 @@ def test_configuration_setter_add_secret_not_required(): secret_name = "foo" secret_val = "bar" - with patch.object( - Module, - "manifest_properties", - return_value=[secret_name], - ), patch.object( - Module, - "manifest_required_properties", - return_value=[], + with ( + patch.object( + Module, + "manifest_properties", + return_value=[secret_name], + ), + patch.object( + Module, + "manifest_required_properties", + return_value=[], + ), ): module.configuration = {secret_name: secret_val} assert module.configuration == {secret_name: secret_val} @@ -158,14 +161,17 @@ def test_configuration_setter_add_secret_required(): secret_name = "foo" secret_val = "bar" - with patch.object( - Module, - "manifest_properties", - return_value=[secret_name], - ), patch.object( - Module, - "manifest_required_properties", - return_value=[secret_name], + with ( + patch.object( + Module, + "manifest_properties", + return_value=[secret_name], + ), + patch.object( + Module, + "manifest_required_properties", + return_value=[secret_name], + ), ): module.configuration = {secret_name: secret_val} assert module.configuration == {secret_name: secret_val} @@ -175,14 +181,17 @@ def test_configuration_setter_missing_required_secret(): module = Module() secret_name = "foo" - with patch.object( - Module, - "manifest_properties", - return_value=[secret_name], - ), patch.object( - Module, - "manifest_required_properties", - return_value=[secret_name], + with ( + patch.object( + Module, + "manifest_properties", + return_value=[secret_name], + ), + patch.object( + Module, + "manifest_required_properties", + return_value=[secret_name], + ), ): with pytest.raises(expected_exception=ModuleConfigurationError): module.configuration = {"not a secret": "some value"} diff --git a/tests/test_storage.py b/tests/test_storage.py index 5e8c097..ca713cc 100644 --- a/tests/test_storage.py +++ b/tests/test_storage.py @@ -70,8 +70,9 @@ def test_write_json(storage): def test_get_data_path_for_local_storage(): mock_file = mock.mock_open(read_data="local") - with mock.patch.object(Path, "is_file", return_value=True), mock.patch.object( - Path, "open", mock_file + with ( + mock.patch.object(Path, "is_file", return_value=True), + mock.patch.object(Path, "open", mock_file), ): data_path = get_data_path() assert isinstance(data_path, PosixPath | WindowsPath) diff --git a/tests/test_trigger.py b/tests/test_trigger.py index b2d0ccb..00c1319 100644 --- a/tests/test_trigger.py +++ b/tests/test_trigger.py @@ -70,9 +70,10 @@ def test_logs_url(): def test_trigger_configuration(): trigger = DummyTrigger() - with patch.object( - Module, "load_config", return_value="trigger_conf" - ) as mock, patch("sentry_sdk.set_context") as sentry_patch: + with ( + patch.object(Module, "load_config", return_value="trigger_conf") as mock, + patch("sentry_sdk.set_context") as sentry_patch, + ): assert trigger.configuration == "trigger_conf" sentry_patch.assert_called_with("trigger_configuration", "trigger_conf") mock.assert_called_with(trigger.TRIGGER_CONFIGURATION_FILE_NAME, "json") @@ -81,9 +82,10 @@ def test_trigger_configuration(): def test_module_configuration(): trigger = DummyTrigger() module_conf = {"conf_key": "conf_val"} - with patch.object(Module, "load_config", return_value=module_conf) as mock, patch( - "sentry_sdk.set_context" - ) as sentry_patch: + with ( + patch.object(Module, "load_config", return_value=module_conf) as mock, + patch("sentry_sdk.set_context") as sentry_patch, + ): assert trigger.module.configuration == module_conf sentry_patch.assert_called_with("module_configuration", module_conf) mock.assert_called_with(Module.MODULE_CONFIGURATION_FILE_NAME, "json") @@ -170,9 +172,11 @@ def send_event_with_mock( mock.return_value, mock_2.return_value, ] # First call token and second url - with requests_mock.Mocker() as rmock, patch.object( - Path, "is_file", return_value=True - ), patch.object(Path, "open", mock): + with ( + requests_mock.Mocker() as rmock, + patch.object(Path, "is_file", return_value=True), + patch.object(Path, "open", mock), + ): rmock.post("http://sekoia-playbooksapi/endpoint") trigger.send_event(name, event, directory, remove_directory) @@ -360,8 +364,9 @@ def run(self): trigger = TestTrigger() trigger._STOP_EVENT_WAIT = 0.001 - with pytest.raises(SystemExit), patch.object( - Module, "load_config", return_value={} + with ( + pytest.raises(SystemExit), + patch.object(Module, "load_config", return_value={}), ): trigger.execute() @@ -391,8 +396,9 @@ def run(self): trigger._startup_time = datetime.datetime.utcnow() - timedelta(hours=1) trigger._last_events_time = datetime.datetime.utcnow() - timedelta(hours=5) trigger._STOP_EVENT_WAIT = 0.001 - with pytest.raises(SystemExit), patch.object( - Module, "load_config", return_value={} + with ( + pytest.raises(SystemExit), + patch.object(Module, "load_config", return_value={}), ): trigger.execute() @@ -430,14 +436,17 @@ def test_get_secrets(_, __, ___): trigger = ErrorTrigger(SampleModule()) trigger.ex = SystemExit - with requests_mock.Mocker() as rmock, patch.object( - Module, - "load_config", - return_value={ - "module_field": "foo", - "api_key": "encrypted", - "password": "secret", - }, + with ( + requests_mock.Mocker() as rmock, + patch.object( + Module, + "load_config", + return_value={ + "module_field": "foo", + "api_key": "encrypted", + "password": "secret", + }, + ), ): rmock.get("http://sekoia-playbooks/secrets", json={"value": TRIGGER_SECRETS})