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