oficina / meet Public

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

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 calls.App refuses
21// outside development (the mail case of resolve,
22// in internal/calls config.go).
23
24var mailVars = []string{"CARLOS_MAIL", "CARLOS_CONTROL_SOCKET",
25 "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM", "CALLS_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{"CALLS_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", "calls-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 (Calls
73// is declared CARLOS_SMTP), and nothing of Calls' own overrides it.
74func 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@calls.oficina.works",
80 // Not delivered by the platform, so not read.
81 "CALLS_MAIL_FROM": "calls@calls.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@calls.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@calls.oficina.works"; got != want {
92 t.Fatalf("got %q, want %q", got, want)
93 }
94}
95
96func 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@calls.oficina.works"})
100 if err != nil || sender == nil || !mail.IsConfigured(sender) {
101 t.Fatalf("sender %v, err %v", sender, err)
102 }
103}
104
105func 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 calls.App 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.
115func 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@calls.oficina.works",
123 "CALLS_MAIL_FROM": "calls@calls.oficina.works",
124 "CALLS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com",
125 "CALLS_SMTP_FROM": "old@calls.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