Skip to content

Commit 238bb7f

Browse files
Add tests for handlers package
1 parent 5ac4da4 commit 238bb7f

12 files changed

Lines changed: 1129 additions & 34 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24.6
44

55
require (
66
firebase.google.com/go/v4 v4.18.0
7+
github.com/alicebob/miniredis/v2 v2.35.0
78
github.com/redis/go-redis/v9 v9.12.1
89
github.com/stretchr/testify v1.11.1
910
google.golang.org/api v0.248.0
@@ -25,7 +26,6 @@ require (
2526
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.51.0 // indirect
2627
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.51.0 // indirect
2728
github.com/MicahParks/keyfunc v1.9.0 // indirect
28-
github.com/alicebob/miniredis/v2 v2.35.0 // indirect
2929
github.com/cespare/xxhash/v2 v2.3.0 // indirect
3030
github.com/cncf/xds/go v0.0.0-20250501225837-2ac532fd4443 // indirect
3131
github.com/davecgh/go-spew v1.1.1 // indirect

internal/auth/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,4 @@ func TestConfig_Validate(t *testing.T) {
111111
err := config.Validate()
112112
assert.NoError(t, err)
113113
})
114-
}
114+
}

internal/auth/logger_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func TestLogFormat_Constants(t *testing.T) {
188188
// Test round-trip conversion for LogLevel
189189
func TestLogLevel_RoundTrip(t *testing.T) {
190190
levels := []LogLevel{LogLevelDebug, LogLevelInfo, LogLevelWarn, LogLevelError}
191-
191+
192192
for _, level := range levels {
193193
str := level.String()
194194
parsed := ParseLogLevel(str)
@@ -199,10 +199,10 @@ func TestLogLevel_RoundTrip(t *testing.T) {
199199
// Test round-trip conversion for LogFormat
200200
func TestLogFormat_RoundTrip(t *testing.T) {
201201
formats := []LogFormat{LogFormatJSON, LogFormatText}
202-
202+
203203
for _, format := range formats {
204204
str := format.String()
205205
parsed := ParseLogFormat(str)
206206
assert.Equal(t, format, parsed, "Round-trip failed for format %v", format)
207207
}
208-
}
208+
}

internal/auth/provider_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ func (suite *AuthGuardTestSuite) TestValidateMultiAuth_ValidationFailure_UserErr
651651
suite.mockProvider.On("Validate", ctx, authCtx).Return(nil, userErr)
652652
suite.mockMetrics.On("ObserveValidationDuration", "firebase", mock.AnythingOfType("time.Duration"))
653653
suite.mockMetrics.On("IncValidationAttempts", "failure")
654-
suite.mockLogger.On("Debug", "authentication validation failed in multi-auth",
654+
suite.mockLogger.On("Debug", "authentication validation failed in multi-auth",
655655
"provider", "firebase", "providers", []string{"firebase"}, "error", userErr)
656656

657657
providerTypes := []ProviderType{ProviderTypeFirebase}
@@ -678,7 +678,7 @@ func (suite *AuthGuardTestSuite) TestValidateMultiAuth_ValidationFailure_SystemE
678678
suite.mockProvider.On("Validate", ctx, authCtx).Return(nil, systemErr)
679679
suite.mockMetrics.On("ObserveValidationDuration", "firebase", mock.AnythingOfType("time.Duration"))
680680
suite.mockMetrics.On("IncValidationAttempts", "failure")
681-
suite.mockLogger.On("Error", "authentication validation failed in multi-auth",
681+
suite.mockLogger.On("Error", "authentication validation failed in multi-auth",
682682
"provider", "firebase", "providers", []string{"firebase"}, "error", systemErr)
683683

684684
providerTypes := []ProviderType{ProviderTypeFirebase}

internal/cache/factory_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestNewCache(t *testing.T) {
6060

6161
assert.NoError(t, err)
6262
assert.NotNil(t, cache)
63-
63+
6464
// Verify it's a memory cache
6565
memCache, ok := cache.(*MemoryCache)
6666
assert.True(t, ok)
@@ -87,7 +87,7 @@ func TestNewCache(t *testing.T) {
8787

8888
assert.NoError(t, err)
8989
assert.NotNil(t, cache)
90-
90+
9191
// Should fallback to memory cache
9292
_, ok := cache.(*MemoryCache)
9393
assert.True(t, ok)
@@ -116,7 +116,7 @@ func TestNewCache(t *testing.T) {
116116

117117
assert.NoError(t, err)
118118
assert.NotNil(t, cache)
119-
119+
120120
// Should fallback to memory cache due to connection failure
121121
_, ok := cache.(*MemoryCache)
122122
assert.True(t, ok)
@@ -141,7 +141,7 @@ func TestNewCache(t *testing.T) {
141141

142142
assert.NoError(t, err)
143143
assert.NotNil(t, cache)
144-
144+
145145
// Should default to memory cache
146146
_, ok := cache.(*MemoryCache)
147147
assert.True(t, ok)
@@ -168,7 +168,7 @@ func TestCreateRedisCache(t *testing.T) {
168168

169169
assert.NoError(t, err)
170170
assert.NotNil(t, cache)
171-
171+
172172
// Should be memory cache
173173
_, ok := cache.(*MemoryCache)
174174
assert.True(t, ok)
@@ -196,7 +196,7 @@ func TestCreateRedisCache(t *testing.T) {
196196

197197
assert.NoError(t, err)
198198
assert.NotNil(t, cache)
199-
199+
200200
// Should fallback to memory cache
201201
_, ok := cache.(*MemoryCache)
202202
assert.True(t, ok)
@@ -251,7 +251,7 @@ func TestCreateMemoryCache(t *testing.T) {
251251

252252
assert.NoError(t, err)
253253
assert.NotNil(t, cache)
254-
254+
255255
memCache, ok := cache.(*MemoryCache)
256256
assert.True(t, ok)
257257
assert.Equal(t, 500, memCache.maxKeys)
@@ -266,7 +266,7 @@ func TestCreateMemoryCache(t *testing.T) {
266266
mockLogger.On("Info", "memory cache initialized successfully")
267267

268268
config := auth.CacheConfig{
269-
MaxKeys: -100, // Invalid
269+
MaxKeys: -100, // Invalid
270270
CleanupInterval: time.Duration(-1), // Invalid
271271
}
272272

@@ -275,7 +275,7 @@ func TestCreateMemoryCache(t *testing.T) {
275275
// Should still work due to defaults in NewMemoryCache
276276
assert.NoError(t, err)
277277
assert.NotNil(t, cache)
278-
278+
279279
memCache, ok := cache.(*MemoryCache)
280280
assert.True(t, ok)
281281
// Should use defaults
@@ -285,4 +285,4 @@ func TestCreateMemoryCache(t *testing.T) {
285285
_ = cache.Close()
286286
mockLogger.AssertExpectations(t)
287287
})
288-
}
288+
}

internal/cache/redis_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ func TestRedisCache_Operations(t *testing.T) {
243243
assert.Equal(t, int64(2), stats.Keys)
244244

245245
// Test some operations to check hits/misses
246-
_, _ = cache.Get(ctx, "stats-key-1") // Hit
247-
_, _ = cache.Get(ctx, "nonexistent") // Miss
246+
_, _ = cache.Get(ctx, "stats-key-1") // Hit
247+
_, _ = cache.Get(ctx, "nonexistent") // Miss
248248

249249
// Note: hits/misses are tracked in the cache struct, not Redis
250250
})
@@ -372,4 +372,4 @@ func TestRedisCache_EdgeCases(t *testing.T) {
372372
assert.NoError(t, err)
373373
assert.Equal(t, []byte{}, value) // Redis stores empty string for nil
374374
})
375-
}
375+
}

internal/config/loader_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,18 @@ func TestLoader_Load_WithEnvironmentOverrides(t *testing.T) {
127127

128128
// Set environment variables
129129
envVars := map[string]string{
130-
"TEST_SERVER_PORT": "3000",
131-
"TEST_SERVER_HOST": "127.0.0.1",
130+
"TEST_SERVER_PORT": "3000",
131+
"TEST_SERVER_HOST": "127.0.0.1",
132132
"TEST_SERVER_READ_TIMEOUT": "5s",
133133
"TEST_SERVER_WRITE_TIMEOUT": "7s",
134-
"TEST_LOG_LEVEL": "error",
135-
"TEST_LOG_FORMAT": "text",
136-
"TEST_METRICS_ENABLED": "false",
137-
"TEST_PROVIDERS": "firebase,ip_whitelist",
138-
"TEST_CACHE_TYPE": "redis",
139-
"TEST_REDIS_URL": "redis://localhost:6379/2",
140-
"TEST_REDIS_PASSWORD": "testpass",
141-
"TEST_REDIS_DB": "3",
134+
"TEST_LOG_LEVEL": "error",
135+
"TEST_LOG_FORMAT": "text",
136+
"TEST_METRICS_ENABLED": "false",
137+
"TEST_PROVIDERS": "firebase,ip_whitelist",
138+
"TEST_CACHE_TYPE": "redis",
139+
"TEST_REDIS_URL": "redis://localhost:6379/2",
140+
"TEST_REDIS_PASSWORD": "testpass",
141+
"TEST_REDIS_DB": "3",
142142
}
143143

144144
for key, value := range envVars {

internal/handlers/handlers.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import (
1010
"authguard/internal/auth"
1111
)
1212

13+
// AuthGuardInterface defines the interface for authentication guard operations
14+
type AuthGuardInterface interface {
15+
ValidateAuth(ctx context.Context, providerType auth.ProviderType, authCtx *auth.AuthContext) (*auth.UserClaims, error)
16+
ValidateMultiAuth(ctx context.Context, providerTypes []auth.ProviderType, authCtx *auth.AuthContext) (*auth.UserClaims, error)
17+
Health(ctx context.Context) map[string]error
18+
}
19+
1320
// HealthStatus represents health check status
1421
type HealthStatus int
1522

@@ -40,14 +47,14 @@ func (h HealthStatus) MarshalJSON() ([]byte, error) {
4047

4148
// Handlers contains all HTTP handlers with shared dependencies
4249
type Handlers struct {
43-
authGuard *auth.AuthGuard
50+
authGuard AuthGuardInterface
4451
cache auth.Cache
4552
logger auth.Logger
4653
metrics auth.Metrics
4754
}
4855

4956
// NewHandlers creates a new handlers instance with injected dependencies
50-
func NewHandlers(authGuard *auth.AuthGuard, cache auth.Cache, logger auth.Logger, metrics auth.Metrics) *Handlers {
57+
func NewHandlers(authGuard AuthGuardInterface, cache auth.Cache, logger auth.Logger, metrics auth.Metrics) *Handlers {
5158
return &Handlers{
5259
authGuard: authGuard,
5360
cache: cache,

0 commit comments

Comments
 (0)