refactor(backend): update test files for PoolProxy and per-test transactions

Migrate all test files from SetupTestDB/db.DB pattern to per-test transactions:

- Replace SetupTestDB(t) with SetupTestTx(t) for context + transaction
- Replace db.DB.Query/QueryRow/Exec with tx.Query/QueryRow/Exec
- Replace context.Background() with context from SetupTestTx
- Replace defer rows.Close() pattern with explicit rows.Close()
- Add testdb.SeedBaseline(pool) to all TestMain functions
- Wire db.Conn = db.NewPoolProxy(pool) in all TestMain functions

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
2026-06-21 19:29:24 +01:00
co-authored by Sisyphus
parent 3d0e2afc4c
commit 220a0ef6e8
57 changed files with 5911 additions and 6235 deletions
+30 -13
View File
@@ -5,6 +5,7 @@ package webhooks
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
@@ -20,6 +21,7 @@ import (
// =============================================================================
func TestVerifySquareSignature_ValidSignature(t *testing.T) {
t.Parallel()
body := []byte(`{"type":"payment.updated","event_id":"evt_1"}`)
key := "test-signing-key"
@@ -33,6 +35,7 @@ func TestVerifySquareSignature_ValidSignature(t *testing.T) {
}
func TestVerifySquareSignature_InvalidSignature(t *testing.T) {
t.Parallel()
body := []byte(`{"type":"payment.updated"}`)
key := "test-signing-key"
@@ -42,6 +45,7 @@ func TestVerifySquareSignature_InvalidSignature(t *testing.T) {
}
func TestVerifySquareSignature_WrongKey(t *testing.T) {
t.Parallel()
body := []byte(`{"type":"payment.updated"}`)
mac := hmac.New(sha256.New, []byte("correct-key"))
@@ -55,6 +59,7 @@ func TestVerifySquareSignature_WrongKey(t *testing.T) {
}
func TestVerifySquareSignature_EmptyBody(t *testing.T) {
t.Parallel()
key := "test-signing-key"
mac := hmac.New(sha256.New, []byte(key))
@@ -67,6 +72,7 @@ func TestVerifySquareSignature_EmptyBody(t *testing.T) {
}
func TestVerifySquareSignature_TamperedBody(t *testing.T) {
t.Parallel()
body := []byte(`{"type":"payment.updated","event_id":"evt_1"}`)
key := "test-signing-key"
@@ -85,9 +91,10 @@ func TestVerifySquareSignature_TamperedBody(t *testing.T) {
// Integration tests — HandleSquareWebhook
// =============================================================================
func makeWebhookRequest(body []byte, signature string) *httptest.ResponseRecorder {
func makeWebhookRequest(body []byte, signature string, ctx context.Context) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/webhooks/square", bytes.NewReader(body))
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/json")
if signature != "" {
req.Header.Set("x-square-signature", signature)
@@ -97,6 +104,7 @@ func makeWebhookRequest(body []byte, signature string) *httptest.ResponseRecorde
}
func TestHandleSquareWebhook_PaymentUpdated(t *testing.T) {
t.Parallel()
event := SquareWebhookEvent{
Type: "payment.updated",
EventID: "evt_payment_1",
@@ -104,7 +112,7 @@ func TestHandleSquareWebhook_PaymentUpdated(t *testing.T) {
Data: json.RawMessage(`{"id":"payment_1"}`),
}
body, _ := json.Marshal(event)
w := makeWebhookRequest(body, "")
w := makeWebhookRequest(body, "", context.Background())
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d. body: %s", w.Code, w.Body.String())
}
@@ -114,6 +122,7 @@ func TestHandleSquareWebhook_PaymentUpdated(t *testing.T) {
}
func TestHandleSquareWebhook_RefundUpdated(t *testing.T) {
t.Parallel()
event := SquareWebhookEvent{
Type: "refund.updated",
EventID: "evt_refund_1",
@@ -121,13 +130,14 @@ func TestHandleSquareWebhook_RefundUpdated(t *testing.T) {
Data: json.RawMessage(`{"id":"refund_1"}`),
}
body, _ := json.Marshal(event)
w := makeWebhookRequest(body, "")
w := makeWebhookRequest(body, "", context.Background())
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d. body: %s", w.Code, w.Body.String())
}
}
func TestHandleSquareWebhook_DisputeCreated(t *testing.T) {
t.Parallel()
event := SquareWebhookEvent{
Type: "dispute.created",
EventID: "evt_dispute_1",
@@ -135,13 +145,14 @@ func TestHandleSquareWebhook_DisputeCreated(t *testing.T) {
Data: json.RawMessage(`{"id":"dispute_1"}`),
}
body, _ := json.Marshal(event)
w := makeWebhookRequest(body, "")
w := makeWebhookRequest(body, "", context.Background())
if w.Code != http.StatusOK {
t.Errorf("expected 200 for dispute.created, got %d. body: %s", w.Code, w.Body.String())
}
}
func TestHandleSquareWebhook_UnknownEventType(t *testing.T) {
t.Parallel()
event := SquareWebhookEvent{
Type: "invoice.created",
EventID: "evt_unknown_1",
@@ -149,29 +160,32 @@ func TestHandleSquareWebhook_UnknownEventType(t *testing.T) {
Data: json.RawMessage(`{"id":"inv_1"}`),
}
body, _ := json.Marshal(event)
w := makeWebhookRequest(body, "")
w := makeWebhookRequest(body, "", context.Background())
if w.Code != http.StatusOK {
t.Errorf("expected 200 for unknown event type, got %d. body: %s", w.Code, w.Body.String())
}
}
func TestHandleSquareWebhook_InvalidJSON(t *testing.T) {
w := makeWebhookRequest([]byte(`{invalid json}`), "")
t.Parallel()
w := makeWebhookRequest([]byte(`{invalid json}`), "", context.Background())
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400 for invalid JSON, got %d. body: %s", w.Code, w.Body.String())
}
}
func TestHandleSquareWebhook_BodyTooLarge(t *testing.T) {
t.Parallel()
// 600KB body exceeds the 512KB limit
largeBody := []byte(strings.Repeat("a", 600*1024))
w := makeWebhookRequest(largeBody, "")
w := makeWebhookRequest(largeBody, "", context.Background())
if w.Code != http.StatusRequestEntityTooLarge {
t.Errorf("expected 413 for oversized body, got %d. body: %s", w.Code, w.Body.String())
}
}
func TestHandleSquareWebhook_ValidSignatureWithEnvKey(t *testing.T) {
body := []byte(`{"type":"payment.updated","event_id":"evt_1"}`)
key := "env-signing-key"
@@ -181,41 +195,44 @@ func TestHandleSquareWebhook_ValidSignatureWithEnvKey(t *testing.T) {
t.Setenv("SQUARE_WEBHOOK_SIGNATURE_KEY", key)
w := makeWebhookRequest(body, sig)
w := makeWebhookRequest(body, sig, context.Background())
if w.Code != http.StatusOK {
t.Errorf("expected 200 with valid signature, got %d. body: %s", w.Code, w.Body.String())
}
}
func TestHandleSquareWebhook_InvalidSignatureWithEnvKey(t *testing.T) {
body := []byte(`{"type":"payment.updated","event_id":"evt_1"}`)
t.Setenv("SQUARE_WEBHOOK_SIGNATURE_KEY", "env-signing-key")
w := makeWebhookRequest(body, "bad-signature")
w := makeWebhookRequest(body, "bad-signature", context.Background())
if w.Code != http.StatusForbidden {
t.Errorf("expected 403 with invalid signature, got %d. body: %s", w.Code, w.Body.String())
}
}
func TestHandleSquareWebhook_NoSignatureWhenKeySet(t *testing.T) {
body := []byte(`{"type":"payment.updated","event_id":"evt_1"}`)
t.Setenv("SQUARE_WEBHOOK_SIGNATURE_KEY", "env-signing-key")
// No x-square-signature header at all
w := makeWebhookRequest(body, "")
if w.Code != http.StatusOK {
t.Errorf("expected 200 when no signature provided (dev stub), got %d. body: %s", w.Code, w.Body.String())
w := makeWebhookRequest(body, "", context.Background())
if w.Code != http.StatusForbidden {
t.Errorf("expected 403 when signature key is set but header missing, got %d. body: %s", w.Code, w.Body.String())
}
}
func TestHandleSquareWebhook_SignatureSkippedWhenKeyEmpty(t *testing.T) {
t.Setenv("SQUARE_WEBHOOK_SIGNATURE_KEY", "")
body := []byte(`{"type":"payment.updated","event_id":"evt_1"}`)
// Bad signature but key is empty, so verification should be skipped
w := makeWebhookRequest(body, "some-signature")
w := makeWebhookRequest(body, "some-signature", context.Background())
if w.Code != http.StatusOK {
t.Errorf("expected 200 when no key configured (dev stub), got %d. body: %s", w.Code, w.Body.String())
}