carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package mail
2
3import (
4 "slices"
5 "strings"
6 "testing"
7 "time"
8
9 "github.com/carlosframework/platform/internal/release"
10)
11
12func planApps(t *testing.T, s release.Store, acct string, p claimMailPlan) []string {
13 t.Helper()
14 apps, err := p.resolve(t.Context(), s, acct)
15 if err != nil {
16 t.Fatal(err)
17 }
18 var out []string
19 for _, a := range apps {
20 if a.Account != acct {
21 t.Fatalf("plan crossed accounts: %+v", a)
22 }
23 out = append(out, a.App)
24 }
25 return out
26}
27
28// The preflighted set is persisted members, the account claim's app and the
29// proposed member, before any SES, DNS or store call. A claim held by
30// another account adds nothing (the walk refuses or lapses it).
31func TestClaimMailPlanIncludesClaimApp(t *testing.T) {
32 s := release.NewDirStore(t.TempDir())
33 ctx := t.Context()
34 now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
35
36 // Fresh domain: only the proposed member.
37 if got := planApps(t, s, "bab", claimMailPlan{Apps: []string{"second"}, Domain: "shared.com"}); !slices.Equal(got, []string{"second"}) {
38 t.Fatalf("fresh: %v", got)
39 }
40 // First claimer's walk failed before its record: a sibling's re-run
41 // admits the claim's app too, so the claim stamp is never refused
42 // halfway through the walk.
43 if err := claimDomain(ctx, s, "shared.com", "bab", "first", now); err != nil {
44 t.Fatal(err)
45 }
46 if got := planApps(t, s, "bab", claimMailPlan{Apps: []string{"second"}, Domain: "shared.com"}); !slices.Equal(got, []string{"first", "second"}) {
47 t.Fatalf("claim app: %v", got)
48 }
49 // Record members join.
50 if _, err := PutDomain(ctx, s, "bab", &DomainRecord{Domain: "shared.com", Apps: []string{"third"}, Regions: map[string]*RegionState{}}, ""); err != nil {
51 t.Fatal(err)
52 }
53 if got := planApps(t, s, "bab", claimMailPlan{Apps: []string{"second"}, Domain: "shared.com"}); !slices.Equal(got, []string{"first", "second", "third"}) {
54 t.Fatalf("record members: %v", got)
55 }
56 // AppDomains (Teardown) covers the claim app of every referred domain.
57 if got := planApps(t, s, "bab", claimMailPlan{Apps: []string{"third"}, AppDomains: "third"}); !slices.Equal(got, []string{"first", "third"}) {
58 t.Fatalf("teardown: %v", got)
59 }
60 // Another account's claim adds nothing to this account's plan.
61 if err := claimDomain(ctx, s, "other.com", "zed", "web", now); err != nil {
62 t.Fatal(err)
63 }
64 if got := planApps(t, s, "bab", claimMailPlan{Apps: []string{"second"}, Domain: "other.com"}); !slices.Equal(got, []string{"second"}) {
65 t.Fatalf("foreign claim: %v", got)
66 }
67 // An invalid domain never becomes a key.
68 if _, err := (claimMailPlan{Apps: []string{"x"}, Domain: "../evil"}).resolve(ctx, s, "bab"); err == nil {
69 t.Fatal("invalid domain resolved")
70 }
71}
72
73func TestSenderOwnsUserNameForwardOnly(t *testing.T) {
74 for _, c := range []struct {
75 acct, app, id, user string
76 want bool
77 }{
78 {"bab", "shop", SenderAppID, UserName("bab", "shop"), true},
79 {"bab", "shop", SenderAppID, StandaloneUserName("bab", "shop", "laptop"), true},
80 {"bab", "shop", SenderAppID, UserName("bab", "shop-eu"), false},
81 {"bab", "shop", SenderAppID, StandaloneUserName("bab", "shop-eu", "laptop"), false},
82 {"bab", "shop-eu", SenderAppID, StandaloneUserName("bab", "shop", "eu"), false},
83 {"bab", "shop", SenderAppID, StandaloneUserName("bab", "shop", ""), false},
84 {"bab", "shop", SenderAppID, StandaloneUserName("bab", "shop", "a+s+b"), false},
85 {"bab", "shop", SenderAppID, StandaloneUserName("zed", "shop", "laptop"), false},
86 } {
87 if got := SenderOwnsUserName(c.acct, c.app, c.id, c.user); got != c.want {
88 t.Errorf("SenderOwnsUserName(%s, %s, %s, %s) = %v", c.acct, c.app, c.id, c.user, got)
89 }
90 }
91
92 // A slugged sender owns its rendered names for any salt, and nothing
93 // outside its own prefix.
94 for _, salt := range []string{"", "0a1b2c3d"} {
95 n, err := SenderNames("bab", "shop", "web1.example.com", salt)
96 if err != nil {
97 t.Fatal(err)
98 }
99 if !SenderOwnsUserName("bab", "shop", "web1.example.com", n.IAMUser) {
100 t.Fatalf("own slugged user %s", n.IAMUser)
101 }
102 sa, err := n.StandaloneUser("bab", "shop", "ci")
103 if err != nil {
104 t.Fatal(err)
105 }
106 if !SenderOwnsUserName("bab", "shop", "web1.example.com", sa) {
107 t.Fatalf("own slugged standalone %s", sa)
108 }
109 if SenderOwnsUserName("bab", "shop", "web1.example.com", strings.Replace(n.IAMUser, "-bab-", "-zed-", 1)) {
110 t.Fatal("another account's slugged name")
111 }
112 if SenderOwnsUserName("bab", "shop", "web1.example.com", n.IAMUser+"x") {
113 t.Fatal("over-long slug")
114 }
115 if SenderOwnsUserName("bab", "shop", SenderAppID, n.IAMUser) {
116 t.Fatal("the app sender owns a slugged name")
117 }
118 }
119}
120