test(loyalty): add integration and unit tests for analytics, pages, and stats
Some checks failed
Some checks failed
- Add merchant stats API tests (GET /merchants/loyalty/stats) with 7 test cases - Add merchant page route tests (program, program-edit, analytics) with 6 test cases - Add store page route tests (terminal, cards, card-detail, program, program-edit, analytics, enroll) with 16 test cases - Add unit tests for get_merchant_stats() enhanced fields (new_this_month, estimated_liability_cents, location breakdown) with 6 test cases - Add unit tests for get_platform_stats() enhanced fields (total_points_issued/redeemed, total_points_balance, new_this_month, estimated_liability_cents) with 4 test cases - Total: 38 new tests (174 -> 212 passing) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -265,3 +265,110 @@ class TestMerchantDeleteProgram:
|
||||
f"{BASE}/program", headers=loyalty_merchant_headers
|
||||
)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# GET /stats
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.api
|
||||
@pytest.mark.loyalty
|
||||
class TestMerchantGetStats:
|
||||
"""Tests for GET /api/v1/merchants/loyalty/stats."""
|
||||
|
||||
def test_get_stats_success(
|
||||
self, client, loyalty_merchant_headers, loyalty_store_setup
|
||||
):
|
||||
"""Returns merchant-wide loyalty statistics."""
|
||||
response = client.get(
|
||||
f"{BASE}/stats", headers=loyalty_merchant_headers
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["merchant_id"] == loyalty_store_setup["merchant"].id
|
||||
assert data["program_id"] == loyalty_store_setup["program"].id
|
||||
|
||||
def test_get_stats_includes_card_counts(
|
||||
self, client, loyalty_merchant_headers, loyalty_store_setup
|
||||
):
|
||||
"""Stats include card counts from existing setup."""
|
||||
response = client.get(
|
||||
f"{BASE}/stats", headers=loyalty_merchant_headers
|
||||
)
|
||||
data = response.json()
|
||||
# loyalty_store_setup creates 1 card
|
||||
assert data["total_cards"] == 1
|
||||
assert data["active_cards"] == 1
|
||||
|
||||
def test_get_stats_includes_all_fields(
|
||||
self, client, loyalty_merchant_headers, loyalty_store_setup
|
||||
):
|
||||
"""Response includes all expected stat fields."""
|
||||
response = client.get(
|
||||
f"{BASE}/stats", headers=loyalty_merchant_headers
|
||||
)
|
||||
data = response.json()
|
||||
expected_fields = [
|
||||
"merchant_id",
|
||||
"program_id",
|
||||
"total_cards",
|
||||
"active_cards",
|
||||
"new_this_month",
|
||||
"total_points_issued",
|
||||
"total_points_redeemed",
|
||||
"points_issued_30d",
|
||||
"points_redeemed_30d",
|
||||
"transactions_30d",
|
||||
"estimated_liability_cents",
|
||||
"program",
|
||||
"locations",
|
||||
]
|
||||
for field in expected_fields:
|
||||
assert field in data, f"Missing field: {field}"
|
||||
|
||||
def test_get_stats_includes_program_info(
|
||||
self, client, loyalty_merchant_headers, loyalty_store_setup
|
||||
):
|
||||
"""Stats include program configuration details."""
|
||||
response = client.get(
|
||||
f"{BASE}/stats", headers=loyalty_merchant_headers
|
||||
)
|
||||
data = response.json()
|
||||
assert data["program"] is not None
|
||||
assert data["program"]["loyalty_type"] == "points"
|
||||
assert data["program"]["points_per_euro"] == 10
|
||||
|
||||
def test_get_stats_includes_locations(
|
||||
self, client, loyalty_merchant_headers, loyalty_store_setup
|
||||
):
|
||||
"""Stats include per-location breakdown."""
|
||||
response = client.get(
|
||||
f"{BASE}/stats", headers=loyalty_merchant_headers
|
||||
)
|
||||
data = response.json()
|
||||
assert isinstance(data["locations"], list)
|
||||
# loyalty_store_setup creates 1 store
|
||||
assert len(data["locations"]) >= 1
|
||||
loc = data["locations"][0]
|
||||
assert "store_id" in loc
|
||||
assert "store_name" in loc
|
||||
assert "enrolled_count" in loc
|
||||
assert "points_earned" in loc
|
||||
assert "points_redeemed" in loc
|
||||
assert "transactions_30d" in loc
|
||||
|
||||
def test_get_stats_no_program(
|
||||
self, client, loyalty_merchant_headers_no_program
|
||||
):
|
||||
"""Returns empty stats when merchant has no program."""
|
||||
response = client.get(
|
||||
f"{BASE}/stats", headers=loyalty_merchant_headers_no_program
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["program_id"] is None
|
||||
assert data["total_cards"] == 0
|
||||
assert data["program"] is None
|
||||
assert data["locations"] == []
|
||||
|
||||
160
app/modules/loyalty/tests/integration/test_merchant_pages.py
Normal file
160
app/modules/loyalty/tests/integration/test_merchant_pages.py
Normal file
@@ -0,0 +1,160 @@
|
||||
# app/modules/loyalty/tests/integration/test_merchant_pages.py
|
||||
"""
|
||||
Integration tests for merchant loyalty page routes (HTML rendering).
|
||||
|
||||
Tests the merchant page routes at:
|
||||
/merchants/loyalty/program
|
||||
/merchants/loyalty/program/edit
|
||||
/merchants/loyalty/analytics
|
||||
|
||||
Authentication: Uses dependency overrides for cookie-based merchant auth.
|
||||
"""
|
||||
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
|
||||
from app.api.deps import (
|
||||
get_current_merchant_from_cookie_or_header,
|
||||
get_merchant_for_current_user_page,
|
||||
)
|
||||
from app.modules.tenancy.models import Merchant, User
|
||||
from app.modules.tenancy.schemas.auth import UserContext
|
||||
from main import app
|
||||
|
||||
BASE = "/merchants/loyalty"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Fixtures
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def merchant_page_setup(db):
|
||||
"""Create a merchant owner for page tests."""
|
||||
from middleware.auth import AuthManager
|
||||
|
||||
auth = AuthManager()
|
||||
uid = uuid.uuid4().hex[:8]
|
||||
|
||||
owner = User(
|
||||
email=f"merchpageowner_{uid}@test.com",
|
||||
username=f"merchpageowner_{uid}",
|
||||
hashed_password=auth.hash_password("testpass123"),
|
||||
role="merchant_owner",
|
||||
is_active=True,
|
||||
is_email_verified=True,
|
||||
)
|
||||
db.add(owner)
|
||||
db.commit()
|
||||
db.refresh(owner)
|
||||
|
||||
merchant = Merchant(
|
||||
name=f"Page Test Merchant {uid}",
|
||||
owner_user_id=owner.id,
|
||||
contact_email=owner.email,
|
||||
is_active=True,
|
||||
is_verified=True,
|
||||
)
|
||||
db.add(merchant)
|
||||
db.commit()
|
||||
db.refresh(merchant)
|
||||
|
||||
return {"owner": owner, "merchant": merchant}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def merchant_page_headers(merchant_page_setup):
|
||||
"""Override auth dependencies for merchant page routes."""
|
||||
owner = merchant_page_setup["owner"]
|
||||
merchant = merchant_page_setup["merchant"]
|
||||
|
||||
user_context = UserContext(
|
||||
id=owner.id,
|
||||
email=owner.email,
|
||||
username=owner.username,
|
||||
role="merchant_owner",
|
||||
is_active=True,
|
||||
)
|
||||
|
||||
app.dependency_overrides[get_current_merchant_from_cookie_or_header] = lambda: user_context
|
||||
app.dependency_overrides[get_merchant_for_current_user_page] = lambda: merchant
|
||||
yield {"Cookie": "merchant_token=fake-token"}
|
||||
app.dependency_overrides.pop(get_current_merchant_from_cookie_or_header, None)
|
||||
app.dependency_overrides.pop(get_merchant_for_current_user_page, None)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Program View Page
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.loyalty
|
||||
class TestMerchantProgramPage:
|
||||
"""Tests for GET /merchants/loyalty/program."""
|
||||
|
||||
def test_program_page_renders(self, client, merchant_page_headers):
|
||||
"""Program page returns HTML."""
|
||||
response = client.get(
|
||||
f"{BASE}/program",
|
||||
headers=merchant_page_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
def test_program_page_requires_auth(self, client):
|
||||
"""Unauthenticated request is rejected."""
|
||||
response = client.get(f"{BASE}/program")
|
||||
assert response.status_code in [401, 403]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Program Edit Page
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.loyalty
|
||||
class TestMerchantProgramEditPage:
|
||||
"""Tests for GET /merchants/loyalty/program/edit."""
|
||||
|
||||
def test_program_edit_page_renders(self, client, merchant_page_headers):
|
||||
"""Program edit page returns HTML."""
|
||||
response = client.get(
|
||||
f"{BASE}/program/edit",
|
||||
headers=merchant_page_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
def test_program_edit_page_requires_auth(self, client):
|
||||
"""Unauthenticated request is rejected."""
|
||||
response = client.get(f"{BASE}/program/edit")
|
||||
assert response.status_code in [401, 403]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Analytics Page
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.loyalty
|
||||
class TestMerchantAnalyticsPage:
|
||||
"""Tests for GET /merchants/loyalty/analytics."""
|
||||
|
||||
def test_analytics_page_renders(self, client, merchant_page_headers):
|
||||
"""Analytics page returns HTML."""
|
||||
response = client.get(
|
||||
f"{BASE}/analytics",
|
||||
headers=merchant_page_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
def test_analytics_page_requires_auth(self, client):
|
||||
"""Unauthenticated request is rejected."""
|
||||
response = client.get(f"{BASE}/analytics")
|
||||
assert response.status_code in [401, 403]
|
||||
268
app/modules/loyalty/tests/integration/test_store_pages.py
Normal file
268
app/modules/loyalty/tests/integration/test_store_pages.py
Normal file
@@ -0,0 +1,268 @@
|
||||
# app/modules/loyalty/tests/integration/test_store_pages.py
|
||||
"""
|
||||
Integration tests for store loyalty page routes (HTML rendering).
|
||||
|
||||
Tests the store page routes at:
|
||||
/store/{store_code}/loyalty (redirect to terminal)
|
||||
/store/{store_code}/loyalty/terminal
|
||||
/store/{store_code}/loyalty/cards
|
||||
/store/{store_code}/loyalty/cards/{card_id}
|
||||
/store/{store_code}/loyalty/program
|
||||
/store/{store_code}/loyalty/program/edit
|
||||
/store/{store_code}/loyalty/analytics
|
||||
/store/{store_code}/loyalty/enroll
|
||||
|
||||
Authentication: Uses real JWT login via loyalty_store_headers fixture.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
# ============================================================================
|
||||
# Helper
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def _base(store_code: str) -> str:
|
||||
return f"/store/{store_code}/loyalty"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Loyalty Root (Redirect)
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.loyalty
|
||||
class TestStoreLoyaltyRoot:
|
||||
"""Tests for GET /store/{store_code}/loyalty — redirects to terminal."""
|
||||
|
||||
def test_root_redirects_to_terminal(
|
||||
self, client, loyalty_store_headers, loyalty_store_setup
|
||||
):
|
||||
"""Root loyalty URL redirects to terminal."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(
|
||||
_base(store_code),
|
||||
headers=loyalty_store_headers,
|
||||
follow_redirects=False,
|
||||
)
|
||||
assert response.status_code == 302
|
||||
assert "/loyalty/terminal" in response.headers["location"]
|
||||
|
||||
def test_root_requires_auth(self, client, loyalty_store_setup):
|
||||
"""Unauthenticated request is rejected."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(
|
||||
_base(store_code),
|
||||
follow_redirects=False,
|
||||
)
|
||||
assert response.status_code in [401, 403]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Terminal Page
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.loyalty
|
||||
class TestStoreTerminalPage:
|
||||
"""Tests for GET /store/{store_code}/loyalty/terminal."""
|
||||
|
||||
def test_terminal_page_renders(
|
||||
self, client, loyalty_store_headers, loyalty_store_setup
|
||||
):
|
||||
"""Terminal page returns HTML."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(
|
||||
f"{_base(store_code)}/terminal",
|
||||
headers=loyalty_store_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
def test_terminal_page_requires_auth(self, client, loyalty_store_setup):
|
||||
"""Unauthenticated request is rejected."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(f"{_base(store_code)}/terminal")
|
||||
assert response.status_code in [401, 403]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Cards (Members List) Page
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.loyalty
|
||||
class TestStoreCardsPage:
|
||||
"""Tests for GET /store/{store_code}/loyalty/cards."""
|
||||
|
||||
def test_cards_page_renders(
|
||||
self, client, loyalty_store_headers, loyalty_store_setup
|
||||
):
|
||||
"""Cards page returns HTML."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(
|
||||
f"{_base(store_code)}/cards",
|
||||
headers=loyalty_store_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
def test_cards_page_requires_auth(self, client, loyalty_store_setup):
|
||||
"""Unauthenticated request is rejected."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(f"{_base(store_code)}/cards")
|
||||
assert response.status_code in [401, 403]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Card Detail Page
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.loyalty
|
||||
class TestStoreCardDetailPage:
|
||||
"""Tests for GET /store/{store_code}/loyalty/cards/{card_id}."""
|
||||
|
||||
def test_card_detail_page_renders(
|
||||
self, client, loyalty_store_headers, loyalty_store_setup
|
||||
):
|
||||
"""Card detail page returns HTML."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
card_id = loyalty_store_setup["card"].id
|
||||
response = client.get(
|
||||
f"{_base(store_code)}/cards/{card_id}",
|
||||
headers=loyalty_store_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
def test_card_detail_page_requires_auth(
|
||||
self, client, loyalty_store_setup
|
||||
):
|
||||
"""Unauthenticated request is rejected."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
card_id = loyalty_store_setup["card"].id
|
||||
response = client.get(f"{_base(store_code)}/cards/{card_id}")
|
||||
assert response.status_code in [401, 403]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Program View Page
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.loyalty
|
||||
class TestStoreProgramPage:
|
||||
"""Tests for GET /store/{store_code}/loyalty/program."""
|
||||
|
||||
def test_program_page_renders(
|
||||
self, client, loyalty_store_headers, loyalty_store_setup
|
||||
):
|
||||
"""Program page returns HTML."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(
|
||||
f"{_base(store_code)}/program",
|
||||
headers=loyalty_store_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
def test_program_page_requires_auth(self, client, loyalty_store_setup):
|
||||
"""Unauthenticated request is rejected."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(f"{_base(store_code)}/program")
|
||||
assert response.status_code in [401, 403]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Program Edit Page
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.loyalty
|
||||
class TestStoreProgramEditPage:
|
||||
"""Tests for GET /store/{store_code}/loyalty/program/edit."""
|
||||
|
||||
def test_program_edit_page_renders(
|
||||
self, client, loyalty_store_headers, loyalty_store_setup
|
||||
):
|
||||
"""Program edit page returns HTML."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(
|
||||
f"{_base(store_code)}/program/edit",
|
||||
headers=loyalty_store_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
def test_program_edit_page_requires_auth(
|
||||
self, client, loyalty_store_setup
|
||||
):
|
||||
"""Unauthenticated request is rejected."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(f"{_base(store_code)}/program/edit")
|
||||
assert response.status_code in [401, 403]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Analytics Page
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.loyalty
|
||||
class TestStoreAnalyticsPage:
|
||||
"""Tests for GET /store/{store_code}/loyalty/analytics."""
|
||||
|
||||
def test_analytics_page_renders(
|
||||
self, client, loyalty_store_headers, loyalty_store_setup
|
||||
):
|
||||
"""Analytics page returns HTML."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(
|
||||
f"{_base(store_code)}/analytics",
|
||||
headers=loyalty_store_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
def test_analytics_page_requires_auth(self, client, loyalty_store_setup):
|
||||
"""Unauthenticated request is rejected."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(f"{_base(store_code)}/analytics")
|
||||
assert response.status_code in [401, 403]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Enrollment Page
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.loyalty
|
||||
class TestStoreEnrollPage:
|
||||
"""Tests for GET /store/{store_code}/loyalty/enroll."""
|
||||
|
||||
def test_enroll_page_renders(
|
||||
self, client, loyalty_store_headers, loyalty_store_setup
|
||||
):
|
||||
"""Enrollment page returns HTML."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(
|
||||
f"{_base(store_code)}/enroll",
|
||||
headers=loyalty_store_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
def test_enroll_page_requires_auth(self, client, loyalty_store_setup):
|
||||
"""Unauthenticated request is rejected."""
|
||||
store_code = loyalty_store_setup["store"].subdomain
|
||||
response = client.get(f"{_base(store_code)}/enroll")
|
||||
assert response.status_code in [401, 403]
|
||||
Reference in New Issue
Block a user