π‘οΈ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!
Tests: file access control, upload limits, void + Total Value statistics
Omega (QA)283304104b61
diff --git a/tests/README.md b/tests/README.mdindex 5ac92e4..b6fe0a3 100644--- a/tests/README.md+++ b/tests/README.md@@ -34,7 +34,20 @@ CI runs the same suite on every pull request- `test_validation_and_stats.py` β product-name length limit,expiration-before-purchase rejection (create and update),statistics bucket boundaries (expiring today is not expired),- claim status validation, CSV import tag splitting.+ claim status validation, CSV import tag splitting, serial-number+ and tag-name limits, whitespace-only product name, passwords over+ 72 characters rejected with a 400, claim-document file-type check,+ and the JSON 413 for oversized uploads (the suite caps uploads at+ 1 MB via `MAX_UPLOAD_MB` so this stays fast).+- `test_file_access.py` β local file authorization (the+ GHSA-w3rm-8qhv-w3f5 IDOR surface): owner and admin get everything;+ other users get the global-view-shared types (photo, invoice,+ manual) but never "other documents" or claim documents; missing+ files return a JSON 404.+- `test_statistics.py` β void status drops a warranty out of Total+ Value and the active counts and into the voided count (#250);+ Total Value counts no-currency and NULL-currency rows as the+ user's currency and excludes foreign-currency rows (#192).- `test_paperless_security.py` β the Paperless-ngx access-controlbattery from GHSA-fv62-3hrm-gf83 / GHSA-3h89-rqfj-5674: cross-usersearch/download/link blocking, claim-document privacy, first-linkerdiff --git a/tests/conftest.py b/tests/conftest.pyindex 900fbfd..9a3db35 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -33,6 +33,8 @@ os.environ.setdefault("DB_NAME", "warranty_test_suite")os.environ.setdefault("DB_USER", "warranty_user")os.environ.setdefault("DB_PASSWORD", "warranty_password")os.environ.setdefault("SECRET_KEY", "warracker-test-secret-key-0123456789")+# Cap uploads at 1 MB so the 413 path is testable with a small file.+os.environ.setdefault("MAX_UPLOAD_MB", "1")os.environ.setdefault("UPLOAD_FOLDER",os.path.join(tempfile.gettempdir(), "warracker-test-uploads"),@@ -142,6 +144,14 @@ def warranty_factory(client):return _make+def db_connection():+ """Direct connection to the suite database (legacy-data setups)."""+ conn = _connect(os.environ["DB_NAME"])+ yield conn+ conn.close()++@pytest.fixture(scope="session")def paperless_url():import mock_paperlessdiff --git a/tests/test_file_access.py b/tests/test_file_access.pynew file mode 100644index 0000000..7824d2b--- /dev/null+++ b/tests/test_file_access.py@@ -0,0 +1,111 @@+"""Local file access control β the /api/files/ authorization rules.++This is the local-files twin of the Paperless battery: the 1.0.3 IDOR+(GHSA-w3rm-8qhv-w3f5) lived here. Rules under test:++- owner and admins: every file.+- other users, with global view on (the default): product photo,+ invoice, and manual are shared by design; "other documents" and+ claim documents are NEVER shared.+- a file that does not exist on disk returns a JSON 404.+"""+import base64+import io+from types import SimpleNamespace++import pytest++PDF = b"%PDF-1.4 test document"+PNG = base64.b64decode(+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII="+)++[email protected](scope="module")+def files_scenario(client, user_factory, admin_user):+ ns = SimpleNamespace()+ ns.owner = user_factory()["headers"]+ ns.stranger = user_factory()["headers"]+ ns.admin = admin_user["headers"]++ r = client.post("/api/warranties", headers=ns.owner,+ data={+ "product_name": "Doc Widget",+ "purchase_date": "2025-01-01",+ "exact_expiration_date": "2027-01-01",+ "invoice": (io.BytesIO(PDF), "inv.pdf"),+ "manual": (io.BytesIO(PDF), "man.pdf"),+ "other_document": (io.BytesIO(PDF), "other.pdf"),+ "product_photo": (io.BytesIO(PNG), "photo.png"),+ },+ content_type="multipart/form-data")+ assert r.status_code == 201, r.get_data(as_text=True)+ ns.wid = r.get_json()["id"]++ body = client.get("/api/warranties", headers=ns.owner).get_json()+ items = body.get("warranties", body) if isinstance(body, dict) else body+ w = next(w for w in items if w["id"] == ns.wid)+ ns.paths = {k: w[k] for k in ("invoice_path", "manual_path",+ "other_document_path", "product_photo_path")}+ assert all(ns.paths.values()), ns.paths++ r = client.post(f"/api/warranties/{ns.wid}/claims", headers=ns.owner,+ json={"claim_date": "2026-01-15", "status": "Submitted"})+ assert r.status_code == 201, r.get_data(as_text=True)+ cid = r.get_json()["id"]+ r = client.post(+ f"/api/warranties/{ns.wid}/claims/{cid}/documents",+ headers=ns.owner,+ data={"file": (io.BytesIO(PDF), "evidence.pdf")},+ content_type="multipart/form-data")+ assert r.status_code == 201, r.get_data(as_text=True)+ docs = client.get(+ f"/api/warranties/{ns.wid}/claims/{cid}/documents",+ headers=ns.owner).get_json()+ assert len(docs) == 1+ ns.claim_path = docs[0]["file_path"]+ return ns+++def _get(client, headers, path):+ return client.get(f"/api/files/{path}", headers=headers)+++def test_owner_downloads_everything(client, files_scenario):+ for path in files_scenario.paths.values():+ r = _get(client, files_scenario.owner, path)+ assert r.status_code == 200, path+ r = _get(client, files_scenario.owner, files_scenario.claim_path)+ assert r.status_code == 200+++def test_stranger_gets_shared_types_only(client, files_scenario):+ # Shared by design under global view.+ for key in ("invoice_path", "manual_path", "product_photo_path"):+ r = _get(client, files_scenario.stranger, files_scenario.paths[key])+ assert r.status_code == 200, key+ # Never shared.+ r = _get(client, files_scenario.stranger,+ files_scenario.paths["other_document_path"])+ assert r.status_code == 403+ r = _get(client, files_scenario.stranger, files_scenario.claim_path)+ assert r.status_code == 403+++def test_admin_downloads_everything(client, files_scenario):+ r = _get(client, files_scenario.admin,+ files_scenario.paths["other_document_path"])+ assert r.status_code == 200+ r = _get(client, files_scenario.admin, files_scenario.claim_path)+ assert r.status_code == 200+++def test_missing_file_returns_json_404(client, files_scenario):+ r = _get(client, files_scenario.admin, "uploads/definitely-missing-1.pdf")+ assert r.status_code == 404+ assert r.is_json+++def test_file_requires_auth(client, files_scenario):+ r = client.get(f"/api/files/{files_scenario.paths['invoice_path']}")+ assert r.status_code == 401diff --git a/tests/test_statistics.py b/tests/test_statistics.pynew file mode 100644index 0000000..ef09fb5--- /dev/null+++ b/tests/test_statistics.py@@ -0,0 +1,64 @@+"""Statistics rules: void status (#250) and Total Value currency (#192).++- A voided warranty drops out of Total Value and the active/expired/+ expiring counts, and appears in the voided count; unvoid restores it.+- Total Value sums only the user's own currency; warranties with no+ currency recorded (legacy rows, imports without a currency column)+ count as the user's currency, foreign-currency rows do not.+"""+++def _stats(client, headers):+ r = client.get("/api/statistics", headers=headers)+ assert r.status_code == 200, r.get_data(as_text=True)+ return r.get_json()+++def test_void_excluded_from_counts_and_value(client, user_factory,+ warranty_factory):+ h = user_factory()["headers"]+ warranty_factory(h, purchase_price="100")+ r = warranty_factory(h, purchase_price="200")+ wid_b = r.get_json()["id"]++ st = _stats(client, h)+ assert st["total_value"] == 300+ assert st["active"] == 2+ assert st["voided"] == 0++ r = client.patch(f"/api/warranties/{wid_b}/void", headers=h,+ json={"voided": True})+ assert r.status_code == 200, r.get_data(as_text=True)++ st = _stats(client, h)+ assert st["total_value"] == 100+ assert st["active"] == 1+ assert st["voided"] == 1++ r = client.patch(f"/api/warranties/{wid_b}/void", headers=h,+ json={"voided": False})+ assert r.status_code == 200, r.get_data(as_text=True)++ st = _stats(client, h)+ assert st["total_value"] == 300+ assert st["active"] == 2+ assert st["voided"] == 0+++def test_total_value_currency_rules(client, user_factory, warranty_factory,+ db_connection):+ h = user_factory()["headers"]+ # No currency recorded on the form -> counts toward the total.+ warranty_factory(h, purchase_price="100")+ # Foreign currency -> excluded from the user's Total Value.+ warranty_factory(h, purchase_price="500", currency="EUR")+ # Legacy-style row: currency explicitly NULL in the database ->+ # counts as the user's currency (this was the undercount bug).+ r = warranty_factory(h, purchase_price="150", currency="EUR")+ wid_null = r.get_json()["id"]+ with db_connection, db_connection.cursor() as cur:+ cur.execute("UPDATE warranties SET currency = NULL WHERE id = %s",+ (wid_null,))++ st = _stats(client, h)+ assert st["total_value"] == 250 # 100 (no currency) + 150 (NULL), not 500diff --git a/tests/test_validation_and_stats.py b/tests/test_validation_and_stats.pyindex 7925212..9932586 100644--- a/tests/test_validation_and_stats.py+++ b/tests/test_validation_and_stats.py@@ -95,6 +95,74 @@ def test_claim_status_validation(client, user_factory, warranty_factory):assert r.status_code == 200+def test_serial_number_too_long(client, user_factory):+ h = user_factory()["headers"]+ base = {"product_name": "SerialWidget", "purchase_date": TODAY_S,+ "exact_expiration_date": NEXT_YEAR}+ r = client.post("/api/warranties", headers=h,+ data={**base, "serial_numbers[]": ["x" * 300]})+ assert r.status_code == 400+ assert r.get_json()["code"] == "serial_number_too_long"+ r = client.post("/api/warranties", headers=h,+ data={**base, "serial_numbers[]": ["x" * 255]})+ assert r.status_code == 201, r.get_data(as_text=True)+++def test_whitespace_only_product_name(client, user_factory):+ h = user_factory()["headers"]+ r = _add(client, h, " ", TODAY_S, exp_date=NEXT_YEAR)+ assert r.status_code == 400+++def test_tag_name_too_long(client, user_factory):+ h = user_factory()["headers"]+ r = client.post("/api/tags", headers=h,+ json={"name": "x" * 51, "color": "#123456"})+ assert r.status_code == 400+ r = client.post("/api/tags", headers=h,+ json={"name": "x" * 50, "color": "#123456"})+ assert r.status_code in (200, 201), r.get_data(as_text=True)+++def test_password_over_72_chars_rejected(client):+ # bcrypt truncates/rejects past 72 bytes; registration must fail+ # cleanly with a 400, not a 500 (1.0.3 fix).+ r = client.post("/api/auth/register", json={+ "username": "longpwuser", "email": "[email protected]",+ "password": "P" * 100, "first_name": "Long", "last_name": "Pw"})+ assert r.status_code == 400+++def test_claim_document_file_type_rejected(client, user_factory,+ warranty_factory):+ h = user_factory()["headers"]+ wid = warranty_factory(h).get_json()["id"]+ r = client.post(f"/api/warranties/{wid}/claims", headers=h, json={+ "claim_date": TODAY_S, "status": "Submitted"})+ cid = r.get_json()["id"]+ r = client.post(f"/api/warranties/{wid}/claims/{cid}/documents",+ headers=h,+ data={"file": (io.BytesIO(b"MZ"), "evil.exe")},+ content_type="multipart/form-data")+ assert r.status_code == 400+ assert r.get_json()["code"] == "file_type_not_allowed"+++def test_oversized_upload_returns_json_413(client, user_factory):+ # The suite caps uploads at 1 MB (see conftest); a 2 MB photo must+ # get the JSON 413, not an HTML error page or a 500 (1.0.3 fix).+ h = user_factory()["headers"]+ big = io.BytesIO(b"\x89PNG\r\n\x1a\n" + b"0" * (2 * 1024 * 1024))+ r = client.post("/api/warranties", headers=h,+ data={"product_name": "BigPhoto",+ "purchase_date": TODAY_S,+ "exact_expiration_date": NEXT_YEAR,+ "product_photo": (big, "big.png")},+ content_type="multipart/form-data")+ assert r.status_code == 413+ assert r.is_json++def test_csv_import_comma_tags(client, user_factory):h = user_factory()["headers"]csv_body = (