oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "context"
5 "encoding/json"
6 "io"
7 "log/slog"
8 "net"
9 "net/http"
10 "os"
11 "path/filepath"
12 "testing"
13 "time"
14
15 "amadan.net/rastrillo/rastrillo/mail"
16)
17
18// mailEnv is every variable the boot's mail choice reads, so each case
19// starts from none of them whatever the test process inherited.
20var mailEnv = []string{"CARLOS_MAIL", "CARLOS_CONTROL_SOCKET",
21 "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM", "CARLOS_SMTP_HOST",
22 "CARLOS_SMTP_PORT", "CARLOS_SMTP_FROM", "CARLOS_SMTP_USER",
23 "CARLOS_SMTP_PASS", "EQUIPO_ALLOW_LOGGED_MAIL"}
24
25// bootMail is main's mail path: LoadConfig, then Mailer, against the
26// process environment, which is what mail.FromCarlos reads. The origin
27// is public unless env names another.
28func bootMail(t *testing.T, env map[string]string) (mail.Sender, error) {
29 t.Helper()
30 for _, key := range mailEnv {
31 t.Setenv(key, "")
32 }
33 t.Setenv("EQUIPO_ORIGIN", "https://equipo.example.com")
34 t.Setenv("EQUIPO_INSTANCE_KEY", "test-instance-key")
35 for key, value := range env {
36 t.Setenv(key, value)
37 }
38 cfg, err := LoadConfig(os.Getenv)
39 if err != nil {
40 return nil, err
41 }
42 return Mailer(cfg, slog.New(slog.DiscardHandler))
43}
44
45type mailCall struct {
46 method, path, auth string
47 body struct {
48 From string `json:"from"`
49 To []string `json:"to"`
50 Raw string `json:"raw"`
51 }
52}
53
54// mailVerb is the agent's control socket, answering POST /v1/mail.
55func mailVerb(t *testing.T) (string, chan mailCall) {
56 t.Helper()
57 // A short directory: a unix socket path has a 104-byte limit.
58 dir, err := os.MkdirTemp("/tmp", "equipo-mail")
59 if err != nil {
60 t.Fatal(err)
61 }
62 t.Cleanup(func() { os.RemoveAll(dir) })
63 socket := filepath.Join(dir, "control.sock")
64 listener, err := net.Listen("unix", socket)
65 if err != nil {
66 t.Fatal(err)
67 }
68 calls := make(chan mailCall, 8)
69 server := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
70 call := mailCall{method: r.Method, path: r.URL.Path, auth: r.Header.Get("Authorization")}
71 raw, _ := io.ReadAll(r.Body)
72 _ = json.Unmarshal(raw, &call.body)
73 calls <- call
74 w.Write([]byte(`{"message_id":"m-1"}`))
75 })}
76 go server.Serve(listener)
77 t.Cleanup(func() { server.Close() })
78 return socket, calls
79}
80
81func TestMailBootsOnSubmissionAndSendsThroughTheSocket(t *testing.T) {
82 socket, calls := mailVerb(t)
83 sender, err := bootMail(t, map[string]string{
84 "CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket,
85 "CARLOS_ADMIN_TOKEN": "admin-token",
86 "CARLOS_MAIL_FROM": "noreply@equipo.example.com"})
87 if err != nil {
88 t.Fatal(err)
89 }
90 if !mail.IsConfigured(sender) {
91 t.Fatal("submission booted a sender that only logs")
92 }
93 if err := sender.Send(context.Background(), "ana@example.com", "Sign in", "link"); err != nil {
94 t.Fatal(err)
95 }
96 var call mailCall
97 select {
98 case call = <-calls:
99 case <-time.After(5 * time.Second):
100 t.Fatal("no request reached the control socket")
101 }
102 if call.method+" "+call.path != "POST /v1/mail" {
103 t.Fatalf("the sign-in went to %s %s, not the mail verb", call.method, call.path)
104 }
105 if call.auth != "Bearer admin-token" {
106 t.Fatalf("Authorization %q", call.auth)
107 }
108 if call.body.From != "noreply@equipo.example.com" || len(call.body.To) != 1 || call.body.To[0] != "ana@example.com" || call.body.Raw == "" {
109 t.Fatalf("submission body %+v", call.body)
110 }
111}
112
113func TestMailBootsOnSMTPAlone(t *testing.T) {
114 sender, err := bootMail(t, map[string]string{
115 "CARLOS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com",
116 "CARLOS_SMTP_FROM": "noreply@equipo.example.com"})
117 if err != nil {
118 t.Fatal(err)
119 }
120 if !mail.IsConfigured(sender) {
121 t.Fatal("SMTP is configured but the sender only logs")
122 }
123}
124
125// CARLOS_MAIL set to anything but "submit" is not submission: Equipo boots
126// on SMTP, as mail.FromCarlos sends over it.
127func TestMailBootsOnSMTPWhenCarlosMailIsNotSubmit(t *testing.T) {
128 sender, err := bootMail(t, map[string]string{"CARLOS_MAIL": "smtp",
129 "CARLOS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com",
130 "CARLOS_SMTP_FROM": "noreply@equipo.example.com"})
131 if err != nil {
132 t.Fatal(err)
133 }
134 if !mail.IsConfigured(sender) {
135 t.Fatal("SMTP is configured but the sender only logs")
136 }
137}
138
139func TestMailRefusesWithNeitherChannel(t *testing.T) {
140 for name, env := range map[string]map[string]string{
141 "nothing": nil,
142 "SMTP host only": {"CARLOS_SMTP_HOST": "smtp.example.com"},
143 "CARLOS_MAIL not submit": {"CARLOS_MAIL": "smtp", "CARLOS_MAIL_FROM": "noreply@equipo.example.com"},
144 "a From with no switch on": {"CARLOS_MAIL_FROM": "noreply@equipo.example.com"},
145 } {
146 t.Run(name, func(t *testing.T) {
147 _, err := bootMail(t, env)
148 if err == nil || err.Error() != copyMissingSMTP {
149 t.Fatalf("booted with no mail: %v", err)
150 }
151 })
152 }
153}
154
155func TestMailRefusesHalfASubmission(t *testing.T) {
156 socket, _ := mailVerb(t)
157 for name, env := range map[string]map[string]string{
158 "no socket": {"CARLOS_ADMIN_TOKEN": "t", "CARLOS_MAIL_FROM": "noreply@equipo.example.com"},
159 "no token": {"CARLOS_CONTROL_SOCKET": socket, "CARLOS_MAIL_FROM": "noreply@equipo.example.com"},
160 "no From": {"CARLOS_CONTROL_SOCKET": socket, "CARLOS_ADMIN_TOKEN": "t"},
161 } {
162 t.Run(name, func(t *testing.T) {
163 env["CARLOS_MAIL"] = "submit"
164 // SMTP beside it does not rescue a broken submission.
165 env["CARLOS_SMTP_HOST"] = "smtp.example.com"
166 env["CARLOS_SMTP_FROM"] = "noreply@equipo.example.com"
167 if _, err := bootMail(t, env); err == nil {
168 t.Fatal("booted with half a submission")
169 }
170 })
171 }
172}
173
174// EQUIPO_ALLOW_LOGGED_MAIL keeps its rules: loopback (or staging) only, and
175// where admitted every message is logged, whatever channel is set.
176func TestMailLoggedAllowanceUnchanged(t *testing.T) {
177 socket, _ := mailVerb(t)
178 for name, env := range map[string]map[string]string{
179 "no channel": {},
180 "SMTP": {"CARLOS_SMTP_HOST": "smtp.example.com", "CARLOS_SMTP_FROM": "noreply@equipo.example.com"},
181 "submission": {"CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket, "CARLOS_ADMIN_TOKEN": "t", "CARLOS_MAIL_FROM": "noreply@equipo.example.com"},
182 } {
183 t.Run(name, func(t *testing.T) {
184 env["EQUIPO_ORIGIN"] = "http://127.0.0.1:8080"
185 env["EQUIPO_ALLOW_LOGGED_MAIL"] = "1"
186 sender, err := bootMail(t, env)
187 if err != nil {
188 t.Fatal(err)
189 }
190 if mail.IsConfigured(sender) {
191 t.Fatal("logged mail built a real sender")
192 }
193 })
194 }
195 _, err := bootMail(t, map[string]string{"EQUIPO_ALLOW_LOGGED_MAIL": "1",
196 "CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket, "CARLOS_ADMIN_TOKEN": "t", "CARLOS_MAIL_FROM": "noreply@equipo.example.com"})
197 if err == nil || err.Error() != copyLoggedMailPublic {
198 t.Fatalf("logged mail on a public origin: %v", err)
199 }
200}
201