Skip to content

Commit

Permalink
fix test mocking wip
Browse files Browse the repository at this point in the history
  • Loading branch information
a-leonardi authored and Darkheir committed Nov 4, 2024
1 parent bb4e012 commit b56591e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
23 changes: 11 additions & 12 deletions sekoia_automation/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def stop_monitoring(self):


class AccountValidator(ModuleItem):
VALIDATION_CALLBACK_URL_FILE_NAME = "validation_callback_url"
CALLBACK_URL_FILE_NAME = "validation_callback_url"

@abstractmethod
def validate(self) -> bool:
Expand All @@ -509,21 +509,20 @@ def validate(self) -> bool:
def execute(self):
"""Validates the account (module_configuration) of the module
and sends the result to Symphony."""
# Retrieve module's secrets
# Call the actual validation procedure
status = self.validate()

# Return result of validation to Symphony ; ask for module's secrets if needed
data = {"validation_status": status, "need_secrets": self.module.has_secrets()}

# Send request to Symphony
response = self._send_request(data, verb="PATCH")

# Set module's secrets if needed
if self.module.has_secrets():
response = self._send_request(
{"status": "running", "need_secrets": True}, verb="PATCH"
)
secrets = {
k: v
for k, v in response.json()["module_configuration"]["value"].items()
if k in self.module.manifest_secrets()
}
self.module.set_secrets(secrets)

# Call the actual validation procedure
status = self.validate()

# Return result of validation to Symphony
data = {"validation_status": status}
self._send_request(data)
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,9 @@ def session_faker(faker_locale: list[str], faker_seed: int) -> Faker:


class MockAccountValidator(AccountValidator):
def validate(self): ...
def __init__(self, mock_return_value: bool = True):
super().__init__()
self.mock_return_value = mock_return_value

def validate(self):
return self.mock_return_value
52 changes: 37 additions & 15 deletions tests/test_account_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import requests
import requests_mock

from sekoia_automation.module import AccountValidator
from tests.conftest import MockAccountValidator


Expand All @@ -17,41 +16,43 @@ def test_execute_success():
) as mock_load_config,
requests_mock.Mocker() as mock_request,
):
mock_request.post("http://example.com/callback", status_code=200)
mock_request.patch("http://example.com/callback", status_code=200)

validator.execute()

assert mock_load_config.call_args_list == [
call(validator.VALIDATION_CALLBACK_URL_FILE_NAME),
call(validator.CALLBACK_URL_FILE_NAME),
call(validator.TOKEN_FILE_NAME),
]
assert mock_request.called
assert mock_request.last_request.json() == {"validation_status": True}
assert mock_request.last_request.json() == {
"validation_status": True,
"need_secrets": False,
}


def test_execute_failure():
class FailingAccountValidator(AccountValidator):
def validator(self) -> bool:
return False

validator = FailingAccountValidator()
validator = MockAccountValidator(mock_return_value=False)

with (
patch.object(
validator.module, "load_config", return_value="http://example.com/callback"
) as mock_load_config,
requests_mock.Mocker() as mock_request,
):
mock_request.post("http://example.com/callback", status_code=200)
mock_request.patch("http://example.com/callback", status_code=200)

validator.execute()

assert mock_load_config.call_args_list == [
call(validator.VALIDATION_CALLBACK_URL_FILE_NAME),
call(validator.CALLBACK_URL_FILE_NAME),
call(validator.TOKEN_FILE_NAME),
]
assert mock_request.called
assert mock_request.last_request.json() == {"validation_status": False}
assert mock_request.last_request.json() == {
"validation_status": False,
"need_secrets": False,
}


def test_execute_request_failure():
Expand All @@ -63,14 +64,35 @@ def test_execute_request_failure():
) as mock_load_config,
requests_mock.Mocker() as mock_request,
):
mock_request.post("http://example.com/callback", status_code=500)
mock_request.patch("http://example.com/callback", status_code=500)

with pytest.raises(requests.exceptions.HTTPError):
validator.execute()

assert mock_load_config.call_args_list == [
call(validator.VALIDATION_CALLBACK_URL_FILE_NAME),
call(validator.CALLBACK_URL_FILE_NAME),
call(validator.TOKEN_FILE_NAME),
]
assert mock_request.called
assert mock_request.last_request.json() == {"validation_status": True}
assert mock_request.last_request.json() == {
"validation_status": True,
"need_secrets": False,
}


def test_retrieve_secrets():
validator = MockAccountValidator()

with (
patch.object(validator.module, "has_secrets", return_value=True),
patch.object(
validator.module, "load_config", return_value="http://example.com/callback"
),
requests_mock.Mocker() as mock_request,
):
mock_request.patch(
"http://example.com/callback",
json={"module_configuration": {"value": {"secret_key": "secret_value"}}},
)

validator.execute()

0 comments on commit b56591e

Please sign in to comment.