Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /test_subdomain_monitor.py
| """ | |
| Tests for Subdomain Monitor Module | |
| Phase 7.3: Continuous Security - Subdomain Monitoring | |
| """ | |
| import asyncio | |
| import json | |
| import tempfile | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| from unittest.mock import AsyncMock, Mock, patch | |
| import pytest | |
| from core.subdomain_monitor import ( | |
| ContinuousSubdomainScanner, | |
| MonitoredDomain, | |
| SubdomainDiff, | |
| SubdomainMonitor, | |
| check_domain_changes, | |
| monitor_domains, | |
| ) | |
| from tools.subfinder_integration import SubfinderResult | |
| class TestSubdomainDiff: | |
| """Tests for SubdomainDiff dataclass""" | |
| def test_create_diff(self): | |
| """Test creating a SubdomainDiff""" | |
| diff = SubdomainDiff( | |
| new=["new1.example.com", "new2.example.com"], | |
| removed=["old1.example.com"], | |
| unchanged=["static.example.com"], | |
| changed=[{"subdomain": "updated.example.com", "changes": ["ip"]}], | |
| ) | |
| assert len(diff.new) == 2 | |
| assert len(diff.removed) == 1 | |
| assert len(diff.unchanged) == 1 | |
| assert len(diff.changed) == 1 | |
| assert isinstance(diff.timestamp, datetime) | |
| class TestMonitoredDomain: | |
| """Tests for MonitoredDomain dataclass""" | |
| def test_create_monitored_domain(self): | |
| """Test creating a MonitoredDomain""" | |
| domain = MonitoredDomain( | |
| domain="example.com", | |
| created_at=datetime.now(timezone.utc), | |
| scan_interval_hours=12, | |
| notify_on_changes=True, | |
| alert_threshold=5, | |
| ) | |
| assert domain.domain == "example.com" | |
| assert domain.scan_interval_hours == 12 | |
| assert domain.notify_on_changes is True | |
| assert domain.alert_threshold == 5 | |
| assert domain.last_scan_at is None | |
| class TestSubdomainMonitor: | |
| """Tests for SubdomainMonitor class""" | |
| def temp_storage(self): | |
| """Create temporary storage directory""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| yield tmpdir | |
| def monitor(self, temp_storage): | |
| """Create a SubdomainMonitor instance""" | |
| return SubdomainMonitor(storage_path=temp_storage) | |
| def test_initialization(self, temp_storage): | |
| """Test monitor initialization""" | |
| monitor = SubdomainMonitor(storage_path=temp_storage) | |
| assert monitor.storage_path == Path(temp_storage) | |
| assert monitor.db is None | |
| assert isinstance(monitor.subfinder, object) | |
| def test_initialization_no_storage(self): | |
| """Test monitor initialization without storage""" | |
| monitor = SubdomainMonitor() | |
| assert monitor.storage_path is None | |
| assert monitor.db is None | |
| def test_get_storage_path(self, monitor): | |
| """Test storage path generation""" | |
| path = monitor._get_storage_path("example.com") | |
| assert path is not None | |
| assert "example_com" in path.name | |
| assert path.suffix == ".json" | |
| def test_get_storage_path_no_storage(self): | |
| """Test storage path when no storage configured""" | |
| monitor = SubdomainMonitor() | |
| path = monitor._get_storage_path("example.com") | |
| assert path is None | |
| async def test_save_and_load_current(self, monitor, temp_storage): | |
| """Test saving and loading subdomain data""" | |
| domain = "test.example.com" | |
| subdomains = ["www.test.example.com", "api.test.example.com"] | |
| # Save data | |
| await monitor.save_current(domain, subdomains) | |
| # Load data | |
| loaded = await monitor.load_previous(domain) | |
| assert sorted(loaded) == sorted(subdomains) | |
| async def test_load_previous_no_data(self, monitor): | |
| """Test loading when no previous data exists""" | |
| loaded = await monitor.load_previous("nonexistent.example.com") | |
| assert loaded == [] | |
| async def test_enumerate(self, mock_subfinder_class, monitor): | |
| """Test subdomain enumeration""" | |
| # Mock subfinder | |
| mock_subfinder = Mock() | |
| mock_subfinder.enumerate = AsyncMock( | |
| return_value=SubfinderResult( | |
| success=True, | |
| domain="example.com", | |
| subdomains=["www.example.com", "api.example.com"], | |
| count=2, | |
| ) | |
| ) | |
| monitor.subfinder = mock_subfinder | |
| result = await monitor.enumerate("example.com") | |
| assert len(result) == 2 | |
| assert "www.example.com" in result | |
| async def test_enumerate_and_diff(self, mock_subfinder_class, monitor, temp_storage): | |
| """Test enumerate and diff functionality""" | |
| domain = "example.com" | |
| # Mock subfinder | |
| mock_subfinder = Mock() | |
| mock_subfinder.enumerate = AsyncMock( | |
| return_value=SubfinderResult( | |
| success=True, | |
| domain=domain, | |
| subdomains=[ | |
| "www.example.com", | |
| "api.example.com", | |
| "new.example.com", | |
| ], | |
| count=3, | |
| ) | |
| ) | |
| monitor.subfinder = mock_subfinder | |
| # First run - no previous data | |
| diff1 = await monitor.enumerate_and_diff(domain, save_results=True) | |
| assert diff1.new == [] # First run, no comparison | |
| assert diff1.removed == [] | |
| # Mock different results for second run | |
| mock_subfinder.enumerate = AsyncMock( | |
| return_value=SubfinderResult( | |
| success=True, | |
| domain=domain, | |
| subdomains=[ | |
| "www.example.com", | |
| "api.example.com", | |
| "brandnew.example.com", | |
| ], | |
| count=3, | |
| ) | |
| ) | |
| # Second run - should detect changes | |
| diff2 = await monitor.enumerate_and_diff(domain, save_results=True) | |
| assert "brandnew.example.com" in diff2.new | |
| assert "new.example.com" in diff2.removed | |
| assert "www.example.com" in diff2.unchanged | |
| async def test_add_monitored_domain(self, mock_subfinder_class, monitor): | |
| """Test adding a monitored domain""" | |
| # Mock subfinder | |
| mock_subfinder = Mock() | |
| mock_subfinder.enumerate = AsyncMock( | |
| return_value=SubfinderResult( | |
| success=True, | |
| domain="example.com", | |
| subdomains=["www.example.com"], | |
| count=1, | |
| ) | |
| ) | |
| monitor.subfinder = mock_subfinder | |
| result = await monitor.add_monitored_domain( | |
| domain="example.com", | |
| scan_interval_hours=6, | |
| notify_on_changes=False, | |
| alert_threshold=3, | |
| ) | |
| assert result.domain == "example.com" | |
| assert result.scan_interval_hours == 6 | |
| assert result.notify_on_changes is False | |
| assert result.alert_threshold == 3 | |
| assert result.last_scan_at is not None | |
| assert "example.com" in monitor._monitored_domains | |
| def test_remove_monitored_domain(self, monitor): | |
| """Test removing a monitored domain""" | |
| # Add a domain first | |
| monitor._monitored_domains["example.com"] = MonitoredDomain( | |
| domain="example.com", | |
| created_at=datetime.now(timezone.utc), | |
| ) | |
| # Remove it | |
| result = monitor.remove_monitored_domain("example.com") | |
| assert result is True | |
| assert "example.com" not in monitor._monitored_domains | |
| def test_remove_nonexistent_domain(self, monitor): | |
| """Test removing a domain that isn't monitored""" | |
| result = monitor.remove_monitored_domain("nonexistent.com") | |
| assert result is False | |
| def test_get_monitored_domains(self, monitor): | |
| """Test getting list of monitored domains""" | |
| monitor._monitored_domains["example.com"] = MonitoredDomain( | |
| domain="example.com", | |
| created_at=datetime.now(timezone.utc), | |
| ) | |
| monitor._monitored_domains["test.com"] = MonitoredDomain( | |
| domain="test.com", | |
| created_at=datetime.now(timezone.utc), | |
| ) | |
| domains = monitor.get_monitored_domains() | |
| assert len(domains) == 2 | |
| domain_names = [d.domain for d in domains] | |
| assert "example.com" in domain_names | |
| assert "test.com" in domain_names | |
| async def test_scan_monitored_domain(self, mock_subfinder_class, monitor): | |
| """Test scanning a monitored domain""" | |
| # Setup | |
| mock_subfinder = Mock() | |
| mock_subfinder.enumerate = AsyncMock( | |
| return_value=SubfinderResult( | |
| success=True, | |
| domain="example.com", | |
| subdomains=["www.example.com"], | |
| count=1, | |
| ) | |
| ) | |
| monitor.subfinder = mock_subfinder | |
| monitor._monitored_domains["example.com"] = MonitoredDomain( | |
| domain="example.com", | |
| created_at=datetime.now(timezone.utc), | |
| ) | |
| # Scan | |
| diff = await monitor.scan_monitored_domain("example.com") | |
| assert diff is not None | |
| assert isinstance(diff, SubdomainDiff) | |
| async def test_scan_non_monitored_domain(self, monitor): | |
| """Test scanning a domain that isn't monitored""" | |
| diff = await monitor.scan_monitored_domain("notmonitored.com") | |
| assert diff is None | |
| async def test_export_monitoring_report(self, mock_subfinder_class, monitor, temp_storage): | |
| """Test report export""" | |
| # Setup monitored domain | |
| mock_subfinder = Mock() | |
| mock_subfinder.enumerate = AsyncMock( | |
| return_value=SubfinderResult( | |
| success=True, | |
| domain="example.com", | |
| subdomains=["www.example.com"], | |
| count=1, | |
| ) | |
| ) | |
| monitor.subfinder = mock_subfinder | |
| await monitor.add_monitored_domain("example.com") | |
| # Export report | |
| report = await monitor.export_monitoring_report() | |
| assert "generated_at" in report | |
| assert "domains" in report | |
| assert len(report["domains"]) == 1 | |
| assert report["domains"][0]["domain"] == "example.com" | |
| class TestContinuousSubdomainScanner: | |
| """Tests for ContinuousSubdomainScanner""" | |
| def temp_storage(self): | |
| """Create temporary storage directory""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| yield tmpdir | |
| def continuous_scanner(self, temp_storage): | |
| """Create a ContinuousSubdomainScanner instance""" | |
| monitor = SubdomainMonitor(storage_path=temp_storage) | |
| return ContinuousSubdomainScanner(monitor) | |
| async def test_start_stop(self, continuous_scanner): | |
| """Test starting and stopping the scanner""" | |
| # Start | |
| await continuous_scanner.start() | |
| assert continuous_scanner._running is True | |
| assert continuous_scanner._task is not None | |
| # Stop | |
| await continuous_scanner.stop() | |
| assert continuous_scanner._running is False | |
| async def test_double_start(self, continuous_scanner): | |
| """Test starting when already running""" | |
| await continuous_scanner.start() | |
| # Try to start again (should not create new task) | |
| await continuous_scanner.start() | |
| assert continuous_scanner._running is True | |
| await continuous_scanner.stop() | |
| class TestConvenienceFunctions: | |
| """Tests for convenience functions""" | |
| async def test_check_domain_changes(self, mock_subfinder_class, temp_storage): | |
| """Test check_domain_changes convenience function""" | |
| mock_subfinder_class.return_value.enumerate = AsyncMock( | |
| return_value=SubfinderResult( | |
| success=True, | |
| domain="example.com", | |
| subdomains=["www.example.com", "api.example.com"], | |
| count=2, | |
| ) | |
| ) | |
| diff = await check_domain_changes( | |
| domain="example.com", | |
| storage_path=temp_storage, | |
| ) | |
| assert isinstance(diff, SubdomainDiff) | |
| async def test_monitor_domains(self, mock_subfinder_class, temp_storage): | |
| """Test monitor_domains convenience function""" | |
| mock_subfinder_class.return_value.enumerate = AsyncMock( | |
| return_value=SubfinderResult( | |
| success=True, | |
| domain="example.com", | |
| subdomains=["www.example.com"], | |
| count=1, | |
| ) | |
| ) | |
| monitor = await monitor_domains( | |
| domains=["example.com", "test.com"], | |
| scan_interval_hours=12, | |
| storage_path=temp_storage, | |
| ) | |
| assert isinstance(monitor, SubdomainMonitor) | |
| assert len(monitor.get_monitored_domains()) == 2 | |
| if __name__ == "__main__": | |
| pytest.main([__file__, "-v"]) | |
Xet Storage Details
- Size:
- 13.5 kB
- Xet hash:
- d49e7b58a6b89fe9301fe4efcdab1d97d0a1cc47550b89d5c64108855ce30343
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.