2026-01-21 05:17:23 +00:00
|
|
|
import unittest
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
|
|
|
|
import json
|
2026-02-18 05:16:02 +00:00
|
|
|
import gzip
|
|
|
|
|
import io
|
2026-01-21 05:17:23 +00:00
|
|
|
|
|
|
|
|
# Add scripts directory to path so we can import web_dashboard
|
|
|
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
|
|
|
|
|
from web_dashboard import app
|
|
|
|
|
|
|
|
|
|
class TestWebDashboard(unittest.TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.app = app.test_client()
|
|
|
|
|
self.app.testing = True
|
|
|
|
|
|
|
|
|
|
def test_dashboard_route(self):
|
|
|
|
|
"""Test that the root route returns HTML."""
|
|
|
|
|
response = self.app.get('/')
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertIn(b'<!DOCTYPE html>', response.data)
|
|
|
|
|
self.assertIn(b'MQL5 Trading Automation Dashboard', response.data)
|
|
|
|
|
|
|
|
|
|
def test_health_route_json(self):
|
|
|
|
|
"""Test that the health route returns a JSON response."""
|
|
|
|
|
response = self.app.get('/health')
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
|
|
# This is what we expect AFTER the optimization.
|
|
|
|
|
# For TDD, this test will fail initially if I ran it now against the current code
|
|
|
|
|
# (because current code returns HTML for /health).
|
|
|
|
|
try:
|
|
|
|
|
data = json.loads(response.data)
|
|
|
|
|
self.assertEqual(data.get('status'), 'healthy')
|
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
|
self.fail("Response is not valid JSON")
|
|
|
|
|
|
2026-02-08 11:20:56 +00:00
|
|
|
def test_skip_link_present(self):
|
|
|
|
|
"""Test that the skip link is present in the dashboard HTML."""
|
|
|
|
|
response = self.app.get('/')
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertIn(b'<a href="#status" class="skip-link">Skip to main content</a>', response.data)
|
|
|
|
|
|
2026-02-09 11:27:48 +00:00
|
|
|
def test_security_headers(self):
|
|
|
|
|
"""Test that security headers are present."""
|
|
|
|
|
response = self.app.get('/')
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertIn('Content-Security-Policy', response.headers)
|
|
|
|
|
self.assertIn('X-Content-Type-Options', response.headers)
|
|
|
|
|
self.assertIn('X-Frame-Options', response.headers)
|
|
|
|
|
self.assertIn('Referrer-Policy', response.headers)
|
|
|
|
|
|
2026-02-18 05:16:02 +00:00
|
|
|
def test_gzip_compression(self):
|
|
|
|
|
"""Test that the response is gzip compressed if requested."""
|
|
|
|
|
# Request with Accept-Encoding: gzip
|
|
|
|
|
response = self.app.get('/', headers={'Accept-Encoding': 'gzip'})
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
|
|
# Check headers
|
|
|
|
|
self.assertIn('Content-Encoding', response.headers)
|
|
|
|
|
self.assertEqual(response.headers['Content-Encoding'], 'gzip')
|
|
|
|
|
self.assertIn('Vary', response.headers)
|
|
|
|
|
self.assertIn('Accept-Encoding', response.headers['Vary'])
|
|
|
|
|
|
|
|
|
|
# Decompress and verify content
|
|
|
|
|
try:
|
|
|
|
|
gzip_buffer = io.BytesIO(response.data)
|
|
|
|
|
with gzip.GzipFile(fileobj=gzip_buffer) as gzip_file:
|
|
|
|
|
decompressed_data = gzip_file.read()
|
|
|
|
|
|
|
|
|
|
# Check for expected content in decompressed data
|
|
|
|
|
self.assertIn(b'<!DOCTYPE html>', decompressed_data)
|
|
|
|
|
self.assertIn(b'MQL5 Trading Automation Dashboard', decompressed_data)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.fail(f"Failed to decompress response: {e}")
|
|
|
|
|
|
2026-01-21 05:17:23 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|