package main import ( "context" "encoding/json" "io" "log/slog" "net" "net/http" "os" "path/filepath" "strings" "testing" "amadan.net/rastrillo/rastrillo/mail" ) // The three boots CARLOS platform mail submission needs (carlos plan // Task 9): on submission with the agent's socket, on SMTP alone, and // with neither, where newMailer returns nil and calls.App refuses // outside development (the mail case of resolve, // in internal/calls config.go). var mailVars = []string{"CARLOS_MAIL", "CARLOS_CONTROL_SOCKET", "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM", "CALLS_MAIL_FROM"} func mailerWith(t *testing.T, env map[string]string) (mail.Sender, error) { t.Helper() for _, k := range mailVars { t.Setenv(k, "") } for _, p := range []string{"CALLS_SMTP_", "CARLOS_SMTP_"} { for _, s := range []string{"HOST", "PORT", "USER", "PASS", "FROM"} { t.Setenv(p+s, "") } } for k, v := range env { t.Setenv(k, v) } return newMailer(slog.New(slog.DiscardHandler)) } // controlSocket stands in for the agent's socket: it answers POST // /v1/mail and reports each request's path and From. func controlSocket(t *testing.T) (string, chan string) { t.Helper() dir, err := os.MkdirTemp("/tmp", "calls-mail") // unix paths are short if err != nil { t.Fatal(err) } t.Cleanup(func() { os.RemoveAll(dir) }) socket := filepath.Join(dir, "control.sock") ln, err := net.Listen("unix", socket) if err != nil { t.Fatal(err) } seen := make(chan string, 1) srv := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var body struct { From string `json:"from"` } raw, _ := io.ReadAll(r.Body) _ = json.Unmarshal(raw, &body) seen <- r.Method + " " + r.URL.Path + " " + body.From w.Write([]byte(`{"message_id":"m-1"}`)) })} go srv.Serve(ln) t.Cleanup(func() { srv.Close() }) return socket, seen } // The From is the one the platform delivers, CARLOS_MAIL_FROM (Calls // is declared CARLOS_SMTP), and nothing of Calls' own overrides it. func TestNewMailerSubmits(t *testing.T) { socket, seen := controlSocket(t) sender, err := mailerWith(t, map[string]string{ "CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket, "CARLOS_ADMIN_TOKEN": "token", "CARLOS_MAIL_FROM": "noreply@calls.oficina.works", // Not delivered by the platform, so not read. "CALLS_MAIL_FROM": "calls@calls.oficina.works", // SMTP left over beside it is not used. "CARLOS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com", "CARLOS_SMTP_FROM": "old@calls.oficina.works"}) if err != nil || sender == nil { t.Fatalf("sender %v, err %v", sender, err) } if err := sender.Send(context.Background(), "ana@example.com", "Sign in", "link"); err != nil { t.Fatal(err) } if got, want := <-seen, "POST /v1/mail noreply@calls.oficina.works"; got != want { t.Fatalf("got %q, want %q", got, want) } } func TestNewMailerUsesSMTPAlone(t *testing.T) { sender, err := mailerWith(t, map[string]string{ "CARLOS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com", "CARLOS_SMTP_FROM": "noreply@calls.oficina.works"}) if err != nil || sender == nil || !mail.IsConfigured(sender) { t.Fatalf("sender %v, err %v", sender, err) } } func TestNewMailerWithNeitherLeavesItToNew(t *testing.T) { sender, err := mailerWith(t, nil) if err != nil || sender != nil { t.Fatalf("sender %v, err %v: with no channel calls.App must decide", sender, err) } } // A submission missing anything the agent or platform delivers is a // boot refusal, never SMTP or logged mail in its place: not even with // SMTP still configured beside it. func TestNewMailerRefusesHalfASubmission(t *testing.T) { socket, _ := controlSocket(t) for _, missing := range []string{"CARLOS_CONTROL_SOCKET", "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM"} { t.Run(missing, func(t *testing.T) { env := map[string]string{"CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket, "CARLOS_ADMIN_TOKEN": "token", "CARLOS_MAIL_FROM": "noreply@calls.oficina.works", "CALLS_MAIL_FROM": "calls@calls.oficina.works", "CALLS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com", "CALLS_SMTP_FROM": "old@calls.oficina.works"} delete(env, missing) _, err := mailerWith(t, env) if err == nil || !strings.Contains(err.Error(), missing) { t.Fatalf("submission with no %s: %v", missing, err) } }) } }