-
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
1 parent
2d03821
commit 1a2aac8
Showing
3 changed files
with
118 additions
and
0 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,37 @@ | ||
from abc import abstractmethod | ||
|
||
import requests | ||
|
||
from sekoia_automation.module import ModuleItem | ||
|
||
|
||
class AccountValidator(ModuleItem): | ||
VALIDATION_CALLBACK_URL_FILE_NAME = "validation_callback_url" | ||
|
||
@abstractmethod | ||
def validator(self) -> bool: | ||
"""To define in subclasses. Validates the configuration of the module. | ||
Returns: | ||
bool: True if the module is valid, False otherwise | ||
""" | ||
|
||
def execute(self): | ||
"""Validates the account (module_configuration) of the module | ||
and sends the result to Symphony.""" | ||
# Call the actual validation procedure | ||
status = self.validator() | ||
|
||
# Return result of validation to Symphony | ||
data = {"validation_status": status} | ||
validation_callback_url = self.module.load_config( | ||
self.VALIDATION_CALLBACK_URL_FILE_NAME | ||
) | ||
response = requests.request( | ||
"POST", | ||
validation_callback_url, | ||
json=data, | ||
headers={"Authorization": f"Bearer {self.token}"}, | ||
timeout=30, | ||
) | ||
response.raise_for_status() |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
import requests | ||
import requests_mock | ||
|
||
from sekoia_automation.account_validator import AccountValidator | ||
|
||
|
||
class TestAccountValidator(AccountValidator): | ||
def validator(self) -> bool: | ||
return True | ||
|
||
|
||
def test_execute_success(): | ||
validator = TestAccountValidator() | ||
|
||
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) | ||
|
||
validator.execute() | ||
|
||
mock_load_config.assert_called_once_with( | ||
validator.VALIDATION_CALLBACK_URL_FILE_NAME | ||
) | ||
assert mock_request.called | ||
assert mock_request.last_request.json() == {"validation_status": True} | ||
|
||
|
||
def test_execute_failure(): | ||
class FailingAccountValidator(AccountValidator): | ||
def validator(self) -> bool: | ||
return False | ||
|
||
validator = FailingAccountValidator() | ||
|
||
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) | ||
|
||
validator.execute() | ||
|
||
mock_load_config.assert_called_once_with( | ||
validator.VALIDATION_CALLBACK_URL_FILE_NAME | ||
) | ||
assert mock_request.called | ||
assert mock_request.last_request.json() == {"validation_status": False} | ||
|
||
|
||
def test_execute_request_failure(): | ||
validator = TestAccountValidator() | ||
|
||
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=500) | ||
|
||
with pytest.raises(requests.exceptions.HTTPError): | ||
validator.execute() | ||
|
||
mock_load_config.assert_called_once_with( | ||
validator.VALIDATION_CALLBACK_URL_FILE_NAME | ||
) | ||
assert mock_request.called | ||
assert mock_request.last_request.json() == {"validation_status": True} |