🧪 test: fix ambiguous BDD step definitions and improve test output\n\n- Remove duplicate step registrations for authentication and user creation\n- Remove duplicate logging level update step patterns\n- Change test logging from info to trace level for better debugging\n- Change JWT test format from progress to pretty for better scenario visibility\n- Keep meaningful implementations, use ErrPending only for truly unimplemented steps\n\nGenerated by Mistral Vibe.\nCo-Authored-By: Mistral Vibe <vibe@mistral.ai>

This commit is contained in:
2026-04-10 08:42:46 +02:00
parent de2e03519e
commit 0011bed168
13 changed files with 237 additions and 89 deletions

View File

@@ -0,0 +1,82 @@
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")
})
}

View File

@@ -18,8 +18,6 @@ import (
"github.com/spf13/viper"
)
// getPostgresHost returns the appropriate PostgreSQL host based on environment
type Server struct {
httpServer *http.Server
port int
@@ -233,6 +231,15 @@ func (s *Server) initDBConnection() error {
cfg.Database.SSLMode,
)
// Log the database configuration being used
log.Debug().
Str("host", cfg.Database.Host).
Int("port", cfg.Database.Port).
Str("user", cfg.Database.User).
Str("dbname", cfg.Database.Name).
Str("sslmode", cfg.Database.SSLMode).
Msg("Database connection initialized with test configuration")
var dbErr error
s.db, dbErr = sql.Open("postgres", dsn)
if dbErr != nil {
@@ -439,64 +446,62 @@ func createTestConfig(port int) *config.Config {
cfg.Auth.AdminMasterPassword = "admin123"
}
log.Debug().Str("config", configPath).Msg("Using test config file")
log.Debug().
Str("config", configPath).
Str("db_host", cfg.Database.Host).
Int("db_port", cfg.Database.Port).
Str("db_user", cfg.Database.User).
Str("db_name", cfg.Database.Name).
Bool("v2flag", cfg.API.V2Enabled).
Msg("Using test config file")
return &cfg
}
}
}
}
// No test config file, use default config
cfg, err := config.LoadConfig()
if err != nil {
log.Warn().Err(err).Msg("Failed to load config, using defaults")
// Fallback to defaults if config loading fails
return &config.Config{
Server: config.ServerConfig{
Host: "localhost",
Port: port,
},
Shutdown: config.ShutdownConfig{
Timeout: 5 * time.Second,
},
Logging: config.LoggingConfig{
JSON: false,
Level: "trace",
},
Telemetry: config.TelemetryConfig{
Enabled: false,
},
API: config.APIConfig{
V2Enabled: true, // Enable v2 by default for most tests
},
Auth: config.AuthConfig{
JWTSecret: "default-secret-key-please-change-in-production",
AdminMasterPassword: "admin123",
},
Database: config.DatabaseConfig{
Host: "localhost", // Fallback if env vars not set
Port: 5432,
User: "postgres",
Password: "postgres",
Name: "dance_lessons_coach_bdd_test", // Separate BDD test database
SSLMode: "disable",
MaxOpenConns: 10,
MaxIdleConns: 5,
ConnMaxLifetime: time.Hour,
},
}
}
// No test config file found, use hardcoded test defaults
// This ensures test suite has complete control and isn't affected by
// environment variables or main config file settings
log.Debug().
Str("db_host", "localhost").
Int("db_port", 5432).
Str("db_user", "postgres").
Str("db_name", "dance_lessons_coach_bdd_test").
Msg("No test config file found, using hardcoded test defaults")
// Override server port for testing
cfg.Server.Port = port
// Set default auth values if not configured
if cfg.Auth.JWTSecret == "" {
cfg.Auth.JWTSecret = "default-secret-key-please-change-in-production"
return &config.Config{
Server: config.ServerConfig{
Host: "localhost",
Port: port,
},
Shutdown: config.ShutdownConfig{
Timeout: 5 * time.Second,
},
Logging: config.LoggingConfig{
JSON: false,
Level: "trace",
},
Telemetry: config.TelemetryConfig{
Enabled: false,
},
API: config.APIConfig{
V2Enabled: true, // Enable v2 by default for most tests
},
Auth: config.AuthConfig{
JWTSecret: "default-secret-key-please-change-in-production",
AdminMasterPassword: "admin123",
},
Database: config.DatabaseConfig{
Host: "localhost", // Fallback if env vars not set
Port: 5432,
User: "postgres",
Password: "postgres",
Name: "dance_lessons_coach_bdd_test", // Separate BDD test database
SSLMode: "disable",
MaxOpenConns: 10,
MaxIdleConns: 5,
ConnMaxLifetime: time.Hour,
},
}
if cfg.Auth.AdminMasterPassword == "" {
cfg.Auth.AdminMasterPassword = "admin123"
}
return cfg
}