Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(tests): replace conditional asserts in auth rate-limit tests (#4097)

LearningCircuit4205d66b870b
diff --git a/tests/auth_tests/test_auth_rate_limiting.py b/tests/auth_tests/test_auth_rate_limiting.py
index 1563da0d80..c3183469f1 100644
--- a/tests/auth_tests/test_auth_rate_limiting.py
+++ b/tests/auth_tests/test_auth_rate_limiting.py
@@ -104,12 +104,17 @@ class TestAuthRateLimiting:
data={"username": f"testuser{i}", "password": "wrongpassword"},
)
- # Check error response format
- if response.status_code == 429:
- data = response.get_json()
- assert "error" in data
- assert "message" in data
- assert "Too many" in data["message"] or "Too many" in data["error"]
+ # Confirm rate limit actually fired before inspecting the body.
+ # Previously the body checks were gated by `if response.status_code == 429`,
+ # which silently passed when rate limiting didn't fire — masking real bugs.
+ assert response.status_code == 429, (
+ "6th attempt must be rate-limited; "
+ f"got status {response.status_code}"
+ )
+ data = response.get_json()
+ assert "error" in data
+ assert "message" in data
+ assert "Too many" in data["message"] or "Too many" in data["error"]
def test_login_rate_limit_includes_retry_after_header(self, client):
"""Test that 429 response includes Retry-After header."""
@@ -120,12 +125,17 @@ class TestAuthRateLimiting:
data={"username": f"testuser{i}", "password": "wrongpassword"},
)
- # Check for Retry-After header
- if response.status_code == 429:
- assert (
- "Retry-After" in response.headers
- or "X-RateLimit-Reset" in response.headers
- ), "Rate limit response should include retry timing header"
+ # Confirm rate limit actually fired before inspecting headers.
+ # Previously the header check was gated by `if response.status_code == 429`,
+ # which silently passed when rate limiting didn't fire.
+ assert response.status_code == 429, (
+ "6th attempt must be rate-limited; "
+ f"got status {response.status_code}"
+ )
+ assert (
+ "Retry-After" in response.headers
+ or "X-RateLimit-Reset" in response.headers
+ ), "Rate limit response should include retry timing header"
def test_registration_rate_limit_allows_3_attempts(self, client):
"""Test that registration allows 3 attempts before rate limiting."""
@@ -364,7 +374,29 @@ class TestAuthRateLimiting:
def test_account_enumeration_prevented(self, client):
"""Test that registration errors don't reveal username existence."""
+ # First, register a real user so the second attempt below collides.
+ # The `app` fixture scope is `function`, so the DB is fresh; the user
+ # this test relies on must be created here rather than depending on
+ # any other test's side effects.
+ existing_username = "enum_target_user"
+ setup_response = client.post(
+ "/auth/register",
+ data={
+ "username": existing_username,
+ "password": "TestPass123",
+ "confirm_password": "TestPass123",
+ "acknowledge": "true",
+ },
+ )
+ # Registration must succeed for the rest of the test to be meaningful.
+ # Accept 200 or 302 (some flows redirect after success).
+ assert setup_response.status_code in (200, 201, 302), (
+ "Setup user registration must succeed; "
+ f"got {setup_response.status_code}"
+ )
+
# Try to register with a username that definitely doesn't exist
+ # but with an invalid password so validation rejects it.
response1 = client.post(
"/auth/register",
data={
@@ -375,31 +407,47 @@ class TestAuthRateLimiting:
},
)
- # Try to register with a username that exists (from previous test)
+ # Try to register with the username that now exists (collision)
response2 = client.post(
"/auth/register",
data={
- "username": "ratelimituser", # Exists from previous test
+ "username": existing_username,
"password": "TestPass123",
"confirm_password": "TestPass123",
"acknowledge": "true",
},
)
- # Both should return generic errors, not revealing if username exists
- if response1.status_code == 400 and response2.status_code == 400:
- # Check that error messages are generic
- data2 = response2.get_data(as_text=True)
+ # Both should return generic errors, not revealing if username exists.
+ # Previously the body checks were gated by
+ # `if response1.status_code == 400 and response2.status_code == 400`,
+ # which silently passed if either response was anything else — masking
+ # account-enumeration regressions (the whole point of this test). Worse,
+ # the test relied on a leftover user from a sibling test, which never
+ # existed because the fixture is function-scoped, so response2 was
+ # always a successful registration and the body checks were always
+ # skipped.
+ assert response1.status_code == 400, (
+ "Short-password registration must return 400; "
+ f"got {response1.status_code}"
+ )
+ assert response2.status_code == 400, (
+ "Duplicate-username registration must return 400; "
+ f"got {response2.status_code}"
+ )
- # Should NOT contain "Username already exists"
- assert "Username already exists" not in data2, (
- "Error should not reveal username existence"
- )
- # Should contain generic message
- assert (
- "Registration failed" in data2
- or "try a different username" in data2
- ), "Should use generic error message"
+ # Check that error messages are generic
+ data2 = response2.get_data(as_text=True)
+
+ # Should NOT contain "Username already exists"
+ assert "Username already exists" not in data2, (
+ "Error should not reveal username existence"
+ )
+ # Should contain generic message
+ assert (
+ "Registration failed" in data2
+ or "try a different username" in data2
+ ), "Should use generic error message"
class TestRateLimitReset: