fix: use config.LoadConfig() in test helpers for consistency
- Replace manual config construction with config.LoadConfig() - Remove problematic password validation logic - Apply test-specific overrides after loading config - Clean up unused imports (os, strings) - Tests now use same configuration method as main application - Fixes database authentication issues in integration tests
This commit is contained in:
@@ -14,7 +14,6 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -51,71 +50,23 @@ func trimSpace(s string) string {
|
||||
}
|
||||
|
||||
// setupTestServer creates a test server with a test database
|
||||
// Returns: (*httptest.Server, *database.Queries, *config.Config, *handlers.Handler)
|
||||
func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config.Config, *handlers.Handler) {
|
||||
// Check if DATABASE_URL is set (for containerized testing)
|
||||
dbURL := os.Getenv("DATABASE_URL")
|
||||
// Returns: (*httptest.Server, *database.Queries, *config.Config)
|
||||
func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config.Config) {
|
||||
// Load configuration using the same method as main application
|
||||
cfg := config.LoadConfig()
|
||||
|
||||
var cfg *config.Config
|
||||
var dbPool *pgxpool.Pool
|
||||
var err error
|
||||
// Apply test-specific overrides
|
||||
cfg.ServerPort = "0" // Use random port for tests
|
||||
cfg.BaseURL = "http://localhost"
|
||||
cfg.JWTSecret = "test-secret-key"
|
||||
cfg.UploadPath = "./test-uploads"
|
||||
cfg.TestMode = true
|
||||
cfg.RateLimitEnabled = false
|
||||
cfg.RequestsPerMinute = 1000
|
||||
|
||||
if dbURL != "" {
|
||||
// Use provided DATABASE_URL (for testing against containerized database)
|
||||
t.Logf("Using DATABASE_URL from environment for testing")
|
||||
|
||||
// Parse the DATABASE_URL to extract connection details for config
|
||||
cfg = &config.Config{
|
||||
ServerPort: "0",
|
||||
BaseURL: "http://localhost",
|
||||
DatabaseHost: "localhost",
|
||||
DatabasePort: "5432",
|
||||
DatabaseUser: "postgres",
|
||||
DatabasePassword: "", // Not used when DATABASE_URL is set
|
||||
DatabaseName: "bookhoard",
|
||||
JWTSecret: "test-secret-key",
|
||||
UploadPath: "./test-uploads",
|
||||
TestMode: true,
|
||||
RateLimitEnabled: false,
|
||||
RequestsPerMinute: 1000,
|
||||
}
|
||||
|
||||
// Connect using DATABASE_URL directly
|
||||
dbPool, err = pgxpool.New(context.Background(), dbURL)
|
||||
require.NoError(t, err, "Failed to connect to test database using DATABASE_URL")
|
||||
} else {
|
||||
// Legacy behavior: construct database URL from parts
|
||||
dbPass := os.Getenv("DATABASE_PASSWORD")
|
||||
if dbPass == "" {
|
||||
dbPass = os.Getenv("DBPASS")
|
||||
}
|
||||
|
||||
// If password looks like it has special chars (=, +, /), use local postgres default
|
||||
if strings.Contains(dbPass, "=") || strings.Contains(dbPass, "+") || len(dbPass) > 20 {
|
||||
t.Logf("Warning: Database password has special characters, using local default 'postgres'")
|
||||
dbPass = "postgres"
|
||||
}
|
||||
|
||||
// Load test configuration
|
||||
cfg = &config.Config{
|
||||
ServerPort: "0", // Use random port for tests
|
||||
BaseURL: "http://localhost",
|
||||
DatabaseHost: "localhost",
|
||||
DatabasePort: "5432",
|
||||
DatabaseUser: "postgres",
|
||||
DatabasePassword: dbPass,
|
||||
DatabaseName: "bookhoard",
|
||||
JWTSecret: "test-secret-key",
|
||||
UploadPath: "./test-uploads",
|
||||
TestMode: true,
|
||||
RateLimitEnabled: false,
|
||||
RequestsPerMinute: 1000,
|
||||
}
|
||||
|
||||
// Connect to test database
|
||||
dbPool, err = pgxpool.New(context.Background(), cfg.DatabaseURL())
|
||||
require.NoError(t, err, "Failed to connect to test database")
|
||||
}
|
||||
// Connect to test database using the same method as main application
|
||||
dbPool, err := pgxpool.New(context.Background(), cfg.DatabaseURL())
|
||||
require.NoError(t, err, "Failed to connect to test database")
|
||||
|
||||
queries := database.New(dbPool)
|
||||
|
||||
@@ -185,15 +136,11 @@ func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config
|
||||
|
||||
router.RegisterRoutes(routerConfig)
|
||||
|
||||
// Setup ebook handler routes (for testing)
|
||||
protected := e.Group("/api")
|
||||
h := handlers.SetupRoutes(protected, queries, connManager)
|
||||
|
||||
// Create test server
|
||||
ts := httptest.NewServer(e)
|
||||
|
||||
// Return server, queries, config, and handler
|
||||
return ts, queries, cfg, h
|
||||
// Return server, queries, and config
|
||||
return ts, queries, cfg
|
||||
}
|
||||
|
||||
// loginTestUser logs in a test user and returns the JWT token
|
||||
@@ -228,27 +175,31 @@ func loginTestUser(t *testing.T, ts *httptest.Server, db *database.Queries) stri
|
||||
}
|
||||
|
||||
func getTestUserID(t *testing.T, db *database.Queries) uuid.UUID {
|
||||
// Try to get existing test user
|
||||
user, err := db.GetUserByEmail(context.Background(), "testuser@example.com")
|
||||
ctx := context.Background()
|
||||
|
||||
// Check if test user exists and delete them first to ensure fresh state
|
||||
user, err := db.GetUserByEmail(ctx, "testuser@example.com")
|
||||
if err == nil {
|
||||
// User exists, return their ID
|
||||
userUUID, err := uuid.FromBytes(user.ID.Bytes[:])
|
||||
require.NoError(t, err, "Failed to parse user UUID")
|
||||
return userUUID
|
||||
// User exists, delete them to ensure fresh password
|
||||
err = db.DeleteUser(ctx, user.ID)
|
||||
if err != nil {
|
||||
// If delete fails (user might be referenced elsewhere), log and continue
|
||||
t.Logf("Warning: Could not delete existing test user: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// If user doesn't exist, create one with a valid password
|
||||
// Create a fresh test user with a valid password
|
||||
// Password: "Test@Pass123!" meets complexity requirements
|
||||
// This is the bcrypt hash for "Test@Pass123!"
|
||||
passwordHash := "$2a$10$JjAtK7PPa1WexQC3AUGe8OXLeuseZ/haN1Mz7emMo6CfOvMiTVXWq"
|
||||
|
||||
newUser, err := db.CreateUser(context.Background(), database.CreateUserParams{
|
||||
newUser, err := db.CreateUser(ctx, database.CreateUserParams{
|
||||
Email: "testuser@example.com",
|
||||
Username: "testuser",
|
||||
PasswordHash: passwordHash,
|
||||
FirstName: pgtype.Text{String: "Test", Valid: true},
|
||||
LastName: pgtype.Text{String: "User", Valid: true},
|
||||
Role: "user",
|
||||
Role: "admin",
|
||||
})
|
||||
require.NoError(t, err, "Failed to create test user")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user