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

132 lines
3.1 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) {
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
handler := corsMiddleware(testHandler)
req := httptest.NewRequest("OPTIONS", "/test", nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
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")
}
if rr.Code != http.StatusOK {
t.Errorf("OPTIONS request returned wrong status code: got %v want %v", rr.Code, http.StatusOK)
}
}
func TestInvoiceStruct(t *testing.T) {
invoice := Invoice{
ID: 1,
InvoiceNumber: "INV-2025-001",
ClientID: 1,
Amount: 1000.00,
Currency: "USD",
Status: "PENDING",
}
if invoice.InvoiceNumber != "INV-2025-001" {
t.Errorf("Invoice number = %v, want %v", invoice.InvoiceNumber, "INV-2025-001")
}
if invoice.Amount != 1000.00 {
t.Errorf("Invoice amount = %v, want %v", invoice.Amount, 1000.00)
}
if invoice.Currency != "USD" {
t.Errorf("Invoice currency = %v, want %v", invoice.Currency, "USD")
}
}
func TestPaymentStruct(t *testing.T) {
payment := Payment{
ID: 1,
InvoiceID: 1,
Amount: 500.00,
Currency: "USD",
PaymentMethod: "STRIPE",
Status: "COMPLETED",
}
if payment.Amount != 500.00 {
t.Errorf("Payment amount = %v, want %v", payment.Amount, 500.00)
}
if payment.PaymentMethod != "STRIPE" {
t.Errorf("Payment method = %v, want %v", payment.PaymentMethod, "STRIPE")
}
if payment.Status != "COMPLETED" {
t.Errorf("Payment status = %v, want %v", payment.Status, "COMPLETED")
}
}
func TestCreatePaymentIntentRequest(t *testing.T) {
req := CreatePaymentIntentRequest{
InvoiceID: 123,
}
if req.InvoiceID != 123 {
t.Errorf("Invoice ID = %v, want %v", req.InvoiceID, 123)
}
}
func TestCreatePaymentIntentResponse(t *testing.T) {
resp := CreatePaymentIntentResponse{
ClientSecret: "pi_test_secret_123",
PaymentID: 456,
}
if resp.ClientSecret != "pi_test_secret_123" {
t.Errorf("Client secret = %v, want %v", resp.ClientSecret, "pi_test_secret_123")
}
if resp.PaymentID != 456 {
t.Errorf("Payment ID = %v, want %v", resp.PaymentID, 456)
}
}