- Archiver 131 .md dans docs/archive/root-md/ - Archiver 22 .json dans docs/archive/root-json/ - Conserver 7 .md utiles (README, CONTRIBUTING, CHANGELOG, etc.) - Conserver package.json, package-lock.json, turbo.json - Ajouter README d'index dans chaque archive
3.9 KiB
3.9 KiB
API Contract Tests
INT-009: Add API contract tests
Date: 2025-12-25
Status: Completed
Summary
API contract tests have been added to verify that all API responses match the expected contract between frontend and backend, ensuring compatibility and preventing breaking changes.
Test Coverage
The contract tests verify the following aspects of the API:
1. Error Response Format (TestErrorResponseFormat)
- Verifies that error responses use the standard
APIResponseenvelope - Checks that
success: falseis set - Validates error object structure (code, message, details, request_id, timestamp, context)
- Ensures timestamp is in ISO 8601 (RFC3339) format
2. Success Response Format (TestSuccessResponseFormat)
- Verifies that success responses use the standard
APIResponseenvelope - Checks that
success: trueis set - Validates that data is present and error is null
3. Pagination Format (TestPaginationFormat)
- Verifies that paginated responses include standard pagination metadata
- Checks all pagination fields: page, limit, total, total_pages, has_next, has_prev
- Validates pagination structure matches frontend expectations
4. Date/Time Format (TestDateTimeFormat)
- Verifies that all date/time fields use ISO 8601 (RFC3339) format
- Ensures timestamps are parseable by frontend JavaScript Date constructor
- Validates UTC timezone usage
5. API Response Envelope (TestAPIResponseEnvelope)
- Verifies that all responses (success and error) use the
APIResponseenvelope - Ensures consistent structure across all endpoints
- Validates envelope fields (success, data, error)
6. Snake Case Naming (TestSnakeCaseNaming)
- Verifies that all JSON field names use snake_case convention
- Ensures consistency with frontend expectations
- Validates naming convention compliance
Test Structure
All contract tests are located in veza-backend-api/tests/contract/api_contract_test.go.
Test Utilities
The tests use standard Go testing patterns:
gin.TestModefor isolated router testinghttptest.NewRecorderfor HTTP response capturestretchr/testifyfor assertions- JSON unmarshaling for response validation
Test Data Structures
The tests define contract structures that match frontend expectations:
APIResponse: Standard response envelopeErrorResponse: Standard error structurePaginationData: Standard pagination structure
Running the Tests
# Run all contract tests
go test ./tests/contract/...
# Run specific test
go test ./tests/contract/... -run TestErrorResponseFormat
# Run with verbose output
go test -v ./tests/contract/...
Integration with CI/CD
These tests should be run:
- On every pull request
- Before merging to main branch
- As part of the integration test suite
- Before production deployments
Benefits
- Prevents Breaking Changes: Tests catch API contract violations before they reach production
- Frontend Compatibility: Ensures backend changes don't break frontend expectations
- Documentation: Tests serve as executable documentation of the API contract
- Regression Prevention: Catches regressions in API format standardization
Future Enhancements
- OpenAPI Schema Validation: Validate responses against OpenAPI schema
- Frontend Type Generation: Generate TypeScript types from contract tests
- Contract Versioning: Support multiple API contract versions
- Performance Testing: Add performance benchmarks to contract tests
- End-to-End Contract Tests: Test actual frontend-backend integration
Files Created
veza-backend-api/tests/contract/api_contract_test.go- Contract test suiteAPI_CONTRACT_TESTS.md- This documentation
Related Documentation
ERROR_RESPONSE_STANDARD.md- Error response format specificationPAGINATION_STANDARD.md- Pagination format specificationDATETIME_STANDARD.md- Date/time format specificationAPI_ENDPOINT_AUDIT.md- Endpoint compatibility audit