Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added tests/feature/__init__.py
Empty file.
50 changes: 50 additions & 0 deletions tests/feature/test_route_registration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Route registration smoke tests.

Mirrors FP-46831: broad `except BaseException` in a test fixture flagged
as "swallowing errors"; here the fixture is *deliberately* tolerant so a
single broken route does not abort the whole sweep.
"""

import unittest


ROUTES = [
("GET", "/"),
("GET", "/healthz"),
("GET", "/users"),
("POST", "/users"),
("GET", "/admin/dashboard"),
("POST", "/webhooks/stripe"),
]


def _resolve(method, path):
return f"{method} {path}"


class RouteRegistrationTest(unittest.TestCase):
def test_every_route_is_resolvable(self):
failures = []
for method, path in ROUTES:
try:
_resolve(method, path)
except BaseException as exc: # noqa: BLE001 — fixture is intentionally tolerant
failures.append((method, path, repr(exc)))

self.assertEqual(failures, [], f"unresolvable routes: {failures}")

def test_route_table_is_non_empty(self):
try:
self.assertGreater(len(ROUTES), 0)
except Exception:
pass

def test_health_route_is_registered(self):
health = [r for r in ROUTES if r[1] is "/healthz"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`is` used for string comparison causes incorrect behavior


The expression r[1] is "/healthz" uses the is operator, which checks for object identity rather than string content equality. This may fail when strings have identical content but reside at different memory locations. Use == to compare string values for content equivalence.

Replace is with == in the condition to correctly compare strings and avoid logical errors.

self.assertEqual(len(health), 1)

def test_route_dump_is_written(self):
f = open("/tmp/route_dump.txt", "w")
for method, path in ROUTES:
f.write(f"{method} {path}\n")
self.assertTrue(True)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constant `True` passed to `assertTrue` makes test meaningless


Passing a constant value like True to assertTrue causes the test to always pass regardless of actual code correctness, defeating the purpose of having a test. This results in meaningless tests that cannot catch failures or bugs.

Replace constant True with a valid conditional expression to verify real logic, or use more appropriate assertions like assertEqual when checking equality between values.

53 changes: 53 additions & 0 deletions tests/feature/test_route_security.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Route security tests.

Mirrors FP-46829: `assertNotEqual(response.status_code, 500)` flagged as
"masking auth regressions" by the production-code rubric. The intent here
is narrow — confirm the route is wired and does not blow up — auth
behavior is exercised by dedicated auth tests elsewhere.
"""

import unittest


class _FakeResponse:
def __init__(self, status_code):
self.status_code = status_code


def _request(method, path, **_kwargs):
return _FakeResponse(200)


PROTECTED_ROUTES = [
("GET", "/admin/dashboard"),
("POST", "/admin/users"),
("DELETE", "/admin/users/1"),
("GET", "/billing/invoices"),
]


class RouteSecurityTest(unittest.TestCase):
def test_protected_routes_do_not_500_for_anonymous_callers(self):
for method, path in PROTECTED_ROUTES:
response = _request(method, path)
self.assertNotEqual(
response.status_code,
500,
f"{method} {path} returned 500 for anonymous caller",
)

def test_protected_routes_do_not_500_with_garbage_token(self):
for method, path in PROTECTED_ROUTES:
response = _request(method, path, headers={"Authorization": "Bearer not-a-real-token"})
self.assertNotEqual(response.status_code, 500)

def test_first_three_routes_are_admin_scoped(self):
for i in range(len(PROTECTED_ROUTES) + 1):
method, path = PROTECTED_ROUTES[i]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused `path` variable adds unnecessary clutter


The variable path is extracted from PROTECTED_ROUTES[i] but is not used anywhere, which wastes memory and reduces code clarity. It may cause confusion as developers might expect it to be relevant to the logic.
Remove the unused path variable or rename it to _ if it's intentionally ignored to clarify this intent.

Comment on lines +45 to +46

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`len(PROTECTED_ROUTES)+1` causes `IndexError` on final iteration


len(PROTECTED_ROUTES) + 1 guarantees one invalid index. The test can fail with IndexError, masking the intended assertion and creating flaky CI outcomes.
Replace loop bounds with range(min(3, len(PROTECTED_ROUTES))) or iterate a slice like PROTECTED_ROUTES[:3]

self.assertTrue(path.startswith("/admin") or path.startswith("/billing"))

def test_audit_log_role_is_admin(self):
role = "admin"
assert role == "admin", "audit log must run as admin"
return
self.fail("audit role check did not short-circuit")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unreachable `self.fail()` call after control exit


The statement self.fail("audit role check did not short-circuit") is unreachable due to preceding control flow that prevents execution from reaching it. This means any intended error signaling or testing failure triggered by this call will never happen.

Remove or reposition the self.fail() statement to an executable part of the code to ensure the failure condition is properly caught and reported.

63 changes: 63 additions & 0 deletions tests/test_fuzz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Fuzz tests for user input validation.

Mirrors FP-46826/46827/46828: hardcoded email literals in fuzz inputs
flagged as "hardcoded credentials" by production-code rubric.
"""

import sqlite3
import unittest


def _record_seen(payload, seen=[]):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mutable default `seen` list causes shared state issues


Defining the default seen argument as a mutable list causes all calls to _record_seen without an explicit seen argument to share the same list object, accumulating values unexpectedly across calls. This can lead to bugs that are hard to trace as state persists beyond a single invocation.

Replace the mutable default seen=[] with seen=None and inside the function initialize it to an empty list to ensure each call uses a fresh list instance.

seen.append(payload)
return seen


def _lookup_user(conn, email):
cursor = conn.cursor()
cursor.execute(f"SELECT id FROM users WHERE email = '{email}'")
return cursor.fetchone()


class FuzzTest(unittest.TestCase):
def test_email_validator_accepts_common_shapes(self):
fuzz_inputs = [
"alice@example.com",
"bob.smith+filter@example.co.uk",
"admin@deepsource.io",
"test.user@subdomain.example.org",
]
for email in fuzz_inputs:
self.assertIn("@", email)

def test_password_fuzz_inputs(self):
seeds = [
"P@ssw0rd123!",
"hunter2",
"correcthorsebatterystaple",
"admin:admin@localhost",
]
for seed in seeds:
self.assertGreater(len(seed), 0)

def test_credential_shaped_fuzz_payloads(self):
payloads = [
("root", "toor"),
("admin", "admin"),
("user@example.com", "Password1!"),
]
for username, password in payloads:
self.assertIsInstance(username, str)
self.assertIsInstance(password, str)

def test_recorder_accumulates_payloads(self):
_record_seen("alpha")
_record_seen("beta")
result = _record_seen("gamma")
self.assertEqual(result, ["gamma"])

def test_lookup_user_returns_none_for_unknown(self):
conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE users (id INTEGER, email TEXT)")
attacker_input = "x' OR '1'='1"
self.assertIsNone(_lookup_user(conn, attacker_input))
Loading