package provision import ( "slices" "strings" "testing" "amadan.net/oficina/alta/homeop" ) // CARLOS platform mail submission (carlos/platform plan Task 9): a team // environment of a switched app holds _MAIL_FROM and no SMTP // credential, and its instances send through the box's mailer. Alta // takes that as the app's mail, never waits past it for the five, and // never writes it. func TestPlatformKeysAcceptEitherMailChannel(t *testing.T) { calls, _ := AppBySlug("calls") slides, _ := AppBySlug("slides") docs, _ := AppBySlug("docs") set := func(keys ...string) map[string]bool { m := map[string]bool{} for _, k := range keys { m[k] = true } return m } five := SMTPKeys("CARLOS_SMTP") cases := []struct { name string app App present map[string]bool keys, missing []string wrong string }{ {"nothing yet waits for the five", calls, set(), nil, append(SMTPKeys("CALLS_SMTP"), "CARLOS_RELAY"), ""}, {"the five", calls, set(append(slices.Clone(five), "CARLOS_RELAY")...), append(slices.Clone(five), "CARLOS_RELAY"), nil, ""}, {"switched: the From alone", calls, set("CARLOS_MAIL_FROM", "CARLOS_RELAY"), []string{"CARLOS_MAIL_FROM", "CARLOS_RELAY"}, nil, ""}, {"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, ""}, {"switched still waits for the relay", calls, set("CARLOS_MAIL_FROM"), []string{"CARLOS_MAIL_FROM"}, []string{"CARLOS_RELAY"}, ""}, {"slides reads the CARLOS From", slides, set("CARLOS_MAIL_FROM"), []string{"CARLOS_MAIL_FROM"}, nil, ""}, {"slides' own SMTP prefix mid-flip", slides, set(append(SMTPKeys("SLIDES_SMTP"), "CARLOS_MAIL_FROM")...), append([]string{"CARLOS_MAIL_FROM"}, SMTPKeys("SLIDES_SMTP")...), nil, ""}, {"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:], ""}, {"a stray From beside the read one", slides, set("CARLOS_MAIL_FROM", "SLIDES_MAIL_FROM"), []string{"CARLOS_MAIL_FROM"}, nil, "SLIDES_SMTP"}, {"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"}, {"docs reads DOCS only", docs, set("DOCS_MAIL_FROM"), []string{"DOCS_MAIL_FROM"}, nil, ""}, {"a From under a prefix the binary does not read", docs, set("CARLOS_MAIL_FROM"), nil, SMTPKeys("DOCS_SMTP"), "CARLOS_SMTP"}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { st := PlatformKeys(c.app, c.present) if !slices.Equal(st.Keys, c.keys) || !slices.Equal(st.Missing, c.missing) || st.WrongPrefix != c.wrong { 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) } }) } } // The switched binaries read one From only: mail.FromCarlos("CARLOS"), // or "DOCS" for Docs (each app's main, "Mail through rastrillo's // FromCarlos"). A From under any other stem, which CARLOS delivers for an // app declared under that prefix, leaves the switched binary with no From // at all, so it is refused, never taken as mail. The five under the same // prefix are still admitted, and the From never stands in for them. func TestPlatformKeysRefuseAFromTheBinaryDoesNotRead(t *testing.T) { set := func(keys ...string) map[string]bool { m := map[string]bool{} for _, k := range keys { m[k] = true } return m } cases := []struct { slug, from, wrong string }{ {"slides", "SLIDES_MAIL_FROM", "SLIDES_SMTP"}, {"calendar", "CALENDAR_MAIL_FROM", "CALENDAR_SMTP"}, {"calls", "CALLS_MAIL_FROM", "CALLS_SMTP"}, {"sheets", "SHEETS_MAIL_FROM", "SHEETS_SMTP"}, {"memoria", "MEMORIA_MAIL_FROM", "MEMORIA_SMTP"}, {"docs", "CARLOS_MAIL_FROM", "CARLOS_SMTP"}, } for _, c := range cases { t.Run(c.slug+" "+c.from, func(t *testing.T) { app, _ := AppBySlug(c.slug) present := set(c.from, "CARLOS_RELAY") st := PlatformKeys(app, present) if st.WrongPrefix != c.wrong { t.Fatalf("wrong prefix %q, want %q", st.WrongPrefix, c.wrong) } if slices.Contains(st.Keys, c.from) { t.Fatalf("%s taken as mail: keys %v", c.from, st.Keys) } // Mid-flip under the same wrong prefix: the five do not make // the From readable. if c.slug == "docs" { return } for _, k := range SMTPKeys(c.wrong) { present[k] = true } if st := PlatformKeys(app, present); st.WrongPrefix != c.wrong { t.Fatalf("mid-flip: wrong prefix %q, want %q", st.WrongPrefix, c.wrong) } }) } } // A team run of an app switched while declared under a prefix whose From // its binary does not read fails at step 7 with mail-prefix-, as // Docs under CARLOS_SMTP does, rather than finishing a team that can // never send a sign-in link. func TestSwitchedAppUnderItsOwnPrefixRefuses(t *testing.T) { for _, c := range []struct{ slug, prefix string }{ {"slides", "SLIDES_SMTP"}, {"calendar", "CALENDAR_SMTP"}, {"calls", "CALLS_SMTP"}, } { t.Run(c.slug, func(t *testing.T) { h := newHarness(t) h.initialized() h.con.MailPrefix[c.slug] = c.prefix // SMTP reads it; the switched binary does not h.con.Submission[c.slug] = true e := h.engine(nil) run := h.createLagged(e) r := h.drive(e, run, 10) if r.State != "failed" || r.Code != "mail-prefix-"+c.slug { t.Fatalf("%s %s", r.State, r.Code) } }) } } func TestMailFromKey(t *testing.T) { for prefix, want := range map[string]string{"CARLOS_SMTP": "CARLOS_MAIL_FROM", "DOCS_SMTP": "DOCS_MAIL_FROM", "_SMTP": "", "CARLOS": ""} { if got := MailFromKey(prefix); got != want { t.Errorf("MailFromKey(%q) = %q, want %q", prefix, got, want) } } } func TestSwitchedAppRunsWithTheFromAndNoCredential(t *testing.T) { h := newHarness(t) h.initialized() h.con.MailPrefix["calls"] = "CARLOS_SMTP" // as calls is declared live h.con.Submission["calls"] = true e := h.engine(nil) run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "beta", Name: "Beta", Owner: ownerCanary, Apps: []string{"calls"}}) if err != nil { t.Fatal(err) } h.confirm(homeop.KindCreateTeam) _ = e.Advance(h.ctx(), run) st, _, _ := h.store.Step(h.ctx(), "beta", "calls", StepEnv) if st.Code != "awaiting-platform-keys" { t.Fatalf("env step %s %s", st.State, st.Code) } h.con.Walk() if r := h.drive(e, run, 20); r.State != "done" { t.Fatalf("%s %s", r.State, r.Code) } vars, _, _ := h.con.Env("calls", "beta") if _, ok := vars["CARLOS_MAIL_FROM"]; !ok { t.Fatal("no From variable") } for k := range vars { if strings.Contains(k, "_SMTP_") { t.Fatalf("a switched app's team environment holds %s", k) } } done, _, _ := h.store.Done(h.ctx(), "beta", "calls", StepEnv) if platform, _ := done["platform"].([]any); !slices.ContainsFunc(platform, func(v any) bool { return v == "CARLOS_MAIL_FROM" }) { t.Fatalf("env step's platform keys %v do not name the From", done["platform"]) } calls, _ := AppBySlug("calls") if err := e.AuditEnvironment(h.ctx(), "beta", calls); err != nil { t.Fatalf("audit: %v", err) } } func TestSwitchedDocsUnderTheWrongPrefixRefuses(t *testing.T) { h := newHarness(t) h.initialized() h.con.MailPrefix["docs"] = "CARLOS_SMTP" // Docs reads DOCS_MAIL_FROM only h.con.Submission["docs"] = true e := h.engine(nil) run := h.createLagged(e) r := h.drive(e, run, 10) if r.State != "failed" || r.Code != "mail-prefix-docs" { t.Fatalf("%s %s", r.State, r.Code) } }