//go:build integration // +build integration package main import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "os" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // Integration tests require a running database // Run with: go test -tags=integration -v ./... func TestIntegration_HealthEndpoint(t *testing.T) { if os.Getenv("INTEGRATION") != "1" { t.Skip("Skipping integration test. Set INTEGRATION=1 to run.") } // Start server go main() time.Sleep(2 * time.Second) // Wait for server to start resp, err := http.Get("http://localhost:9092/health") require.NoError(t, err) defer resp.Body.Close() assert.Equal(t, http.StatusOK, resp.StatusCode) var result map[string]interface{} err = json.NewDecoder(resp.Body).Decode(&result) require.NoError(t, err) assert.Equal(t, true, result["ok"]) } func TestIntegration_FullWorkflow(t *testing.T) { if os.Getenv("INTEGRATION") != "1" { t.Skip("Skipping integration test. Set INTEGRATION=1 to run.") } baseURL := "http://localhost:9092" // 1. Create customer customer := map[string]interface{}{ "name": "Integration Test Customer", "email": "integration@test.com", "phone": "+46701234567", "company": "Test AB", "status": "lead", } customerBody, _ := json.Marshal(customer) resp, err := http.Post(baseURL+"/api/v1/crm/customers", "application/json", bytes.NewReader(customerBody)) require.NoError(t, err) assert.Equal(t, http.StatusCreated, resp.StatusCode) var customerResult map[string]interface{} json.NewDecoder(resp.Body).Decode(&customerResult) resp.Body.Close() customerID := customerResult["id"].(string) assert.NotEmpty(t, customerID) // 2. Create quote quote := map[string]interface{}{ "customer_id": customerID, "title": "Test Quote", "description": "Integration test quote", "valid_until": time.Now().AddDate(0, 1, 0).Format("2006-01-02"), "items": []map[string]interface{}{ { "description": "Service A", "quantity": 10, "unit_price": 100.00, "tax_rate": 25.0, }, }, } quoteBody, _ := json.Marshal(quote) resp, err = http.Post(baseURL+"/api/v1/sales/quotes", "application/json", bytes.NewReader(quoteBody)) require.NoError(t, err) assert.Equal(t, http.StatusCreated, resp.StatusCode) var quoteResult map[string]interface{} json.NewDecoder(resp.Body).Decode("eResult) resp.Body.Close() quoteID := quoteResult["id"].(string) assert.NotEmpty(t, quoteID) // 3. Accept quote req, _ := http.NewRequest(http.MethodPost, baseURL+"/api/v1/sales/quotes/"+quoteID+"/accept", nil) resp, err = http.DefaultClient.Do(req) require.NoError(t, err) assert.Equal(t, http.StatusOK, resp.StatusCode) resp.Body.Close() // 4. Convert to order req, _ = http.NewRequest(http.MethodPost, baseURL+"/api/v1/sales/quotes/"+quoteID+"/convert", nil) resp, err = http.DefaultClient.Do(req) require.NoError(t, err) assert.Equal(t, http.StatusCreated, resp.StatusCode) var orderResult map[string]interface{} json.NewDecoder(resp.Body).Decode(&orderResult) resp.Body.Close() orderID := orderResult["order_id"].(string) assert.NotEmpty(t, orderID) // 5. Create invoice from order (would need invoice handler) // Skipped for now t.Logf("Created customer: %s, quote: %s, order: %s", customerID, quoteID, orderID) } func TestIntegration_Performance(t *testing.T) { if os.Getenv("INTEGRATION") != "1" { t.Skip("Skipping integration test. Set INTEGRATION=1 to run.") } baseURL := "http://localhost:9092" // Test response time for health endpoint start := time.Now() resp, err := http.Get(baseURL + "/health") elapsed := time.Since(start) require.NoError(t, err) resp.Body.Close() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Less(t, elapsed, 100*time.Millisecond, "Health endpoint too slow") t.Logf("Health endpoint response time: %v", elapsed) } // Mock test for handlers without DB func TestMock_CRMHandler(t *testing.T) { // This is a placeholder for future mock-based tests // Would use sqlmock to mock database interactions assert.True(t, true) }