Skip to content

Commit

Permalink
chore: Apply black
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkheir committed Jan 29, 2024
1 parent 672ccf6 commit eeb78ca
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 86 deletions.
1 change: 1 addition & 0 deletions sekoia_automation/aio/connector.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/aio/helpers/http/test_http_client_session.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for sekoia_automation.helpers.aio.http.http_client."""

import time

import pytest
Expand Down
1 change: 1 addition & 0 deletions tests/aio/helpers/http/test_http_token_refresher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for `sekoia_automation.aio.helpers.http.token_refresher`."""

import asyncio
import time
from asyncio import Lock
Expand Down
1 change: 1 addition & 0 deletions tests/aio/helpers/http/test_http_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for `sekoia_automation.aio.helpers.http.utils`."""

import os

import aiofiles
Expand Down
11 changes: 7 additions & 4 deletions tests/aio/test_connector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test async connector."""

from unittest.mock import Mock, patch
from urllib.parse import urljoin

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
37 changes: 20 additions & 17 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 4 additions & 3 deletions tests/connectors/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tests/loguru/test_loguru_formatters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Formatters tests."""

import pytest

from sekoia_automation.loguru.formatters import format_record
Expand Down
1 change: 1 addition & 0 deletions tests/loguru/test_loguru_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests related to logging."""

import logging
from logging import LogRecord

Expand Down
35 changes: 20 additions & 15 deletions tests/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]
57 changes: 33 additions & 24 deletions tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand All @@ -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"}
Expand Down
5 changes: 3 additions & 2 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 30 additions & 21 deletions tests/test_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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})

Expand Down

0 comments on commit eeb78ca

Please sign in to comment.