7.1 KiB
7.1 KiB
Acupedia API - Test Suite Implementation Plan
1. Overview & Objectives
This implementation plan outlines the strategy, architecture, and step-by-step tasks to achieve comprehensive test coverage for acupedia-api using pytest.
Goals:
- Fast & Deterministic: Run tests without requiring external services (no live Razorpay, MinIO, or Redis connections required).
- High Coverage: Cover all core business logic (Cart, Subscriptions, Pricing, Security) and API endpoints (Auth, Content, Admin, Webhooks).
- Maintainable Architecture: Modular test structure following standard FastAPI and pytest conventions.
2. Directory Structure
acupedia-api/
├── pyproject.toml
└── tests/
├── conftest.py # Global fixtures (AsyncClient, DB mocks, Auth tokens, Service mocks)
├── unit/ # Unit tests (Business logic & schemas, isolated)
│ ├── test_security.py # Password strength, hashing, JWT encode/decode
│ ├── test_cart_service.py # Pricing logic, currency conversion (INR/USD), coupon rules
│ ├── test_plans_service.py # Plan validation & subscription business rules
│ ├── test_schemas.py # Pydantic input/output validations
│ └── test_exception_handlers.py# HTTP error mappings and custom exceptions
└── api/ # Integration tests for FastAPI routers
├── test_auth_api.py # /api/auth (register, login, refresh, password reset)
├── test_access_api.py # /api/access (entitlements & module access)
├── test_content_api.py # /api/content (modules, channels, points, topics)
├── test_plans_api.py # /api/plans (plan listings, active filters)
├── test_cart_api.py # /api/cart (add items, calculate subtotal & tax)
├── test_checkout_api.py # /api/checkout (order creation, invoice initiation)
├── test_webhooks_api.py # /api/webhooks (Razorpay payment signature & status)
├── test_search_api.py # /api/search (query parameters, search scopes)
└── test_admin_api.py # /api/admin (RBAC 403 vs 200, CRUD management)
3. Phased Implementation Roadmap
Phase 1: Environment & Test Harness Setup
- Install Test Dependencies into
.venv:pytest>=7.0.0pytest-asyncio>=0.21.0httpx>=0.24.0pytest-cov(coverage reporting)pytest-mock(for clean mock fixtures)
- Configure
conftest.pywith Reusable Fixtures:app&async_client: Setuphttpx.AsyncClientwithASGITransporttargeting FastAPIapp.- Database Fixture: Mock session / transaction rollback fixture for safe, isolated database testing.
- Auth Fixtures:
user_token&auth_headers(standard authenticated user)admin_token&admin_headers(superuser / admin role)expired_token&invalid_token(for testing 401 Unauthorized responses)
- Service Mocks:
- Mock MinIO client (
get_presigned_url, file uploads) - Mock Redis client (
get,set,delete) - Mock Razorpay client (
order.create,utility.verify_payment_signature) - Mock Email/SMTP dispatcher
- Mock MinIO client (
Phase 2: Unit Testing (Domain & Business Logic)
Fast, isolated tests without database or network overhead.
- Security & Auth (
tests/unit/test_security.py):- Password hashing & verification (
verify_password,hash_password). - Password strength validation edge cases (spaces, minimum length, special characters, uppercase, digits).
- JWT creation, decoding, expiration validation, invalid signature handling.
- Password hashing & verification (
- Cart & Pricing Engine (
tests/unit/test_cart_pricing.py):- Currency resolution (
USDvsINR). - Item resolution (e.g.
POINTS_ACU,LIFETIME_ALL, suffixed items). - Coupon code validation (valid coupon, expired coupon, minimum purchase requirement).
- Subtotal, discount calculations, convenience fees, and tax calculations.
- Currency resolution (
- Schema Validation (
tests/unit/test_schemas.py):- Verify Pydantic validation rejects invalid email formats, missing fields, or negative prices.
- Exception Handlers (
tests/test_exception_handlers.py):- Verify standard error schema (
{"detail": {"code": ..., "message": ...}}).
- Verify standard error schema (
Phase 3: API Integration Testing (Endpoints & Routes)
Testing HTTP responses, route security, and status codes.
- Auth Router (
tests/api/test_auth_api.py):POST /api/auth/register(success, duplicate email, weak password).POST /api/auth/login(valid credentials, wrong password, non-existent user).GET /api/auth/me(unauthorized 401 when anonymous, profile when authenticated).POST /api/auth/forgot-password(triggers OTP email delivery flow).
- Access & Entitlements (
tests/api/test_content_and_access_api.py):- Check user entitlement and active subscriptions via
/api/access/me(401 vs 200).
- Check user entitlement and active subscriptions via
- Content Router (
tests/api/test_content_and_access_api.py):GET /api/contents/modules(retrieves active module listing).
- Plans Router (
tests/api/test_plans_api.py):GET /api/plans(retrieves public subscription plans catalog).
- Cart & Checkout (
tests/api/test_cart_api.py,tests/api/test_checkout_api.py):POST /api/cart/calculate(valid pricing calculations, 422 on empty cart, 400 on invalid codes).GET /api/checkout/config(Razorpay public key).POST /api/checkout/create-order(order creation with Razorpay integration, 401 on unauthorized).
- Webhooks (
tests/api/test_webhooks_api.py):POST /api/webhooks/razorpaywith valid signature & successful payment event.POST /api/webhooks/razorpaywith invalid signature (rejects with 400).POST /api/webhooks/razorpaywith malformed JSON (rejects with 400).
- Admin Operations & RBAC (
tests/api/test_admin_api.py):- Non-authenticated user gets
401 Unauthorized. - Non-admin user gets
403 Forbidden: Administrative privileges required. - Admin user gets
200 OKon admin management endpoints.
- Non-authenticated user gets
- Search Router (
tests/api/test_search_api.py):GET /api/search/capabilities(retrieves searchable kinds and facets).POST /api/search/facet-values(404 on unknown facet).
Phase 4: Coverage & Verification
- Run full test suite:
.venv/bin/pytest -v - Run test coverage report:
.venv/bin/pytest --cov=app --cov-report=term-missing - Verify 100% of tests pass cleanly (88 of 88 passed).
4. Execution Order
| Step | Action | Output |
|---|---|---|
| Step 1 | Install dev dependencies | .venv ready with pytest & httpx |
| Step 2 | Create conftest.py with mock fixtures |
Standardized fixtures ready for use |
| Step 3 | Implement Unit Tests (tests/unit/) |
Fast unit test coverage for logic & security |
| Step 4 | Implement API Route Tests (tests/api/) |
Full endpoint coverage across all features |
| Step 5 | Verify & Coverage Summary | Clean passing test suite with coverage metrics |