| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "log/slog" |
| 8 | "net" |
| 9 | "net/http" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "amadan.net/rastrillo/rastrillo/mail" |
| 16 | ) |
| 17 | |
| 18 | // The three boots CARLOS platform mail submission needs (carlos plan |
| 19 | // Task 9): on submission with the agent's socket, on SMTP alone, and |
| 20 | // with neither, where newMailer returns nil and slides.New refuses |
| 21 | // outside development (TestProductionRefusesEachMissingSetting, |
| 22 | // "Mailer", in internal/slides). |
| 23 | |
| 24 | var mailVars = []string{"CARLOS_MAIL", "CARLOS_CONTROL_SOCKET", |
| 25 | "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM", "SLIDES_MAIL_FROM"} |
| 26 | |
| 27 | func mailerWith(t *testing.T, env map[string]string) (mail.Sender, error) { |
| 28 | t.Helper() |
| 29 | for _, k := range mailVars { |
| 30 | t.Setenv(k, "") |
| 31 | } |
| 32 | for _, p := range []string{"SLIDES_SMTP_", "CARLOS_SMTP_"} { |
| 33 | for _, s := range []string{"HOST", "PORT", "USER", "PASS", "FROM"} { |
| 34 | t.Setenv(p+s, "") |
| 35 | } |
| 36 | } |
| 37 | for k, v := range env { |
| 38 | t.Setenv(k, v) |
| 39 | } |
| 40 | return newMailer(slog.New(slog.DiscardHandler)) |
| 41 | } |
| 42 | |
| 43 | // controlSocket stands in for the agent's socket: it answers POST |
| 44 | // /v1/mail and reports each request's path and From. |
| 45 | func controlSocket(t *testing.T) (string, chan string) { |
| 46 | t.Helper() |
| 47 | dir, err := os.MkdirTemp("/tmp", "slides-mail") // unix paths are short |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | t.Cleanup(func() { os.RemoveAll(dir) }) |
| 52 | socket := filepath.Join(dir, "control.sock") |
| 53 | ln, err := net.Listen("unix", socket) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | seen := make(chan string, 1) |
| 58 | srv := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 59 | var body struct { |
| 60 | From string `json:"from"` |
| 61 | } |
| 62 | raw, _ := io.ReadAll(r.Body) |
| 63 | _ = json.Unmarshal(raw, &body) |
| 64 | seen <- r.Method + " " + r.URL.Path + " " + body.From |
| 65 | w.Write([]byte(`{"message_id":"m-1"}`)) |
| 66 | })} |
| 67 | go srv.Serve(ln) |
| 68 | t.Cleanup(func() { srv.Close() }) |
| 69 | return socket, seen |
| 70 | } |
| 71 | |
| 72 | // The From is the one the platform delivers, CARLOS_MAIL_FROM (Slides |
| 73 | // is declared CARLOS_SMTP), and nothing of Slides' own overrides it. |
| 74 | func TestNewMailerSubmits(t *testing.T) { |
| 75 | socket, seen := controlSocket(t) |
| 76 | sender, err := mailerWith(t, map[string]string{ |
| 77 | "CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket, |
| 78 | "CARLOS_ADMIN_TOKEN": "token", |
| 79 | "CARLOS_MAIL_FROM": "noreply@slides.oficina.works", |
| 80 | // Not delivered by the platform, so not read. |
| 81 | "SLIDES_MAIL_FROM": "decks@slides.oficina.works", |
| 82 | // SMTP left over beside it is not used. |
| 83 | "CARLOS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com", |
| 84 | "CARLOS_SMTP_FROM": "old@slides.oficina.works"}) |
| 85 | if err != nil || sender == nil { |
| 86 | t.Fatalf("sender %v, err %v", sender, err) |
| 87 | } |
| 88 | if err := sender.Send(context.Background(), "ana@example.com", "Sign in", "link"); err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | if got, want := <-seen, "POST /v1/mail noreply@slides.oficina.works"; got != want { |
| 92 | t.Fatalf("got %q, want %q", got, want) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestNewMailerUsesSMTPAlone(t *testing.T) { |
| 97 | sender, err := mailerWith(t, map[string]string{ |
| 98 | "CARLOS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com", |
| 99 | "CARLOS_SMTP_FROM": "noreply@slides.oficina.works"}) |
| 100 | if err != nil || sender == nil || !mail.IsConfigured(sender) { |
| 101 | t.Fatalf("sender %v, err %v", sender, err) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestNewMailerWithNeitherLeavesItToNew(t *testing.T) { |
| 106 | sender, err := mailerWith(t, nil) |
| 107 | if err != nil || sender != nil { |
| 108 | t.Fatalf("sender %v, err %v: with no channel slides.New must decide", sender, err) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // A submission missing anything the agent or platform delivers is a |
| 113 | // boot refusal, never SMTP or logged mail in its place: not even with |
| 114 | // SMTP still configured beside it. |
| 115 | func TestNewMailerRefusesHalfASubmission(t *testing.T) { |
| 116 | socket, _ := controlSocket(t) |
| 117 | for _, missing := range []string{"CARLOS_CONTROL_SOCKET", |
| 118 | "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM"} { |
| 119 | t.Run(missing, func(t *testing.T) { |
| 120 | env := map[string]string{"CARLOS_MAIL": "submit", |
| 121 | "CARLOS_CONTROL_SOCKET": socket, "CARLOS_ADMIN_TOKEN": "token", |
| 122 | "CARLOS_MAIL_FROM": "noreply@slides.oficina.works", |
| 123 | "SLIDES_MAIL_FROM": "decks@slides.oficina.works", |
| 124 | "SLIDES_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com", |
| 125 | "SLIDES_SMTP_FROM": "old@slides.oficina.works"} |
| 126 | delete(env, missing) |
| 127 | _, err := mailerWith(t, env) |
| 128 | if err == nil || !strings.Contains(err.Error(), missing) { |
| 129 | t.Fatalf("submission with no %s: %v", missing, err) |
| 130 | } |
| 131 | }) |
| 132 | } |
| 133 | } |
| 134 | |