Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

119 lines
4.0 KiB
Python

"""
Tests for uncovered code paths in security/rate_limiter.py.
Targets:
- get_client_ip: X-Forwarded-For, X-Real-IP, fallback
- get_current_username: from g.current_user, from session, missing
- _get_upload_user_key: authenticated vs unauthenticated
"""
from unittest.mock import patch
MODULE = "local_deep_research.security.rate_limiter"
class TestGetClientIp:
def test_x_forwarded_for_first_ip(self, app):
"""Uses first IP from X-Forwarded-For header."""
from local_deep_research.security.rate_limiter import get_client_ip
with app.test_request_context(
environ_base={"HTTP_X_FORWARDED_FOR": "1.2.3.4, 5.6.7.8"}
):
assert get_client_ip() == "1.2.3.4"
def test_x_real_ip(self, app):
"""Uses X-Real-IP when no X-Forwarded-For."""
from local_deep_research.security.rate_limiter import get_client_ip
with app.test_request_context(
environ_base={"HTTP_X_REAL_IP": "10.0.0.1"}
):
assert get_client_ip() == "10.0.0.1"
def test_fallback_to_remote_addr(self, app):
"""Falls back to remote address when no proxy headers."""
from local_deep_research.security.rate_limiter import get_client_ip
with app.test_request_context(
environ_base={"REMOTE_ADDR": "127.0.0.1"}
):
result = get_client_ip()
assert result is not None
class TestGetCurrentUsername:
def test_from_g_current_user(self, app):
"""Returns username from g.current_user."""
from flask import g
from local_deep_research.security.rate_limiter import (
get_current_username,
)
with app.test_request_context():
g.current_user = "alice"
assert get_current_username() == "alice"
def test_from_session_fallback(self, app):
"""Falls back to session username."""
# audit: PUNCHLIST reviewed 2026-05 — issue resolved by prior PR (recommendation: DELETE).
with app.test_request_context():
# No g.current_user set
with app.test_client() as client:
with client.session_transaction() as sess:
sess["username"] = "bob"
def test_returns_none_when_no_user(self, app):
"""Returns None when no user info available."""
from local_deep_research.security.rate_limiter import (
get_current_username,
)
with app.test_request_context():
result = get_current_username()
assert result is None
class TestGetUploadUserKey:
def test_authenticated_user_key(self, app):
"""Returns user-keyed string for authenticated users."""
from local_deep_research.security.rate_limiter import (
_get_upload_user_key,
)
with app.test_request_context():
with patch(f"{MODULE}.get_current_username", return_value="alice"):
result = _get_upload_user_key()
assert result == "upload_user:alice"
def test_unauthenticated_ip_key(self, app):
"""Returns IP-keyed string for unauthenticated requests."""
from local_deep_research.security.rate_limiter import (
_get_upload_user_key,
)
with app.test_request_context():
with patch(f"{MODULE}.get_current_username", return_value=None):
with patch(f"{MODULE}.get_client_ip", return_value="1.2.3.4"):
result = _get_upload_user_key()
assert result == "upload_ip:1.2.3.4"
class TestModuleConstants:
def test_shared_limits_exist(self):
"""Shared limit decorators are created."""
from local_deep_research.security.rate_limiter import (
login_limit,
registration_limit,
settings_limit,
upload_rate_limit_user,
)
assert login_limit is not None
assert registration_limit is not None
assert settings_limit is not None
assert upload_rate_limit_user is not None