83 lines
2.9 KiB
Go
83 lines
2.9 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
|
|
`
|
|
|
|
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 be overridden by config file
|
|
|
|
// Values from config file should be used
|
|
assert.Equal(t, "testhost", cfg.Server.Host)
|
|
assert.Equal(t, 1234, cfg.Server.Port, "port from config file should override parameter")
|
|
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")
|
|
})
|
|
}
|