Files
dance-lessons-coach/pkg/bdd/testserver/config_test.go
Gabriel Radureau 9467fd942c
Some checks failed
CI/CD Pipeline / Build Docker Cache (push) Successful in 8s
CI/CD Pipeline / CI Pipeline (push) Failing after 4m1s
🧪 test: fix unit tests for testserver config
2026-04-10 14:23:43 +02:00

86 lines
3.0 KiB
Go

package testserver
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCreateTestConfig(t *testing.T) {
// Test 1: Default config (no test config file)
t.Run("DefaultConfig", func(t *testing.T) {
cfg := createTestConfig(9999)
assert.Equal(t, "localhost", cfg.Server.Host)
assert.Equal(t, 9999, cfg.Server.Port)
assert.Equal(t, true, cfg.API.V2Enabled, "v2 should be enabled by default")
assert.Equal(t, "default-secret-key-please-change-in-production", cfg.Auth.JWTSecret)
assert.Equal(t, "admin123", cfg.Auth.AdminMasterPassword)
assert.Equal(t, "dance_lessons_coach_bdd_test", cfg.Database.Name)
})
// Test 2: Config with environment variable override should NOT affect test config
t.Run("EnvironmentVariableIsolation", func(t *testing.T) {
// Set environment variables that would normally override config
os.Setenv("DLC_API_V2_ENABLED", "false")
os.Setenv("DLC_AUTH_JWT_SECRET", "env-secret")
defer func() {
os.Unsetenv("DLC_API_V2_ENABLED")
os.Unsetenv("DLC_AUTH_JWT_SECRET")
}()
cfg := createTestConfig(8888)
// These should NOT be affected by environment variables
assert.Equal(t, true, cfg.API.V2Enabled, "v2 should still be enabled despite env var")
assert.Equal(t, "default-secret-key-please-change-in-production", cfg.Auth.JWTSecret, "should use default secret, not env var")
})
// Test 3: Test config file loading
t.Run("TestConfigFileLoading", func(t *testing.T) {
// Create a temporary test config file
testConfig := `server:
host: testhost
port: 1234
api:
v2_enabled: false
auth:
jwt_secret: test-secret
admin_master_password: test-admin
database:
name: test_db
`
tempFile := "test-config-test.yaml"
if err := os.WriteFile(tempFile, []byte(testConfig), 0644); err != nil {
t.Fatal("Failed to create test config file:", err)
}
defer os.Remove(tempFile)
// Set FEATURE env to trigger config file loading
os.Setenv("FEATURE", "test")
defer os.Unsetenv("FEATURE")
// Create a feature-specific config file that points to our test file
featureConfigDir := "features/test"
os.MkdirAll(featureConfigDir, 0755)
defer os.RemoveAll(featureConfigDir)
if err := os.Symlink("../../"+tempFile, featureConfigDir+"/test-test-config.yaml"); err != nil {
t.Fatal("Failed to create symlink:", err)
}
defer os.Remove(featureConfigDir + "/test-test-config.yaml")
cfg := createTestConfig(7777) // This port should override config file port
// Values from config file should be used, except port which is overridden by parameter
assert.Equal(t, "testhost", cfg.Server.Host)
assert.Equal(t, 7777, cfg.Server.Port, "parameter port should override config file port")
assert.Equal(t, false, cfg.API.V2Enabled, "v2_enabled from config file should be used")
assert.Equal(t, "test-secret", cfg.Auth.JWTSecret, "jwt_secret from config file should be used")
assert.Equal(t, "test-admin", cfg.Auth.AdminMasterPassword, "admin_master_password from config file should be used")
assert.Equal(t, "test_db", cfg.Database.Name, "database name from config file should be used")
})
}