-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
125 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from abc import abstractmethod | ||
from pathlib import Path | ||
|
||
from sekoia_automation.module import Module, ModuleItem | ||
|
||
|
||
class AccountValidator(ModuleItem): | ||
CALLBACK_URL_FILE_NAME = "validation_callback_url" | ||
|
||
def __init__(self, module: Module | None = None, data_path: Path | None = None): | ||
super().__init__(module, data_path) | ||
self._error: str | None = None | ||
|
||
@abstractmethod | ||
def validate(self) -> bool: | ||
"""To define in subclasses. Validates the configuration of the module. | ||
Returns: | ||
bool: True if the module is valid, False otherwise | ||
""" | ||
|
||
def error(self, message: str) -> None: | ||
"""Allow to set an error message explaining why the validation failed.""" | ||
self._error = message | ||
|
||
def execute(self): | ||
"""Validates the account (module_configuration) of the module | ||
and sends the result to Symphony.""" | ||
self.set_task_as_running() | ||
# Call the actual validation procedure | ||
success = self.validate() | ||
self.send_results(success) | ||
|
||
def set_task_as_running(self): | ||
"""Send a request to indicate the action started.""" | ||
data = {"status": "running"} | ||
response = self._send_request(data, verb="PATCH") | ||
if self.module.has_secrets(): | ||
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) | ||
|
||
def send_results(self, success: bool): | ||
data = {"status": "finished", "results": {"success": success}} | ||
if self._error: | ||
data["error"] = self._error | ||
self._send_request(data, verb="PATCH") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,112 @@ | ||
from unittest.mock import call, patch | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
import requests | ||
import requests_mock | ||
|
||
from tests.conftest import MockAccountValidator | ||
from sekoia_automation.account_validator import AccountValidator | ||
from sekoia_automation.module import Module | ||
|
||
|
||
class MockAccountValidator(AccountValidator): | ||
mock_return_value = True | ||
|
||
def validate(self): | ||
if not self.mock_return_value: | ||
self.error("Validation failed") | ||
return self.mock_return_value | ||
|
||
|
||
def test_execute_success(): | ||
validator = MockAccountValidator() | ||
validator.mock_return_value = True | ||
|
||
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.patch("http://example.com/callback", status_code=200) | ||
|
||
validator.execute() | ||
|
||
assert mock_load_config.call_args_list == [ | ||
call(validator.CALLBACK_URL_FILE_NAME), | ||
call(validator.TOKEN_FILE_NAME), | ||
] | ||
assert mock_request.called | ||
# Check the callback has been called | ||
assert mock_request.call_count == 2 | ||
assert mock_request.request_history[0].json() == {"status": "running"} | ||
assert mock_request.last_request.json() == { | ||
"validation_status": True, | ||
"need_secrets": False, | ||
"results": {"success": True}, | ||
"status": "finished", | ||
} | ||
|
||
|
||
def test_execute_failure(): | ||
validator = MockAccountValidator(mock_return_value=False) | ||
validator = MockAccountValidator() | ||
validator.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.patch("http://example.com/callback", status_code=200) | ||
|
||
validator.execute() | ||
|
||
assert mock_load_config.call_args_list == [ | ||
call(validator.CALLBACK_URL_FILE_NAME), | ||
call(validator.TOKEN_FILE_NAME), | ||
] | ||
assert mock_request.called | ||
# Check the callback has been called | ||
assert mock_request.call_count == 2 | ||
assert mock_request.request_history[0].json() == {"status": "running"} | ||
assert mock_request.last_request.json() == { | ||
"validation_status": False, | ||
"need_secrets": False, | ||
"error": "Validation failed", | ||
"results": {"success": False}, | ||
"status": "finished", | ||
} | ||
|
||
|
||
def test_execute_request_failure(): | ||
validator = MockAccountValidator() | ||
|
||
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.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.CALLBACK_URL_FILE_NAME), | ||
call(validator.TOKEN_FILE_NAME), | ||
] | ||
assert mock_request.called | ||
assert mock_request.last_request.json() == { | ||
"validation_status": True, | ||
"need_secrets": False, | ||
def test_execute_with_secrets(): | ||
module = Module() | ||
module._manifest = { | ||
"configuration": { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"properties": { | ||
"api_key": {"description": "SEKOIA.IO API key", "type": "string"}, | ||
"base_url": { | ||
"description": "SEKOIA.IO base URL (ex. https://api.sekoia.io)", | ||
"type": "string", | ||
}, | ||
}, | ||
"required": ["api_key"], | ||
"secrets": ["api_key"], | ||
"title": "SEKOIA.IO Configuration", | ||
"type": "object", | ||
} | ||
|
||
|
||
def test_retrieve_secrets(): | ||
validator = MockAccountValidator() | ||
} | ||
module._configuration = {"base_url": "https://api.sekoia.io"} | ||
validator = MockAccountValidator(module=module) | ||
validator.mock_return_value = True | ||
|
||
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"}}}, | ||
status_code=200, | ||
json={"module_configuration": {"value": {"api_key": "foo"}}}, | ||
) | ||
|
||
validator.execute() | ||
|
||
# Check the configuration has been updated with the secrets | ||
assert module.configuration == { | ||
"api_key": "foo", | ||
"base_url": "https://api.sekoia.io", | ||
} | ||
# Check the callback has been called | ||
assert mock_request.call_count == 2 | ||
assert mock_request.request_history[0].json() == {"status": "running"} | ||
assert mock_request.last_request.json() == { | ||
"results": {"success": True}, | ||
"status": "finished", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters