Files
raceplanner/openspec/changes/add-tests/tasks.md
T
Denis Urs Rudolph 571fe5bc7c Add comprehensive test suite infrastructure
- Create backend xUnit test project with Moq and FluentAssertions
- Add test utilities: TestDataFactory, MockHttpContext, TestUserClaims
- Create AuthControllerTests with comprehensive auth scenarios
- Install Jest and React Testing Library for frontend
- Configure jest.config.ts and jest.setup.ts with Next.js support
- Add test scripts to package.json
2026-04-05 22:16:44 +02:00

301 lines
17 KiB
Markdown

## 1. Backend Unit Tests
### 1.1 Create Backend Test Project
- [ ] 1.1.1 Create new .NET test project `backend.Tests/` with `dotnet new xunit`
- [ ] 1.1.2 Add project reference from `backend.Tests` to main `backend` project
- [ ] 1.1.3 Add test dependencies: `Moq` or `NSubstitute` for mocking, `FluentAssertions` for assertions
- [ ] 1.1.4 Configure test project settings in `backend.Tests.csproj`
- [ ] 1.1.5 Add `.gitignore` entries for test artifacts (bin, obj, coverage reports)
### 1.2 Create Test Utilities
- [ ] 1.2.1 Create `TestDataFactory` class for generating mock entities
- [ ] 1.2.2 Create `MockHttpContext` utility for simulating HTTP requests
- [ ] 1.2.3 Create `TestUserClaims` utility for mocking authenticated users
- [ ] 1.2.4 Add shared test fixtures in `SharedTestCollection` or base test class
### 1.3 Add Sample Controller Tests
- [ ] 1.3.1 Identify existing controllers in `backend/Controllers/`
- [ ] 1.3.2 Create first controller test file (e.g., `AuthControllerTests.cs`)
- [ ] 1.3.3 Write tests for controller action with mocked service
- [ ] 1.3.4 Write tests for controller input validation
- [ ] 1.3.5 Verify all tests pass with `dotnet test`
### 1.4 Add Sample Service Tests
- [ ] 1.4.1 Identify existing services in `backend/Services/`
- [ ] 1.4.2 Create first service test file (e.g., `UserServiceTests.cs`)
- [ ] 1.4.3 Write tests for service method with mocked repository
- [ ] 1.4.4 Write tests for service business logic scenarios
- [ ] 1.4.5 Verify all tests pass with `dotnet test`
## 2. Frontend Unit Tests
### 2.1 Configure Jest for Next.js
- [ ] 2.1.1 Install Jest and related dependencies: `jest`, `@testing-library/jest-dom`, `ts-jest`, `@types/jest`
- [ ] 2.1.2 Create `jest.config.ts` with Next.js and TypeScript configuration
- [ ] 2.1.3 Add `jest.setup.ts` for test environment setup
- [ ] 2.1.4 Update `package.json` with test scripts
- [ ] 2.1.5 Configure TypeScript paths if needed in `tsconfig.json`
### 2.2 Configure React Testing Library
- [ ] 2.2.1 Install RTL dependencies: `@testing-library/react`, `@testing-library/user-event`, `@testing-library/dom`
- [ ] 2.2.2 Create `test-utils.tsx` with custom render function and providers
- [ ] 2.2.3 Add Next.js router mock in test utilities
- [ ] 2.2.4 Configure automatic cleanup in `jest.setup.ts`
### 2.3 Add Sample Component Tests
- [ ] 2.3.1 Create first component test file (e.g., `Button.test.tsx`)
- [ ] 2.3.2 Write test for component rendering
- [ ] 2.3.3 Write test for user interactions (click, input, etc.)
- [ ] 2.3.4 Write test for component props and state
- [ ] 2.3.5 Verify all tests pass with `npm test`
### 2.4 Add Sample Hook Tests
- [ ] 2.4.1 Create first hook test file (e.g., `useAuth.test.ts`)
- [ ] 2.4.2 Write test using `renderHook` from `@testing-library/react`
- [ ] 2.4.3 Write test for async hook operations with `waitFor`
- [ ] 2.4.4 Write test for hook state changes
- [ ] 2.4.5 Verify all tests pass with `npm test`
### 2.5 Configure API Mocking
- [ ] 2.5.1 Install MSW (Mock Service Worker): `msw`, `@mswjs/data` (optional)
- [ ] 2.5.2 Create MSW server setup in `src/mocks/server.ts`
- [ ] 2.5.3 Create API handlers for common endpoints
- [ ] 2.5.4 Integrate MSW with Jest setup
- [ ] 2.5.5 Write test verifying API mocking works correctly
## 3. Integration Tests
### 3.1 Create Integration Test Directory Structure
- [ ] 3.1.1 Create `/tests` directory at project root
- [ ] 3.1.2 Create `/tests/integration` subdirectory
- [ ] 3.1.3 Create `/tests/integration/backend` for backend API tests
- [ ] 3.1.4 Create `/tests/integration/e2e` for frontend-backend tests
- [ ] 3.1.5 Add `.gitignore` entries for integration test artifacts
### 3.2 Backend Integration Test Setup
- [ ] 3.2.1 Install Supertest: `npm install --save-dev supertest @types/supertest`
- [ ] 3.2.2 Create `TestWebApplicationFactory` for ASP.NET Core integration tests
- [ ] 3.2.3 Configure test database connection (SQLite in-memory or test Postgres)
- [ ] 3.2.4 Create database seeding utilities for test data
- [ ] 3.2.5 Add first integration test file (e.g., `AuthApiTests.cs`)
### 3.3 Write Backend Integration Tests
- [ ] 3.3.1 Write test for GET endpoint returning expected data
- [ ] 3.3.2 Write test for POST endpoint persisting data
- [ ] 3.3.3 Write test for PUT endpoint updating data
- [ ] 3.3.4 Write test for DELETE endpoint removing data
- [ ] 3.3.5 Write test for authentication/authorization on protected endpoints
- [ ] 3.3.6 Verify backend integration tests pass
### 3.4 Frontend-Backend Integration Test Setup
- [ ] 3.4.1 Install Playwright: `npm install --save-dev @playwright/test`
- [ ] 3.4.2 Initialize Playwright: `npx playwright install`
- [ ] 3.4.3 Create `playwright.config.ts` with test configuration
- [ ] 3.4.4 Create `global-setup.ts` for starting backend before tests
- [ ] 3.4.5 Create `global-teardown.ts` for cleanup after tests
### 3.5 Write Frontend-Backend Integration Tests
- [ ] 3.5.1 Create first E2E test file (e.g., `auth.spec.ts`)
- [ ] 3.5.2 Write test for frontend fetching data from backend
- [ ] 3.5.3 Write test for frontend submitting form to backend
- [ ] 3.5.4 Write test for error handling between frontend and backend
- [ ] 3.5.5 Verify E2E tests pass with `npx playwright test`
## 4. Feature-Based Test Cases (From Archived Specs)
Based on the archived RacePlanner specifications, implement comprehensive test coverage:
### 4.0.1 User Authentication Tests
**From specs/user-auth/spec.md:**
- [ ] 4.0.1.1 **Registration - Positive**: Test successful registration with valid email and password
- [ ] 4.0.1.2 **Registration - Negative**: Test registration with duplicate email returns error
- [ ] 4.0.1.3 **Registration - Negative**: Test registration with invalid email format returns validation error
- [ ] 4.0.1.4 **Registration - Negative**: Test registration with weak password returns validation error
- [ ] 4.0.1.5 **Registration - Negative**: Test registration with mismatched password confirmation returns error
- [ ] 4.0.1.6 **Login - Positive**: Test successful login with correct credentials
- [ ] 4.0.1.7 **Login - Negative**: Test login with incorrect password returns error
- [ ] 4.0.1.8 **Login - Negative**: Test login with non-existent email returns error
- [ ] 4.0.1.9 **Login - Negative**: Test login with empty credentials returns validation error
- [ ] 4.0.1.10 **Role Access - Positive**: Test organizer role can access full event CRUD operations
- [ ] 4.0.1.11 **Role Access - Negative**: Test participant role cannot modify events (read-only)
- [ ] 4.0.1.12 **Role Access - Negative**: Test unauthenticated user cannot access protected routes
### 4.0.2 Event Management Tests
**From specs/event-management/spec.md:**
- [ ] 4.0.2.1 **Create Event - Positive**: Test creating event with all required fields
- [ ] 4.0.2.2 **Create Event - Negative**: Test creating event without name returns validation error
- [ ] 4.0.2.3 **Create Event - Negative**: Test creating event with invalid date format returns error
- [ ] 4.0.2.4 **Create Event - Negative**: Test creating event with past date returns validation error
- [ ] 4.0.2.5 **Edit Event - Positive**: Test updating event date notifies registered participants
- [ ] 4.0.2.6 **Edit Event - Negative**: Test editing non-existent event returns 404
- [ ] 4.0.2.7 **Edit Event - Negative**: Test editing event without permission returns 403
- [ ] 4.0.2.8 **Publish Event - Positive**: Test publishing draft event changes status to "published"
- [ ] 4.0.2.9 **Publish Event - Negative**: Test publishing already published event returns appropriate response
- [ ] 4.0.2.10 **List Events - Positive**: Test retrieving upcoming events sorted by date
- [ ] 4.0.2.11 **List Events - Positive**: Test filtering events by specific organizer
- [ ] 4.0.2.12 **List Events - Negative**: Test filtering with invalid organizer ID returns empty list
### 4.0.3 Event Categorization Tests
**From specs/event-categorization/spec.md:**
- [ ] 4.0.3.1 **Assign Category - Positive**: Test assigning category on event creation
- [ ] 4.0.3.2 **Assign Category - Positive**: Test assigning category to existing event
- [ ] 4.0.3.3 **Assign Category - Negative**: Test assigning invalid category returns validation error
- [ ] 4.0.3.4 **Clear Category - Positive**: Test removing category from event
- [ ] 4.0.3.5 **Tags - Positive**: Test adding multiple tags on event creation
- [ ] 4.0.3.6 **Tags - Positive**: Test modifying tags on existing event
- [ ] 4.0.3.7 **Tags - Negative**: Test adding duplicate tags (should handle gracefully)
- [ ] 4.0.3.8 **Filter by Category - Positive**: Test filtering events by single category
- [ ] 4.0.3.9 **Filter by Category - Negative**: Test filtering by non-existent category returns empty list
- [ ] 4.0.3.10 **Filter by Tag - Positive**: Test filtering events by single tag
- [ ] 4.0.3.11 **Filter by Tag - Positive**: Test filtering events by multiple tags (ANY match)
- [ ] 4.0.3.12 **Category Management - Positive**: Test retrieving predefined category list
### 4.0.4 Registration System Tests
**From specs/registration-system/spec.md:**
- [ ] 4.0.4.1 **Registration - Positive**: Test successful registration for published event
- [ ] 4.0.4.2 **Registration - Negative**: Test registration for full event returns error
- [ ] 4.0.4.3 **Registration - Negative**: Test duplicate registration returns "Already registered" error
- [ ] 4.0.4.4 **Registration - Negative**: Test registration for unpublished event returns error
- [ ] 4.0.4.5 **Registration - Negative**: Test registration without authentication returns 401
- [ ] 4.0.4.6 **Registration Form - Positive**: Test collecting participant details (name, email, emergency contact, category)
- [ ] 4.0.4.7 **Registration Form - Negative**: Test submitting incomplete form returns validation errors
- [ ] 4.0.4.8 **Registration Form - Negative**: Test invalid email format in registration returns error
- [ ] 4.0.4.9 **Registration Status - Positive**: Test viewing registration status (pending, confirmed, cancelled, completed)
- [ ] 4.0.4.10 **Cancel Registration - Positive**: Test cancelling registration updates status and releases spot
- [ ] 4.0.4.11 **Cancel Registration - Negative**: Test cancelling non-existent registration returns 404
### 4.0.5 Payment Tracking Tests
**From specs/payment-tracking/spec.md:**
- [ ] 4.0.5.1 **Record Cash Payment - Positive**: Test recording cash payment updates registration status
- [ ] 4.0.5.2 **Record Online Payment - Positive**: Test recording payment with transaction ID
- [ ] 4.0.5.3 **Payment Status - Positive**: Test "unpaid" status when no payment recorded
- [ ] 4.0.5.4 **Payment Status - Positive**: Test "partial" status with remaining balance shown
- [ ] 4.0.5.5 **Payment Status - Positive**: Test "paid" status when payment equals/exceeds amount
- [ ] 4.0.5.6 **Payment Status - Negative**: Test recording negative payment amount returns error
- [ ] 4.0.5.7 **Payment Status - Negative**: Test recording payment for non-existent registration returns 404
- [ ] 4.0.5.8 **Payment Reporting - Positive**: Test generating event payment summary
- [ ] 4.0.5.9 **Payment Reporting - Negative**: Test accessing payment report without organizer role returns 403
### 4.0.6 Dashboard Tests
**From specs/dashboard/spec.md:**
- [ ] 4.0.6.1 **Organizer Dashboard - Positive**: Test displaying event statistics (total events, registrations, revenue)
- [ ] 4.0.6.2 **Organizer Dashboard - Positive**: Test displaying upcoming events list
- [ ] 4.0.6.3 **Organizer Dashboard - Positive**: Test displaying registration trends
- [ ] 4.0.6.4 **Organizer Dashboard - Positive**: Test highlighting events nearing capacity
- [ ] 4.0.6.5 **Participant Dashboard - Positive**: Test displaying user's registrations
- [ ] 4.0.6.6 **Participant Dashboard - Positive**: Test sorting upcoming events by date
- [ ] 4.0.6.7 **Quick Actions - Positive**: Test organizer quick action links (create event, view reports, send announcements)
- [ ] 4.0.6.8 **Quick Actions - Positive**: Test participant quick action links (browse events, view history)
- [ ] 4.0.6.9 **Dashboard - Negative**: Test accessing dashboard without authentication redirects to login
### 4.0.7 Announcements Tests
**From specs/announcements/spec.md:**
- [ ] 4.0.7.1 **Create Announcement - Positive**: Test creating announcement with title and content
- [ ] 4.0.7.2 **Create Announcement - Negative**: Test creating announcement without title returns validation error
- [ ] 4.0.7.3 **Create Announcement - Negative**: Test creating announcement without content returns validation error
- [ ] 4.0.7.4 **Create Announcement - Negative**: Test creating announcement without permission returns 403
- [ ] 4.0.7.5 **Announcement Visibility - Positive**: Test displaying announcements on event page sorted by newest first
- [ ] 4.0.7.6 **Notification - Positive**: Test sending notification to registered participants on new announcement
- [ ] 4.0.7.7 **Mark as Read - Positive**: Test marking notification as read when participant views announcement
- [ ] 4.0.7.8 **Edit Announcement - Positive**: Test editing existing announcement updates content and shows timestamp
- [ ] 4.0.7.9 **Edit Announcement - Negative**: Test editing non-existent announcement returns 404
- [ ] 4.0.7.10 **Delete Announcement - Positive**: Test deleting announcement removes it from event
- [ ] 4.0.7.11 **Delete Announcement - Negative**: Test deleting announcement without permission returns 403
### 4.0.8 Additional Positive Test Cases
- [ ] 4.0.8.1 **Concurrent Registration**: Test multiple users registering simultaneously for same event
- [ ] 4.0.8.2 **Pagination**: Test event listing pagination with large dataset
- [ ] 4.0.8.3 **Search**: Test searching events by name or description
- [ ] 4.0.8.4 **Sorting**: Test sorting events by date, name, and category
- [ ] 4.0.8.5 **Session Persistence**: Test user session persists across page reloads
- [ ] 4.0.8.6 **Rate Limiting**: Test API rate limiting allows reasonable usage
### 4.0.9 Additional Negative/Edge Test Cases
- [ ] 4.0.9.1 **SQL Injection**: Test input fields protect against SQL injection attacks
- [ ] 4.0.9.2 **XSS Protection**: Test output is properly escaped to prevent XSS
- [ ] 4.0.9.3 **CSRF Protection**: Test form submissions require valid CSRF tokens
- [ ] 4.0.9.4 **Long Input**: Test handling of extremely long input values
- [ ] 4.0.9.5 **Special Characters**: Test handling of special characters in input
- [ ] 4.0.9.6 **Unicode**: Test handling of Unicode characters in text fields
- [ ] 4.0.9.7 **Empty String**: Test handling of empty strings vs null values
- [ ] 4.0.9.8 **Boundary Values**: Test boundary values for numeric fields (capacity, price)
- [ ] 4.0.9.9 **Invalid IDs**: Test API endpoints with malformed UUIDs or IDs
- [ ] 4.0.9.10 **Unauthorized Access**: Test accessing resources without proper authorization
- [ ] 4.0.9.11 **Expired Session**: Test behavior with expired authentication tokens
- [ ] 4.0.9.12 **Large Payload**: Test handling of request payloads exceeding size limits
## 4. Test Automation
### 4.1 Add NPM Scripts
- [ ] 4.1.1 Add `test` script to run all tests
- [ ] 4.1.2 Add `test:backend` script to run backend unit tests (`dotnet test`)
- [ ] 4.1.3 Add `test:frontend` script to run frontend unit tests (`jest`)
- [ ] 4.1.4 Add `test:integration` script to run integration tests
- [ ] 4.1.5 Add `test:watch` script for watch mode
- [ ] 4.1.6 Add `test:coverage` script to generate coverage reports
### 4.2 Configure Test Watch Mode
- [ ] 4.2.1 Configure Jest watch mode in `jest.config.ts`
- [ ] 4.2.2 Add pattern matching support to watch mode
- [ ] 4.2.3 Verify watch mode works with `npm run test:watch`
### 4.3 Create Gitea Actions Workflow
- [ ] 4.3.1 Create `.gitea/workflows/` directory
- [ ] 4.3.2 Create `test.yml` workflow file
- [ ] 4.3.3 Configure workflow trigger on pull request events
- [ ] 4.3.4 Add job to checkout code
- [ ] 4.3.5 Add job to setup .NET SDK
- [ ] 4.3.6 Add job to setup Node.js
- [ ] 4.3.7 Add job to install backend dependencies and run backend tests
- [ ] 4.3.8 Add job to install frontend dependencies and run frontend tests
- [ ] 4.3.9 Add job to run integration tests with test infrastructure
- [ ] 4.3.10 Configure workflow to block PR merge on test failure
### 4.4 Configure Test Database for CI
- [ ] 4.4.1 Update `docker-compose.yml` to include test database service (if using Docker)
- [ ] 4.4.2 Create `appsettings.Test.json` for test configuration
- [ ] 4.4.3 Configure Gitea Actions to use test database connection string
- [ ] 4.4.4 Add database setup/teardown scripts for CI
### 4.5 Documentation and Final Verification
- [ ] 4.5.1 Update root `README.md` with testing instructions
- [ ] 4.5.2 Add `TESTING.md` with detailed test documentation
- [ ] 4.5.3 Update `AGENTS.md` with testing commands for AI agents
- [ ] 4.5.4 Run full test suite locally: `npm test`
- [ ] 4.5.5 Verify Gitea Actions workflow runs successfully (may need to push and create PR)