7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
310 lines
11 KiB
Python
310 lines
11 KiB
Python
"""
|
|
Tests for web/auth/middleware_optimizer.py and related middleware.
|
|
|
|
Tests cover:
|
|
- should_skip_database_middleware() function
|
|
- should_skip_queue_checks() function
|
|
- should_skip_session_cleanup() function
|
|
- Database middleware behavior
|
|
- Session cleanup middleware behavior
|
|
"""
|
|
|
|
from flask import Flask
|
|
|
|
|
|
class TestShouldSkipDatabaseMiddleware:
|
|
"""Tests for should_skip_database_middleware function."""
|
|
|
|
def test_skip_for_static_files(self):
|
|
"""Should return True for static file requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/static/js/app.js"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True
|
|
|
|
def test_skip_for_favicon(self):
|
|
"""Should return True for favicon.ico requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/favicon.ico"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True
|
|
|
|
def test_skip_for_robots_txt(self):
|
|
"""Should return True for robots.txt requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/robots.txt"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True
|
|
|
|
def test_skip_for_health_check(self):
|
|
"""Should return True for health check requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/api/v1/health"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True
|
|
|
|
def test_skip_for_socket_io(self):
|
|
"""Should return True for Socket.IO requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/socket.io/poll"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True
|
|
|
|
def test_skip_for_auth_login(self):
|
|
"""Should return True for login requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/auth/login"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True
|
|
|
|
def test_skip_for_auth_register(self):
|
|
"""Should return True for register requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/auth/register"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True
|
|
|
|
def test_skip_for_auth_logout(self):
|
|
"""Should return True for logout requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/auth/logout"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True
|
|
|
|
def test_skip_for_options_request(self):
|
|
"""Should return True for OPTIONS (CORS preflight) requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/api/data", method="OPTIONS"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True
|
|
|
|
def test_not_skip_for_api_requests(self):
|
|
"""Should return False for regular API requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/api/v1/research"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is False
|
|
|
|
def test_not_skip_for_regular_page_requests(self):
|
|
"""Should return False for regular page requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/dashboard"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is False
|
|
|
|
|
|
class TestShouldSkipQueueChecks:
|
|
"""Tests for should_skip_queue_checks function."""
|
|
|
|
def test_skip_for_get_requests(self):
|
|
"""Should return True for GET requests."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/api/data", method="GET"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_queue_checks,
|
|
)
|
|
|
|
assert should_skip_queue_checks() is True
|
|
|
|
def test_not_skip_for_post_requests(self):
|
|
"""Should return False for POST requests to regular endpoints."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/api/research", method="POST"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_queue_checks,
|
|
)
|
|
|
|
assert should_skip_queue_checks() is False
|
|
|
|
def test_skip_for_static_post(self):
|
|
"""Should return True for POST to static (inherits from database middleware)."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/static/upload", method="POST"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_queue_checks,
|
|
)
|
|
|
|
assert should_skip_queue_checks() is True
|
|
|
|
def test_skip_for_options_post(self):
|
|
"""Should return True for OPTIONS method (CORS preflight)."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/api/data", method="OPTIONS"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_queue_checks,
|
|
)
|
|
|
|
assert should_skip_queue_checks() is True
|
|
|
|
|
|
class TestShouldSkipSessionCleanup:
|
|
"""Tests for should_skip_session_cleanup function."""
|
|
|
|
def test_skip_for_static_files(self):
|
|
"""Should always skip for static files."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/static/css/app.css"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_session_cleanup,
|
|
)
|
|
|
|
# For static files, always skip
|
|
assert should_skip_session_cleanup() is True
|
|
|
|
def test_skip_based_on_random_sampling(self):
|
|
"""Should skip based on random sampling (1% chance)."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/dashboard"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_session_cleanup,
|
|
)
|
|
|
|
# Call multiple times - most should skip due to random sampling
|
|
skip_count = sum(should_skip_session_cleanup() for _ in range(100))
|
|
# With 1% chance of running, we expect ~99 skips
|
|
# Allow some variance for randomness
|
|
assert skip_count >= 90 # Should skip at least 90% of the time
|
|
|
|
def test_inherits_database_middleware_skips(self):
|
|
"""Should skip for paths that skip database middleware."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/favicon.ico"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_session_cleanup,
|
|
)
|
|
|
|
assert should_skip_session_cleanup() is True
|
|
|
|
|
|
class TestMiddlewareOptimizerIntegration:
|
|
"""Integration tests for middleware optimizer."""
|
|
|
|
def test_function_imports_work(self):
|
|
"""All middleware optimizer functions can be imported."""
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
should_skip_queue_checks,
|
|
should_skip_session_cleanup,
|
|
)
|
|
|
|
assert callable(should_skip_database_middleware)
|
|
assert callable(should_skip_queue_checks)
|
|
assert callable(should_skip_session_cleanup)
|
|
|
|
def test_consistent_skip_behavior(self):
|
|
"""Database middleware skip implies queue check skip."""
|
|
app = Flask(__name__)
|
|
|
|
# Test several paths that should skip database middleware
|
|
skip_paths = ["/static/app.js", "/favicon.ico", "/socket.io/poll"]
|
|
|
|
for path in skip_paths:
|
|
with app.test_request_context(path):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
should_skip_queue_checks,
|
|
)
|
|
|
|
db_skip = should_skip_database_middleware()
|
|
if db_skip:
|
|
# If database is skipped, queue should also be skipped
|
|
assert should_skip_queue_checks() is True
|
|
|
|
|
|
class TestDatabaseMiddlewarePaths:
|
|
"""Tests for specific database middleware path patterns."""
|
|
|
|
def test_deep_static_paths(self):
|
|
"""Should skip for nested static paths."""
|
|
app = Flask(__name__)
|
|
with app.test_request_context("/static/dist/assets/js/app.chunk.js"):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True
|
|
|
|
def test_socket_io_websocket_paths(self):
|
|
"""Should skip for Socket.IO websocket paths."""
|
|
app = Flask(__name__)
|
|
test_paths = [
|
|
"/socket.io/",
|
|
"/socket.io/poll",
|
|
"/socket.io/websocket",
|
|
]
|
|
|
|
for path in test_paths:
|
|
with app.test_request_context(path):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True, (
|
|
f"Failed for {path}"
|
|
)
|
|
|
|
def test_auth_routes_only_exact_match(self):
|
|
"""Should only skip for exact auth paths."""
|
|
app = Flask(__name__)
|
|
|
|
# These should skip
|
|
skip_paths = ["/auth/login", "/auth/register", "/auth/logout"]
|
|
for path in skip_paths:
|
|
with app.test_request_context(path):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is True, (
|
|
f"Should skip {path}"
|
|
)
|
|
|
|
# These should NOT skip
|
|
no_skip_paths = [
|
|
"/auth/profile",
|
|
"/auth/settings",
|
|
"/auth/login/callback",
|
|
]
|
|
for path in no_skip_paths:
|
|
with app.test_request_context(path):
|
|
from local_deep_research.web.auth.middleware_optimizer import (
|
|
should_skip_database_middleware,
|
|
)
|
|
|
|
assert should_skip_database_middleware() is False, (
|
|
f"Should not skip {path}"
|
|
)
|