| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "log/slog" |
| 7 | "net" |
| 8 | "net/http" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "amadan.net/rastrillo/rastrillo/mail" |
| 15 | ) |
| 16 | |
| 17 | // The boots CARLOS platform mail submission needs (carlos plan Task 9): |
| 18 | // on submission with the agent's socket, on SMTP alone, and with |
| 19 | // neither, which refuses. |
| 20 | |
| 21 | // mailEnv is every variable the boot's mail choice reads, so each case |
| 22 | // starts from none of them whatever the test process inherited. |
| 23 | var mailEnv = []string{"CARLOS_MAIL", "CARLOS_CONTROL_SOCKET", |
| 24 | "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM", "CARLOS_SMTP_HOST", |
| 25 | "CARLOS_SMTP_PORT", "CARLOS_SMTP_FROM", "CARLOS_SMTP_USER", |
| 26 | "CARLOS_SMTP_PASS"} |
| 27 | |
| 28 | // boot is main's mail path: LoadConfig, then Mailer. LoadConfig reads |
| 29 | // the map; mail.FromCarlos reads the process environment, so the map |
| 30 | // is set there too. |
| 31 | func boot(t *testing.T, env map[string]string) (mail.Sender, error) { |
| 32 | t.Helper() |
| 33 | for _, key := range mailEnv { |
| 34 | t.Setenv(key, "") |
| 35 | } |
| 36 | for key, value := range env { |
| 37 | t.Setenv(key, value) |
| 38 | } |
| 39 | config, err := LoadConfig(func(key string) string { return env[key] }) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | return Mailer(config, slog.New(slog.DiscardHandler)) |
| 44 | } |
| 45 | |
| 46 | // public is a deployed instance: no logged mail, a public origin. |
| 47 | func public(env map[string]string) map[string]string { |
| 48 | full := baseEnv() |
| 49 | delete(full, "MEMORIA_ALLOW_LOGGED_MAIL") |
| 50 | full["MEMORIA_ORIGIN"] = "https://memoria.oficina.works" |
| 51 | for key, value := range env { |
| 52 | full[key] = value |
| 53 | } |
| 54 | return full |
| 55 | } |
| 56 | |
| 57 | // mailVerb is the agent's control socket, answering POST /v1/mail. |
| 58 | func mailVerb(t *testing.T) (string, chan string) { |
| 59 | t.Helper() |
| 60 | // A short directory: a unix socket path has a 104-byte limit. |
| 61 | dir, err := os.MkdirTemp("/tmp", "memoria-mail") |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | t.Cleanup(func() { os.RemoveAll(dir) }) |
| 66 | socket := filepath.Join(dir, "control.sock") |
| 67 | listener, err := net.Listen("unix", socket) |
| 68 | if err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | paths := make(chan string, 1) |
| 72 | server := &http.Server{Handler: http.HandlerFunc( |
| 73 | func(writer http.ResponseWriter, request *http.Request) { |
| 74 | paths <- request.Method + " " + request.URL.Path |
| 75 | writer.Write([]byte(`{"message_id":"m-1"}`)) |
| 76 | })} |
| 77 | go server.Serve(listener) |
| 78 | t.Cleanup(func() { server.Close() }) |
| 79 | return socket, paths |
| 80 | } |
| 81 | |
| 82 | func TestBootsOnMailSubmission(t *testing.T) { |
| 83 | socket, paths := mailVerb(t) |
| 84 | sender, err := boot(t, public(map[string]string{ |
| 85 | "CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket, |
| 86 | "CARLOS_ADMIN_TOKEN": "token", |
| 87 | "CARLOS_MAIL_FROM": "noreply@memoria.oficina.works"})) |
| 88 | if err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | err = sender.Send(context.Background(), "ana@example.com", |
| 92 | "Sign in", "link") |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | if got := <-paths; got != "POST /v1/mail" { |
| 97 | t.Fatalf("the sign-in went to %s, not the mail verb", got) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestBootsOnSMTPAlone(t *testing.T) { |
| 102 | sender, err := boot(t, public(map[string]string{ |
| 103 | "CARLOS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com", |
| 104 | "CARLOS_SMTP_FROM": "noreply@memoria.oficina.works"})) |
| 105 | if err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | if !mail.IsConfigured(sender) { |
| 109 | t.Fatal("SMTP is configured but the sender only logs") |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func TestRefusesWithNeitherChannel(t *testing.T) { |
| 114 | _, err := boot(t, public(nil)) |
| 115 | if !errors.Is(err, ErrMissing) { |
| 116 | t.Fatalf("booted with no mail: %v", err) |
| 117 | } |
| 118 | if !strings.Contains(err.Error(), "CARLOS_MAIL=submit") { |
| 119 | t.Errorf("refusal %q does not name submission", err) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // A submission missing anything the agent or platform delivers is a |
| 124 | // boot refusal, never SMTP or logged mail in its place: not even with |
| 125 | // MEMORIA_ALLOW_LOGGED_MAIL set. |
| 126 | func TestRefusesHalfASubmission(t *testing.T) { |
| 127 | socket, _ := mailVerb(t) |
| 128 | for _, missing := range []string{"CARLOS_CONTROL_SOCKET", |
| 129 | "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM"} { |
| 130 | t.Run(missing, func(t *testing.T) { |
| 131 | env := public(map[string]string{"CARLOS_MAIL": "submit", |
| 132 | "CARLOS_CONTROL_SOCKET": socket, "CARLOS_ADMIN_TOKEN": "token", |
| 133 | "CARLOS_MAIL_FROM": "noreply@memoria.oficina.works", |
| 134 | "MEMORIA_ALLOW_LOGGED_MAIL": "1"}) |
| 135 | delete(env, missing) |
| 136 | _, err := boot(t, env) |
| 137 | if err == nil || !strings.Contains(err.Error(), missing) { |
| 138 | t.Fatalf("booted on submission with no %s: %v", missing, err) |
| 139 | } |
| 140 | }) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Logged mail stays what it was: only with MEMORIA_ALLOW_LOGGED_MAIL |
| 145 | // and no channel at all. |
| 146 | func TestLoggedMailOnlyWhenAskedAndNoChannel(t *testing.T) { |
| 147 | sender, err := boot(t, baseEnv()) |
| 148 | if err != nil { |
| 149 | t.Fatal(err) |
| 150 | } |
| 151 | if mail.IsConfigured(sender) { |
| 152 | t.Fatal("logged mail with no channel built a real sender") |
| 153 | } |
| 154 | } |
| 155 | |