Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions core/utils_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ def post_assessment(
token: str,
timeout: int = 10,
) -> dict[str, Any]:
host = getattr(config, "HOST", "").strip()
host = os.environ.get("HOST", "").strip() or getattr(config, "HOST", "").strip()
if not host:
return {"success": False, "payload": None, "logs": "HOST missing in config.py"}
return {
"success": False,
"payload": None,
"logs": "HOST missing in environment and config.py",
}

url = _assess_url(host)
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils_and_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def test_host_and_key_env_override_config(self):

def test_falls_back_to_config_when_env_not_set(self):
fake_config = types.SimpleNamespace(HOST="", KEY="")
env = {k: v for k, v in os.environ.items() if k not in ("ESC_HOST", "ESC_KEY")}
env = {k: v for k, v in os.environ.items() if k not in ("HOST", "KEY")}
with (
patch("utils.connection.config", fake_config),
patch.dict(os.environ, env, clear=True),
Expand Down
69 changes: 69 additions & 0 deletions tests/test_utils_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import os
import types
import unittest
from unittest.mock import MagicMock, patch

from core.utils_sync import post_assessment


class PostAssessmentHostResolutionTests(unittest.TestCase):
def test_uses_host_from_environment_when_config_host_empty(self):
fake_response = MagicMock()
fake_response.ok = True
fake_response.status_code = 200
fake_response.json.return_value = {"data": {"ok": True}}

with (
patch.dict(os.environ, {"HOST": "env.exitcloud.io"}, clear=False),
patch(
"core.utils_sync.config",
types.SimpleNamespace(HOST="", CLI_VERSION="v1"),
),
patch("core.utils_sync._build_payload", return_value={"sample": "payload"}),
patch(
"core.utils_sync.requests.post", return_value=fake_response
) as mock_post,
):
result = post_assessment(
name="Demo",
started_at=123,
report_path="/tmp/report",
meta={
"exit_strategy": 1,
"cloud_service_provider": 2,
"assessment_type": 1,
},
token="jwt-token",
)

self.assertTrue(result["success"])
called_url = mock_post.call_args.args[0]
self.assertEqual(called_url, "https://env.exitcloud.io/api/v1/assessments/")

def test_returns_clear_error_when_host_missing_everywhere(self):
with (
patch.dict(os.environ, {}, clear=True),
patch(
"core.utils_sync.config",
types.SimpleNamespace(HOST="", CLI_VERSION="v1"),
),
):
result = post_assessment(
name="Demo",
started_at=123,
report_path="/tmp/report",
meta={
"exit_strategy": 1,
"cloud_service_provider": 2,
"assessment_type": 1,
},
token="jwt-token",
)

self.assertFalse(result["success"])
self.assertEqual(result["payload"], None)
self.assertEqual(result["logs"], "HOST missing in environment and config.py")


if __name__ == "__main__":
unittest.main()