| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/carlosframework/platform/internal/hostops" |
| 8 | "github.com/carlosframework/platform/internal/mail" |
| 9 | "github.com/carlosframework/platform/internal/release" |
| 10 | ) |
| 11 | |
| 12 | // Rev 5 P2 (C6): the enabled purge's mail readiness uses strict, complete |
| 13 | // inventories. Any claim or domain record it cannot read, parse or match to |
| 14 | // its own key stops the purge. The original used ListDomainClaims and |
| 15 | // ListDomains, which log and skip such objects, so an unreadable orphan claim |
| 16 | // let the purge complete (nil error here) with its SES identity standing. |
| 17 | func TestClaimPurgeMailReadyRefusesIncompleteInventory(t *testing.T) { |
| 18 | const orphan = "email/domain-claims/orphan.example.com.json" |
| 19 | cases := []struct { |
| 20 | name string |
| 21 | key string |
| 22 | body string |
| 23 | }{ |
| 24 | {"malformed claim", orphan, "{not json"}, |
| 25 | {"newer schema claim", orphan, `{"schema":2,"domain":"orphan.example.com","account":"bab","app":"web"}`}, |
| 26 | {"claim body for another domain", orphan, `{"schema":1,"domain":"other.example.com","account":"bab","app":"web"}`}, |
| 27 | {"claim with an invalid account", orphan, `{"schema":1,"domain":"orphan.example.com","account":"","app":"web"}`}, |
| 28 | {"stray object under the claim prefix", "email/domain-claims/notes.txt", "x"}, |
| 29 | {"malformed domain record", mail.DomainKey("bab", "broken.example.com"), "{not json"}, |
| 30 | {"domain record under the wrong key", mail.DomainKey("bab", "wrong.example.com"), `{"schema":1,"domain":"right.example.com","apps":["web"]}`}, |
| 31 | } |
| 32 | for _, tc := range cases { |
| 33 | t.Run(tc.name, func(t *testing.T) { |
| 34 | raw := release.NewDirStore(t.TempDir()) |
| 35 | p := &ClaimPurgeCoordinator{Server: &Server{}, Controller: hostops.Controller{Store: raw}} |
| 36 | // Control: a clean store is ready (the app has no mail estate). |
| 37 | if err := p.mailReady(t.Context(), "bab", "web"); err != nil { |
| 38 | t.Fatalf("clean store: %v", err) |
| 39 | } |
| 40 | if _, err := raw.Put(t.Context(), tc.key, []byte(tc.body), ""); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if err := p.mailReady(t.Context(), "bab", "web"); !errors.Is(err, mail.ErrDomainInventory) { |
| 44 | t.Fatalf("mailReady with %s: err=%v, want ErrDomainInventory", tc.name, err) |
| 45 | } |
| 46 | }) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // A complete inventory of valid claims, other accounts' included, is ready. |
| 51 | func TestClaimPurgeMailReadyAcceptsCompleteInventory(t *testing.T) { |
| 52 | raw := release.NewDirStore(t.TempDir()) |
| 53 | putMailClaim(t, raw, mail.DomainClaim{Domain: "a.example.com", Account: "zed", App: "other"}) |
| 54 | putMailJSON(t, raw, mail.DomainKey("bab", "b.example.com"), mail.DomainRecord{Schema: 1, Domain: "b.example.com", Apps: []string{"sibling"}}) |
| 55 | p := &ClaimPurgeCoordinator{Server: &Server{}, Controller: hostops.Controller{Store: raw}} |
| 56 | if err := p.mailReady(t.Context(), "bab", "web"); err != nil { |
| 57 | t.Fatalf("complete inventory: %v", err) |
| 58 | } |
| 59 | // Its own orphan claim is an estate, and with no provisioner that is the |
| 60 | // contract refusal, not an inventory one. |
| 61 | putMailClaim(t, raw, mail.DomainClaim{Domain: "mine.example.com", Account: "bab", App: "web"}) |
| 62 | if err := p.mailReady(t.Context(), "bab", "web"); !errors.Is(err, hostops.ErrContract) { |
| 63 | t.Fatalf("own claim without a provisioner: %v", err) |
| 64 | } |
| 65 | } |
| 66 | |