Initial commit: Custom Start Page application with authentication and DynamoDB storage
This commit is contained in:
191
pkg/config/config_test.go
Normal file
191
pkg/config/config_test.go
Normal file
@@ -0,0 +1,191 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
// Set required environment variables
|
||||
os.Setenv("GOOGLE_CLIENT_ID", "test-client-id")
|
||||
os.Setenv("GOOGLE_CLIENT_SECRET", "test-client-secret")
|
||||
defer func() {
|
||||
os.Unsetenv("GOOGLE_CLIENT_ID")
|
||||
os.Unsetenv("GOOGLE_CLIENT_SECRET")
|
||||
}()
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load() failed: %v", err)
|
||||
}
|
||||
|
||||
// Test default values
|
||||
if cfg.Server.Port != "8080" {
|
||||
t.Errorf("Expected default port 8080, got %s", cfg.Server.Port)
|
||||
}
|
||||
|
||||
if cfg.Server.Host != "localhost" {
|
||||
t.Errorf("Expected default host localhost, got %s", cfg.Server.Host)
|
||||
}
|
||||
|
||||
if cfg.Database.Region != "us-east-1" {
|
||||
t.Errorf("Expected default region us-east-1, got %s", cfg.Database.Region)
|
||||
}
|
||||
|
||||
if cfg.OAuth.Google.ClientID != "test-client-id" {
|
||||
t.Errorf("Expected client ID test-client-id, got %s", cfg.OAuth.Google.ClientID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadMissingOAuthCredentials(t *testing.T) {
|
||||
// Ensure OAuth credentials are not set
|
||||
os.Unsetenv("GOOGLE_CLIENT_ID")
|
||||
os.Unsetenv("GOOGLE_CLIENT_SECRET")
|
||||
|
||||
_, err := Load()
|
||||
if err == nil {
|
||||
t.Error("Expected error when OAuth credentials are missing, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnv(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
key string
|
||||
defaultValue string
|
||||
envValue string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "returns environment variable when set",
|
||||
key: "TEST_KEY",
|
||||
defaultValue: "default",
|
||||
envValue: "custom",
|
||||
expected: "custom",
|
||||
},
|
||||
{
|
||||
name: "returns default when environment variable not set",
|
||||
key: "TEST_KEY_NOT_SET",
|
||||
defaultValue: "default",
|
||||
envValue: "",
|
||||
expected: "default",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.envValue != "" {
|
||||
os.Setenv(tt.key, tt.envValue)
|
||||
defer os.Unsetenv(tt.key)
|
||||
}
|
||||
|
||||
result := getEnv(tt.key, tt.defaultValue)
|
||||
if result != tt.expected {
|
||||
t.Errorf("Expected %s, got %s", tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnvBool(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
key string
|
||||
defaultValue bool
|
||||
envValue string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "returns true when set to true",
|
||||
key: "TEST_BOOL",
|
||||
defaultValue: false,
|
||||
envValue: "true",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "returns false when set to false",
|
||||
key: "TEST_BOOL",
|
||||
defaultValue: true,
|
||||
envValue: "false",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "returns default when not set",
|
||||
key: "TEST_BOOL_NOT_SET",
|
||||
defaultValue: true,
|
||||
envValue: "",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "returns default when invalid value",
|
||||
key: "TEST_BOOL",
|
||||
defaultValue: true,
|
||||
envValue: "invalid",
|
||||
expected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.envValue != "" {
|
||||
os.Setenv(tt.key, tt.envValue)
|
||||
defer os.Unsetenv(tt.key)
|
||||
} else {
|
||||
os.Unsetenv(tt.key)
|
||||
}
|
||||
|
||||
result := getEnvBool(tt.key, tt.defaultValue)
|
||||
if result != tt.expected {
|
||||
t.Errorf("Expected %v, got %v", tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnvInt(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
key string
|
||||
defaultValue int
|
||||
envValue string
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
name: "returns integer when valid",
|
||||
key: "TEST_INT",
|
||||
defaultValue: 100,
|
||||
envValue: "200",
|
||||
expected: 200,
|
||||
},
|
||||
{
|
||||
name: "returns default when not set",
|
||||
key: "TEST_INT_NOT_SET",
|
||||
defaultValue: 100,
|
||||
envValue: "",
|
||||
expected: 100,
|
||||
},
|
||||
{
|
||||
name: "returns default when invalid value",
|
||||
key: "TEST_INT",
|
||||
defaultValue: 100,
|
||||
envValue: "invalid",
|
||||
expected: 100,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.envValue != "" {
|
||||
os.Setenv(tt.key, tt.envValue)
|
||||
defer os.Unsetenv(tt.key)
|
||||
} else {
|
||||
os.Unsetenv(tt.key)
|
||||
}
|
||||
|
||||
result := getEnvInt(tt.key, tt.defaultValue)
|
||||
if result != tt.expected {
|
||||
t.Errorf("Expected %d, got %d", tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user