Files
2025-12-26 13:38:04 +01:00

120 lines
2.8 KiB
Go

package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHealthCheck(t *testing.T) {
req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok\n"))
})
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected := "ok\n"
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
}
func TestCORSMiddleware(t *testing.T) {
// Create a test handler
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
// Wrap with CORS middleware
handler := corsMiddleware(testHandler)
// Test OPTIONS request
req := httptest.NewRequest("OPTIONS", "/test", nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
// Check CORS headers
if rr.Header().Get("Access-Control-Allow-Origin") != "*" {
t.Error("Access-Control-Allow-Origin header not set correctly")
}
if rr.Header().Get("Access-Control-Allow-Methods") == "" {
t.Error("Access-Control-Allow-Methods header not set")
}
if rr.Header().Get("Access-Control-Allow-Headers") == "" {
t.Error("Access-Control-Allow-Headers header not set")
}
// OPTIONS should return 200
if rr.Code != http.StatusOK {
t.Errorf("OPTIONS request returned wrong status code: got %v want %v", rr.Code, http.StatusOK)
}
}
func TestProjectStruct(t *testing.T) {
project := Project{
ID: 1,
Name: "Test Project",
Description: "Test Description",
Status: "PLANNING",
}
if project.Name != "Test Project" {
t.Errorf("Project name = %v, want %v", project.Name, "Test Project")
}
if project.Status != "PLANNING" {
t.Errorf("Project status = %v, want %v", project.Status, "PLANNING")
}
}
func TestTaskStruct(t *testing.T) {
task := Task{
ID: 1,
ProjectID: 1,
Title: "Test Task",
Description: "Test Description",
Status: "TODO",
Priority: 1,
}
if task.Title != "Test Task" {
t.Errorf("Task title = %v, want %v", task.Title, "Test Task")
}
if task.Priority != 1 {
t.Errorf("Task priority = %v, want %v", task.Priority, 1)
}
}
func TestWorkOrderStruct(t *testing.T) {
workOrder := WorkOrder{
ID: 1,
ProjectID: 1,
Title: "Test Work Order",
Description: "Test Description",
OrderNumber: "WO-2025-001",
}
if workOrder.OrderNumber != "WO-2025-001" {
t.Errorf("Work order number = %v, want %v", workOrder.OrderNumber, "WO-2025-001")
}
if workOrder.Title != "Test Work Order" {
t.Errorf("Work order title = %v, want %v", workOrder.Title, "Test Work Order")
}
}