add step list for server creation
This commit is contained in:
@@ -24,14 +24,12 @@ import (
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// TestHelper provides utilities for testing
|
||||
type TestHelper struct {
|
||||
DB *gorm.DB
|
||||
TempDir string
|
||||
TestData *TestData
|
||||
}
|
||||
|
||||
// TestData contains common test data structures
|
||||
type TestData struct {
|
||||
ServerID uuid.UUID
|
||||
Server *model.Server
|
||||
@@ -39,37 +37,29 @@ type TestData struct {
|
||||
SampleConfig *model.Configuration
|
||||
}
|
||||
|
||||
// SetTestEnv sets the required environment variables for tests
|
||||
func SetTestEnv() {
|
||||
// Set required environment variables for testing
|
||||
os.Setenv("APP_SECRET", "test-secret-key-for-testing-123456")
|
||||
os.Setenv("APP_SECRET_CODE", "test-code-for-testing-123456789012")
|
||||
os.Setenv("ENCRYPTION_KEY", "12345678901234567890123456789012")
|
||||
os.Setenv("JWT_SECRET", "test-jwt-secret-key-for-testing-123456789012345678901234567890")
|
||||
os.Setenv("ACCESS_KEY", "test-access-key-for-testing")
|
||||
// Set test-specific environment variables
|
||||
os.Setenv("TESTING_ENV", "true") // Used to bypass
|
||||
os.Setenv("TESTING_ENV", "true")
|
||||
|
||||
configs.Init()
|
||||
}
|
||||
|
||||
// NewTestHelper creates a new test helper with in-memory database
|
||||
func NewTestHelper(t *testing.T) *TestHelper {
|
||||
// Set required environment variables
|
||||
SetTestEnv()
|
||||
|
||||
// Create temporary directory for test files
|
||||
tempDir := t.TempDir()
|
||||
|
||||
// Create in-memory SQLite database for testing
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent), // Suppress SQL logs in tests
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to connect to test database: %v", err)
|
||||
}
|
||||
|
||||
// Auto-migrate the schema
|
||||
err = db.AutoMigrate(
|
||||
&model.Server{},
|
||||
&model.Config{},
|
||||
@@ -79,7 +69,6 @@ func NewTestHelper(t *testing.T) *TestHelper {
|
||||
&model.StateHistory{},
|
||||
)
|
||||
|
||||
// Explicitly ensure tables exist with correct structure
|
||||
if !db.Migrator().HasTable(&model.StateHistory{}) {
|
||||
err = db.Migrator().CreateTable(&model.StateHistory{})
|
||||
if err != nil {
|
||||
@@ -90,7 +79,6 @@ func NewTestHelper(t *testing.T) *TestHelper {
|
||||
t.Fatalf("Failed to migrate test database: %v", err)
|
||||
}
|
||||
|
||||
// Create test data
|
||||
testData := createTestData(t, tempDir)
|
||||
|
||||
return &TestHelper{
|
||||
@@ -100,11 +88,9 @@ func NewTestHelper(t *testing.T) *TestHelper {
|
||||
}
|
||||
}
|
||||
|
||||
// createTestData creates common test data structures
|
||||
func createTestData(t *testing.T, tempDir string) *TestData {
|
||||
serverID := uuid.New()
|
||||
|
||||
// Create sample server
|
||||
server := &model.Server{
|
||||
ID: serverID,
|
||||
Name: "Test Server",
|
||||
@@ -115,13 +101,11 @@ func createTestData(t *testing.T, tempDir string) *TestData {
|
||||
FromSteamCMD: false,
|
||||
}
|
||||
|
||||
// Create server directory
|
||||
serverConfigDir := filepath.Join(tempDir, "server", "cfg")
|
||||
if err := os.MkdirAll(serverConfigDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create server config directory: %v", err)
|
||||
}
|
||||
|
||||
// Sample configuration files content
|
||||
configFiles := map[string]string{
|
||||
"configuration.json": `{
|
||||
"udpPort": "9231",
|
||||
@@ -212,7 +196,6 @@ func createTestData(t *testing.T, tempDir string) *TestData {
|
||||
}`,
|
||||
}
|
||||
|
||||
// Sample configuration struct
|
||||
sampleConfig := &model.Configuration{
|
||||
UdpPort: model.IntString(9231),
|
||||
TcpPort: model.IntString(9232),
|
||||
@@ -230,14 +213,12 @@ func createTestData(t *testing.T, tempDir string) *TestData {
|
||||
}
|
||||
}
|
||||
|
||||
// CreateTestConfigFiles creates actual config files in the test directory
|
||||
func (th *TestHelper) CreateTestConfigFiles() error {
|
||||
serverConfigDir := filepath.Join(th.TestData.Server.Path, "cfg")
|
||||
|
||||
for filename, content := range th.TestData.ConfigFiles {
|
||||
filePath := filepath.Join(serverConfigDir, filename)
|
||||
|
||||
// Encode content to UTF-16 LE BOM format as expected by the application
|
||||
utf16Content, err := EncodeUTF16LEBOM([]byte(content))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -251,7 +232,6 @@ func (th *TestHelper) CreateTestConfigFiles() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateMalformedConfigFile creates a config file with invalid JSON
|
||||
func (th *TestHelper) CreateMalformedConfigFile(filename string) error {
|
||||
serverConfigDir := filepath.Join(th.TestData.Server.Path, "cfg")
|
||||
filePath := filepath.Join(serverConfigDir, filename)
|
||||
@@ -259,67 +239,51 @@ func (th *TestHelper) CreateMalformedConfigFile(filename string) error {
|
||||
malformedJSON := `{
|
||||
"udpPort": "9231",
|
||||
"tcpPort": "9232"
|
||||
"maxConnections": "30" // Missing comma - invalid JSON
|
||||
"maxConnections": "30"
|
||||
}`
|
||||
|
||||
return os.WriteFile(filePath, []byte(malformedJSON), 0644)
|
||||
}
|
||||
|
||||
// RemoveConfigFile removes a config file to simulate missing file scenarios
|
||||
func (th *TestHelper) RemoveConfigFile(filename string) error {
|
||||
serverConfigDir := filepath.Join(th.TestData.Server.Path, "cfg")
|
||||
filePath := filepath.Join(serverConfigDir, filename)
|
||||
return os.Remove(filePath)
|
||||
}
|
||||
|
||||
// InsertTestServer inserts the test server into the database
|
||||
func (th *TestHelper) InsertTestServer() error {
|
||||
return th.DB.Create(th.TestData.Server).Error
|
||||
}
|
||||
|
||||
// CreateContext creates a test context
|
||||
func (th *TestHelper) CreateContext() context.Context {
|
||||
return context.Background()
|
||||
}
|
||||
|
||||
// CreateFiberCtx creates a fiber.Ctx for testing
|
||||
func (th *TestHelper) CreateFiberCtx() *fiber.Ctx {
|
||||
// Create app and request for fiber context
|
||||
app := fiber.New()
|
||||
// Create a dummy request that doesn't depend on external http objects
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
// Create the fiber context from real request/response
|
||||
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
|
||||
// Store the original request for release later
|
||||
ctx.Locals("original-request", req)
|
||||
// Return the context which can be safely used in tests
|
||||
return ctx
|
||||
}
|
||||
|
||||
// ReleaseFiberCtx properly releases a fiber context created with CreateFiberCtx
|
||||
func (th *TestHelper) ReleaseFiberCtx(app *fiber.App, ctx *fiber.Ctx) {
|
||||
if app != nil && ctx != nil {
|
||||
app.ReleaseCtx(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup performs cleanup operations after tests
|
||||
func (th *TestHelper) Cleanup() {
|
||||
// Close database connection
|
||||
if sqlDB, err := th.DB.DB(); err == nil {
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
// Temporary directory is automatically cleaned up by t.TempDir()
|
||||
}
|
||||
|
||||
// LoadTestEnvFile loads environment variables from a .env file for testing
|
||||
func LoadTestEnvFile() error {
|
||||
// Try to load from .env file
|
||||
return godotenv.Load()
|
||||
}
|
||||
|
||||
// AssertNoError is a helper function to check for errors in tests
|
||||
func AssertNoError(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
@@ -327,7 +291,6 @@ func AssertNoError(t *testing.T, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
// AssertError is a helper function to check for expected errors
|
||||
func AssertError(t *testing.T, err error, expectedMsg string) {
|
||||
t.Helper()
|
||||
if err == nil {
|
||||
@@ -338,7 +301,6 @@ func AssertError(t *testing.T, err error, expectedMsg string) {
|
||||
}
|
||||
}
|
||||
|
||||
// AssertEqual checks if two values are equal
|
||||
func AssertEqual(t *testing.T, expected, actual interface{}) {
|
||||
t.Helper()
|
||||
if expected != actual {
|
||||
@@ -346,7 +308,6 @@ func AssertEqual(t *testing.T, expected, actual interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// AssertNotNil checks if a value is not nil
|
||||
func AssertNotNil(t *testing.T, value interface{}) {
|
||||
t.Helper()
|
||||
if value == nil {
|
||||
@@ -354,12 +315,9 @@ func AssertNotNil(t *testing.T, value interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// AssertNil checks if a value is nil
|
||||
func AssertNil(t *testing.T, value interface{}) {
|
||||
t.Helper()
|
||||
if value != nil {
|
||||
// Special handling for interface values that contain nil but aren't nil themselves
|
||||
// For example, (*jwt.Claims)(nil) is not equal to nil, but it contains nil
|
||||
switch v := value.(type) {
|
||||
case *interface{}:
|
||||
if v == nil || *v == nil {
|
||||
@@ -374,13 +332,11 @@ func AssertNil(t *testing.T, value interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// EncodeUTF16LEBOM encodes UTF-8 bytes to UTF-16 LE BOM format
|
||||
func EncodeUTF16LEBOM(input []byte) ([]byte, error) {
|
||||
encoder := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM)
|
||||
return transformBytes(encoder.NewEncoder(), input)
|
||||
}
|
||||
|
||||
// transformBytes applies a transform to input bytes
|
||||
func transformBytes(t transform.Transformer, input []byte) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
w := transform.NewWriter(&buf, t)
|
||||
@@ -396,7 +352,6 @@ func transformBytes(t transform.Transformer, input []byte) ([]byte, error) {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// ErrorForTesting creates an error for testing purposes
|
||||
func ErrorForTesting(message string) error {
|
||||
return errors.New(message)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user