oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "context"
5 "log/slog"
6 "net"
7 "net/http"
8 "os"
9 "path/filepath"
10 "testing"
11
12 "amadan.net/rastrillo/rastrillo/mail"
13)
14
15// The three boots CARLOS platform mail submission needs (carlos plan
16// Task 9): on submission with the agent's socket, on SMTP alone, and
17// with neither, which leaves Mailer nil for docs.New to refuse outside
18// development (TestNoMailRefusesOutsideDevelopment, internal/docs).
19
20// mailVars is every variable platformMailer and mail.FromCarlos read,
21// so each case starts from none of them.
22var mailVars = []string{"CARLOS_MAIL", "CARLOS_CONTROL_SOCKET",
23 "CARLOS_ADMIN_TOKEN", "DOCS_MAIL_FROM", "DOCS_SMTP_HOST",
24 "DOCS_SMTP_PORT", "DOCS_SMTP_FROM", "DOCS_SMTP_USER", "DOCS_SMTP_PASS"}
25
26func mailerWith(t *testing.T, env map[string]string) (mail.Sender, error) {
27 t.Helper()
28 for _, k := range mailVars {
29 t.Setenv(k, "")
30 }
31 for k, v := range env {
32 t.Setenv(k, v)
33 }
34 return platformMailer(slog.New(slog.DiscardHandler))
35}
36
37// controlSocket stands in for the agent's socket: it answers POST
38// /v1/mail and reports each request's method and path.
39func controlSocket(t *testing.T) (string, chan string) {
40 t.Helper()
41 dir, err := os.MkdirTemp("/tmp", "docs-mail") // unix paths are short
42 if err != nil {
43 t.Fatal(err)
44 }
45 t.Cleanup(func() { os.RemoveAll(dir) })
46 socket := filepath.Join(dir, "control.sock")
47 ln, err := net.Listen("unix", socket)
48 if err != nil {
49 t.Fatal(err)
50 }
51 seen := make(chan string, 1)
52 srv := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
53 seen <- r.Method + " " + r.URL.Path
54 w.Write([]byte(`{"message_id":"m-1"}`))
55 })}
56 go srv.Serve(ln)
57 t.Cleanup(func() { srv.Close() })
58 return socket, seen
59}
60
61func TestPlatformMailerSubmits(t *testing.T) {
62 socket, seen := controlSocket(t)
63 sender, err := mailerWith(t, map[string]string{
64 "CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket,
65 "CARLOS_ADMIN_TOKEN": "token", "DOCS_MAIL_FROM": "noreply@docs.oficina.works"})
66 if err != nil || sender == nil {
67 t.Fatalf("sender %v, err %v", sender, err)
68 }
69 if err := sender.Send(context.Background(), "ana@example.com", "Sign in", "link"); err != nil {
70 t.Fatal(err)
71 }
72 if got := <-seen; got != "POST /v1/mail" {
73 t.Fatalf("the link went to %s, not the platform's mail verb", got)
74 }
75}
76
77func TestPlatformMailerUsesSMTPAlone(t *testing.T) {
78 sender, err := mailerWith(t, map[string]string{
79 "DOCS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com",
80 "DOCS_SMTP_FROM": "noreply@docs.oficina.works"})
81 if err != nil || sender == nil || !mail.IsConfigured(sender) {
82 t.Fatalf("sender %v, err %v", sender, err)
83 }
84}
85
86func TestPlatformMailerWithNeitherLeavesItToNew(t *testing.T) {
87 sender, err := mailerWith(t, nil)
88 if err != nil || sender != nil {
89 t.Fatalf("sender %v, err %v: with no channel docs.New must decide", sender, err)
90 }
91}
92
93func TestPlatformMailerRefusesHalfASubmission(t *testing.T) {
94 // No socket: only the agent sets CARLOS_MAIL, and it sets both.
95 if _, err := mailerWith(t, map[string]string{"CARLOS_MAIL": "submit",
96 "DOCS_MAIL_FROM": "noreply@docs.oficina.works"}); err == nil {
97 t.Fatal("built a submit sender with no control socket")
98 }
99 // The From is DOCS_MAIL_FROM, not CARLOS_MAIL_FROM: the platform
100 // names it from Docs' DOCS_SMTP declaration.
101 socket, _ := controlSocket(t)
102 if _, err := mailerWith(t, map[string]string{"CARLOS_MAIL": "submit",
103 "CARLOS_CONTROL_SOCKET": socket, "CARLOS_ADMIN_TOKEN": "token",
104 "CARLOS_MAIL_FROM": "noreply@docs.oficina.works"}); err == nil {
105 t.Fatal("built a submit sender with no DOCS_MAIL_FROM")
106 }
107}
108