mirror of
https://github.com/Ekultek/Zeus-Scanner.git
synced 2026-03-11 08:55:51 +00:00
- Migrated from requirements.txt to Poetry package management - Added pytest, pytest-cov, and pytest-mock as dev dependencies - Configured pytest with coverage reporting (80% threshold) - Created test directory structure with unit/integration subdirectories - Added comprehensive test fixtures in conftest.py - Updated .gitignore with testing and build artifacts - Added Poetry script commands for running tests - Updated dependencies to modern compatible versions
227 lines
No EOL
6 KiB
Python
227 lines
No EOL
6 KiB
Python
import os
|
|
import tempfile
|
|
import shutil
|
|
from pathlib import Path
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_dir():
|
|
"""Create a temporary directory for test files."""
|
|
temp_path = tempfile.mkdtemp()
|
|
yield Path(temp_path)
|
|
shutil.rmtree(temp_path, ignore_errors=True)
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_config():
|
|
"""Create a mock configuration object."""
|
|
config = Mock()
|
|
config.debug = False
|
|
config.verbose = False
|
|
config.timeout = 30
|
|
config.user_agent = "Mozilla/5.0 (Test)"
|
|
config.proxy = None
|
|
config.ssl_verify = True
|
|
return config
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_html():
|
|
"""Sample HTML content for testing."""
|
|
return """
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Test Page</title>
|
|
<meta name="generator" content="WordPress 5.8">
|
|
</head>
|
|
<body>
|
|
<h1>Test Content</h1>
|
|
<form action="/submit" method="post">
|
|
<input type="text" name="username">
|
|
<input type="password" name="password">
|
|
<button type="submit">Submit</button>
|
|
</form>
|
|
<script src="/js/jquery-3.6.0.min.js"></script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_headers():
|
|
"""Sample HTTP headers for testing."""
|
|
return {
|
|
'Server': 'nginx/1.21.0',
|
|
'Content-Type': 'text/html; charset=UTF-8',
|
|
'X-Powered-By': 'PHP/7.4.21',
|
|
'X-Frame-Options': 'SAMEORIGIN',
|
|
'X-Content-Type-Options': 'nosniff',
|
|
'Set-Cookie': 'session=abc123; HttpOnly; Secure',
|
|
'Cache-Control': 'no-cache, no-store, must-revalidate'
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_response():
|
|
"""Create a mock HTTP response object."""
|
|
response = Mock()
|
|
response.status_code = 200
|
|
response.headers = {
|
|
'Server': 'nginx/1.21.0',
|
|
'Content-Type': 'text/html; charset=UTF-8'
|
|
}
|
|
response.text = "<html><body>Test Response</body></html>"
|
|
response.content = response.text.encode('utf-8')
|
|
response.url = "http://example.com"
|
|
response.cookies = {}
|
|
response.elapsed = Mock(total_seconds=lambda: 0.5)
|
|
return response
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_requests(monkeypatch):
|
|
"""Mock the requests library."""
|
|
mock_session = Mock()
|
|
mock_get = Mock()
|
|
mock_post = Mock()
|
|
|
|
def get_side_effect(url, **kwargs):
|
|
response = Mock()
|
|
response.status_code = 200
|
|
response.text = f"<html><body>Response from {url}</body></html>"
|
|
response.headers = {'Server': 'TestServer/1.0'}
|
|
response.url = url
|
|
return response
|
|
|
|
mock_get.side_effect = get_side_effect
|
|
mock_post.side_effect = get_side_effect
|
|
|
|
mock_session.get = mock_get
|
|
mock_session.post = mock_post
|
|
|
|
import requests
|
|
monkeypatch.setattr(requests, 'Session', lambda: mock_session)
|
|
monkeypatch.setattr(requests, 'get', mock_get)
|
|
monkeypatch.setattr(requests, 'post', mock_post)
|
|
|
|
return mock_session
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_urls():
|
|
"""Sample URLs for testing."""
|
|
return [
|
|
"http://example.com",
|
|
"https://test.example.com/path",
|
|
"http://subdomain.example.com:8080/",
|
|
"https://example.com/admin",
|
|
"http://example.com/login.php"
|
|
]
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_payloads():
|
|
"""Sample XSS payloads for testing."""
|
|
return [
|
|
"<script>alert('XSS')</script>",
|
|
"';alert(String.fromCharCode(88,83,83))//",
|
|
'"><img src=x onerror=alert(1)>',
|
|
"<svg/onload=alert('XSS')>",
|
|
"javascript:alert('XSS')"
|
|
]
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_nmap_scan():
|
|
"""Mock nmap scan results."""
|
|
return {
|
|
'scan': {
|
|
'192.168.1.1': {
|
|
'tcp': {
|
|
80: {'state': 'open', 'name': 'http'},
|
|
443: {'state': 'open', 'name': 'https'},
|
|
22: {'state': 'closed', 'name': 'ssh'}
|
|
},
|
|
'addresses': {'ipv4': '192.168.1.1'},
|
|
'hostnames': [{'name': 'example.com', 'type': 'PTR'}],
|
|
'status': {'state': 'up', 'reason': 'echo-reply'}
|
|
}
|
|
},
|
|
'nmap': {
|
|
'command_line': 'nmap -sV -sC 192.168.1.1',
|
|
'scaninfo': {'tcp': {'method': 'connect', 'services': '1-1000'}}
|
|
}
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def test_file(temp_dir):
|
|
"""Create a test file with content."""
|
|
test_path = temp_dir / "test_file.txt"
|
|
test_path.write_text("Test content\nLine 2\nLine 3")
|
|
return test_path
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_selenium_driver(monkeypatch):
|
|
"""Mock Selenium WebDriver."""
|
|
mock_driver = Mock()
|
|
mock_driver.get = Mock()
|
|
mock_driver.quit = Mock()
|
|
mock_driver.page_source = "<html><body>Selenium Test</body></html>"
|
|
mock_driver.current_url = "http://example.com"
|
|
mock_driver.title = "Test Page"
|
|
|
|
mock_element = Mock()
|
|
mock_element.text = "Element Text"
|
|
mock_element.get_attribute = Mock(return_value="attribute_value")
|
|
|
|
mock_driver.find_element = Mock(return_value=mock_element)
|
|
mock_driver.find_elements = Mock(return_value=[mock_element])
|
|
|
|
return mock_driver
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset_singletons():
|
|
"""Reset any singleton instances between tests."""
|
|
yield
|
|
# Add any singleton reset logic here if needed
|
|
|
|
|
|
@pytest.fixture
|
|
def captured_output(monkeypatch):
|
|
"""Capture stdout and stderr."""
|
|
import sys
|
|
from io import StringIO
|
|
|
|
captured_out = StringIO()
|
|
captured_err = StringIO()
|
|
|
|
monkeypatch.setattr(sys, 'stdout', captured_out)
|
|
monkeypatch.setattr(sys, 'stderr', captured_err)
|
|
|
|
yield captured_out, captured_err
|
|
|
|
|
|
@pytest.fixture
|
|
def env_vars(monkeypatch):
|
|
"""Set and restore environment variables."""
|
|
original_env = os.environ.copy()
|
|
|
|
def set_env(**kwargs):
|
|
for key, value in kwargs.items():
|
|
if value is None:
|
|
monkeypatch.delenv(key, raising=False)
|
|
else:
|
|
monkeypatch.setenv(key, str(value))
|
|
|
|
yield set_env
|
|
|
|
# Restore original environment
|
|
os.environ.clear()
|
|
os.environ.update(original_env) |