console: witness first-route record before claim in app-gone cleanup
An ordinary first-route claim (Route "") on a paid purchase whose app is in Trash: the record retires before the claim; a failed retirement keeps the claim and record and the purchase captured for retry.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+78
−0
internal/console/domainbuy_account_test.go+78 −0
diff --git a/internal/console/domainbuy_account_test.go b/internal/console/domainbuy_account_test.go| index 1d9a84ed1..c947d0e4c 100644 |
| --- a/internal/console/domainbuy_account_test.go |
| +++ b/internal/console/domainbuy_account_test.go |
| @@ -10,6 +10,7 @@ import ( |
| "errors" |
| "net/http" |
| "os" |
| + "slices" |
| "strings" |
| "sync" |
| "testing" |
| @@ -17,6 +18,7 @@ import ( |
| "github.com/carlosframework/platform/internal/billing" |
| "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/provision" |
| "github.com/carlosframework/platform/internal/registrar" |
| "github.com/carlosframework/platform/internal/registrar/registrartest" |
| "github.com/carlosframework/platform/internal/release" |
| @@ -765,3 +767,79 @@ func TestAppGoneCleanupFinishesAfterAppRestored(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// TestPurchaseAppGoneFirstRouteRecordBeforeClaim: a paid purchase whose app |
| +// went to Trash, with an ordinary first-route claim (Route "") and its |
| +// record on the apex. The cleanup retires the record before it deletes the |
| +// claim; a failed record retirement leaves the claim and the record, and |
| +// keeps the purchase captured on its app for the next pass. |
| +func TestPurchaseAppGoneFirstRouteRecordBeforeClaim(t *testing.T) { |
| + f := newPurchaseFixture(t) |
| + ctx := context.Background() |
| + p := f.start(t) |
| + f.authorise(t, p) |
| + f.until(t, p.ID, PurchaseRegistered) |
| + foreign := domains.Claim{Schema: 1, Host: "www." + p.Domain, Account: "foreign", App: "other", CreatedBy: "other"} |
| + if _, _, err := domains.AttachClaim(ctx, f.rel, foreign); err != nil { |
| + t.Fatal(err) |
| + } |
| + // Capture, then the apex attaches and www is refused. |
| + p = f.advance(t, p.ID) |
| + p = f.advance(t, p.ID) |
| + if p.State != PurchaseCaptured { |
| + t.Fatalf("partial attach: %+v", p) |
| + } |
| + // The apex becomes an ordinary first-route claim with its record. |
| + if err := f.rel.Delete(ctx, domains.ClaimKey(p.Domain)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, _, err := domains.AttachClaim(ctx, f.rel, domains.Claim{Schema: 1, Host: p.Domain, Account: f.acct.Sqid, App: "kass", |
| + CreatedAt: time.Now().UTC().Format(time.RFC3339)}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := provision.CreateRecord(ctx, f.rel, provision.Record{Schema: 1, Host: p.Domain, App: "kass", Account: f.acct.Sqid, |
| + Channel: "edge", Kind: "static", CreatedAt: time.Now().UTC()}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := f.st.TrashAppRow(f.acct.ID, "kass", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + recordKey := provision.RecordKey(f.acct.Sqid, "kass", p.Domain) |
| + h := &hookStore{Store: f.srv.cfg.Releases, failDelete: map[string]int{recordKey: 1}} |
| + f.srv.cfg.Releases = h |
| + |
| + if err := f.srv.advancePurchase(ctx, p.ID); err == nil { |
| + t.Fatal("a failed record retirement must be an error for the pass") |
| + } |
| + if p = f.read(t, p.ID); p.State != PurchaseCaptured || p.App != "kass" || p.Reason != "release_failed" { |
| + t.Fatalf("after a failed record retirement: %+v", p) |
| + } |
| + if c, err := domains.GetClaim(ctx, f.rel, p.Domain); err != nil || c.App != "kass" || c.Route != "" { |
| + t.Fatalf("the claim went with its record still there: %+v %v", c, err) |
| + } |
| + if _, err := provision.GetRecord(ctx, f.rel, f.acct.Sqid, "kass", p.Domain); err != nil { |
| + t.Fatalf("record after the failure: %v", err) |
| + } |
| + if len(f.zone.Managed[p.Domain].Apex) == 0 { |
| + t.Fatal("zone unpointed before the claims were released") |
| + } |
| + |
| + p = f.advance(t, p.ID) |
| + if p.State != PurchaseDone || p.App != "" || p.Reason != "app_gone" { |
| + t.Fatalf("finish: %+v", p) |
| + } |
| + if _, err := provision.GetRecord(ctx, f.rel, f.acct.Sqid, "kass", p.Domain); err == nil { |
| + t.Fatal("the record outlived the cleanup") |
| + } |
| + if c, err := domains.GetClaim(ctx, f.rel, p.Domain); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("apex claim kept: %+v %v", c, err) |
| + } |
| + rec, claim := slices.Index(h.deletes, recordKey), slices.Index(h.deletes, domains.ClaimKey(p.Domain)) |
| + if rec < 0 || claim < 0 || rec > claim { |
| + t.Fatalf("deletes %v: want the record (%d) before the claim (%d)", h.deletes, rec, claim) |
| + } |
| + if c, err := domains.GetClaim(ctx, f.rel, "www."+p.Domain); err != nil || c.Account != "foreign" { |
| + t.Fatalf("foreign www claim touched: %+v %v", c, err) |
| + } |
| + f.assertUnpointed(t, p.Domain) |
| +} |