|
| 1 | +# Testing Guide for SimScore API |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This guide describes the testing approach for SimScore API, covering test organization, setup instructions, and common testing scenarios. |
| 6 | + |
| 7 | +## Test Organization |
| 8 | + |
| 9 | +``` |
| 10 | +tests/ |
| 11 | +├── api/ # API endpoint tests |
| 12 | +│ └── v1/ |
| 13 | +│ └── routes/ |
| 14 | +│ ├── test_ideas.py # Idea ranking endpoint tests |
| 15 | +│ ├── test_auth.py # Authentication endpoint tests |
| 16 | +│ └── test_rate_limit.py # Rate limiting tests |
| 17 | +├── integration/ # End-to-end flows |
| 18 | +│ ├── test_auth_basic.py # Basic authentication flows |
| 19 | +│ ├── test_auth_guest.py # Guest user flows |
| 20 | +│ └── test_auth_verified.py # Verified user flows |
| 21 | +└── conftest.py # Shared test fixtures |
| 22 | +``` |
| 23 | + |
| 24 | +## Test Environment Setup |
| 25 | + |
| 26 | +1. Create a `.env` file with test configuration: |
| 27 | + |
| 28 | +``` |
| 29 | +# Environment |
| 30 | +ENVIRONMENT=DEV |
| 31 | +
|
| 32 | +# API Configuration |
| 33 | +RATE_LIMIT_PER_USER=20/minute |
| 34 | +GLOBAL_RATE_LIMIT=1000/minute |
| 35 | +
|
| 36 | +# Test Settings |
| 37 | +SKIP_EMAIL_VERIFICATION=true |
| 38 | +TEST_API_TOKEN=<your-test-token> |
| 39 | +
|
| 40 | +# Database (Local Supabase) |
| 41 | +DATABASE_URL=http://127.0.0.1:54321 |
| 42 | +DATABASE_KEY=<your-supabase-service-role-key> |
| 43 | +
|
| 44 | +# Credits Configuration |
| 45 | +GUEST_DAILY_CREDITS=10 |
| 46 | +GUEST_MAX_CREDITS=100 |
| 47 | +USER_DAILY_CREDITS=100 |
| 48 | +USER_MAX_CREDITS=1000 |
| 49 | +``` |
| 50 | + |
| 51 | +2. Start the API server in development mode: |
| 52 | + |
| 53 | +```bash |
| 54 | +poetry run uvicorn app.main:app --reload |
| 55 | +``` |
| 56 | + |
| 57 | +3. Ensure Supabase is running locally: |
| 58 | + |
| 59 | +```bash |
| 60 | +supabase start |
| 61 | +``` |
| 62 | + |
| 63 | +## Running Tests |
| 64 | + |
| 65 | +### API Endpoint Tests |
| 66 | + |
| 67 | +```bash |
| 68 | +# Run all API tests |
| 69 | +poetry run pytest tests/api/ |
| 70 | + |
| 71 | +# Run specific API tests |
| 72 | +poetry run pytest tests/api/v1/routes/test_ideas.py |
| 73 | +poetry run pytest tests/api/v1/routes/test_auth.py |
| 74 | +poetry run pytest tests/api/v1/routes/test_rate_limit.py |
| 75 | +``` |
| 76 | + |
| 77 | +### Integration Tests |
| 78 | + |
| 79 | +```bash |
| 80 | +# Run all integration tests |
| 81 | +poetry run pytest tests/integration/ |
| 82 | + |
| 83 | +# Run specific integration flows |
| 84 | +poetry run pytest tests/integration/test_auth_basic.py |
| 85 | +poetry run pytest tests/integration/test_auth_guest.py |
| 86 | +poetry run pytest tests/integration/test_auth_verified.py |
| 87 | +``` |
| 88 | + |
| 89 | +### Running Tests by Marker |
| 90 | + |
| 91 | +```bash |
| 92 | +# Auth-related tests |
| 93 | +poetry run pytest -m verified |
| 94 | +poetry run pytest -m guest |
| 95 | +poetry run pytest -m integration |
| 96 | + |
| 97 | +# Rate limiting tests |
| 98 | +poetry run pytest -m rate_limited |
| 99 | +``` |
| 100 | + |
| 101 | +## Test Categories |
| 102 | + |
| 103 | +### Ideas API Tests |
| 104 | + |
| 105 | +Tests the idea ranking endpoint functionality: |
| 106 | + |
| 107 | +- Input validation (minimum/maximum ideas, size limits) |
| 108 | +- Cluster analysis and visualization |
| 109 | +- Advanced features (relationship graphs, cluster naming) |
| 110 | +- Credit system integration |
| 111 | +- Error handling |
| 112 | + |
| 113 | +### Authentication Tests |
| 114 | + |
| 115 | +Tests user management and authentication: |
| 116 | + |
| 117 | +- User registration and login |
| 118 | +- Email verification |
| 119 | +- API key creation and management |
| 120 | +- Rate limiting on auth endpoints |
| 121 | +- User credits and quotas |
| 122 | + |
| 123 | +### Rate Limiting Tests |
| 124 | + |
| 125 | +Verifies API protection against abuse: |
| 126 | + |
| 127 | +- Request limiting based on IP address |
| 128 | +- Appropriate rate limit configuration |
| 129 | +- 429 response handling |
| 130 | + |
| 131 | +## Key Test Fixtures |
| 132 | + |
| 133 | +The `conftest.py` file provides shared fixtures: |
| 134 | + |
| 135 | +- `client`: HTTP client for API requests |
| 136 | +- `test_user`: Dynamically generated test credentials |
| 137 | +- `auth_headers`: Pre-authenticated request headers |
| 138 | +- `mock_ideas`: Sample idea data for testing |
| 139 | +- `mock_credit_service`: Credit system bypass |
| 140 | +- `disable_limiter`: Disables rate limiting during tests |
| 141 | + |
| 142 | +## Troubleshooting |
| 143 | + |
| 144 | +### Rate Limit Errors |
| 145 | + |
| 146 | +If tests fail with 429 status codes: |
| 147 | + |
| 148 | +```bash |
| 149 | +# Option 1: Wait for rate limit reset |
| 150 | +sleep 60 |
| 151 | + |
| 152 | +# Option 2: Run with disabled rate limiting |
| 153 | +DISABLE_RATE_LIMITS=true poetry run pytest |
| 154 | +``` |
| 155 | + |
| 156 | +### Database Connection Issues |
| 157 | + |
| 158 | +If tests fail to connect to Supabase: |
| 159 | + |
| 160 | +```bash |
| 161 | +# Check if Supabase is running |
| 162 | +supabase status |
| 163 | + |
| 164 | +# Restart Supabase if needed |
| 165 | +supabase stop |
| 166 | +supabase start |
| 167 | +``` |
| 168 | + |
| 169 | +### Authentication Failures |
| 170 | + |
| 171 | +For auth test failures: |
| 172 | + |
| 173 | +```bash |
| 174 | +# Ensure email verification is disabled for tests |
| 175 | +SKIP_EMAIL_VERIFICATION=true |
| 176 | + |
| 177 | +# Use a pre-configured test token |
| 178 | +TEST_API_TOKEN=<valid-test-token> |
| 179 | +``` |
| 180 | + |
| 181 | +## CI/CD Integration |
| 182 | + |
| 183 | +Tests run automatically on: |
| 184 | +- Pull requests to main branch |
| 185 | +- Nightly builds |
| 186 | + |
| 187 | +The CI pipeline runs tests in a containerized environment with: |
| 188 | +- Isolated test database |
| 189 | +- Test-specific rate limits |
| 190 | +- Email verification disabled |
0 commit comments