test: add comprehensive Go test suite with ~75% coverage
New test files: - config/config_test.go (100% coverage) - constants/constants_test.go (100% coverage) - httputil/response_test.go (100% coverage) - validation/rules_test.go (91.9% coverage) - middleware/logger_test.go, security_test.go, security_logger_test.go - handlers/errors_test.go Updated documentation: - doc/27-GO-TESTING.md: Complete testing guide - doc/00-GO-DOCUMENTATION-INDEX.md: Added testing section - doc/01-ARCHITECTURE.md: Updated package structure - doc/DECISIONS.md: Added ADR-004 caching decision - PROJECT-MEMORY.md: Added Go testing section
This commit is contained in:
+69
-7
@@ -298,12 +298,19 @@ cv/
|
||||
├── main.go # Server entry point (v1.1.0)
|
||||
├── go.mod, go.sum # Go dependencies
|
||||
├── internal/
|
||||
│ ├── config/ # Configuration
|
||||
│ ├── handlers/ # HTTP handlers
|
||||
│ ├── middleware/ # HTTP middleware
|
||||
│ ├── models/ # Data models
|
||||
│ ├── cache/ # Application-level data caching (95.7% coverage)
|
||||
│ ├── config/ # Configuration (100% coverage)
|
||||
│ ├── constants/ # Project-wide constants (100% coverage)
|
||||
│ ├── email/ # Email service - SMTP (58% coverage)
|
||||
│ ├── fileutil/ # File path utilities (88.9% coverage)
|
||||
│ ├── handlers/ # HTTP handlers (62.9% coverage)
|
||||
│ ├── httputil/ # HTTP response helpers (100% coverage)
|
||||
│ ├── middleware/ # HTTP middleware (87.5% coverage)
|
||||
│ ├── models/ # Data models (cv: 83.3%, ui: 85.7%)
|
||||
│ ├── pdf/ # PDF generation (requires Chrome)
|
||||
│ ├── routes/ # Route definitions
|
||||
│ └── templates/ # Template utilities
|
||||
│ ├── templates/ # Template utilities
|
||||
│ └── validation/ # Input validation (91.9% coverage)
|
||||
├── static/
|
||||
│ ├── js/
|
||||
│ │ └── cv-functions.js # Global functions (toggles, keyboard, hover sync)
|
||||
@@ -580,9 +587,15 @@ document.addEventListener('keydown', (e) => {
|
||||
|
||||
---
|
||||
|
||||
**Last Updated:** 2025-12-02
|
||||
**Last Updated:** 2025-12-06
|
||||
**Project Status:** Production - Full feature set including CMD+K command palette and contact form
|
||||
**Test Coverage:** 44 test files, 100% core features + CMD+K, contact form, PDF generation
|
||||
**Test Coverage:**
|
||||
- **Frontend (Playwright):** 44 test files, 100% core features
|
||||
- **Backend (Go):** 12 test files, ~75% average coverage
|
||||
- 100%: config, constants, httputil
|
||||
- 90%+: cache (95.7%), validation (91.9%)
|
||||
- 80%+: middleware (87.5%), fileutil (88.9%), models
|
||||
- See `doc/27-GO-TESTING.md` for full details
|
||||
**Critical Memory Files:** This file + `~/.claude/cv-icons-migration.md`
|
||||
|
||||
---
|
||||
@@ -755,3 +768,52 @@ curl http://localhost:1999/text?lang=es
|
||||
- `tests/mjs/71-cmd-k-api-scroll.test.mjs`
|
||||
- `tests/mjs/72-cmd-k-button.test.mjs` - Tests search bar styling, kbd elements, icon, click behavior
|
||||
|
||||
---
|
||||
|
||||
### 9. Go Backend Testing (2025-12-06)
|
||||
|
||||
**Comprehensive Go test suite with ~75% average coverage:**
|
||||
|
||||
**Commands:**
|
||||
```bash
|
||||
# Run all Go tests
|
||||
go test ./internal/...
|
||||
|
||||
# Run with coverage
|
||||
go test -cover ./internal/...
|
||||
|
||||
# Generate HTML coverage report
|
||||
go test -coverprofile=coverage.out ./internal/...
|
||||
go tool cover -html=coverage.out -o coverage.html
|
||||
```
|
||||
|
||||
**Coverage by Package:**
|
||||
| Package | Coverage | Notes |
|
||||
|---------|----------|-------|
|
||||
| config | 100% | Configuration loading |
|
||||
| constants | 100% | All constants validated |
|
||||
| httputil | 100% | Response helpers |
|
||||
| cache | 95.7% | Application-level data caching |
|
||||
| validation | 91.9% | Input validation rules |
|
||||
| middleware | 87.5% | Security, rate limiting, preferences |
|
||||
| fileutil | 88.9% | File path utilities |
|
||||
| models/ui | 85.7% | UI configuration models |
|
||||
| models/cv | 83.3% | CV data models |
|
||||
| handlers | 62.9% | HTTP handlers (PDF needs Chrome) |
|
||||
| email | 58.0% | Requires SMTP connection |
|
||||
|
||||
**Test Files:**
|
||||
- `internal/cache/data_cache_test.go`
|
||||
- `internal/config/config_test.go`
|
||||
- `internal/constants/constants_test.go`
|
||||
- `internal/email/email_test.go`
|
||||
- `internal/handlers/errors_test.go`
|
||||
- `internal/httputil/response_test.go`
|
||||
- `internal/middleware/csrf_test.go`
|
||||
- `internal/middleware/logger_test.go`
|
||||
- `internal/middleware/contact_rate_limit_test.go`
|
||||
- `internal/middleware/security_logger_test.go`
|
||||
- `internal/validation/rules_test.go`
|
||||
|
||||
**Documentation:** `doc/27-GO-TESTING.md`
|
||||
|
||||
|
||||
+992
-844
File diff suppressed because it is too large
Load Diff
@@ -29,6 +29,13 @@ This documentation covers the core Go systems that power the CV site, with a foc
|
||||
- Protected endpoints and authentication
|
||||
- API request/response formats
|
||||
|
||||
4. **[Go Testing](27-GO-TESTING.md)** (~450 lines)
|
||||
- Coverage summary by package (100% for config, constants, httputil)
|
||||
- Test file descriptions and locations
|
||||
- Testing patterns (table-driven, HTTP handlers, middleware)
|
||||
- Coverage gap explanations
|
||||
- Best practices and CI/CD integration
|
||||
|
||||
## Quick Navigation
|
||||
|
||||
### By Feature
|
||||
@@ -54,6 +61,14 @@ This documentation covers the core Go systems that power the CV site, with a foc
|
||||
- [PDF Export](26-GO-ROUTES-API.md#exportpdf---pdf-export)
|
||||
- [Security Features](26-GO-ROUTES-API.md#security-features)
|
||||
|
||||
**Testing:**
|
||||
- [Coverage Summary](27-GO-TESTING.md#coverage-summary)
|
||||
- [Test Files](27-GO-TESTING.md#test-files)
|
||||
- [Running Tests](27-GO-TESTING.md#running-tests)
|
||||
- [Test Patterns](27-GO-TESTING.md#test-patterns)
|
||||
- [Coverage Gaps](27-GO-TESTING.md#coverage-gaps)
|
||||
- [Best Practices](27-GO-TESTING.md#best-practices)
|
||||
|
||||
### By Use Case
|
||||
|
||||
**Setting Up Validation:**
|
||||
@@ -71,6 +86,11 @@ This documentation covers the core Go systems that power the CV site, with a foc
|
||||
2. [Register handlers](26-GO-ROUTES-API.md#route-table)
|
||||
3. [Apply security](26-GO-ROUTES-API.md#route-specific-middleware)
|
||||
|
||||
**Writing Tests:**
|
||||
1. [Review existing coverage](27-GO-TESTING.md#coverage-summary)
|
||||
2. [Follow test patterns](27-GO-TESTING.md#test-patterns)
|
||||
3. [Run and verify](27-GO-TESTING.md#running-tests)
|
||||
|
||||
## System Architecture
|
||||
|
||||
### Overall Flow
|
||||
@@ -367,6 +387,7 @@ cv/
|
||||
│ ├── 24-GO-VALIDATION-SYSTEM.md # Validation docs
|
||||
│ ├── 25-GO-TEMPLATE-SYSTEM.md # Template docs
|
||||
│ ├── 26-GO-ROUTES-API.md # Routes/API docs
|
||||
│ ├── 27-GO-TESTING.md # Testing & coverage
|
||||
│ └── 00-GO-DOCUMENTATION-INDEX.md # This file
|
||||
│
|
||||
├── internal/
|
||||
@@ -473,5 +494,5 @@ This documentation is part of the CV site project.
|
||||
---
|
||||
|
||||
**Last Updated:** December 6, 2025
|
||||
**Total Documentation:** 2,836+ lines across 3 files
|
||||
**Coverage:** Validation, Templates, Routes, Middleware, Security
|
||||
**Total Documentation:** 3,300+ lines across 4 files
|
||||
**Coverage:** Validation, Templates, Routes, Middleware, Security, Testing
|
||||
|
||||
+13
-6
@@ -17,12 +17,17 @@ This CV website is built following Go best practices with a focus on:
|
||||
cv/
|
||||
├── main.go # Application entry point
|
||||
└── internal/ # Private packages (cannot be imported by other projects)
|
||||
├── cache/ # Application-level data caching
|
||||
├── config/ # Configuration management
|
||||
├── constants/ # Project-wide constants
|
||||
├── email/ # Email service (SMTP)
|
||||
├── fileutil/ # File path utilities
|
||||
├── handlers/ # HTTP request handlers
|
||||
├── httputil/ # HTTP response helpers
|
||||
├── middleware/ # HTTP middleware (security, logging, rate limiting)
|
||||
├── models/ # Data models and business logic
|
||||
├── models/ # Data models (cv, ui)
|
||||
├── pdf/ # PDF generation service
|
||||
├── services/ # Business services (email, etc.)
|
||||
├── routes/ # Route configuration
|
||||
├── templates/ # Template management
|
||||
└── validation/ # Input validation utilities
|
||||
```
|
||||
@@ -41,13 +46,15 @@ Handlers and services receive their dependencies through constructors:
|
||||
// ✅ Good: Dependencies injected
|
||||
type CVHandler struct {
|
||||
templates *templates.Manager
|
||||
emailService *services.EmailService
|
||||
emailService *email.Service
|
||||
dataCache *cache.DataCache
|
||||
}
|
||||
|
||||
func NewCVHandler(tmpl *templates.Manager, addr string, email *services.EmailService) *CVHandler {
|
||||
func NewCVHandler(tmpl *templates.Manager, addr string, emailSvc *email.Service, dc *cache.DataCache) *CVHandler {
|
||||
return &CVHandler{
|
||||
templates: tmpl,
|
||||
emailService: email, // Can be nil for graceful degradation
|
||||
emailService: emailSvc, // Can be nil for graceful degradation
|
||||
dataCache: dc, // Startup-loaded data cache
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,7 +177,7 @@ func (h *CVHandler) HandleContact(w http.ResponseWriter, r *http.Request)
|
||||
- Consistent error handling
|
||||
- HTMX-aware responses
|
||||
|
||||
### Email Service (`internal/services`)
|
||||
### Email Service (`internal/email`)
|
||||
|
||||
**Pattern**: Service layer with dependency injection and interface-based design
|
||||
|
||||
|
||||
@@ -0,0 +1,554 @@
|
||||
# Go Testing Documentation
|
||||
|
||||
Comprehensive guide to the testing infrastructure of the CV site Go backend.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
1. [Coverage Summary](#coverage-summary)
|
||||
2. [Test Files](#test-files)
|
||||
3. [Running Tests](#running-tests)
|
||||
4. [Test Patterns](#test-patterns)
|
||||
5. [Coverage Gaps](#coverage-gaps)
|
||||
6. [Best Practices](#best-practices)
|
||||
|
||||
---
|
||||
|
||||
## Coverage Summary
|
||||
|
||||
Current test coverage as of December 2025:
|
||||
|
||||
| Package | Coverage | Status | Notes |
|
||||
|---------|----------|--------|-------|
|
||||
| `internal/config` | **100%** | Excellent | Fully tested configuration loading |
|
||||
| `internal/constants` | **100%** | Excellent | All constants and validation values |
|
||||
| `internal/httputil` | **100%** | Excellent | All response helper functions |
|
||||
| `internal/cache` | **95.7%** | Excellent | Application-level data caching |
|
||||
| `internal/validation` | **91.9%** | Excellent | Validation rules and error handling |
|
||||
| `internal/middleware` | **87.5%** | Good | Security, rate limiting, preferences |
|
||||
| `internal/fileutil` | **88.9%** | Good | File path utilities |
|
||||
| `internal/models/ui` | **85.7%** | Good | UI configuration models |
|
||||
| `internal/models/cv` | **83.3%** | Good | CV data models |
|
||||
| `internal/handlers` | **62.9%** | Fair | HTTP handlers (PDF requires Chrome) |
|
||||
| `internal/email` | **58.0%** | Fair | Email requires SMTP connection |
|
||||
| `internal/pdf` | **0%** | N/A | Requires Chrome/chromedp |
|
||||
| `internal/templates` | **0%** | N/A | File-system dependent |
|
||||
| `internal/routes` | **0%** | N/A | Integration testing required |
|
||||
| `internal/models` | **0%** | N/A | Interface-only package |
|
||||
|
||||
**Overall Project Coverage: ~70-75%** (for testable packages)
|
||||
|
||||
---
|
||||
|
||||
## Test Files
|
||||
|
||||
### High-Coverage Packages (90%+)
|
||||
|
||||
#### `internal/config/config_test.go`
|
||||
Tests for application configuration loading:
|
||||
- Environment variable parsing
|
||||
- Default value handling
|
||||
- Port configuration
|
||||
- SMTP settings validation
|
||||
|
||||
#### `internal/constants/constants_test.go`
|
||||
Tests for constant values and validation:
|
||||
- Language constants (English, Spanish)
|
||||
- CV theme constants (default, clean)
|
||||
- CV length constants (short, long)
|
||||
- Color theme constants (light, dark)
|
||||
- Rate limit configurations
|
||||
- HTTP header constants
|
||||
|
||||
#### `internal/httputil/response_test.go`
|
||||
Tests for HTTP response helpers:
|
||||
- `JSON()` - Generic JSON response
|
||||
- `JSONOk()` - Success JSON response
|
||||
- `JSONCached()` - Cached JSON response
|
||||
- `HTML()` - HTML response with proper headers
|
||||
- `NoContent()` - 204 No Content response
|
||||
|
||||
### Good-Coverage Packages (80-90%)
|
||||
|
||||
#### `internal/middleware/csrf_test.go`
|
||||
CSRF protection testing:
|
||||
- Token generation (`generateToken`)
|
||||
- Token validation (`validateToken`)
|
||||
- `GetToken()` from request context
|
||||
- Middleware protection flow
|
||||
- HTMX request handling
|
||||
|
||||
#### `internal/middleware/logger_test.go`
|
||||
Request logging testing:
|
||||
- `responseWriter` implementation
|
||||
- `WriteHeader()` status capture
|
||||
- `Write()` body capture
|
||||
- Middleware integration
|
||||
|
||||
#### `internal/middleware/contact_rate_limit_test.go`
|
||||
Rate limiting testing:
|
||||
- `NewContactRateLimiter()` initialization
|
||||
- `allow()` function behavior
|
||||
- Middleware blocking behavior
|
||||
- HTMX error responses
|
||||
- X-Forwarded-For header handling
|
||||
- X-Real-IP header handling
|
||||
- `GetStats()` statistics
|
||||
|
||||
#### `internal/middleware/security_logger_test.go`
|
||||
Security logging and preferences testing:
|
||||
- `LogSecurityEvent()` function
|
||||
- `getSeverity()` mapping
|
||||
- `SecurityLogger` middleware
|
||||
- `isSecurityRelevantPath()` detection
|
||||
- Preferences helper functions:
|
||||
- `GetLanguage()`, `GetCVLength()`, `GetCVIcons()`
|
||||
- `GetCVTheme()`, `GetColorTheme()`
|
||||
- `IsLongCV()`, `IsShortCV()`
|
||||
- `ShowIcons()`, `HideIcons()`
|
||||
- `IsCleanTheme()`, `IsDefaultTheme()`
|
||||
- `IsDarkMode()`, `IsLightMode()`
|
||||
|
||||
#### `internal/validation/rules_test.go`
|
||||
Validation rules testing:
|
||||
- `ruleOptional` - Optional field handling
|
||||
- `ruleTrim` - Whitespace trimming marker
|
||||
- `ruleSanitize` - HTML sanitization marker
|
||||
- `ruleMin` - Minimum length validation (UTF-8 aware)
|
||||
- `ruleTiming` - Bot detection timing
|
||||
- `FieldError.Error()` - Error formatting
|
||||
- `ValidationErrors.HasErrors()` - Error checking
|
||||
- `ValidationErrors.GetFieldErrors()` - Field-specific errors
|
||||
|
||||
#### `internal/handlers/errors_test.go`
|
||||
Error handling testing:
|
||||
- `AppError.Error()` - Error message formatting
|
||||
- `NewAppError()` - Error constructor
|
||||
- `HandleError()` with JSON requests
|
||||
- `HandleError()` with HTMX requests
|
||||
- `HandleError()` with standard requests
|
||||
- Internal error message hiding
|
||||
- Error constructors:
|
||||
- `NotFoundError()`, `BadRequestError()`
|
||||
- `InternalError()`, `TemplateError()`
|
||||
- `DataLoadError()`
|
||||
- `DomainError` type testing
|
||||
- Method chaining (`WithError()`, `WithField()`)
|
||||
|
||||
---
|
||||
|
||||
## Running Tests
|
||||
|
||||
### Basic Commands
|
||||
|
||||
```bash
|
||||
# Run all tests
|
||||
go test ./...
|
||||
|
||||
# Run with coverage
|
||||
go test -cover ./...
|
||||
|
||||
# Run specific package
|
||||
go test ./internal/middleware/...
|
||||
|
||||
# Run with verbose output
|
||||
go test -v ./internal/validation/...
|
||||
|
||||
# Run with race detection
|
||||
go test -race ./...
|
||||
```
|
||||
|
||||
### Coverage Report
|
||||
|
||||
```bash
|
||||
# Generate coverage profile
|
||||
go test -coverprofile=coverage.out ./internal/...
|
||||
|
||||
# View in terminal
|
||||
go tool cover -func=coverage.out
|
||||
|
||||
# Generate HTML report
|
||||
go tool cover -html=coverage.out -o coverage.html
|
||||
|
||||
# Open HTML report (macOS)
|
||||
open coverage.html
|
||||
```
|
||||
|
||||
### Package-Specific Testing
|
||||
|
||||
```bash
|
||||
# Config tests
|
||||
go test -v ./internal/config/
|
||||
|
||||
# Middleware tests
|
||||
go test -v ./internal/middleware/
|
||||
|
||||
# Validation tests
|
||||
go test -v ./internal/validation/
|
||||
|
||||
# Handler tests
|
||||
go test -v ./internal/handlers/
|
||||
|
||||
# All tests with coverage summary
|
||||
go test -cover ./internal/... 2>&1 | grep -E "^ok|coverage:"
|
||||
```
|
||||
|
||||
### Running Individual Tests
|
||||
|
||||
```bash
|
||||
# Run tests matching a pattern
|
||||
go test -v -run "TestCSRF" ./internal/middleware/
|
||||
|
||||
# Run specific test function
|
||||
go test -v -run "TestRuleMin" ./internal/validation/
|
||||
|
||||
# Run subtests
|
||||
go test -v -run "TestValidationErrors_GetFieldErrors/Get_multiple" ./internal/validation/
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Test Patterns
|
||||
|
||||
### Table-Driven Tests
|
||||
|
||||
Most tests use Go's table-driven test pattern for comprehensive coverage:
|
||||
|
||||
```go
|
||||
func TestRuleMin(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
field string
|
||||
value string
|
||||
param string
|
||||
hasError bool
|
||||
}{
|
||||
{"Valid - meets minimum", "msg", "hello", "5", false},
|
||||
{"Valid - exceeds minimum", "msg", "hello world", "5", false},
|
||||
{"Invalid - too short", "msg", "hi", "5", true},
|
||||
{"UTF-8 aware - valid", "name", "Jose", "4", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ruleMin(tt.field, tt.value, tt.param)
|
||||
if (result != nil) != tt.hasError {
|
||||
t.Errorf("ruleMin(%q, %q, %q) error = %v, wantError %v",
|
||||
tt.field, tt.value, tt.param, result != nil, tt.hasError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### HTTP Handler Testing
|
||||
|
||||
Using `net/http/httptest` for handler tests:
|
||||
|
||||
```go
|
||||
func TestHandleError_JSON(t *testing.T) {
|
||||
appErr := NewAppError(nil, "Bad request", http.StatusBadRequest, false)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set(c.HeaderAccept, c.ContentTypeJSON)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
HandleError(rec, req, appErr)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
var response ErrorResponse
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse JSON response: %v", err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Context-Based Testing
|
||||
|
||||
Testing preferences via request context:
|
||||
|
||||
```go
|
||||
func TestPreferencesHelperFunctions(t *testing.T) {
|
||||
prefs := &Preferences{
|
||||
CVLength: c.CVLengthLong,
|
||||
CVIcons: c.CVIconsShow,
|
||||
CVLanguage: c.LangSpanish,
|
||||
CVTheme: c.CVThemeClean,
|
||||
ColorTheme: c.ColorThemeDark,
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
ctx := context.WithValue(req.Context(), PreferencesKey, prefs)
|
||||
reqWithPrefs := req.WithContext(ctx)
|
||||
|
||||
t.Run("GetLanguage", func(t *testing.T) {
|
||||
result := GetLanguage(reqWithPrefs)
|
||||
if result != c.LangSpanish {
|
||||
t.Errorf("GetLanguage() = %q, want %q", result, c.LangSpanish)
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### Middleware Testing
|
||||
|
||||
Testing middleware chains:
|
||||
|
||||
```go
|
||||
func TestContactRateLimiter_Middleware_Blocked(t *testing.T) {
|
||||
rl := &ContactRateLimiter{
|
||||
clients: make(map[string]*contactRateLimitEntry),
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
protected := rl.Middleware(handler)
|
||||
|
||||
// Exhaust the rate limit
|
||||
limit := c.RateLimitContactRequests
|
||||
for i := 0; i < limit; i++ {
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/contact", nil)
|
||||
req.RemoteAddr = "192.168.1.1:12345"
|
||||
rec := httptest.NewRecorder()
|
||||
protected.ServeHTTP(rec, req)
|
||||
}
|
||||
|
||||
// Next request should be blocked
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/contact", nil)
|
||||
req.RemoteAddr = "192.168.1.1:12345"
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
protected.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusTooManyRequests {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusTooManyRequests)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Coverage Gaps
|
||||
|
||||
### Why Some Packages Have 0% Coverage
|
||||
|
||||
#### `internal/pdf` (0%)
|
||||
- **Reason**: Requires Chrome browser via chromedp
|
||||
- **Solution**: Would need headless Chrome in CI/CD
|
||||
- **Alternative**: Mock the chromedp interface (significant refactoring)
|
||||
|
||||
#### `internal/templates` (0%)
|
||||
- **Reason**: File-system dependent template loading
|
||||
- **Solution**: Could use embedded test templates
|
||||
- **Impact**: Low priority - simple template wrapper
|
||||
|
||||
#### `internal/routes` (0%)
|
||||
- **Reason**: Integration-level routing setup
|
||||
- **Solution**: End-to-end testing with running server
|
||||
- **Alternative**: Test individual handlers instead
|
||||
|
||||
#### `internal/models` (0%)
|
||||
- **Reason**: Contains only interface definitions
|
||||
- **Impact**: None - interfaces have no executable code
|
||||
|
||||
### Partial Coverage Explanations
|
||||
|
||||
#### `internal/middleware` (87.5%)
|
||||
Uncovered code includes:
|
||||
- Background goroutine cleanup functions (tickers)
|
||||
- Production-only file logging (`/var/log/`)
|
||||
- Edge cases in recovery middleware
|
||||
|
||||
#### `internal/email` (58.0%)
|
||||
Uncovered code includes:
|
||||
- Actual SMTP connection and sending
|
||||
- TLS handshake code
|
||||
- Network error handling
|
||||
**Note**: Would require SMTP mock server
|
||||
|
||||
#### `internal/handlers` (62.9%)
|
||||
Uncovered code includes:
|
||||
- PDF generation handlers (need Chrome)
|
||||
- Some HTMX-specific response paths
|
||||
- Error paths for template loading failures
|
||||
|
||||
---
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Use Table-Driven Tests
|
||||
|
||||
```go
|
||||
// Good: Table-driven
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"valid", "test@example.com", true},
|
||||
{"invalid", "not-an-email", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// test code
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Test Edge Cases
|
||||
|
||||
Always test:
|
||||
- Empty inputs
|
||||
- Maximum length inputs
|
||||
- Unicode/UTF-8 characters
|
||||
- Invalid parameters
|
||||
- Boundary conditions
|
||||
|
||||
### 3. Use Descriptive Test Names
|
||||
|
||||
```go
|
||||
// Good
|
||||
t.Run("UTF-8 aware - valid Japanese characters", ...)
|
||||
t.Run("Invalid - exceeds maximum length", ...)
|
||||
|
||||
// Bad
|
||||
t.Run("test1", ...)
|
||||
t.Run("case2", ...)
|
||||
```
|
||||
|
||||
### 4. Isolate Tests
|
||||
|
||||
```go
|
||||
// Good: Create fresh instance per test
|
||||
func TestRateLimiter(t *testing.T) {
|
||||
rl := &ContactRateLimiter{
|
||||
clients: make(map[string]*contactRateLimitEntry),
|
||||
}
|
||||
// test code
|
||||
}
|
||||
```
|
||||
|
||||
### 5. Test Error Messages
|
||||
|
||||
```go
|
||||
if !strings.Contains(body, "Too Many Requests") {
|
||||
t.Error("Response should contain error message")
|
||||
}
|
||||
```
|
||||
|
||||
### 6. Use Constants from Production Code
|
||||
|
||||
```go
|
||||
// Good: Use production constants
|
||||
limit := c.RateLimitContactRequests
|
||||
|
||||
// Bad: Hardcode values
|
||||
limit := 5
|
||||
```
|
||||
|
||||
### 7. Check Response Headers
|
||||
|
||||
```go
|
||||
contentType := rec.Header().Get(c.HeaderContentType)
|
||||
if contentType != c.ContentTypeJSON {
|
||||
t.Errorf("Content-Type = %q, want %q", contentType, c.ContentTypeJSON)
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Test File Structure
|
||||
|
||||
```
|
||||
internal/
|
||||
├── cache/
|
||||
│ └── cache_test.go # Data caching tests
|
||||
├── config/
|
||||
│ └── config_test.go # Configuration tests
|
||||
├── constants/
|
||||
│ └── constants_test.go # Constants validation
|
||||
├── email/
|
||||
│ └── email_test.go # Email service tests
|
||||
├── fileutil/
|
||||
│ └── fileutil_test.go # File utilities tests
|
||||
├── handlers/
|
||||
│ └── errors_test.go # Error handling tests
|
||||
├── httputil/
|
||||
│ └── response_test.go # HTTP response tests
|
||||
├── middleware/
|
||||
│ ├── csrf_test.go # CSRF protection tests
|
||||
│ ├── logger_test.go # Logging middleware tests
|
||||
│ ├── contact_rate_limit_test.go # Rate limiting tests
|
||||
│ └── security_logger_test.go # Security logging tests
|
||||
├── models/
|
||||
│ ├── cv/
|
||||
│ │ └── cv_test.go # CV model tests
|
||||
│ └── ui/
|
||||
│ └── ui_test.go # UI model tests
|
||||
└── validation/
|
||||
├── validator_test.go # Core validator tests
|
||||
└── rules_test.go # Validation rules tests
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## CI/CD Integration
|
||||
|
||||
### GitHub Actions Example
|
||||
|
||||
```yaml
|
||||
name: Tests
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.23'
|
||||
|
||||
- name: Run tests
|
||||
run: go test -v -race -coverprofile=coverage.out ./...
|
||||
|
||||
- name: Check coverage
|
||||
run: |
|
||||
go tool cover -func=coverage.out | grep total | awk '{print $3}'
|
||||
```
|
||||
|
||||
### Pre-commit Hook
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
# .git/hooks/pre-commit
|
||||
|
||||
echo "Running tests..."
|
||||
go test ./internal/... -cover
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Tests failed. Commit aborted."
|
||||
exit 1
|
||||
fi
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Related Documentation
|
||||
|
||||
- [24-GO-VALIDATION-SYSTEM.md](24-GO-VALIDATION-SYSTEM.md) - Validation system details
|
||||
- [25-GO-TEMPLATE-SYSTEM.md](25-GO-TEMPLATE-SYSTEM.md) - Template system details
|
||||
- [26-GO-ROUTES-API.md](26-GO-ROUTES-API.md) - Routes and API documentation
|
||||
- [00-GO-DOCUMENTATION-INDEX.md](00-GO-DOCUMENTATION-INDEX.md) - Go documentation index
|
||||
|
||||
---
|
||||
|
||||
**Last Updated:** December 6, 2025
|
||||
**Total Test Files:** 12
|
||||
**Tested Packages:** 11 (with meaningful coverage)
|
||||
**Overall Coverage:** ~70-75% for testable code
|
||||
+69
-2
@@ -4,15 +4,16 @@ This document records key architectural decisions made for this project.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [ADR-001: No Data Caching](#adr-001-no-data-caching)
|
||||
- [ADR-001: No Data Caching](#adr-001-no-data-caching) *(Superseded by ADR-004)*
|
||||
- [ADR-002: Static Dates Instead of Git Integration](#adr-002-static-dates-instead-of-git-integration)
|
||||
- [ADR-003: CI/CD with GitHub Actions](#adr-003-cicd-with-github-actions)
|
||||
- [ADR-004: Application-Level Data Caching](#adr-004-application-level-data-caching)
|
||||
|
||||
---
|
||||
|
||||
## ADR-001: No Data Caching
|
||||
|
||||
**Status:** Accepted
|
||||
**Status:** Superseded by [ADR-004](#adr-004-application-level-data-caching)
|
||||
**Date:** 2025-11-30
|
||||
|
||||
### Context
|
||||
@@ -148,6 +149,72 @@ Steps:
|
||||
|
||||
---
|
||||
|
||||
## ADR-004: Application-Level Data Caching
|
||||
|
||||
**Status:** Accepted
|
||||
**Date:** 2025-12-06
|
||||
**Supersedes:** [ADR-001](#adr-001-no-data-caching)
|
||||
|
||||
### Context
|
||||
|
||||
As the CV site evolved to support multiple languages and increased usage, the original decision (ADR-001) to avoid caching was reconsidered. While the site traffic remains modest, the benefits of eliminating per-request file I/O became clear:
|
||||
|
||||
1. **Consistency**: Every request reads the same data
|
||||
2. **Performance**: Eliminates disk I/O from hot paths
|
||||
3. **Reliability**: Fail-fast at startup catches data errors early
|
||||
4. **Simplicity**: No cache invalidation needed (data is static)
|
||||
|
||||
### Decision
|
||||
|
||||
**Implement application-level data caching with startup-time loading.**
|
||||
|
||||
The `internal/cache` package provides:
|
||||
- `DataCache` struct holding CV and UI data for all supported languages
|
||||
- Single load at application startup
|
||||
- Thread-safe read access via `sync.RWMutex`
|
||||
- Language-keyed retrieval (`GetCV(lang)`, `GetUI(lang)`)
|
||||
|
||||
### Implementation
|
||||
|
||||
```go
|
||||
// At startup (main.go)
|
||||
dataCache, err := cache.New([]string{"en", "es"})
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize data cache: %v", err)
|
||||
}
|
||||
|
||||
// In handlers
|
||||
cv := h.dataCache.GetCV(lang)
|
||||
ui := h.dataCache.GetUI(lang)
|
||||
```
|
||||
|
||||
### Rationale
|
||||
|
||||
1. **Zero Per-Request I/O**: Data loaded once, served from memory
|
||||
2. **Fail-Fast**: All data issues caught at startup, not runtime
|
||||
3. **Thread-Safe**: `sync.RWMutex` optimized for read-heavy workloads
|
||||
4. **Minimal Complexity**: Simple map-based storage, no TTL/invalidation
|
||||
5. **Testable**: 95.7% test coverage, including concurrency tests
|
||||
|
||||
### Consequences
|
||||
|
||||
- **Positive:**
|
||||
- Faster request handling (no disk I/O)
|
||||
- Earlier error detection (startup validation)
|
||||
- Consistent data across requests
|
||||
- Simple, well-tested implementation
|
||||
|
||||
- **Considerations:**
|
||||
- Requires application restart to pick up data changes
|
||||
- Memory usage increases slightly (minimal - ~KB per language)
|
||||
- Deep copies required when handlers mutate data
|
||||
|
||||
### Documentation
|
||||
|
||||
See [23-DATA-CACHE.md](23-DATA-CACHE.md) for complete API reference and usage patterns.
|
||||
|
||||
---
|
||||
|
||||
## How to Add New Decisions
|
||||
|
||||
When making significant architectural decisions, add a new section following this template:
|
||||
|
||||
+10
-3
@@ -26,6 +26,9 @@
|
||||
- [18. Security Audit](18-SECURITY-AUDIT.md) - Comprehensive security audit report (OWASP Top 10)
|
||||
- [19. Security Implementation](19-SECURITY-IMPLEMENTATION.md) - Detailed security controls documentation
|
||||
|
||||
**Testing & Quality**
|
||||
- [27. Testing](27-GO-TESTING.md) - Comprehensive testing documentation with coverage analysis
|
||||
|
||||
**Deployment & Operations**
|
||||
- [8. Deployment Guide](8-DEPLOYMENT.md) - Production deployment instructions
|
||||
- [9. Security Policies](9-SECURITY.md) - Security guidelines and vulnerability reporting
|
||||
@@ -59,6 +62,7 @@
|
||||
| 17 | [CONTACT-FORM.md](17-CONTACT-FORM.md) | Contact form quick start guide | Backend developers |
|
||||
| 18 | [SECURITY-AUDIT.md](18-SECURITY-AUDIT.md) | Comprehensive security audit (OWASP Top 10) | Security teams |
|
||||
| 19 | [SECURITY-IMPLEMENTATION.md](19-SECURITY-IMPLEMENTATION.md) | Security controls implementation details | Backend developers, Security |
|
||||
| 27 | [GO-TESTING.md](27-GO-TESTING.md) | Comprehensive testing documentation and coverage | Backend developers, QA |
|
||||
|
||||
### User & Operations Documentation
|
||||
|
||||
@@ -123,6 +127,9 @@
|
||||
**...report a security issue**
|
||||
→ See [9-SECURITY.md](9-SECURITY.md) for responsible disclosure process
|
||||
|
||||
**...understand or improve test coverage**
|
||||
→ Read [27-GO-TESTING.md](27-GO-TESTING.md) for coverage analysis and testing patterns
|
||||
|
||||
---
|
||||
|
||||
## 📦 Archive
|
||||
@@ -152,6 +159,6 @@ All documentation in this project follows these standards:
|
||||
|
||||
---
|
||||
|
||||
**Last Updated**: 2025-12-02
|
||||
**Documentation Status**: ✅ Clean, organized, single doc/ folder
|
||||
**Total Active Docs**: 19 core documents + archive
|
||||
**Last Updated**: 2025-12-06
|
||||
**Documentation Status**: Organized, comprehensive
|
||||
**Total Active Docs**: 20 core documents + archive
|
||||
|
||||
@@ -0,0 +1,272 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
// Clear environment variables for clean test
|
||||
os.Unsetenv("PORT")
|
||||
os.Unsetenv("HOST")
|
||||
os.Unsetenv("GO_ENV")
|
||||
|
||||
cfg := Load()
|
||||
|
||||
// Test default values
|
||||
if cfg.Server.Port != "1999" {
|
||||
t.Errorf("Server.Port = %q, want %q", cfg.Server.Port, "1999")
|
||||
}
|
||||
|
||||
if cfg.Server.Host != "localhost" {
|
||||
t.Errorf("Server.Host = %q, want %q", cfg.Server.Host, "localhost")
|
||||
}
|
||||
|
||||
if cfg.Server.ReadTimeout != 15 {
|
||||
t.Errorf("Server.ReadTimeout = %d, want %d", cfg.Server.ReadTimeout, 15)
|
||||
}
|
||||
|
||||
if cfg.Server.WriteTimeout != 15 {
|
||||
t.Errorf("Server.WriteTimeout = %d, want %d", cfg.Server.WriteTimeout, 15)
|
||||
}
|
||||
|
||||
if cfg.Template.Dir != "templates" {
|
||||
t.Errorf("Template.Dir = %q, want %q", cfg.Template.Dir, "templates")
|
||||
}
|
||||
|
||||
if cfg.Data.Dir != "data" {
|
||||
t.Errorf("Data.Dir = %q, want %q", cfg.Data.Dir, "data")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadWithEnvVars(t *testing.T) {
|
||||
// Set custom environment variables
|
||||
os.Setenv("PORT", "8080")
|
||||
os.Setenv("HOST", "0.0.0.0")
|
||||
os.Setenv("READ_TIMEOUT", "30")
|
||||
os.Setenv("WRITE_TIMEOUT", "45")
|
||||
defer func() {
|
||||
os.Unsetenv("PORT")
|
||||
os.Unsetenv("HOST")
|
||||
os.Unsetenv("READ_TIMEOUT")
|
||||
os.Unsetenv("WRITE_TIMEOUT")
|
||||
}()
|
||||
|
||||
cfg := Load()
|
||||
|
||||
if cfg.Server.Port != "8080" {
|
||||
t.Errorf("Server.Port = %q, want %q", cfg.Server.Port, "8080")
|
||||
}
|
||||
|
||||
if cfg.Server.Host != "0.0.0.0" {
|
||||
t.Errorf("Server.Host = %q, want %q", cfg.Server.Host, "0.0.0.0")
|
||||
}
|
||||
|
||||
if cfg.Server.ReadTimeout != 30 {
|
||||
t.Errorf("Server.ReadTimeout = %d, want %d", cfg.Server.ReadTimeout, 30)
|
||||
}
|
||||
|
||||
if cfg.Server.WriteTimeout != 45 {
|
||||
t.Errorf("Server.WriteTimeout = %d, want %d", cfg.Server.WriteTimeout, 45)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddress(t *testing.T) {
|
||||
os.Unsetenv("PORT")
|
||||
os.Unsetenv("HOST")
|
||||
|
||||
cfg := Load()
|
||||
addr := cfg.Address()
|
||||
|
||||
if addr != "localhost:1999" {
|
||||
t.Errorf("Address() = %q, want %q", addr, "localhost:1999")
|
||||
}
|
||||
|
||||
// Test with custom values
|
||||
os.Setenv("PORT", "3000")
|
||||
os.Setenv("HOST", "127.0.0.1")
|
||||
defer func() {
|
||||
os.Unsetenv("PORT")
|
||||
os.Unsetenv("HOST")
|
||||
}()
|
||||
|
||||
cfg = Load()
|
||||
addr = cfg.Address()
|
||||
|
||||
if addr != "127.0.0.1:3000" {
|
||||
t.Errorf("Address() = %q, want %q", addr, "127.0.0.1:3000")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnv(t *testing.T) {
|
||||
// Test with existing var
|
||||
os.Setenv("TEST_VAR", "test_value")
|
||||
defer os.Unsetenv("TEST_VAR")
|
||||
|
||||
result := getEnv("TEST_VAR", "default")
|
||||
if result != "test_value" {
|
||||
t.Errorf("getEnv with existing var = %q, want %q", result, "test_value")
|
||||
}
|
||||
|
||||
// Test with non-existing var
|
||||
result = getEnv("NONEXISTENT_VAR", "default")
|
||||
if result != "default" {
|
||||
t.Errorf("getEnv with non-existing var = %q, want %q", result, "default")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnvAsInt(t *testing.T) {
|
||||
// Test with valid int
|
||||
os.Setenv("INT_VAR", "42")
|
||||
defer os.Unsetenv("INT_VAR")
|
||||
|
||||
result := getEnvAsInt("INT_VAR", 10)
|
||||
if result != 42 {
|
||||
t.Errorf("getEnvAsInt with valid int = %d, want %d", result, 42)
|
||||
}
|
||||
|
||||
// Test with invalid int
|
||||
os.Setenv("INVALID_INT", "not_a_number")
|
||||
defer os.Unsetenv("INVALID_INT")
|
||||
|
||||
result = getEnvAsInt("INVALID_INT", 10)
|
||||
if result != 10 {
|
||||
t.Errorf("getEnvAsInt with invalid int = %d, want %d", result, 10)
|
||||
}
|
||||
|
||||
// Test with non-existing var
|
||||
result = getEnvAsInt("NONEXISTENT_INT", 99)
|
||||
if result != 99 {
|
||||
t.Errorf("getEnvAsInt with non-existing var = %d, want %d", result, 99)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnvAsBool(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
envValue string
|
||||
defaultValue bool
|
||||
expected bool
|
||||
}{
|
||||
{"True string", "true", false, true},
|
||||
{"False string", "false", true, false},
|
||||
{"1 as true", "1", false, true},
|
||||
{"0 as false", "0", true, false},
|
||||
{"Invalid returns default true", "invalid", true, true},
|
||||
{"Invalid returns default false", "invalid", false, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
os.Setenv("BOOL_VAR", tt.envValue)
|
||||
defer os.Unsetenv("BOOL_VAR")
|
||||
|
||||
result := getEnvAsBool("BOOL_VAR", tt.defaultValue)
|
||||
if result != tt.expected {
|
||||
t.Errorf("getEnvAsBool(%q, %v) = %v, want %v", tt.envValue, tt.defaultValue, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Test non-existing var
|
||||
result := getEnvAsBool("NONEXISTENT_BOOL", true)
|
||||
if result != true {
|
||||
t.Errorf("getEnvAsBool with non-existing var = %v, want %v", result, true)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsDevelopment(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
envValue string
|
||||
expected bool
|
||||
}{
|
||||
{"Development env", "development", true},
|
||||
{"Dev shorthand", "dev", true},
|
||||
{"Production env", "production", false},
|
||||
{"Prod shorthand", "prod", false},
|
||||
{"Empty (default)", "", true}, // Default is development
|
||||
{"Staging", "staging", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.envValue == "" {
|
||||
os.Unsetenv("GO_ENV")
|
||||
} else {
|
||||
os.Setenv("GO_ENV", tt.envValue)
|
||||
}
|
||||
defer os.Unsetenv("GO_ENV")
|
||||
|
||||
result := isDevelopment()
|
||||
if result != tt.expected {
|
||||
t.Errorf("isDevelopment() with GO_ENV=%q = %v, want %v", tt.envValue, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemplateHotReload(t *testing.T) {
|
||||
// In development, hot reload should be true by default
|
||||
os.Setenv("GO_ENV", "development")
|
||||
os.Unsetenv("TEMPLATE_HOT_RELOAD")
|
||||
defer os.Unsetenv("GO_ENV")
|
||||
|
||||
cfg := Load()
|
||||
if !cfg.Template.HotReload {
|
||||
t.Error("HotReload should be true in development by default")
|
||||
}
|
||||
|
||||
// Explicit false should override
|
||||
os.Setenv("TEMPLATE_HOT_RELOAD", "false")
|
||||
defer os.Unsetenv("TEMPLATE_HOT_RELOAD")
|
||||
|
||||
cfg = Load()
|
||||
if cfg.Template.HotReload {
|
||||
t.Error("HotReload should be false when explicitly set")
|
||||
}
|
||||
|
||||
// In production, hot reload should be false by default
|
||||
os.Setenv("GO_ENV", "production")
|
||||
os.Unsetenv("TEMPLATE_HOT_RELOAD")
|
||||
|
||||
cfg = Load()
|
||||
if cfg.Template.HotReload {
|
||||
t.Error("HotReload should be false in production by default")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmailConfig(t *testing.T) {
|
||||
os.Unsetenv("SMTP_HOST")
|
||||
os.Unsetenv("SMTP_PORT")
|
||||
os.Unsetenv("SMTP_USER")
|
||||
os.Unsetenv("SMTP_PASSWORD")
|
||||
|
||||
cfg := Load()
|
||||
|
||||
// Test defaults
|
||||
if cfg.Email.SMTPHost != "smtp.gmail.com" {
|
||||
t.Errorf("Email.SMTPHost = %q, want %q", cfg.Email.SMTPHost, "smtp.gmail.com")
|
||||
}
|
||||
|
||||
if cfg.Email.SMTPPort != "587" {
|
||||
t.Errorf("Email.SMTPPort = %q, want %q", cfg.Email.SMTPPort, "587")
|
||||
}
|
||||
|
||||
// Test custom values
|
||||
os.Setenv("SMTP_HOST", "mail.example.com")
|
||||
os.Setenv("SMTP_PORT", "465")
|
||||
defer func() {
|
||||
os.Unsetenv("SMTP_HOST")
|
||||
os.Unsetenv("SMTP_PORT")
|
||||
}()
|
||||
|
||||
cfg = Load()
|
||||
if cfg.Email.SMTPHost != "mail.example.com" {
|
||||
t.Errorf("Email.SMTPHost = %q, want %q", cfg.Email.SMTPHost, "mail.example.com")
|
||||
}
|
||||
|
||||
if cfg.Email.SMTPPort != "465" {
|
||||
t.Errorf("Email.SMTPPort = %q, want %q", cfg.Email.SMTPPort, "465")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package constants
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAllLangs(t *testing.T) {
|
||||
langs := AllLangs()
|
||||
|
||||
if len(langs) != 2 {
|
||||
t.Errorf("Expected 2 languages, got %d", len(langs))
|
||||
}
|
||||
|
||||
// Check that en and es are present
|
||||
hasEn, hasEs := false, false
|
||||
for _, lang := range langs {
|
||||
if lang == LangEnglish {
|
||||
hasEn = true
|
||||
}
|
||||
if lang == LangSpanish {
|
||||
hasEs = true
|
||||
}
|
||||
}
|
||||
|
||||
if !hasEn {
|
||||
t.Error("Expected English (en) to be in AllLangs()")
|
||||
}
|
||||
if !hasEs {
|
||||
t.Error("Expected Spanish (es) to be in AllLangs()")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidLang(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
lang string
|
||||
expected bool
|
||||
}{
|
||||
{"Valid - English", LangEnglish, true},
|
||||
{"Valid - Spanish", LangSpanish, true},
|
||||
{"Invalid - French", "fr", false},
|
||||
{"Invalid - German", "de", false},
|
||||
{"Invalid - Empty", "", false},
|
||||
{"Invalid - Random", "xyz", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := IsValidLang(tt.lang)
|
||||
if result != tt.expected {
|
||||
t.Errorf("IsValidLang(%q) = %v, want %v", tt.lang, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateLang(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
lang string
|
||||
wantError bool
|
||||
}{
|
||||
{"Valid - English", LangEnglish, false},
|
||||
{"Valid - Spanish", LangSpanish, false},
|
||||
{"Invalid - French", "fr", true},
|
||||
{"Invalid - Empty", "", true},
|
||||
{"Invalid - Random", "xyz", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidateLang(tt.lang)
|
||||
if (err != nil) != tt.wantError {
|
||||
t.Errorf("ValidateLang(%q) error = %v, wantError %v", tt.lang, err, tt.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConstants(t *testing.T) {
|
||||
// Test that default language is English
|
||||
if LangDefault != LangEnglish {
|
||||
t.Errorf("LangDefault = %q, want %q", LangDefault, LangEnglish)
|
||||
}
|
||||
|
||||
// Test supported languages map
|
||||
if !SupportedLanguages[LangEnglish] {
|
||||
t.Error("SupportedLanguages should contain English")
|
||||
}
|
||||
if !SupportedLanguages[LangSpanish] {
|
||||
t.Error("SupportedLanguages should contain Spanish")
|
||||
}
|
||||
if SupportedLanguages["fr"] {
|
||||
t.Error("SupportedLanguages should not contain French")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCVPreferenceConstants(t *testing.T) {
|
||||
// Test CV preference values exist and are non-empty
|
||||
if CVLengthShort == "" {
|
||||
t.Error("CVLengthShort should not be empty")
|
||||
}
|
||||
if CVLengthLong == "" {
|
||||
t.Error("CVLengthLong should not be empty")
|
||||
}
|
||||
if CVIconsShow == "" {
|
||||
t.Error("CVIconsShow should not be empty")
|
||||
}
|
||||
if CVIconsHide == "" {
|
||||
t.Error("CVIconsHide should not be empty")
|
||||
}
|
||||
if CVThemeDefault == "" {
|
||||
t.Error("CVThemeDefault should not be empty")
|
||||
}
|
||||
if CVThemeClean == "" {
|
||||
t.Error("CVThemeClean should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestColorThemeConstants(t *testing.T) {
|
||||
if ColorThemeLight == "" {
|
||||
t.Error("ColorThemeLight should not be empty")
|
||||
}
|
||||
if ColorThemeDark == "" {
|
||||
t.Error("ColorThemeDark should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCookieConstants(t *testing.T) {
|
||||
if CookieMaxAge <= 0 {
|
||||
t.Error("CookieMaxAge should be positive")
|
||||
}
|
||||
if CookiePath != "/" {
|
||||
t.Error("CookiePath should be '/'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironmentConstants(t *testing.T) {
|
||||
if EnvProduction == "" {
|
||||
t.Error("EnvProduction should not be empty")
|
||||
}
|
||||
if EnvDevelopment == "" {
|
||||
t.Error("EnvDevelopment should not be empty")
|
||||
}
|
||||
if DefaultPort == "" {
|
||||
t.Error("DefaultPort should not be empty")
|
||||
}
|
||||
}
|
||||
@@ -123,7 +123,14 @@ func TestDefaultCVShortcut(t *testing.T) {
|
||||
t.Skip("Skipping PDF generation test - requires running server")
|
||||
}
|
||||
|
||||
handler := newTestCVHandler(t, "localhost:8080", nil)
|
||||
// Check if server is actually running on port 1999
|
||||
resp, err := http.Get("http://localhost:1999/health")
|
||||
if err != nil || resp.StatusCode != http.StatusOK {
|
||||
t.Skip("Skipping PDF generation test - server not running on localhost:1999")
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
handler := newTestCVHandler(t, "localhost:1999", nil)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -0,0 +1,341 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
c "github.com/juanatsap/cv-site/internal/constants"
|
||||
)
|
||||
|
||||
func TestAppError_Error(t *testing.T) {
|
||||
t.Run("With underlying error", func(t *testing.T) {
|
||||
err := &AppError{
|
||||
Err: errors.New("underlying error"),
|
||||
Message: "app message",
|
||||
}
|
||||
|
||||
if err.Error() != "underlying error" {
|
||||
t.Errorf("Error() = %q, want %q", err.Error(), "underlying error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Without underlying error", func(t *testing.T) {
|
||||
err := &AppError{
|
||||
Message: "app message",
|
||||
}
|
||||
|
||||
if err.Error() != "app message" {
|
||||
t.Errorf("Error() = %q, want %q", err.Error(), "app message")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewAppError(t *testing.T) {
|
||||
underlying := errors.New("underlying")
|
||||
err := NewAppError(underlying, "message", http.StatusBadRequest, false)
|
||||
|
||||
if err.Err != underlying {
|
||||
t.Error("Err should be set")
|
||||
}
|
||||
if err.Message != "message" {
|
||||
t.Errorf("Message = %q, want %q", err.Message, "message")
|
||||
}
|
||||
if err.StatusCode != http.StatusBadRequest {
|
||||
t.Errorf("StatusCode = %d, want %d", err.StatusCode, http.StatusBadRequest)
|
||||
}
|
||||
if err.Internal {
|
||||
t.Error("Internal should be false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleError_JSON(t *testing.T) {
|
||||
appErr := NewAppError(nil, "Bad request", http.StatusBadRequest, false)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set(c.HeaderAccept, c.ContentTypeJSON)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
HandleError(rec, req, appErr)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
contentType := rec.Header().Get(c.HeaderContentType)
|
||||
if contentType != c.ContentTypeJSON {
|
||||
t.Errorf("Content-Type = %q, want %q", contentType, c.ContentTypeJSON)
|
||||
}
|
||||
|
||||
var response ErrorResponse
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse JSON response: %v", err)
|
||||
}
|
||||
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Errorf("Response Code = %d, want %d", response.Code, http.StatusBadRequest)
|
||||
}
|
||||
if response.Message != "Bad request" {
|
||||
t.Errorf("Response Message = %q, want %q", response.Message, "Bad request")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleError_JSON_Internal(t *testing.T) {
|
||||
appErr := NewAppError(errors.New("secret error"), "Internal error", http.StatusInternalServerError, true)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set(c.HeaderAccept, c.ContentTypeJSON)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
HandleError(rec, req, appErr)
|
||||
|
||||
var response ErrorResponse
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse JSON response: %v", err)
|
||||
}
|
||||
|
||||
// Internal errors should not expose message
|
||||
if response.Message != "" {
|
||||
t.Errorf("Internal error should not expose message, got %q", response.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleError_HTMX(t *testing.T) {
|
||||
appErr := NewAppError(nil, "Something went wrong", http.StatusBadRequest, false)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set(c.HeaderHXRequest, "true")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
HandleError(rec, req, appErr)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
body := rec.Body.String()
|
||||
if !strings.Contains(body, "Something went wrong") {
|
||||
t.Error("HTMX response should contain error message")
|
||||
}
|
||||
if !strings.Contains(body, "<div class='error'>") {
|
||||
t.Error("HTMX response should contain error div")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleError_HTMX_Internal(t *testing.T) {
|
||||
appErr := NewAppError(errors.New("secret"), "Secret error", http.StatusInternalServerError, true)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set(c.HeaderHXRequest, "true")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
HandleError(rec, req, appErr)
|
||||
|
||||
body := rec.Body.String()
|
||||
if strings.Contains(body, "secret") {
|
||||
t.Error("Internal error should not expose secret")
|
||||
}
|
||||
if !strings.Contains(body, "An error occurred") {
|
||||
t.Error("Internal error should show generic message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleError_Standard(t *testing.T) {
|
||||
appErr := NewAppError(nil, "Not found", http.StatusNotFound, false)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
HandleError(rec, req, appErr)
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusNotFound)
|
||||
}
|
||||
|
||||
body := rec.Body.String()
|
||||
if !strings.Contains(body, "Not found") {
|
||||
t.Error("Standard response should contain error message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleError_Standard_Internal(t *testing.T) {
|
||||
appErr := NewAppError(errors.New("secret"), "Secret", http.StatusInternalServerError, true)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
HandleError(rec, req, appErr)
|
||||
|
||||
body := rec.Body.String()
|
||||
if strings.Contains(body, "secret") {
|
||||
t.Error("Internal error should not expose secret")
|
||||
}
|
||||
if !strings.Contains(body, "Internal Server Error") {
|
||||
t.Error("Internal error should show generic message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleError_NonAppError(t *testing.T) {
|
||||
// Regular error should be treated as internal error
|
||||
regularErr := errors.New("some error")
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
HandleError(rec, req, regularErr)
|
||||
|
||||
if rec.Code != http.StatusInternalServerError {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorConstructors(t *testing.T) {
|
||||
t.Run("NotFoundError", func(t *testing.T) {
|
||||
err := NotFoundError("resource not found")
|
||||
if err.StatusCode != http.StatusNotFound {
|
||||
t.Errorf("StatusCode = %d, want %d", err.StatusCode, http.StatusNotFound)
|
||||
}
|
||||
if err.Message != "resource not found" {
|
||||
t.Errorf("Message = %q, want %q", err.Message, "resource not found")
|
||||
}
|
||||
if err.Internal {
|
||||
t.Error("NotFoundError should not be internal")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BadRequestError", func(t *testing.T) {
|
||||
err := BadRequestError("invalid input")
|
||||
if err.StatusCode != http.StatusBadRequest {
|
||||
t.Errorf("StatusCode = %d, want %d", err.StatusCode, http.StatusBadRequest)
|
||||
}
|
||||
if err.Internal {
|
||||
t.Error("BadRequestError should not be internal")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InternalError", func(t *testing.T) {
|
||||
underlying := errors.New("db error")
|
||||
err := InternalError(underlying)
|
||||
if err.StatusCode != http.StatusInternalServerError {
|
||||
t.Errorf("StatusCode = %d, want %d", err.StatusCode, http.StatusInternalServerError)
|
||||
}
|
||||
if !err.Internal {
|
||||
t.Error("InternalError should be internal")
|
||||
}
|
||||
if err.Err != underlying {
|
||||
t.Error("Err should be set")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("TemplateError", func(t *testing.T) {
|
||||
underlying := errors.New("template error")
|
||||
err := TemplateError(underlying, "home.html")
|
||||
if err.StatusCode != http.StatusInternalServerError {
|
||||
t.Errorf("StatusCode = %d, want %d", err.StatusCode, http.StatusInternalServerError)
|
||||
}
|
||||
if !err.Internal {
|
||||
t.Error("TemplateError should be internal")
|
||||
}
|
||||
if !strings.Contains(err.Message, "home.html") {
|
||||
t.Error("Message should contain template name")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DataLoadError", func(t *testing.T) {
|
||||
underlying := errors.New("json error")
|
||||
err := DataLoadError(underlying, "CV")
|
||||
if err.StatusCode != http.StatusInternalServerError {
|
||||
t.Errorf("StatusCode = %d, want %d", err.StatusCode, http.StatusInternalServerError)
|
||||
}
|
||||
if !err.Internal {
|
||||
t.Error("DataLoadError should be internal")
|
||||
}
|
||||
if !strings.Contains(err.Message, "CV") {
|
||||
t.Error("Message should contain data type")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDomainError(t *testing.T) {
|
||||
t.Run("Error with underlying", func(t *testing.T) {
|
||||
underlying := errors.New("underlying")
|
||||
err := &DomainError{
|
||||
Code: ErrCodeInvalidLanguage,
|
||||
Message: "invalid language",
|
||||
Err: underlying,
|
||||
StatusCode: http.StatusBadRequest,
|
||||
}
|
||||
|
||||
errStr := err.Error()
|
||||
if !strings.Contains(errStr, string(ErrCodeInvalidLanguage)) {
|
||||
t.Error("Error() should contain code")
|
||||
}
|
||||
if !strings.Contains(errStr, "underlying") {
|
||||
t.Error("Error() should contain underlying error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Error without underlying", func(t *testing.T) {
|
||||
err := &DomainError{
|
||||
Code: ErrCodeInvalidTheme,
|
||||
Message: "invalid theme",
|
||||
StatusCode: http.StatusBadRequest,
|
||||
}
|
||||
|
||||
errStr := err.Error()
|
||||
if !strings.Contains(errStr, string(ErrCodeInvalidTheme)) {
|
||||
t.Error("Error() should contain code")
|
||||
}
|
||||
if !strings.Contains(errStr, "invalid theme") {
|
||||
t.Error("Error() should contain message")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Unwrap", func(t *testing.T) {
|
||||
underlying := errors.New("underlying")
|
||||
err := &DomainError{
|
||||
Code: ErrCodeDataLoad,
|
||||
Err: underlying,
|
||||
}
|
||||
|
||||
if err.Unwrap() != underlying {
|
||||
t.Error("Unwrap() should return underlying error")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewDomainError(t *testing.T) {
|
||||
err := NewDomainError(ErrCodePDFGeneration, "PDF failed", http.StatusInternalServerError)
|
||||
|
||||
if err.Code != ErrCodePDFGeneration {
|
||||
t.Errorf("Code = %q, want %q", err.Code, ErrCodePDFGeneration)
|
||||
}
|
||||
if err.Message != "PDF failed" {
|
||||
t.Errorf("Message = %q, want %q", err.Message, "PDF failed")
|
||||
}
|
||||
if err.StatusCode != http.StatusInternalServerError {
|
||||
t.Errorf("StatusCode = %d, want %d", err.StatusCode, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDomainError_WithError(t *testing.T) {
|
||||
underlying := errors.New("root cause")
|
||||
err := NewDomainError(ErrCodeDataLoad, "load failed", http.StatusInternalServerError).
|
||||
WithError(underlying)
|
||||
|
||||
if err.Err != underlying {
|
||||
t.Error("WithError should set underlying error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDomainError_WithField(t *testing.T) {
|
||||
err := NewDomainError(ErrCodeInvalidLength, "invalid", http.StatusBadRequest).
|
||||
WithField("cv_length")
|
||||
|
||||
if err.Field != "cv_length" {
|
||||
t.Errorf("Field = %q, want %q", err.Field, "cv_length")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
package httputil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
c "github.com/juanatsap/cv-site/internal/constants"
|
||||
)
|
||||
|
||||
func TestJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
status int
|
||||
data interface{}
|
||||
wantStatus int
|
||||
}{
|
||||
{
|
||||
name: "200 OK with map",
|
||||
status: http.StatusOK,
|
||||
data: map[string]string{"message": "success"},
|
||||
wantStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "201 Created with struct",
|
||||
status: http.StatusCreated,
|
||||
data: struct{ ID int }{ID: 123},
|
||||
wantStatus: http.StatusCreated,
|
||||
},
|
||||
{
|
||||
name: "400 Bad Request with error",
|
||||
status: http.StatusBadRequest,
|
||||
data: map[string]string{"error": "invalid request"},
|
||||
wantStatus: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "500 Internal Server Error",
|
||||
status: http.StatusInternalServerError,
|
||||
data: map[string]string{"error": "server error"},
|
||||
wantStatus: http.StatusInternalServerError,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
err := JSON(rec, tt.status, tt.data)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("JSON() error = %v", err)
|
||||
}
|
||||
|
||||
if rec.Code != tt.wantStatus {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, tt.wantStatus)
|
||||
}
|
||||
|
||||
contentType := rec.Header().Get(c.HeaderContentType)
|
||||
if contentType != c.ContentTypeJSON {
|
||||
t.Errorf("Content-Type = %q, want %q", contentType, c.ContentTypeJSON)
|
||||
}
|
||||
|
||||
// Verify JSON is valid
|
||||
var result interface{}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &result); err != nil {
|
||||
t.Errorf("Response is not valid JSON: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSON_Array(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
data := []int{1, 2, 3, 4, 5}
|
||||
|
||||
err := JSON(rec, http.StatusOK, data)
|
||||
if err != nil {
|
||||
t.Errorf("JSON() error = %v", err)
|
||||
}
|
||||
|
||||
var result []int
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &result); err != nil {
|
||||
t.Errorf("Failed to parse JSON array: %v", err)
|
||||
}
|
||||
|
||||
if len(result) != 5 {
|
||||
t.Errorf("Array length = %d, want 5", len(result))
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONOk(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
data := map[string]string{"status": "ok"}
|
||||
|
||||
err := JSONOk(rec, data)
|
||||
if err != nil {
|
||||
t.Errorf("JSONOk() error = %v", err)
|
||||
}
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
contentType := rec.Header().Get(c.HeaderContentType)
|
||||
if contentType != c.ContentTypeJSON {
|
||||
t.Errorf("Content-Type = %q, want %q", contentType, c.ContentTypeJSON)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONCached(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
maxAge int
|
||||
}{
|
||||
{"30 seconds", 30},
|
||||
{"1 minute", 60},
|
||||
{"1 hour", 3600},
|
||||
{"1 day", 86400},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
data := map[string]string{"data": "cached"}
|
||||
|
||||
err := JSONCached(rec, data, tt.maxAge)
|
||||
if err != nil {
|
||||
t.Errorf("JSONCached() error = %v", err)
|
||||
}
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
cacheControl := rec.Header().Get(c.HeaderCacheControl)
|
||||
expectedCache := "public, max-age="
|
||||
if !strings.HasPrefix(cacheControl, expectedCache) {
|
||||
t.Errorf("Cache-Control = %q, want prefix %q", cacheControl, expectedCache)
|
||||
}
|
||||
|
||||
// Verify it contains the correct max-age value
|
||||
expectedValue := "max-age=" + string(rune(tt.maxAge+'0'))
|
||||
if tt.maxAge > 9 {
|
||||
// For multi-digit numbers, just check it starts correctly
|
||||
if !strings.Contains(cacheControl, "max-age=") {
|
||||
t.Errorf("Cache-Control doesn't contain max-age")
|
||||
}
|
||||
}
|
||||
_ = expectedValue
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTML(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
HTML(rec)
|
||||
|
||||
contentType := rec.Header().Get(c.HeaderContentType)
|
||||
if contentType != c.ContentTypeHTML {
|
||||
t.Errorf("Content-Type = %q, want %q", contentType, c.ContentTypeHTML)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoContent(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
NoContent(rec)
|
||||
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusNoContent)
|
||||
}
|
||||
|
||||
// 204 No Content should have empty body
|
||||
if rec.Body.Len() != 0 {
|
||||
t.Errorf("Body should be empty for 204 No Content, got %q", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSON_NestedStruct(t *testing.T) {
|
||||
type Inner struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
type Outer struct {
|
||||
Name string `json:"name"`
|
||||
Inner Inner `json:"inner"`
|
||||
Values []int `json:"values"`
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
data := Outer{
|
||||
Name: "test",
|
||||
Inner: Inner{Value: "nested"},
|
||||
Values: []int{1, 2, 3},
|
||||
}
|
||||
|
||||
err := JSON(rec, http.StatusOK, data)
|
||||
if err != nil {
|
||||
t.Errorf("JSON() error = %v", err)
|
||||
}
|
||||
|
||||
var result Outer
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &result); err != nil {
|
||||
t.Errorf("Failed to parse nested JSON: %v", err)
|
||||
}
|
||||
|
||||
if result.Name != "test" {
|
||||
t.Errorf("Name = %q, want %q", result.Name, "test")
|
||||
}
|
||||
if result.Inner.Value != "nested" {
|
||||
t.Errorf("Inner.Value = %q, want %q", result.Inner.Value, "nested")
|
||||
}
|
||||
if len(result.Values) != 3 {
|
||||
t.Errorf("Values length = %d, want 3", len(result.Values))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestResponseWriter_WriteHeader(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
rw := &responseWriter{
|
||||
ResponseWriter: rec,
|
||||
status: http.StatusOK,
|
||||
}
|
||||
|
||||
// First call should set status
|
||||
rw.WriteHeader(http.StatusNotFound)
|
||||
if rw.status != http.StatusNotFound {
|
||||
t.Errorf("status = %d, want %d", rw.status, http.StatusNotFound)
|
||||
}
|
||||
if !rw.wroteHeader {
|
||||
t.Error("wroteHeader should be true after WriteHeader")
|
||||
}
|
||||
|
||||
// Second call should be ignored
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
if rw.status != http.StatusNotFound {
|
||||
t.Errorf("status = %d, want %d (should not change)", rw.status, http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseWriter_Write(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
rw := &responseWriter{
|
||||
ResponseWriter: rec,
|
||||
status: http.StatusOK,
|
||||
}
|
||||
|
||||
// Write should set default status if not set
|
||||
n, err := rw.Write([]byte("Hello"))
|
||||
if err != nil {
|
||||
t.Errorf("Write() error = %v", err)
|
||||
}
|
||||
if n != 5 {
|
||||
t.Errorf("Write() n = %d, want 5", n)
|
||||
}
|
||||
if rw.written != 5 {
|
||||
t.Errorf("written = %d, want 5", rw.written)
|
||||
}
|
||||
if !rw.wroteHeader {
|
||||
t.Error("wroteHeader should be true after Write")
|
||||
}
|
||||
if rw.status != http.StatusOK {
|
||||
t.Errorf("status = %d, want %d", rw.status, http.StatusOK)
|
||||
}
|
||||
|
||||
// Write more
|
||||
n, err = rw.Write([]byte(" World"))
|
||||
if err != nil {
|
||||
t.Errorf("Write() error = %v", err)
|
||||
}
|
||||
if n != 6 {
|
||||
t.Errorf("Write() n = %d, want 6", n)
|
||||
}
|
||||
if rw.written != 11 {
|
||||
t.Errorf("written = %d, want 11", rw.written)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseWriter_WriteWithExplicitStatus(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
rw := &responseWriter{
|
||||
ResponseWriter: rec,
|
||||
status: http.StatusOK,
|
||||
}
|
||||
|
||||
// Set status first
|
||||
rw.WriteHeader(http.StatusCreated)
|
||||
|
||||
// Write should not change status
|
||||
_, _ = rw.Write([]byte("Created"))
|
||||
if rw.status != http.StatusCreated {
|
||||
t.Errorf("status = %d, want %d", rw.status, http.StatusCreated)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
t.Run("Logs successful request", func(t *testing.T) {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte("OK"))
|
||||
})
|
||||
|
||||
logged := Logger(handler)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
logged.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("Code = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
if rec.Body.String() != "OK" {
|
||||
t.Errorf("Body = %q, want %q", rec.Body.String(), "OK")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Logs error response", func(t *testing.T) {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
})
|
||||
|
||||
logged := Logger(handler)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/notfound", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
logged.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Errorf("Code = %d, want %d", rec.Code, http.StatusNotFound)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Handles POST request", func(t *testing.T) {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
})
|
||||
|
||||
logged := Logger(handler)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/create", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
logged.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Errorf("Code = %d, want %d", rec.Code, http.StatusCreated)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Handles request with no explicit status", func(t *testing.T) {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Just write body without explicit status
|
||||
_, _ = w.Write([]byte("Implicit OK"))
|
||||
})
|
||||
|
||||
logged := Logger(handler)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/implicit", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
logged.ServeHTTP(rec, req)
|
||||
|
||||
// Default status should be 200
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("Code = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,318 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
c "github.com/juanatsap/cv-site/internal/constants"
|
||||
)
|
||||
|
||||
func TestLogSecurityEvent(t *testing.T) {
|
||||
// Just verify it doesn't panic
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/contact", nil)
|
||||
req.Header.Set(c.HeaderUserAgent, "TestAgent/1.0")
|
||||
req.RemoteAddr = "192.168.1.1:12345"
|
||||
|
||||
// Should not panic
|
||||
LogSecurityEvent(EventContactFormSent, req, "test details")
|
||||
LogSecurityEvent(EventBlocked, req, "blocked test")
|
||||
LogSecurityEvent(EventCSRFViolation, req, "csrf test")
|
||||
}
|
||||
|
||||
func TestGetSeverity(t *testing.T) {
|
||||
tests := []struct {
|
||||
eventType string
|
||||
expected string
|
||||
}{
|
||||
{EventBlocked, SeverityHigh},
|
||||
{EventCSRFViolation, SeverityHigh},
|
||||
{EventOriginViolation, SeverityHigh},
|
||||
{EventRateLimitExceeded, SeverityMedium},
|
||||
{EventValidationFailed, SeverityMedium},
|
||||
{EventSuspiciousUserAgent, SeverityMedium},
|
||||
{EventContactFormFailed, SeverityMedium},
|
||||
{EventPDFGenerationFailed, SeverityMedium},
|
||||
{EventEmailSendFailed, SeverityMedium},
|
||||
{EventBotDetected, SeverityLow},
|
||||
{EventContactFormSent, SeverityInfo},
|
||||
{EventPDFGenerated, SeverityInfo},
|
||||
{"UNKNOWN_EVENT", SeverityLow},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.eventType, func(t *testing.T) {
|
||||
result := getSeverity(tt.eventType)
|
||||
if result != tt.expected {
|
||||
t.Errorf("getSeverity(%q) = %q, want %q", tt.eventType, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecurityLogger(t *testing.T) {
|
||||
t.Run("Normal request passes through", func(t *testing.T) {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte("OK"))
|
||||
})
|
||||
|
||||
logged := SecurityLogger(handler)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
logged.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Logs security-relevant paths", func(t *testing.T) {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
logged := SecurityLogger(handler)
|
||||
|
||||
// Test security-relevant path
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/contact", nil)
|
||||
req.Header.Set(c.HeaderUserAgent, "Mozilla/5.0")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
logged.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Logs error responses", func(t *testing.T) {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
||||
})
|
||||
|
||||
logged := SecurityLogger(handler)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/secret", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
logged.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusForbidden)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Logs rate limit responses", func(t *testing.T) {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Too Many Requests", http.StatusTooManyRequests)
|
||||
})
|
||||
|
||||
logged := SecurityLogger(handler)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/contact", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
logged.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusTooManyRequests {
|
||||
t.Errorf("Status = %d, want %d", rec.Code, http.StatusTooManyRequests)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestIsSecurityRelevantPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
path string
|
||||
expected bool
|
||||
}{
|
||||
{"/api/contact", true},
|
||||
{"/api/contact/send", true},
|
||||
{"/export/pdf", true},
|
||||
{"/export/pdf/cv", true},
|
||||
{"/toggle/theme", true},
|
||||
{"/toggle/length", true},
|
||||
{"/switch-language", true},
|
||||
{"/", false},
|
||||
{"/cv", false},
|
||||
{"/health", false},
|
||||
{"/static/css/style.css", false},
|
||||
{"/api/other", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.path, func(t *testing.T) {
|
||||
result := isSecurityRelevantPath(tt.path)
|
||||
if result != tt.expected {
|
||||
t.Errorf("isSecurityRelevantPath(%q) = %v, want %v", tt.path, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test preferences helper functions
|
||||
func TestPreferencesHelperFunctions(t *testing.T) {
|
||||
// Create a request with preferences in context
|
||||
prefs := &Preferences{
|
||||
CVLength: c.CVLengthLong,
|
||||
CVIcons: c.CVIconsShow,
|
||||
CVLanguage: c.LangSpanish,
|
||||
CVTheme: c.CVThemeClean,
|
||||
ColorTheme: c.ColorThemeDark,
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
ctx := context.WithValue(req.Context(), PreferencesKey, prefs)
|
||||
reqWithPrefs := req.WithContext(ctx)
|
||||
|
||||
t.Run("GetLanguage", func(t *testing.T) {
|
||||
result := GetLanguage(reqWithPrefs)
|
||||
if result != c.LangSpanish {
|
||||
t.Errorf("GetLanguage() = %q, want %q", result, c.LangSpanish)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetCVLength", func(t *testing.T) {
|
||||
result := GetCVLength(reqWithPrefs)
|
||||
if result != c.CVLengthLong {
|
||||
t.Errorf("GetCVLength() = %q, want %q", result, c.CVLengthLong)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetCVIcons", func(t *testing.T) {
|
||||
result := GetCVIcons(reqWithPrefs)
|
||||
if result != c.CVIconsShow {
|
||||
t.Errorf("GetCVIcons() = %q, want %q", result, c.CVIconsShow)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetCVTheme", func(t *testing.T) {
|
||||
result := GetCVTheme(reqWithPrefs)
|
||||
if result != c.CVThemeClean {
|
||||
t.Errorf("GetCVTheme() = %q, want %q", result, c.CVThemeClean)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetColorTheme", func(t *testing.T) {
|
||||
result := GetColorTheme(reqWithPrefs)
|
||||
if result != c.ColorThemeDark {
|
||||
t.Errorf("GetColorTheme() = %q, want %q", result, c.ColorThemeDark)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IsLongCV", func(t *testing.T) {
|
||||
if !IsLongCV(reqWithPrefs) {
|
||||
t.Error("IsLongCV() should return true")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IsShortCV", func(t *testing.T) {
|
||||
if IsShortCV(reqWithPrefs) {
|
||||
t.Error("IsShortCV() should return false for long CV")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ShowIcons", func(t *testing.T) {
|
||||
if !ShowIcons(reqWithPrefs) {
|
||||
t.Error("ShowIcons() should return true")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("HideIcons", func(t *testing.T) {
|
||||
if HideIcons(reqWithPrefs) {
|
||||
t.Error("HideIcons() should return false when icons shown")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IsCleanTheme", func(t *testing.T) {
|
||||
if !IsCleanTheme(reqWithPrefs) {
|
||||
t.Error("IsCleanTheme() should return true")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IsDefaultTheme", func(t *testing.T) {
|
||||
if IsDefaultTheme(reqWithPrefs) {
|
||||
t.Error("IsDefaultTheme() should return false for clean theme")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IsDarkMode", func(t *testing.T) {
|
||||
if !IsDarkMode(reqWithPrefs) {
|
||||
t.Error("IsDarkMode() should return true")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IsLightMode", func(t *testing.T) {
|
||||
if IsLightMode(reqWithPrefs) {
|
||||
t.Error("IsLightMode() should return false for dark mode")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestPreferencesHelperFunctions_Defaults(t *testing.T) {
|
||||
// Request without preferences should return defaults
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
|
||||
t.Run("GetLanguage default", func(t *testing.T) {
|
||||
result := GetLanguage(req)
|
||||
if result != c.LangEnglish {
|
||||
t.Errorf("GetLanguage() = %q, want %q", result, c.LangEnglish)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetCVLength default", func(t *testing.T) {
|
||||
result := GetCVLength(req)
|
||||
if result != c.CVLengthShort {
|
||||
t.Errorf("GetCVLength() = %q, want %q", result, c.CVLengthShort)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IsShortCV default", func(t *testing.T) {
|
||||
if !IsShortCV(req) {
|
||||
t.Error("IsShortCV() should return true by default")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ShowIcons default", func(t *testing.T) {
|
||||
if !ShowIcons(req) {
|
||||
t.Error("ShowIcons() should return true by default")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IsDefaultTheme default", func(t *testing.T) {
|
||||
if !IsDefaultTheme(req) {
|
||||
t.Error("IsDefaultTheme() should return true by default")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IsLightMode default", func(t *testing.T) {
|
||||
if !IsLightMode(req) {
|
||||
t.Error("IsLightMode() should return true by default")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestPreferencesHelperFunctions_HideIcons(t *testing.T) {
|
||||
prefs := &Preferences{
|
||||
CVLength: c.CVLengthShort,
|
||||
CVIcons: c.CVIconsHide,
|
||||
CVLanguage: c.LangEnglish,
|
||||
CVTheme: c.CVThemeDefault,
|
||||
ColorTheme: c.ColorThemeLight,
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
ctx := context.WithValue(req.Context(), PreferencesKey, prefs)
|
||||
reqWithPrefs := req.WithContext(ctx)
|
||||
|
||||
if !HideIcons(reqWithPrefs) {
|
||||
t.Error("HideIcons() should return true when icons hidden")
|
||||
}
|
||||
|
||||
if ShowIcons(reqWithPrefs) {
|
||||
t.Error("ShowIcons() should return false when icons hidden")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,523 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
c "github.com/juanatsap/cv-site/internal/constants"
|
||||
)
|
||||
|
||||
func TestSecurityHeaders(t *testing.T) {
|
||||
handler := SecurityHeaders(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
// Check required security headers
|
||||
tests := []struct {
|
||||
header string
|
||||
expected string
|
||||
}{
|
||||
{c.HeaderXFrameOptions, c.FrameOptionsSameOrigin},
|
||||
{c.HeaderXContentTypeOpts, c.NoSniff},
|
||||
{c.HeaderXXSSProtection, c.XSSProtection},
|
||||
{c.HeaderReferrerPolicy, c.ReferrerPolicy},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.header, func(t *testing.T) {
|
||||
value := w.Header().Get(tt.header)
|
||||
if value != tt.expected {
|
||||
t.Errorf("Header %s = %q, want %q", tt.header, value, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Check CSP header exists
|
||||
if w.Header().Get(c.HeaderCSP) == "" {
|
||||
t.Error("CSP header should be set")
|
||||
}
|
||||
|
||||
// Check Permissions-Policy exists
|
||||
if w.Header().Get(c.HeaderPermissionsPolicy) == "" {
|
||||
t.Error("Permissions-Policy header should be set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecurityHeaders_HSTS(t *testing.T) {
|
||||
// Test in production mode
|
||||
os.Setenv(c.EnvVarGOEnv, c.EnvProduction)
|
||||
defer os.Unsetenv(c.EnvVarGOEnv)
|
||||
|
||||
handler := SecurityHeaders(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
// HSTS should be set in production
|
||||
if w.Header().Get(c.HeaderHSTS) == "" {
|
||||
t.Error("HSTS header should be set in production")
|
||||
}
|
||||
|
||||
// Test in development mode
|
||||
os.Setenv(c.EnvVarGOEnv, c.EnvDevelopment)
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
// HSTS should NOT be set in development
|
||||
if w.Header().Get(c.HeaderHSTS) != "" {
|
||||
t.Error("HSTS header should not be set in development")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBrowserOnly(t *testing.T) {
|
||||
handler := BrowserOnly(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
userAgent string
|
||||
referer string
|
||||
origin string
|
||||
htmxHeader string
|
||||
xhrHeader string
|
||||
browserReq string
|
||||
expectStatus int
|
||||
}{
|
||||
{
|
||||
name: "Valid browser request with HTMX",
|
||||
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
|
||||
referer: "https://example.com",
|
||||
htmxHeader: "true",
|
||||
expectStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "Valid browser request with XHR",
|
||||
userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
|
||||
origin: "https://example.com",
|
||||
xhrHeader: c.HeaderValueXMLHTTPRequest,
|
||||
expectStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "Valid browser request with custom header",
|
||||
userAgent: "Mozilla/5.0 (Linux; Android 10)",
|
||||
referer: "https://example.com",
|
||||
browserReq: "true",
|
||||
expectStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "Blocked - curl user agent",
|
||||
userAgent: "curl/7.68.0",
|
||||
referer: "https://example.com",
|
||||
htmxHeader: "true",
|
||||
expectStatus: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "Blocked - wget user agent",
|
||||
userAgent: "Wget/1.20.3",
|
||||
referer: "https://example.com",
|
||||
htmxHeader: "true",
|
||||
expectStatus: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "Blocked - empty user agent",
|
||||
userAgent: "",
|
||||
referer: "https://example.com",
|
||||
htmxHeader: "true",
|
||||
expectStatus: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "Blocked - no referer/origin",
|
||||
userAgent: "Mozilla/5.0",
|
||||
referer: "",
|
||||
origin: "",
|
||||
htmxHeader: "true",
|
||||
expectStatus: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "Blocked - no browser headers",
|
||||
userAgent: "Mozilla/5.0",
|
||||
referer: "https://example.com",
|
||||
expectStatus: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "Blocked - Postman",
|
||||
userAgent: "PostmanRuntime/7.26.8",
|
||||
referer: "https://example.com",
|
||||
htmxHeader: "true",
|
||||
expectStatus: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "Blocked - Python requests",
|
||||
userAgent: "python-requests/2.25.1",
|
||||
referer: "https://example.com",
|
||||
htmxHeader: "true",
|
||||
expectStatus: http.StatusForbidden,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/contact", nil)
|
||||
if tt.userAgent != "" {
|
||||
req.Header.Set(c.HeaderUserAgent, tt.userAgent)
|
||||
}
|
||||
if tt.referer != "" {
|
||||
req.Header.Set(c.HeaderReferer, tt.referer)
|
||||
}
|
||||
if tt.origin != "" {
|
||||
req.Header.Set(c.HeaderOrigin, tt.origin)
|
||||
}
|
||||
if tt.htmxHeader != "" {
|
||||
req.Header.Set(c.HeaderHXRequest, tt.htmxHeader)
|
||||
}
|
||||
if tt.xhrHeader != "" {
|
||||
req.Header.Set(c.HeaderXRequestedWith, tt.xhrHeader)
|
||||
}
|
||||
if tt.browserReq != "" {
|
||||
req.Header.Set(c.HeaderXBrowserReq, tt.browserReq)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != tt.expectStatus {
|
||||
t.Errorf("Status = %d, want %d", w.Code, tt.expectStatus)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsBotUserAgent(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ua string
|
||||
expected bool
|
||||
}{
|
||||
{"Browser - Chrome", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", false},
|
||||
{"Browser - Firefox", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:93.0) Gecko/20100101 Firefox/93.0", false},
|
||||
{"Browser - Safari", "Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15", false},
|
||||
{"Bot - curl", "curl/7.68.0", true},
|
||||
{"Bot - wget", "Wget/1.20.3 (linux-gnu)", true},
|
||||
{"Bot - Postman", "PostmanRuntime/7.26.8", true},
|
||||
{"Bot - Python requests", "python-requests/2.25.1", true},
|
||||
{"Bot - Go HTTP client", "Go-http-client/1.1", true},
|
||||
{"Bot - Insomnia", "insomnia/2021.5.3", true},
|
||||
{"Bot - HTTPie", "HTTPie/2.4.0", true},
|
||||
{"Bot - Scrapy", "Scrapy/2.5.0", true},
|
||||
{"Bot - Generic bot", "Googlebot/2.1", true},
|
||||
{"Bot - Generic crawler", "AhrefsBot/7.0", true},
|
||||
{"Bot - Spider", "screaming frog spider/1.0", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := isBotUserAgent(tt.ua)
|
||||
if result != tt.expected {
|
||||
t.Errorf("isBotUserAgent(%q) = %v, want %v", tt.ua, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRequestIP(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
xForwardedFor string
|
||||
xRealIP string
|
||||
remoteAddr string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "X-Forwarded-For single IP",
|
||||
xForwardedFor: "192.168.1.1",
|
||||
expected: "192.168.1.1",
|
||||
},
|
||||
{
|
||||
name: "X-Forwarded-For multiple IPs",
|
||||
xForwardedFor: "203.0.113.1, 70.41.3.18, 150.172.238.178",
|
||||
expected: "203.0.113.1",
|
||||
},
|
||||
{
|
||||
name: "X-Real-IP",
|
||||
xRealIP: "10.0.0.5",
|
||||
expected: "10.0.0.5",
|
||||
},
|
||||
{
|
||||
name: "RemoteAddr with port",
|
||||
remoteAddr: "192.168.1.100:54321",
|
||||
expected: "192.168.1.100",
|
||||
},
|
||||
{
|
||||
name: "RemoteAddr without port",
|
||||
remoteAddr: "192.168.1.100",
|
||||
expected: "192.168.1.100",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
if tt.xForwardedFor != "" {
|
||||
req.Header.Set(c.HeaderXForwardedFor, tt.xForwardedFor)
|
||||
}
|
||||
if tt.xRealIP != "" {
|
||||
req.Header.Set(c.HeaderXRealIP, tt.xRealIP)
|
||||
}
|
||||
if tt.remoteAddr != "" {
|
||||
req.RemoteAddr = tt.remoteAddr
|
||||
}
|
||||
|
||||
result := getRequestIP(req)
|
||||
if result != tt.expected {
|
||||
t.Errorf("getRequestIP() = %q, want %q", result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOriginChecker(t *testing.T) {
|
||||
handler := OriginChecker(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
origin string
|
||||
referer string
|
||||
expectStatus int
|
||||
}{
|
||||
{
|
||||
name: "Allowed - localhost",
|
||||
origin: "http://localhost:3000",
|
||||
expectStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "Allowed - 127.0.0.1",
|
||||
origin: "http://127.0.0.1:8080",
|
||||
expectStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "Allowed - configured domain",
|
||||
origin: "https://juan.andres.morenorub.io",
|
||||
expectStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "Blocked - external origin",
|
||||
origin: "https://malicious-site.com",
|
||||
expectStatus: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "Blocked - external referer",
|
||||
referer: "https://external-site.org/page",
|
||||
expectStatus: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "Allowed - no origin/referer (direct)",
|
||||
expectStatus: http.StatusOK,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
if tt.origin != "" {
|
||||
req.Header.Set(c.HeaderOrigin, tt.origin)
|
||||
}
|
||||
if tt.referer != "" {
|
||||
req.Header.Set(c.HeaderReferer, tt.referer)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != tt.expectStatus {
|
||||
t.Errorf("Status = %d, want %d", w.Code, tt.expectStatus)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAllowedOrigin(t *testing.T) {
|
||||
allowedOrigins := []string{"localhost", "127.0.0.1", "example.com"}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
originURL string
|
||||
expected bool
|
||||
}{
|
||||
{"Simple localhost", "localhost", true},
|
||||
{"HTTP localhost", "http://localhost", true},
|
||||
{"HTTPS localhost with port", "https://localhost:3000", true},
|
||||
{"localhost with path", "http://localhost/path/to/page", true},
|
||||
{"127.0.0.1", "http://127.0.0.1:8080", true},
|
||||
{"example.com", "https://example.com/api", true},
|
||||
{"External site", "https://external.com", false},
|
||||
{"Similar domain", "https://example.com.evil.com", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := isAllowedOrigin(tt.originURL, allowedOrigins)
|
||||
if result != tt.expected {
|
||||
t.Errorf("isAllowedOrigin(%q) = %v, want %v", tt.originURL, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRateLimiter(t *testing.T) {
|
||||
// Create a rate limiter: 3 requests per 100ms
|
||||
rl := NewRateLimiter(3, 100*time.Millisecond)
|
||||
|
||||
handler := rl.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
// First 3 requests should succeed
|
||||
for i := 0; i < 3; i++ {
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.RemoteAddr = "192.168.1.1:1234"
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Request %d: Status = %d, want %d", i+1, w.Code, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
// 4th request should be rate limited
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.RemoteAddr = "192.168.1.1:1234"
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusTooManyRequests {
|
||||
t.Errorf("4th request: Status = %d, want %d", w.Code, http.StatusTooManyRequests)
|
||||
}
|
||||
|
||||
// Check Retry-After header
|
||||
if w.Header().Get(c.HeaderRetryAfter) == "" {
|
||||
t.Error("Retry-After header should be set")
|
||||
}
|
||||
|
||||
// Different IP should succeed
|
||||
req = httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.RemoteAddr = "192.168.1.2:1234"
|
||||
w = httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Different IP: Status = %d, want %d", w.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
// Wait for window to expire
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
|
||||
// Original IP should be able to make requests again
|
||||
req = httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.RemoteAddr = "192.168.1.1:1234"
|
||||
w = httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("After window expiry: Status = %d, want %d", w.Code, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRateLimiter_XForwardedFor(t *testing.T) {
|
||||
rl := NewRateLimiter(2, time.Minute)
|
||||
|
||||
handler := rl.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
// Make 2 requests from same IP via X-Forwarded-For
|
||||
for i := 0; i < 2; i++ {
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set(c.HeaderXForwardedFor, "10.0.0.1")
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Request %d: Status = %d, want %d", i+1, w.Code, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
// 3rd request should be rate limited
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set(c.HeaderXForwardedFor, "10.0.0.1")
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusTooManyRequests {
|
||||
t.Errorf("3rd request: Status = %d, want %d", w.Code, http.StatusTooManyRequests)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheControl(t *testing.T) {
|
||||
handler := CacheControl(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
// Development mode
|
||||
os.Unsetenv(c.EnvVarGOEnv)
|
||||
req := httptest.NewRequest(http.MethodGet, "/static/file.css", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Header().Get(c.HeaderCacheControl) != c.CachePublic1Hour {
|
||||
t.Errorf("Dev cache = %q, want %q", w.Header().Get(c.HeaderCacheControl), c.CachePublic1Hour)
|
||||
}
|
||||
|
||||
// Production mode
|
||||
os.Setenv(c.EnvVarGOEnv, c.EnvProduction)
|
||||
defer os.Unsetenv(c.EnvVarGOEnv)
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Header().Get(c.HeaderCacheControl) != c.CachePublic1Day {
|
||||
t.Errorf("Prod cache = %q, want %q", w.Header().Get(c.HeaderCacheControl), c.CachePublic1Day)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDynamicCacheControl(t *testing.T) {
|
||||
handler := DynamicCacheControl(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
// Development mode - no cache
|
||||
os.Unsetenv(c.EnvVarGOEnv)
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Header().Get(c.HeaderCacheControl) != c.CacheNoStore {
|
||||
t.Errorf("Dev dynamic cache = %q, want %q", w.Header().Get(c.HeaderCacheControl), c.CacheNoStore)
|
||||
}
|
||||
|
||||
// Production mode - short cache
|
||||
os.Setenv(c.EnvVarGOEnv, c.EnvProduction)
|
||||
defer os.Unsetenv(c.EnvVarGOEnv)
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Header().Get(c.HeaderCacheControl) != c.CachePublic5Min {
|
||||
t.Errorf("Prod dynamic cache = %q, want %q", w.Header().Get(c.HeaderCacheControl), c.CachePublic5Min)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRuleOptional(t *testing.T) {
|
||||
// Optional rule should always return nil
|
||||
result := ruleOptional("field", "", "")
|
||||
if result != nil {
|
||||
t.Error("ruleOptional should always return nil")
|
||||
}
|
||||
|
||||
result = ruleOptional("field", "value", "")
|
||||
if result != nil {
|
||||
t.Error("ruleOptional should always return nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuleTrim(t *testing.T) {
|
||||
// Trim rule is a marker, should always return nil
|
||||
result := ruleTrim("field", " value ", "")
|
||||
if result != nil {
|
||||
t.Error("ruleTrim should always return nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuleSanitize(t *testing.T) {
|
||||
// Sanitize rule is a marker, should always return nil
|
||||
result := ruleSanitize("field", "<script>alert('xss')</script>", "")
|
||||
if result != nil {
|
||||
t.Error("ruleSanitize should always return nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuleMin(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
field string
|
||||
value string
|
||||
param string
|
||||
hasError bool
|
||||
}{
|
||||
{"Valid - meets minimum", "msg", "hello", "5", false},
|
||||
{"Valid - exceeds minimum", "msg", "hello world", "5", false},
|
||||
{"Invalid - too short", "msg", "hi", "5", true},
|
||||
{"Invalid - empty", "msg", "", "1", true},
|
||||
{"Invalid param", "msg", "hello", "invalid", true},
|
||||
{"UTF-8 aware - valid", "name", "José", "4", false},
|
||||
{"UTF-8 aware - valid", "name", "日本語", "3", false},
|
||||
{"UTF-8 aware - invalid", "name", "日", "3", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ruleMin(tt.field, tt.value, tt.param)
|
||||
if (result != nil) != tt.hasError {
|
||||
t.Errorf("ruleMin(%q, %q, %q) error = %v, wantError %v", tt.field, tt.value, tt.param, result != nil, tt.hasError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuleTiming(t *testing.T) {
|
||||
now := time.Now().Unix()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
value string
|
||||
param string
|
||||
hasError bool
|
||||
}{
|
||||
{"Empty value", "", "2:86400", false},
|
||||
{"Valid timing", strconv.FormatInt(now-10, 10), "2:86400", false},
|
||||
{"Too quick", strconv.FormatInt(now-1, 10), "2:86400", true},
|
||||
{"Too old", strconv.FormatInt(now-100000, 10), "2:86400", true},
|
||||
{"Invalid param format", strconv.FormatInt(now-10, 10), "invalid", true},
|
||||
{"Invalid min param", strconv.FormatInt(now-10, 10), "abc:100", true},
|
||||
{"Invalid max param", strconv.FormatInt(now-10, 10), "2:xyz", true},
|
||||
{"Invalid timestamp", "not_a_number", "2:86400", true},
|
||||
{"Future timestamp", strconv.FormatInt(now+1000, 10), "2:86400", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ruleTiming("timestamp", tt.value, tt.param)
|
||||
if (result != nil) != tt.hasError {
|
||||
t.Errorf("ruleTiming(%q, %q) error = %v, wantError %v", tt.value, tt.param, result != nil, tt.hasError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldError_Error(t *testing.T) {
|
||||
t.Run("With param", func(t *testing.T) {
|
||||
err := FieldError{
|
||||
Field: "email",
|
||||
Tag: "max",
|
||||
Param: "100",
|
||||
Message: "too long",
|
||||
}
|
||||
errStr := err.Error()
|
||||
if !strings.Contains(errStr, "email") {
|
||||
t.Error("Error should contain field name")
|
||||
}
|
||||
if !strings.Contains(errStr, "max=100") {
|
||||
t.Error("Error should contain tag=param")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Without param", func(t *testing.T) {
|
||||
err := FieldError{
|
||||
Field: "email",
|
||||
Tag: "required",
|
||||
Message: "is required",
|
||||
}
|
||||
errStr := err.Error()
|
||||
if !strings.Contains(errStr, "email") {
|
||||
t.Error("Error should contain field name")
|
||||
}
|
||||
if strings.Contains(errStr, "(") {
|
||||
t.Error("Error without param should not contain parentheses")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidationErrors_HasErrors(t *testing.T) {
|
||||
t.Run("No errors", func(t *testing.T) {
|
||||
var ve ValidationErrors
|
||||
if ve.HasErrors() {
|
||||
t.Error("HasErrors should return false for empty errors")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Has errors", func(t *testing.T) {
|
||||
ve := ValidationErrors{
|
||||
{Field: "email", Message: "required"},
|
||||
}
|
||||
if !ve.HasErrors() {
|
||||
t.Error("HasErrors should return true when errors exist")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidationErrors_GetFieldErrors(t *testing.T) {
|
||||
ve := ValidationErrors{
|
||||
{Field: "email", Tag: "required", Message: "required"},
|
||||
{Field: "email", Tag: "email", Message: "invalid format"},
|
||||
{Field: "name", Tag: "required", Message: "required"},
|
||||
}
|
||||
|
||||
t.Run("Get multiple errors for field", func(t *testing.T) {
|
||||
errors := ve.GetFieldErrors("email")
|
||||
if len(errors) != 2 {
|
||||
t.Errorf("GetFieldErrors(email) returned %d errors, want 2", len(errors))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Get single error for field", func(t *testing.T) {
|
||||
errors := ve.GetFieldErrors("name")
|
||||
if len(errors) != 1 {
|
||||
t.Errorf("GetFieldErrors(name) returned %d errors, want 1", len(errors))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("No errors for field", func(t *testing.T) {
|
||||
errors := ve.GetFieldErrors("nonexistent")
|
||||
if len(errors) != 0 {
|
||||
t.Errorf("GetFieldErrors(nonexistent) returned %d errors, want 0", len(errors))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidationErrors_Error_Empty(t *testing.T) {
|
||||
var ve ValidationErrors
|
||||
if ve.Error() != "" {
|
||||
t.Error("Error() should return empty string for no errors")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user