Skip to content

Commit

Permalink
fix codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
PiranhaSa committed Dec 4, 2024
1 parent 3797d49 commit 5d62164
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
8 changes: 4 additions & 4 deletions agent/subfinder_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
CONFIG_PATH = "/root/.config/subfinder/provider-config.yaml"


def _update_provider_config(
def update_provider_config(
virustotal_key: str,
config_path: str = CONFIG_PATH,
) -> None:
Expand All @@ -38,8 +38,8 @@ def _update_provider_config(
with open(config_path, "r") as config_file:
config = yaml.load(config_file) or {}
except FileNotFoundError:
logger.warning("Configuration file not found. Creating a new one.")

logger.error("Configuration file not found. Creating a new one.")
return
# Update the 'virustotal' section
if "virustotal" in config:
if virustotal_key not in config["virustotal"]:
Expand Down Expand Up @@ -69,7 +69,7 @@ def __init__(
virustotal_key = self.args.get("virustotal_key")
if virustotal_key is not None:
logger.info("Updating configuration with VirusTotal API key.")
_update_provider_config(virustotal_key)
update_provider_config(virustotal_key)

agent_persist_mixin.AgentPersistMixin.__init__(self, agent_settings)

Expand Down
27 changes: 27 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,30 @@ def fixture_subfinder_agent_max_subdomains():

agent = subfinder_agent.SubfinderAgent(definition, settings)
return agent


@pytest.fixture
def subfinder_definition() -> agent_definitions.AgentDefinition:
with (pathlib.Path(__file__).parent.parent / "ostorlab.yaml").open() as yaml_o:
definition = agent_definitions.AgentDefinition.from_yaml(yaml_o)
definition.args = [
{
"name": "virustotal_key",
"value": "Justrandomvalue",
"type": "string",
},
]
return definition


@pytest.fixture
def subfinder_settings() -> runtime_definitions.AgentSettings:
settings = runtime_definitions.AgentSettings(
key="agent/ostorlab/subfinder",
bus_url="NA",
bus_exchange_topic="NA",
args=[],
healthcheck_port=random.randint(5000, 6000),
redis_url="redis://guest:guest@localhost:6379",
)
return settings
Empty file added tests/provider-config.yaml
Empty file.
51 changes: 51 additions & 0 deletions tests/subfinder_agent_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
"""Unittests for the Subfinder Agent."""

import pathlib

import pytest
from pytest_mock import plugin

from ostorlab.agent.message import message
from agent import subfinder_agent as sub_agent
from ostorlab.agent import definitions as agent_definitions
from ostorlab.runtimes import definitions as runtime_definitions


def testAgentSubfinder_whenFindsSubDomains_emitsBackFindings(
Expand Down Expand Up @@ -72,3 +80,46 @@ def testAgentSubfinder_whenMaxSubDomainsSet_emitsBackFindings(
assert agent_mock[0].selector == "v3.asset.domain_name", (
agent_mock[0].data["name"] == "subdomain1"
)


def testAgentSubfinder_whenVirustotalKeyPassed_emitsBackFindings(
subfinder_definition: agent_definitions.AgentDefinition,
subfinder_settings: runtime_definitions.AgentSettings,
mocker: plugin.MockerFixture,
) -> None:
"""
Test that the Subfinder agent correctly updates the provider configuration
with the VirusTotal key.
"""
mocker_update_provider_config = mocker.patch(
"agent.subfinder_agent.update_provider_config"
)

sub_agent.SubfinderAgent(subfinder_definition, subfinder_settings)

assert mocker_update_provider_config.called is True
assert mocker_update_provider_config.call_args[0][0] == "Justrandomvalue"


def testUpdateConfigurationFile_whenConfNotFound_handelFileNotFoundError(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that the provider configuration correctly handles a missing configuration file."""

sub_agent.update_provider_config("existing_key", "test_not.yaml")

assert "Configuration file not found. Creating a new one." in caplog.text


def testupdateconfigupdate_whenWriteConfigurationFail_handelWriteErro(
mocker: plugin.MockerFixture,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that the provider configuration handles a write error correctly and logs the failure."""
mocker.patch("ruamel.yaml.main.YAML.dump", side_effect=FileNotFoundError)

sub_agent.update_provider_config(
"existing_key", str(pathlib.Path(__file__).parent / "provider-config.yaml")
)

assert "Failed to write configuration file" in caplog.text

0 comments on commit 5d62164

Please sign in to comment.