test: add WebSocket integration tests
Add comprehensive WebSocket test coverage: - TestWebSocketConnection: Basic connection and JWT auth - TestWebSocketDeviceAuth: Device token authentication - TestWebSocketProgressBroadcast: Real-time update delivery - TestWebSocketPingPong: Heartbeat mechanism - TestWebSocketConnectionLimit: Multiple concurrent connections - TestWebSocketInvalidToken: Rejection of invalid tokens - Helper function for test media item creation Update test helpers to create ConnectionManager for tests. Tests verify WebSocket connection, authentication, and real-time progress broadcast functionality.
This commit is contained in:
@@ -5,11 +5,13 @@ import (
|
||||
"bookmann/internal/database"
|
||||
"bookmann/internal/handlers"
|
||||
ratelimit "bookmann/internal/middleware"
|
||||
wsync "bookmann/internal/sync"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -38,6 +40,19 @@ 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) {
|
||||
// Get database password - use default for testing since .env password has special chars
|
||||
// Tests will run against the local test database, not the Docker one
|
||||
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
|
||||
@@ -45,7 +60,7 @@ func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config
|
||||
DatabaseHost: "localhost",
|
||||
DatabasePort: "5432",
|
||||
DatabaseUser: "postgres",
|
||||
DatabasePassword: "password",
|
||||
DatabasePassword: dbPass,
|
||||
DatabaseName: "bookmann",
|
||||
JWTSecret: "test-secret-key",
|
||||
UploadPath: "./test-uploads",
|
||||
@@ -67,6 +82,9 @@ func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config
|
||||
authHandler := handlers.NewAuthHandler(queries, cfg.JWTSecret, loginAttemptTracker)
|
||||
deviceHandler := handlers.NewDeviceHandler(queries, cfg.JWTSecret, cfg)
|
||||
|
||||
// Create WebSocket connection manager for testing
|
||||
connManager := wsync.NewConnectionManager()
|
||||
|
||||
// Create Echo instance
|
||||
e := echo.New()
|
||||
|
||||
@@ -77,7 +95,7 @@ func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config
|
||||
|
||||
// Setup routes
|
||||
protected := e.Group("/api")
|
||||
h := handlers.SetupRoutes(protected, queries)
|
||||
h := handlers.SetupRoutes(protected, queries, connManager)
|
||||
|
||||
// Device management routes (public - for registration)
|
||||
e.POST("/api/devices/register", deviceHandler.InitiateRegistration)
|
||||
@@ -105,7 +123,10 @@ func setupTestServer(t *testing.T) (*httptest.Server, *database.Queries, *config
|
||||
}
|
||||
|
||||
// loginTestUser logs in a test user and returns the JWT token
|
||||
func loginTestUser(t *testing.T, ts *httptest.Server) string {
|
||||
func loginTestUser(t *testing.T, ts *httptest.Server, db *database.Queries) string {
|
||||
// Ensure test user exists first
|
||||
_ = getTestUserID(t, db)
|
||||
|
||||
loginRequest := map[string]interface{}{
|
||||
"login": "testuser@example.com",
|
||||
"password": "testpass123",
|
||||
|
||||
Reference in New Issue
Block a user