package mail import ( "slices" "strings" "testing" "time" "github.com/carlosframework/platform/internal/release" ) func planApps(t *testing.T, s release.Store, acct string, p claimMailPlan) []string { t.Helper() apps, err := p.resolve(t.Context(), s, acct) if err != nil { t.Fatal(err) } var out []string for _, a := range apps { if a.Account != acct { t.Fatalf("plan crossed accounts: %+v", a) } out = append(out, a.App) } return out } // The preflighted set is persisted members, the account claim's app and the // proposed member, before any SES, DNS or store call. A claim held by // another account adds nothing (the walk refuses or lapses it). func TestClaimMailPlanIncludesClaimApp(t *testing.T) { s := release.NewDirStore(t.TempDir()) ctx := t.Context() now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC) // Fresh domain: only the proposed member. if got := planApps(t, s, "bab", claimMailPlan{Apps: []string{"second"}, Domain: "shared.com"}); !slices.Equal(got, []string{"second"}) { t.Fatalf("fresh: %v", got) } // First claimer's walk failed before its record: a sibling's re-run // admits the claim's app too, so the claim stamp is never refused // halfway through the walk. if err := claimDomain(ctx, s, "shared.com", "bab", "first", now); err != nil { t.Fatal(err) } if got := planApps(t, s, "bab", claimMailPlan{Apps: []string{"second"}, Domain: "shared.com"}); !slices.Equal(got, []string{"first", "second"}) { t.Fatalf("claim app: %v", got) } // Record members join. if _, err := PutDomain(ctx, s, "bab", &DomainRecord{Domain: "shared.com", Apps: []string{"third"}, Regions: map[string]*RegionState{}}, ""); err != nil { t.Fatal(err) } if got := planApps(t, s, "bab", claimMailPlan{Apps: []string{"second"}, Domain: "shared.com"}); !slices.Equal(got, []string{"first", "second", "third"}) { t.Fatalf("record members: %v", got) } // AppDomains (Teardown) covers the claim app of every referred domain. if got := planApps(t, s, "bab", claimMailPlan{Apps: []string{"third"}, AppDomains: "third"}); !slices.Equal(got, []string{"first", "third"}) { t.Fatalf("teardown: %v", got) } // Another account's claim adds nothing to this account's plan. if err := claimDomain(ctx, s, "other.com", "zed", "web", now); err != nil { t.Fatal(err) } if got := planApps(t, s, "bab", claimMailPlan{Apps: []string{"second"}, Domain: "other.com"}); !slices.Equal(got, []string{"second"}) { t.Fatalf("foreign claim: %v", got) } // An invalid domain never becomes a key. if _, err := (claimMailPlan{Apps: []string{"x"}, Domain: "../evil"}).resolve(ctx, s, "bab"); err == nil { t.Fatal("invalid domain resolved") } } func TestSenderOwnsUserNameForwardOnly(t *testing.T) { for _, c := range []struct { acct, app, id, user string want bool }{ {"bab", "shop", SenderAppID, UserName("bab", "shop"), true}, {"bab", "shop", SenderAppID, StandaloneUserName("bab", "shop", "laptop"), true}, {"bab", "shop", SenderAppID, UserName("bab", "shop-eu"), false}, {"bab", "shop", SenderAppID, StandaloneUserName("bab", "shop-eu", "laptop"), false}, {"bab", "shop-eu", SenderAppID, StandaloneUserName("bab", "shop", "eu"), false}, {"bab", "shop", SenderAppID, StandaloneUserName("bab", "shop", ""), false}, {"bab", "shop", SenderAppID, StandaloneUserName("bab", "shop", "a+s+b"), false}, {"bab", "shop", SenderAppID, StandaloneUserName("zed", "shop", "laptop"), false}, } { if got := SenderOwnsUserName(c.acct, c.app, c.id, c.user); got != c.want { t.Errorf("SenderOwnsUserName(%s, %s, %s, %s) = %v", c.acct, c.app, c.id, c.user, got) } } // A slugged sender owns its rendered names for any salt, and nothing // outside its own prefix. for _, salt := range []string{"", "0a1b2c3d"} { n, err := SenderNames("bab", "shop", "web1.example.com", salt) if err != nil { t.Fatal(err) } if !SenderOwnsUserName("bab", "shop", "web1.example.com", n.IAMUser) { t.Fatalf("own slugged user %s", n.IAMUser) } sa, err := n.StandaloneUser("bab", "shop", "ci") if err != nil { t.Fatal(err) } if !SenderOwnsUserName("bab", "shop", "web1.example.com", sa) { t.Fatalf("own slugged standalone %s", sa) } if SenderOwnsUserName("bab", "shop", "web1.example.com", strings.Replace(n.IAMUser, "-bab-", "-zed-", 1)) { t.Fatal("another account's slugged name") } if SenderOwnsUserName("bab", "shop", "web1.example.com", n.IAMUser+"x") { t.Fatal("over-long slug") } if SenderOwnsUserName("bab", "shop", SenderAppID, n.IAMUser) { t.Fatal("the app sender owns a slugged name") } } }