Overview
BookMe uses Go’s built-in testing framework with table-driven tests and comprehensive coverage across critical components. Tests focus on authentication, middleware, validation, and business logic.Running Tests
Basic Test Execution
Coverage Reports
Themake test-coverage command (defined in Makefile:22-24) generates:
coverage.out- Raw coverage data- HTML report - Opens in browser
Test Structure
Tests are located alongside implementation files following Go conventions:| Test File | Purpose | Location |
|---|---|---|
auth_test.go | JWT token operations | internal/auth/ |
auth_test.go | Auth middleware | internal/middleware/ |
ratelimit_test.go | Rate limiting | internal/middleware/ |
validator_test.go | Input validation | internal/validator/ |
parser_test.go | Request parsing | internal/handler/ |
email_service_test.go | Email sending | internal/email/ |
calendar_test.go | Google Calendar | internal/google/ |
Testing Patterns
Table-Driven Tests
Most tests use the table-driven pattern for comprehensive coverage.Example: Token Verification
Frominternal/auth/auth_test.go:45-112:
HTTP Handler Testing
Frominternal/middleware/auth_test.go:103-149:
Test Categories
Authentication Tests
File:internal/auth/auth_test.goLines: 281 total Tests cover:
Token Issuance (Lines 13-43)
Token Issuance (Lines 13-43)
TestIssueAccessToken- Generates valid JWT tokens- Verifies token claims (user ID, name, role)
- Ensures tokens are parseable
Token Verification (Lines 45-112)
Token Verification (Lines 45-112)
TestVerifyAccessToken- Validates JWT tokens- Tests: valid tokens, invalid format, empty tokens, malformed tokens
- Verifies claim extraction
Security (Lines 114-131)
Security (Lines 114-131)
TestVerifyAccessToken_WrongSecret- Rejects tokens signed with different secret- Ensures cryptographic security
Expiration (Lines 133-149)
Expiration (Lines 133-149)
TestVerifyAccessToken_ExpiredToken- Rejects expired tokens- Sets TTL to -1 hour to simulate expiration
- Verifies
ErrExpiredTokenis returned
Bearer Token Extraction (Lines 151-215)
Bearer Token Extraction (Lines 151-215)
TestGetBearerToken- Parses Authorization header- Tests: missing header, valid bearer, wrong scheme, empty token
- Edge case: tokens with leading spaces
Context Management (Lines 217-250)
Context Management (Lines 217-250)
TestWithUserAndUserFromContext- User storage in request context- Tests retrieval from empty and populated contexts
Refresh Tokens (Lines 252-267)
Refresh Tokens (Lines 252-267)
TestMakeRefreshToken- Generates random refresh tokens- Verifies 64-character hex output (32 bytes)
- Ensures uniqueness
Middleware Tests
File:internal/middleware/auth_test.goLines: 203 total
Authenticate Middleware
From lines 12-101:
- Allows requests without tokens
- Authenticates valid Bearer tokens
- Gracefully handles invalid tokens
- Rejects malformed Authorization headers
RequireAuth Middleware
From lines 103-149:
- Blocks unauthenticated requests (401)
- Allows authenticated requests
- Sets correct Content-Type headers
internal/middleware/ratelimit_test.go
Tests rate limiting functionality:
- Token bucket algorithm
- Per-IP rate limits
- 429 Too Many Requests responses
Validation Tests
File:internal/validator/validator_test.go
Tests input validation:
- Struct tag validation
- Email format validation
- Required field checks
- Custom validation rules
Handler Tests
File:internal/handler/parser_test.go
Tests request parsing:
- JSON body parsing
- Query parameter extraction
- URL path parameters
- Error handling for malformed input
Service Tests
File:internal/email/email_service_test.go
Email functionality:
- SMTP connection
- Email template rendering
- HTML and plain text emails
- Error handling
internal/google/calendar_test.go
Google Calendar integration:
- Service account authentication
- Event creation
- Calendar API calls
- Error handling
Testing Conventions
Naming Conventions
Test Data
Use realistic test data:Error Checking
Assertions
Mocking and Test Doubles
HTTP Test Helpers
Context Injection
Frominternal/middleware/auth_test.go:128-137:
Writing New Tests
Test File Template
Best Practices
Test Coverage Goals
Target coverage for critical packages:- auth: 90%+ (security critical)
- middleware: 85%+ (affects all requests)
- handler: 80%+ (business logic)
- validator: 90%+ (input validation)
- service: 85%+ (core functionality)
Integration Testing
While BookMe primarily uses unit tests, integration tests can be added:Continuous Integration
Tests should pass before merging:.github/workflows/test.yml
Troubleshooting Tests
Tests fail with database errors
Tests fail with database errors
Tests requiring a database should be skipped or use an in-memory mock.Add skip condition:
Flaky tests
Flaky tests
Common causes:
- Time-dependent logic (use fixed time in tests)
- Race conditions (run with
-raceflag) - External dependencies (mock them)
Slow tests
Slow tests
Profile test execution:Optimize slow tests or mark as integration tests.
Next Steps
- Contributing Guide - Submit your tests
- Project Structure - Understand where to add tests
- Building - Run tests as part of your workflow