Files
traefikoidc/main_simple_test.go
T
lukaszraczylo c3f23cb99b Release 0.7.5 (#70)
* Resolve issue with opaque tokens not being parsed correctly

* Increase test coverage

* Further improvements to test coverage and code quality

* Add new providers.

* fixup! Add new providers.

* Cleanup.

* fixup! Cleanup.

* fixup! fixup! Cleanup.

* fixup! fixup! fixup! Cleanup.

* fixup! fixup! fixup! fixup! Cleanup.

* Memory management optimisation

24 bytes per Put < 256-4096 bytes per buffer allocation avoided (10-170x difference)

* Pooling cleanup.
2025-10-01 12:13:10 +01:00

176 lines
4.6 KiB
Go

package traefikoidc
import (
"os"
"testing"
)
// TestIsTestMode tests the isTestMode function
func TestIsTestMode(t *testing.T) {
// Save original environment
originalSuppressLogs := os.Getenv("SUPPRESS_DIAGNOSTIC_LOGS")
originalGoTest := os.Getenv("GO_TEST")
defer func() {
os.Setenv("SUPPRESS_DIAGNOSTIC_LOGS", originalSuppressLogs)
os.Setenv("GO_TEST", originalGoTest)
}()
tests := []struct {
name string
suppressDiagnostics string
goTestEnv string
description string
}{
{
name: "SUPPRESS_DIAGNOSTIC_LOGS=1",
suppressDiagnostics: "1",
goTestEnv: "",
description: "Should return true when diagnostic logs are suppressed",
},
{
name: "GO_TEST=1",
suppressDiagnostics: "",
goTestEnv: "1",
description: "Should return true when GO_TEST is set",
},
{
name: "Both environment variables set",
suppressDiagnostics: "1",
goTestEnv: "1",
description: "Should return true when both env vars are set",
},
{
name: "No environment variables",
suppressDiagnostics: "",
goTestEnv: "",
description: "Should detect test mode from binary name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set environment variables
os.Setenv("SUPPRESS_DIAGNOSTIC_LOGS", tt.suppressDiagnostics)
os.Setenv("GO_TEST", tt.goTestEnv)
// Call function
result := isTestMode()
// The result should always be true during testing because
// os.Args[0] contains ".test" when running via go test
if !result {
t.Error("Expected isTestMode to return true during testing")
}
})
}
}
// TestIsTestMode_DefaultBehavior tests default detection
func TestIsTestMode_DefaultBehavior(t *testing.T) {
// Clear test-related environment variables
os.Unsetenv("SUPPRESS_DIAGNOSTIC_LOGS")
os.Unsetenv("GO_TEST")
// Function should still detect test mode from os.Args[0] or runtime
result := isTestMode()
if !result {
t.Error("Expected isTestMode to return true when running tests")
}
}
// TestVerifyAudience tests the verifyAudience function
func TestVerifyAudience(t *testing.T) {
tests := []struct {
name string
tokenAudience interface{}
expectedAudience string
expectError bool
description string
}{
{
name: "Audience matches",
tokenAudience: "test-client-id",
expectedAudience: "test-client-id",
expectError: false,
description: "Should pass when audience matches",
},
{
name: "Audience array contains expected",
tokenAudience: []interface{}{"other", "test-client-id", "another"},
expectedAudience: "test-client-id",
expectError: false,
description: "Should pass when audience array contains expected",
},
{
name: "Nil audience",
tokenAudience: nil,
expectedAudience: "test-client-id",
expectError: true,
description: "Should fail when audience is nil",
},
{
name: "Audience doesn't match",
tokenAudience: "different-client-id",
expectedAudience: "test-client-id",
expectError: true,
description: "Should fail when audience doesn't match",
},
{
name: "Audience array doesn't contain expected",
tokenAudience: []interface{}{"other", "another"},
expectedAudience: "test-client-id",
expectError: true,
description: "Should fail when audience array doesn't contain expected",
},
{
name: "Invalid audience type",
tokenAudience: 12345,
expectedAudience: "test-client-id",
expectError: true,
description: "Should fail when audience is not string or array",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := verifyAudience(tt.tokenAudience, tt.expectedAudience)
if tt.expectError {
if err == nil {
t.Errorf("Expected error for test case: %s", tt.description)
}
} else {
if err != nil {
t.Errorf("Unexpected error for test case: %s, error: %v", tt.description, err)
}
}
})
}
}
// Benchmark tests
func BenchmarkIsTestMode(b *testing.B) {
for i := 0; i < b.N; i++ {
isTestMode()
}
}
func BenchmarkVerifyAudience_String(b *testing.B) {
audience := "test-client-id"
expected := "test-client-id"
b.ResetTimer()
for i := 0; i < b.N; i++ {
verifyAudience(audience, expected)
}
}
func BenchmarkVerifyAudience_Array(b *testing.B) {
audience := []interface{}{"other", "test-client-id", "another"}
expected := "test-client-id"
b.ResetTimer()
for i := 0; i < b.N; i++ {
verifyAudience(audience, expected)
}
}