import json
from pathlib import Path
import tempfile
import unittest
from samsara.core.validation import validate_hostname
from samsara.collectors.demo import collect_demo, DEMO_HOST
from samsara.analyzers.assessment import assess
from report_helpers import export_html, export_json
class CoreTests(unittest.TestCase):
def test_hostname(self):
self.assertEqual(validate_hostname(' DEMO-WEB-01.example.test. '), DEMO_HOST)
for value in ['', '-host', 'a..b', 'https://host', 'host;whoami', 'host$(id)', 'a'*64]:
with self.subTest(value=value), self.assertRaises(ValueError):
validate_hostname(value)
def test_demo_rejects_real_hosts(self):
with self.assertRaises(ValueError):
collect_demo('production.example.test')
data = collect_demo(DEMO_HOST)
data['server']['hostname'] = 'changed'
self.assertEqual(collect_demo(DEMO_HOST)['server']['hostname'], DEMO_HOST)
def test_evidence_and_missing_activity(self):
data = collect_demo(DEMO_HOST)
result = assess(data)
self.assertTrue(all(f['evidence'] for f in result['findings']))
self.assertTrue(any(f['classification'] == 'Potential workload dependency' for f in result['findings']))
self.assertFalse(any(f['classification'] == 'Confirmed workload dependency' for f in result['findings']))
data['inventory']['network'] = []
self.assertTrue(any('does not prove' in f['evidence'] for f in assess(data)['findings']))
self.assertTrue(any('ad:' in f['evidence'] for f in result['findings']))
def test_export_and_escape(self):
data = collect_demo(DEMO_HOST)
data['server']['hostname'] = ''
with tempfile.TemporaryDirectory() as directory:
report = Path(directory) / 'report.html'
export_html(report, data, assess(data))
text = report.read_text()
self.assertNotIn('