oficina / alta Public

Clone
git clone https://amadan.net/oficina/alta

Plain git — no account needed to clone.

Download

Download this file

1package provision
2
3import (
4 "slices"
5 "strings"
6 "testing"
7
8 "amadan.net/oficina/alta/homeop"
9)
10
11// CARLOS platform mail submission (carlos/platform plan Task 9): a team
12// environment of a switched app holds <stem>_MAIL_FROM and no SMTP
13// credential, and its instances send through the box's mailer. Alta
14// takes that as the app's mail, never waits past it for the five, and
15// never writes it.
16
17func TestPlatformKeysAcceptEitherMailChannel(t *testing.T) {
18 calls, _ := AppBySlug("calls")
19 slides, _ := AppBySlug("slides")
20 docs, _ := AppBySlug("docs")
21 set := func(keys ...string) map[string]bool {
22 m := map[string]bool{}
23 for _, k := range keys {
24 m[k] = true
25 }
26 return m
27 }
28 five := SMTPKeys("CARLOS_SMTP")
29 cases := []struct {
30 name string
31 app App
32 present map[string]bool
33 keys, missing []string
34 wrong string
35 }{
36 {"nothing yet waits for the five", calls, set(), nil, append(SMTPKeys("CALLS_SMTP"), "CARLOS_RELAY"), ""},
37 {"the five", calls, set(append(slices.Clone(five), "CARLOS_RELAY")...), append(slices.Clone(five), "CARLOS_RELAY"), nil, ""},
38 {"switched: the From alone", calls, set("CARLOS_MAIL_FROM", "CARLOS_RELAY"), []string{"CARLOS_MAIL_FROM", "CARLOS_RELAY"}, nil, ""},
39 {"between a flip: both", calls, set(append(slices.Clone(five), "CARLOS_MAIL_FROM", "CARLOS_RELAY")...), append(append([]string{"CARLOS_MAIL_FROM"}, five...), "CARLOS_RELAY"), nil, ""},
40 {"switched still waits for the relay", calls, set("CARLOS_MAIL_FROM"), []string{"CARLOS_MAIL_FROM"}, []string{"CARLOS_RELAY"}, ""},
41 {"slides reads the CARLOS From", slides, set("CARLOS_MAIL_FROM"), []string{"CARLOS_MAIL_FROM"}, nil, ""},
42 {"slides' own SMTP prefix mid-flip", slides, set(append(SMTPKeys("SLIDES_SMTP"), "CARLOS_MAIL_FROM")...), append([]string{"CARLOS_MAIL_FROM"}, SMTPKeys("SLIDES_SMTP")...), nil, ""},
43 {"switched, the five partly gone", calls, set("CARLOS_MAIL_FROM", "CARLOS_SMTP_HOST", "CARLOS_RELAY"), []string{"CARLOS_MAIL_FROM", "CARLOS_SMTP_HOST", "CARLOS_RELAY"}, SMTPKeys("CARLOS_SMTP")[1:], ""},
44 {"a stray From beside the read one", slides, set("CARLOS_MAIL_FROM", "SLIDES_MAIL_FROM"), []string{"CARLOS_MAIL_FROM"}, nil, "SLIDES_SMTP"},
45 {"the read From beside five under a wrong prefix", docs, set(append(SMTPKeys("CARLOS_SMTP"), "DOCS_MAIL_FROM")...), []string{"DOCS_MAIL_FROM"}, nil, "CARLOS_SMTP"},
46 {"docs reads DOCS only", docs, set("DOCS_MAIL_FROM"), []string{"DOCS_MAIL_FROM"}, nil, ""},
47 {"a From under a prefix the binary does not read", docs, set("CARLOS_MAIL_FROM"), nil, SMTPKeys("DOCS_SMTP"), "CARLOS_SMTP"},
48 }
49 for _, c := range cases {
50 t.Run(c.name, func(t *testing.T) {
51 st := PlatformKeys(c.app, c.present)
52 if !slices.Equal(st.Keys, c.keys) || !slices.Equal(st.Missing, c.missing) || st.WrongPrefix != c.wrong {
53 t.Fatalf("got keys %v missing %v wrong %q; want %v %v %q", st.Keys, st.Missing, st.WrongPrefix, c.keys, c.missing, c.wrong)
54 }
55 })
56 }
57}
58
59// The switched binaries read one From only: mail.FromCarlos("CARLOS"),
60// or "DOCS" for Docs (each app's main, "Mail through rastrillo's
61// FromCarlos"). A From under any other stem, which CARLOS delivers for an
62// app declared under that prefix, leaves the switched binary with no From
63// at all, so it is refused, never taken as mail. The five under the same
64// prefix are still admitted, and the From never stands in for them.
65func TestPlatformKeysRefuseAFromTheBinaryDoesNotRead(t *testing.T) {
66 set := func(keys ...string) map[string]bool {
67 m := map[string]bool{}
68 for _, k := range keys {
69 m[k] = true
70 }
71 return m
72 }
73 cases := []struct {
74 slug, from, wrong string
75 }{
76 {"slides", "SLIDES_MAIL_FROM", "SLIDES_SMTP"},
77 {"calendar", "CALENDAR_MAIL_FROM", "CALENDAR_SMTP"},
78 {"calls", "CALLS_MAIL_FROM", "CALLS_SMTP"},
79 {"sheets", "SHEETS_MAIL_FROM", "SHEETS_SMTP"},
80 {"memoria", "MEMORIA_MAIL_FROM", "MEMORIA_SMTP"},
81 {"docs", "CARLOS_MAIL_FROM", "CARLOS_SMTP"},
82 }
83 for _, c := range cases {
84 t.Run(c.slug+" "+c.from, func(t *testing.T) {
85 app, _ := AppBySlug(c.slug)
86 present := set(c.from, "CARLOS_RELAY")
87 st := PlatformKeys(app, present)
88 if st.WrongPrefix != c.wrong {
89 t.Fatalf("wrong prefix %q, want %q", st.WrongPrefix, c.wrong)
90 }
91 if slices.Contains(st.Keys, c.from) {
92 t.Fatalf("%s taken as mail: keys %v", c.from, st.Keys)
93 }
94 // Mid-flip under the same wrong prefix: the five do not make
95 // the From readable.
96 if c.slug == "docs" {
97 return
98 }
99 for _, k := range SMTPKeys(c.wrong) {
100 present[k] = true
101 }
102 if st := PlatformKeys(app, present); st.WrongPrefix != c.wrong {
103 t.Fatalf("mid-flip: wrong prefix %q, want %q", st.WrongPrefix, c.wrong)
104 }
105 })
106 }
107}
108
109// A team run of an app switched while declared under a prefix whose From
110// its binary does not read fails at step 7 with mail-prefix-<app>, as
111// Docs under CARLOS_SMTP does, rather than finishing a team that can
112// never send a sign-in link.
113func TestSwitchedAppUnderItsOwnPrefixRefuses(t *testing.T) {
114 for _, c := range []struct{ slug, prefix string }{
115 {"slides", "SLIDES_SMTP"},
116 {"calendar", "CALENDAR_SMTP"},
117 {"calls", "CALLS_SMTP"},
118 } {
119 t.Run(c.slug, func(t *testing.T) {
120 h := newHarness(t)
121 h.initialized()
122 h.con.MailPrefix[c.slug] = c.prefix // SMTP reads it; the switched binary does not
123 h.con.Submission[c.slug] = true
124 e := h.engine(nil)
125 run := h.createLagged(e)
126 r := h.drive(e, run, 10)
127 if r.State != "failed" || r.Code != "mail-prefix-"+c.slug {
128 t.Fatalf("%s %s", r.State, r.Code)
129 }
130 })
131 }
132}
133
134func TestMailFromKey(t *testing.T) {
135 for prefix, want := range map[string]string{"CARLOS_SMTP": "CARLOS_MAIL_FROM", "DOCS_SMTP": "DOCS_MAIL_FROM", "_SMTP": "", "CARLOS": ""} {
136 if got := MailFromKey(prefix); got != want {
137 t.Errorf("MailFromKey(%q) = %q, want %q", prefix, got, want)
138 }
139 }
140}
141
142func TestSwitchedAppRunsWithTheFromAndNoCredential(t *testing.T) {
143 h := newHarness(t)
144 h.initialized()
145 h.con.MailPrefix["calls"] = "CARLOS_SMTP" // as calls is declared live
146 h.con.Submission["calls"] = true
147 e := h.engine(nil)
148 run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "beta", Name: "Beta", Owner: ownerCanary, Apps: []string{"calls"}})
149 if err != nil {
150 t.Fatal(err)
151 }
152 h.confirm(homeop.KindCreateTeam)
153 _ = e.Advance(h.ctx(), run)
154 st, _, _ := h.store.Step(h.ctx(), "beta", "calls", StepEnv)
155 if st.Code != "awaiting-platform-keys" {
156 t.Fatalf("env step %s %s", st.State, st.Code)
157 }
158 h.con.Walk()
159 if r := h.drive(e, run, 20); r.State != "done" {
160 t.Fatalf("%s %s", r.State, r.Code)
161 }
162 vars, _, _ := h.con.Env("calls", "beta")
163 if _, ok := vars["CARLOS_MAIL_FROM"]; !ok {
164 t.Fatal("no From variable")
165 }
166 for k := range vars {
167 if strings.Contains(k, "_SMTP_") {
168 t.Fatalf("a switched app's team environment holds %s", k)
169 }
170 }
171 done, _, _ := h.store.Done(h.ctx(), "beta", "calls", StepEnv)
172 if platform, _ := done["platform"].([]any); !slices.ContainsFunc(platform, func(v any) bool { return v == "CARLOS_MAIL_FROM" }) {
173 t.Fatalf("env step's platform keys %v do not name the From", done["platform"])
174 }
175 calls, _ := AppBySlug("calls")
176 if err := e.AuditEnvironment(h.ctx(), "beta", calls); err != nil {
177 t.Fatalf("audit: %v", err)
178 }
179}
180
181func TestSwitchedDocsUnderTheWrongPrefixRefuses(t *testing.T) {
182 h := newHarness(t)
183 h.initialized()
184 h.con.MailPrefix["docs"] = "CARLOS_SMTP" // Docs reads DOCS_MAIL_FROM only
185 h.con.Submission["docs"] = true
186 e := h.engine(nil)
187 run := h.createLagged(e)
188 r := h.drive(e, run, 10)
189 if r.State != "failed" || r.Code != "mail-prefix-docs" {
190 t.Fatalf("%s %s", r.State, r.Code)
191 }
192}
193