feat: migrar backend unificado a Go 1.27 + SPA Vite embebido
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package webhooks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHMACSigningAndEvents(t *testing.T) {
|
||||
if len(EventosOficiales) != 8 {
|
||||
t.Fatalf("expected 8 official events, got %d", len(EventosOficiales))
|
||||
}
|
||||
payload := []byte(`{"event":"cotizacion.aprobada","data":{"id":"c1"}}`)
|
||||
secret := "whsec_test_123"
|
||||
sig := FirmarPayload(payload, secret)
|
||||
if !strings.HasPrefix(sig, "sha256=") || len(sig) != 7+64 {
|
||||
t.Fatalf("invalid signature format: %s", sig)
|
||||
}
|
||||
if !VerificarFirma(payload, sig, secret) {
|
||||
t.Fatal("valid signature rejected")
|
||||
}
|
||||
if VerificarFirma([]byte(`{"tampered":true}`), sig, secret) {
|
||||
t.Fatal("tampered payload accepted")
|
||||
}
|
||||
if VerificarFirma(payload, sig, "wrong-secret") {
|
||||
t.Fatal("wrong secret accepted")
|
||||
}
|
||||
if VerificarFirma(payload, "invalid", secret) {
|
||||
t.Fatal("malformed header accepted")
|
||||
}
|
||||
}
|
||||
|
||||
type memClaimStore struct {
|
||||
mu sync.Mutex
|
||||
claimed map[string]bool
|
||||
}
|
||||
|
||||
func (m *memClaimStore) ClaimVencido(_ context.Context, pagoID string) (bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.claimed[pagoID] {
|
||||
return false, nil
|
||||
}
|
||||
m.claimed[pagoID] = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func TestPagoVencidoIdempotencyConcurrency(t *testing.T) {
|
||||
store := &memClaimStore{claimed: map[string]bool{}}
|
||||
items := []PagoVencidoItem{{PagoID: "p1", ClienteID: "c1", Monto: 750, Moneda: "MXN", DiasAtraso: 3}}
|
||||
|
||||
var mu sync.Mutex
|
||||
totalEmitted := 0
|
||||
emit := func(_ context.Context, _ string, _ map[string]any) {
|
||||
mu.Lock()
|
||||
totalEmitted++
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
EmitirPagosVencidosIdempotente(context.Background(), store, items, emit)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if totalEmitted != 1 {
|
||||
t.Fatalf("expected exactly 1 emission under concurrency, got %d", totalEmitted)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user