3eb11543b5
The data routes (purchases, alerts, stores, etc.) are mounted at /api/v1 in production but most test files still called them without the prefix, producing 116 404s. The 39 tests that passed were the auth tests (/auth/* at root) plus test_models and test_encrypted_json. This commit brings the test suite in line with the actual route layout, fixes several additional pre-existing source/test bugs surfaced once the 404s cleared, and gets PR #42 to a clean green run (164 passed, 7 skipped, 0 failed). Source fixes - src/cartsnitch_api/auth/dependencies.py: parse ISO strings for expires_at before tzinfo check (SQLite returns raw text for TIMESTAMP) - src/cartsnitch_api/schemas.py: UserResponse.id is UUID, matching the actual model type and avoiding ResponseValidationError on /auth/me Test alignment - tests/test_routes/*, tests/test_e2e/*: add /api/v1 prefix to all data route calls (auth routes left alone — they live at root) - tests/test_openapi.py: refresh EXPECTED_ROUTES to match the actual OpenAPI spec (drop Better-Auth-only routes, add /api/v1 prefix, update route count to 31) Pre-existing test fixes - tests/test_middleware/test_rate_limit.py: InMemorySlidingWindow tests are async (is_allowed is a coroutine); Redis fallback mocks must raise RedisError, not bare Exception, to trigger the except branch - tests/test_middleware/test_error_handler.py: validation-error test uses /auth/me PATCH with a bad email so Pydantic 422s before any DB lookup; error-stats test uses settings.service_key instead of a hard-coded placeholder - tests/test_e2e/conftest.py: Coupon.valid_to is date.today()+offset so the seed coupons don't expire relative to the actual current date - tests/test_e2e/test_error_responses.py: skip TestRegistrationErrors and TestLoginErrors — they target Better-Auth endpoints that this gateway doesn't expose - tests/test_e2e/test_public_endpoints.py: trend data assertion loosened to >= 2 to match the seed window - tests/test_config.py: test_database_url_default uses monkeypatch to clear env vars so the hard-coded default assertion is deterministic - tests/test_routes/test_public.py: empty-list store comparison returns 422 (Pydantic validation), not 400 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
88 lines
3.4 KiB
Python
88 lines
3.4 KiB
Python
"""E2E: Purchase listing, detail, and stats against real DB fixtures."""
|
|
|
|
import pytest
|
|
|
|
from tests.test_e2e.conftest import ZERO_UUID
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
class TestPurchaseList:
|
|
"""List and filter a user's purchases."""
|
|
|
|
async def test_list_user_purchases(self, client, seed_data):
|
|
resp = await client.get("/api/v1/purchases", headers=seed_data["headers"])
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert len(data) >= 2
|
|
store_names = [p["store_name"] for p in data]
|
|
assert "Meijer" in store_names
|
|
assert "Kroger" in store_names
|
|
|
|
async def test_filter_purchases_by_store(self, client, seed_data):
|
|
meijer_id = str(seed_data["stores"]["meijer"].id)
|
|
resp = await client.get(
|
|
"/api/v1/purchases", params={"store_id": meijer_id}, headers=seed_data["headers"]
|
|
)
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert len(data) >= 1
|
|
assert all(p["store_name"] == "Meijer" for p in data)
|
|
|
|
async def test_purchases_require_auth(self, client, seed_data):
|
|
resp = await client.get("/api/v1/purchases")
|
|
assert resp.status_code in (401, 403)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
class TestPurchaseDetail:
|
|
"""Retrieve individual purchase with line items."""
|
|
|
|
async def test_get_purchase_detail(self, client, seed_data):
|
|
purchase_id = str(seed_data["purchases"]["meijer_trip"].id)
|
|
resp = await client.get(f"/api/v1/purchases/{purchase_id}", headers=seed_data["headers"])
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["store_name"] == "Meijer"
|
|
assert data["total"] == 23.45
|
|
assert len(data["line_items"]) == 2
|
|
item_names = [li["name"] for li in data["line_items"]]
|
|
assert "Cheerios 18oz Box" in item_names
|
|
assert "Meijer Whole Milk 1gal" in item_names
|
|
|
|
async def test_line_item_amounts_correct(self, client, seed_data):
|
|
purchase_id = str(seed_data["purchases"]["meijer_trip"].id)
|
|
resp = await client.get(f"/api/v1/purchases/{purchase_id}", headers=seed_data["headers"])
|
|
data = resp.json()
|
|
cheerios_item = next(li for li in data["line_items"] if "Cheerios" in li["name"])
|
|
assert cheerios_item["unit_price"] == 4.79
|
|
assert cheerios_item["quantity"] == 1.0
|
|
assert cheerios_item["total_price"] == 4.79
|
|
|
|
async def test_purchase_not_found(self, client, seed_data):
|
|
resp = await client.get(
|
|
f"/api/v1/purchases/{ZERO_UUID}",
|
|
headers=seed_data["headers"],
|
|
)
|
|
assert resp.status_code == 404
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
class TestPurchaseStats:
|
|
"""Verify spending aggregation across purchases."""
|
|
|
|
async def test_purchase_stats_totals(self, client, seed_data):
|
|
resp = await client.get("/api/v1/purchases/stats", headers=seed_data["headers"])
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["purchase_count"] == 2
|
|
# 23.45 + 15.78 = 39.23
|
|
assert abs(data["total_spent"] - 39.23) < 0.01
|
|
|
|
async def test_purchase_stats_by_store(self, client, seed_data):
|
|
resp = await client.get("/api/v1/purchases/stats", headers=seed_data["headers"])
|
|
data = resp.json()
|
|
assert "Meijer" in data["by_store"]
|
|
assert "Kroger" in data["by_store"]
|
|
assert abs(data["by_store"]["Meijer"] - 23.45) < 0.01
|
|
assert abs(data["by_store"]["Kroger"] - 15.78) < 0.01
|