oficina / calendar Public

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

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
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 calendar.New refuses
21// outside development (TestNoMailRefusesOutsideDevelopment, in
22// internal/calendar).
23
24var mailVars = []string{"CARLOS_MAIL", "CARLOS_CONTROL_SOCKET",
25 "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM", "CALENDAR_MAIL_FROM"}
26
27func 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{"CALENDAR_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.
45func controlSocket(t *testing.T) (string, chan string) {
46 t.Helper()
47 dir, err := os.MkdirTemp("/tmp", "calendar-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
73// (Calendar is declared CARLOS_SMTP), and nothing of Calendar's own
74// overrides it.
75func TestNewMailerSubmits(t *testing.T) {
76 socket, seen := controlSocket(t)
77 sender, err := mailerWith(t, map[string]string{
78 "CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket,
79 "CARLOS_ADMIN_TOKEN": "token",
80 "CARLOS_MAIL_FROM": "noreply@calendar.oficina.works",
81 // Not delivered by the platform, so not read.
82 "CALENDAR_MAIL_FROM": "reminders@calendar.oficina.works",
83 // SMTP left over beside it is not used.
84 "CALENDAR_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com",
85 "CALENDAR_SMTP_FROM": "old@calendar.oficina.works"})
86 if err != nil || sender == nil {
87 t.Fatalf("sender %v, err %v", sender, err)
88 }
89 if err := sender.Send(context.Background(), "ana@example.com", "Reminder", "body"); err != nil {
90 t.Fatal(err)
91 }
92 if got, want := <-seen, "POST /v1/mail noreply@calendar.oficina.works"; got != want {
93 t.Fatalf("got %q, want %q", got, want)
94 }
95}
96
97// SMTP alone is what Calendar has always booted on, CALENDAR_ first.
98func TestNewMailerUsesSMTPAlone(t *testing.T) {
99 for _, prefix := range []string{"CARLOS", "CALENDAR"} {
100 t.Run(prefix, func(t *testing.T) {
101 sender, err := mailerWith(t, map[string]string{
102 prefix + "_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com",
103 prefix + "_SMTP_FROM": "noreply@calendar.oficina.works"})
104 if err != nil || sender == nil || !mail.IsConfigured(sender) {
105 t.Fatalf("sender %v, err %v", sender, err)
106 }
107 })
108 }
109}
110
111func TestNewMailerWithNeitherLeavesItToNew(t *testing.T) {
112 sender, err := mailerWith(t, nil)
113 if err != nil || sender != nil {
114 t.Fatalf("sender %v, err %v: with no channel calendar.New must decide", sender, err)
115 }
116}
117
118// A submission missing anything the agent or platform delivers is a
119// boot refusal, never SMTP or logged mail in its place: not even with
120// SMTP still configured beside it.
121func TestNewMailerRefusesHalfASubmission(t *testing.T) {
122 socket, _ := controlSocket(t)
123 for _, missing := range []string{"CARLOS_CONTROL_SOCKET",
124 "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM"} {
125 t.Run(missing, func(t *testing.T) {
126 env := map[string]string{"CARLOS_MAIL": "submit",
127 "CARLOS_CONTROL_SOCKET": socket, "CARLOS_ADMIN_TOKEN": "token",
128 "CARLOS_MAIL_FROM": "noreply@calendar.oficina.works",
129 "CALENDAR_MAIL_FROM": "reminders@calendar.oficina.works",
130 "CARLOS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com",
131 "CARLOS_SMTP_FROM": "old@calendar.oficina.works"}
132 delete(env, missing)
133 _, err := mailerWith(t, env)
134 if err == nil || !strings.Contains(err.Error(), missing) {
135 t.Fatalf("submission with no %s: %v", missing, err)
136 }
137 })
138 }
139}
140