refactor(handlers): migrate remaining backend handlers to clock.Now() and transaction patterns

Apply clock.Now() migration, transaction wrapping, and minor refactors across admin, scheduling, today, user, auth handler, notifications, webhooks, services, portfolio, ratelimit, testutils, and main.go.

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-24 23:43:50 +01:00
co-authored by Sisyphus
parent 7b24f8e484
commit e4b9003439
36 changed files with 1923 additions and 590 deletions
+28 -17
View File
@@ -8,7 +8,7 @@ import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -24,12 +24,14 @@ func TestVerifySquareSignature_ValidSignature(t *testing.T) {
t.Parallel()
body := []byte(`{"type":"payment.updated","event_id":"evt_1"}`)
key := "test-signing-key"
notificationURL := "http://localhost:8080/webhooks/square"
payload := notificationURL + string(body)
mac := hmac.New(sha256.New, []byte(key))
mac.Write(body)
expectedSig := hex.EncodeToString(mac.Sum(nil))
mac.Write([]byte(payload))
expectedSig := base64.StdEncoding.EncodeToString(mac.Sum(nil))
if !verifySquareSignature(body, expectedSig, key) {
if !verifySquareSignature(body, expectedSig, key, notificationURL) {
t.Error("expected valid signature to verify")
}
}
@@ -38,8 +40,9 @@ func TestVerifySquareSignature_InvalidSignature(t *testing.T) {
t.Parallel()
body := []byte(`{"type":"payment.updated"}`)
key := "test-signing-key"
notificationURL := "http://localhost:8080/webhooks/square"
if verifySquareSignature(body, "invalid-signature", key) {
if verifySquareSignature(body, "invalid-signature", key, notificationURL) {
t.Error("expected invalid signature to fail")
}
}
@@ -47,13 +50,15 @@ func TestVerifySquareSignature_InvalidSignature(t *testing.T) {
func TestVerifySquareSignature_WrongKey(t *testing.T) {
t.Parallel()
body := []byte(`{"type":"payment.updated"}`)
notificationURL := "http://localhost:8080/webhooks/square"
payload := notificationURL + string(body)
mac := hmac.New(sha256.New, []byte("correct-key"))
mac.Write(body)
sig := hex.EncodeToString(mac.Sum(nil))
mac.Write([]byte(payload))
sig := base64.StdEncoding.EncodeToString(mac.Sum(nil))
// Verify with a different key
if verifySquareSignature(body, sig, "wrong-key") {
if verifySquareSignature(body, sig, "wrong-key", notificationURL) {
t.Error("expected wrong key to produce failing verification")
}
}
@@ -61,12 +66,14 @@ func TestVerifySquareSignature_WrongKey(t *testing.T) {
func TestVerifySquareSignature_EmptyBody(t *testing.T) {
t.Parallel()
key := "test-signing-key"
notificationURL := "http://localhost:8080/webhooks/square"
payload := notificationURL + string([]byte{})
mac := hmac.New(sha256.New, []byte(key))
mac.Write([]byte{})
expectedSig := hex.EncodeToString(mac.Sum(nil))
mac.Write([]byte(payload))
expectedSig := base64.StdEncoding.EncodeToString(mac.Sum(nil))
if !verifySquareSignature([]byte{}, expectedSig, key) {
if !verifySquareSignature([]byte{}, expectedSig, key, notificationURL) {
t.Error("expected empty body verification to succeed with matching signature")
}
}
@@ -75,14 +82,16 @@ func TestVerifySquareSignature_TamperedBody(t *testing.T) {
t.Parallel()
body := []byte(`{"type":"payment.updated","event_id":"evt_1"}`)
key := "test-signing-key"
notificationURL := "http://localhost:8080/webhooks/square"
payload := notificationURL + string(body)
mac := hmac.New(sha256.New, []byte(key))
mac.Write(body)
sig := hex.EncodeToString(mac.Sum(nil))
mac.Write([]byte(payload))
sig := base64.StdEncoding.EncodeToString(mac.Sum(nil))
// Verify with a tampered body
tamperedBody := []byte(`{"type":"payment.updated","event_id":"evt_2"}`)
if verifySquareSignature(tamperedBody, sig, key) {
if verifySquareSignature(tamperedBody, sig, key, notificationURL) {
t.Error("expected tampered body to fail verification")
}
}
@@ -97,7 +106,7 @@ func makeWebhookRequest(body []byte, signature string, ctx context.Context) *htt
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/json")
if signature != "" {
req.Header.Set("x-square-signature", signature)
req.Header.Set("x-square-hmacsha256-signature", signature)
}
HandleSquareWebhook(w, req)
return w
@@ -188,10 +197,12 @@ func TestHandleSquareWebhook_ValidSignatureWithEnvKey(t *testing.T) {
body := []byte(`{"type":"payment.updated","event_id":"evt_1"}`)
key := "env-signing-key"
notificationURL := "http://localhost:8080/webhooks/square"
payload := notificationURL + string(body)
mac := hmac.New(sha256.New, []byte(key))
mac.Write(body)
sig := hex.EncodeToString(mac.Sum(nil))
mac.Write([]byte(payload))
sig := base64.StdEncoding.EncodeToString(mac.Sum(nil))
t.Setenv("SQUARE_WEBHOOK_SIGNATURE_KEY", key)