package tests import ( "context" "net/http" "testing" "github.com/gofiber/fiber/v2" "knowfoolery/backend/shared/auth" "knowfoolery/backend/shared/database" "knowfoolery/backend/services/game-service/internal/services" ) // TestBasicHealthEndpoint tests the health endpoint without containers func TestBasicHealthEndpoint(t *testing.T) { app := fiber.New(fiber.Config{ DisableStartupMessage: true, }) app.Get("/health", func(c *fiber.Ctx) error { return c.JSON(fiber.Map{ "status": "healthy", "service": "game-service", }) }) req, err := http.NewRequest("GET", "/health", nil) if err != nil { t.Fatalf("Failed to create request: %v", err) } resp, err := app.Test(req) if err != nil { t.Fatalf("Failed to test request: %v", err) } if resp.StatusCode != 200 { t.Errorf("Expected status 200, got %d", resp.StatusCode) } } // TestSQLiteDatabase tests SQLite database functionality func TestSQLiteDatabase(t *testing.T) { // Use in-memory SQLite database dbConfig := database.Config{ Driver: "sqlite3", Database: ":memory:", } db, err := database.NewClient(dbConfig) if err != nil { t.Fatalf("Failed to create database client: %v", err) } defer db.Close() ctx := context.Background() // Test database health if err := db.Health(ctx); err != nil { t.Errorf("Database health check failed: %v", err) } // Create tables if err := db.CreateTables(ctx); err != nil { t.Fatalf("Failed to create tables: %v", err) } t.Log("SQLite database test completed successfully") } // TestMockAuthentication tests the mock authentication system func TestMockAuthentication(t *testing.T) { authService, err := auth.NewMockAuthService("test-service") if err != nil { t.Fatalf("Failed to create auth service: %v", err) } // Create mock tokens tokens := authService.CreateMockUsers() // Test admin token ctx := context.Background() adminClaims, err := authService.ValidateToken(ctx, tokens["admin"]) if err != nil { t.Errorf("Failed to validate admin token: %v", err) } if adminClaims.UserID != "admin-1" { t.Errorf("Expected admin user ID 'admin-1', got '%s'", adminClaims.UserID) } // Verify admin role hasAdminRole := false for _, role := range adminClaims.Roles { if role == "admin" { hasAdminRole = true break } } if !hasAdminRole { t.Error("Admin token should have admin role") } t.Log("Mock authentication test completed successfully") } // TestGameServiceIntegration tests game service with SQLite func TestGameServiceIntegration(t *testing.T) { // Use in-memory SQLite database dbConfig := database.Config{ Driver: "sqlite3", Database: ":memory:", } db, err := database.NewClient(dbConfig) if err != nil { t.Fatalf("Failed to create database client: %v", err) } defer db.Close() ctx := context.Background() // Create tables if err := db.CreateTables(ctx); err != nil { t.Fatalf("Failed to create tables: %v", err) } // Create game service gameService := services.NewGameService(db) // Test creating a session userID := "test-user-1" session, err := gameService.CreateSession(ctx, "Test Player", &userID) if err != nil { t.Fatalf("Failed to create session: %v", err) } if session.PlayerName != "Test Player" { t.Errorf("Expected player name 'Test Player', got '%s'", session.PlayerName) } // Test retrieving the session retrievedSession, err := gameService.GetSessionByID(ctx, session.ID) if err != nil { t.Fatalf("Failed to retrieve session: %v", err) } if retrievedSession.ID != session.ID { t.Errorf("Retrieved session ID doesn't match") } t.Log("Game service integration test completed successfully") } // TestFiberWithAuth tests Fiber app with authentication middleware func TestFiberWithAuth(t *testing.T) { authService, err := auth.NewMockAuthService("test-service") if err != nil { t.Fatalf("Failed to create auth service: %v", err) } middleware := auth.NewJWTMiddleware(authService) tokens := authService.CreateMockUsers() app := fiber.New(fiber.Config{ DisableStartupMessage: true, }) // Add authenticated route api := app.Group("/api") api.Use(middleware.Optional()) api.Get("/test", func(c *fiber.Ctx) error { user, err := auth.GetUserFromContext(c) if err != nil { return c.JSON(fiber.Map{"authenticated": false}) } return c.JSON(fiber.Map{ "authenticated": true, "user": user.Username, }) }) // Test without authentication req, err := http.NewRequest("GET", "/api/test", nil) if err != nil { t.Fatalf("Failed to create request: %v", err) } resp, err := app.Test(req) if err != nil { t.Fatalf("Failed to test request: %v", err) } if resp.StatusCode != 200 { t.Errorf("Expected status 200, got %d", resp.StatusCode) } // Test with authentication authReq, err := http.NewRequest("GET", "/api/test", nil) if err != nil { t.Fatalf("Failed to create authenticated request: %v", err) } authReq.Header.Set("Authorization", "Bearer "+tokens["player"]) authResp, err := app.Test(authReq) if err != nil { t.Fatalf("Failed to test authenticated request: %v", err) } if authResp.StatusCode != 200 { t.Errorf("Expected status 200 for authenticated request, got %d", authResp.StatusCode) } t.Log("Fiber authentication test completed successfully") }