mirror of
https://github.com/marcusDenslow/terminalShop.git
synced 2026-10-25 18:08:45 +01:00
* feat(sca): expire inactive saved cards * fix(sca): address card ttl review feedback - Correct sql typos breaking GET /cards and /view/init (used_id, id_default) - Stop swallowing backfillCardStorageExpiration error in Migrate - Wrap expireStoredCard local writes in a transaction and run stripe detach + audit only after commit, preventing double-fire on retry - Drop redundant ttl refresh in ConvertCart; webhook is authoritative - Rename validation_expired_at metric label to validation_card_expired - Fix CardStorageExpiresAt doc comment and test failure message typos - Add handler-level tests for expireStoredCards (user scoping + cart deref) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * refactor(sca): move card expiration to periodic reconciler Read paths (GetCards, GetViewInit) now filter expired cards from query results instead of detaching them inline, so request handlers no longer block on Stripe API calls. Physical deletion runs in a new ReconcileExpiredCards goroutine on an hourly ticker, mirroring the existing ReconcileOrders / ReconcileUnshipped jobs. Per-card action sites (SetCard, SetDefaultCard, ConvertCart) still expire synchronously since they operate on one row the user is acting on. ViewHandler's stripeKey dependency is dropped now that GetViewInit no longer touches Stripe. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(sca): address card ttl review feedback - guard zero order.CardID before refreshing card storage ttl in payment_intent.succeeded webhook to avoid noisy warn logs on payment paths that did not use a saved card - delete unused Card.RefreshStorageTTL model method; the SQL-only refreshCardStorageTTL helper is the single canonical path - clarify duplicate-card webhook path: storage_expires_at reset is a card-add refresh, not a payment-success refresh, so last_used_at intentionally stays untouched Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * test(sca): cover card ttl read paths and expired-card sync sites --------- Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package database
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"terminalShop/pkg/models"
|
|
)
|
|
|
|
func TestBackfillCardStorageExpiration_FillsNullRowsOnly(t *testing.T) {
|
|
testDB := "test_backfill.db"
|
|
defer func() { _ = os.Remove(testDB) }()
|
|
defer ResetForTesting()
|
|
|
|
db, err := Connect(testDB)
|
|
if err != nil {
|
|
t.Fatalf("connect: %v", err)
|
|
}
|
|
if err := Migrate(db); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
|
|
user := models.User{
|
|
SSHKeyFingerprint: "SHA256:backfill",
|
|
SSHPublicKey: "ssh-ed25519 AAAA backfill",
|
|
Name: "Backfill",
|
|
Email: "backfill@example.com",
|
|
}
|
|
if err := db.Create(&user).Error; err != nil {
|
|
t.Fatalf("seed user: %v", err)
|
|
}
|
|
|
|
legacy := models.Card{
|
|
UserID: user.ID, StripePaymentID: "pm_legacy", Last4: "0000",
|
|
Brand: "Visa", ExpMonth: 1, ExpYear: 2030,
|
|
}
|
|
if err := db.Create(&legacy).Error; err != nil {
|
|
t.Fatalf("seed legacy: %v", err)
|
|
}
|
|
|
|
fixed := time.Date(2030, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
preset := models.Card{
|
|
UserID: user.ID, StripePaymentID: "pm_preset", Last4: "9999",
|
|
Brand: "Visa", ExpMonth: 1, ExpYear: 2030, StorageExpiresAt: &fixed,
|
|
}
|
|
if err := db.Create(&preset).Error; err != nil {
|
|
t.Fatalf("ssed preset: %v", err)
|
|
}
|
|
if err := backfillCardStorageExpiration(db); err != nil {
|
|
t.Fatalf("seed preset: %v", err)
|
|
}
|
|
|
|
var reloadedLegacy, reloadedPreset models.Card
|
|
if err := db.First(&reloadedLegacy, legacy.ID).Error; err != nil {
|
|
t.Fatalf("reload legacy: %v", err)
|
|
}
|
|
if err := db.First(&reloadedPreset, preset.ID).Error; err != nil {
|
|
t.Fatalf("reload preset: %v", err)
|
|
}
|
|
if reloadedLegacy.StorageExpiresAt == nil {
|
|
t.Fatalf("legacy card storage_expires_at still NULL after backfill")
|
|
}
|
|
if reloadedPreset.StorageExpiresAt == nil || !reloadedPreset.StorageExpiresAt.Equal(fixed) {
|
|
t.Fatalf("preset card storage_expires_at was overwritten: %v", reloadedPreset.StorageExpiresAt)
|
|
}
|
|
}
|