| 1 | package fichas |
| 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 | // loopback is a local instance that asked for logged mail. |
| 41 | func loopback() map[string]string { |
| 42 | return map[string]string{ |
| 43 | "FICHAS_DEK": goodDEK(), "FICHAS_INSTANCE_KEY": "k", |
| 44 | "FICHAS_OWNERS": "ana@example.com", |
| 45 | "FICHAS_ORIGIN": "http://127.0.0.1:8824", |
| 46 | "FICHAS_ALLOW_LOGGED_MAIL": "1", |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // public is a deployed instance: no logged mail, a public origin. |
| 51 | func public(env map[string]string) map[string]string { |
| 52 | full := loopback() |
| 53 | full["FICHAS_ALLOW_LOGGED_MAIL"] = "" |
| 54 | full["FICHAS_ORIGIN"] = "https://paulca.fichas.oficina.works" |
| 55 | for key, value := range env { |
| 56 | full[key] = value |
| 57 | } |
| 58 | return full |
| 59 | } |
| 60 | |
| 61 | // mailVerb is the agent's control socket, answering POST /v1/mail. |
| 62 | func mailVerb(t *testing.T) (string, chan string) { |
| 63 | t.Helper() |
| 64 | // A short directory: a unix socket path has a 104-byte limit. |
| 65 | dir, err := os.MkdirTemp("/tmp", "fichas-mail") |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | t.Cleanup(func() { os.RemoveAll(dir) }) |
| 70 | socket := filepath.Join(dir, "control.sock") |
| 71 | listener, err := net.Listen("unix", socket) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | paths := make(chan string, 1) |
| 76 | server := &http.Server{Handler: http.HandlerFunc( |
| 77 | func(writer http.ResponseWriter, request *http.Request) { |
| 78 | paths <- request.Method + " " + request.URL.Path |
| 79 | writer.Write([]byte(`{"message_id":"m-1"}`)) |
| 80 | })} |
| 81 | go server.Serve(listener) |
| 82 | t.Cleanup(func() { server.Close() }) |
| 83 | return socket, paths |
| 84 | } |
| 85 | |
| 86 | func TestBootsOnMailSubmission(t *testing.T) { |
| 87 | socket, paths := mailVerb(t) |
| 88 | sender, err := boot(t, public(map[string]string{ |
| 89 | "CARLOS_MAIL": "submit", "CARLOS_CONTROL_SOCKET": socket, |
| 90 | "CARLOS_ADMIN_TOKEN": "token", |
| 91 | "CARLOS_MAIL_FROM": "noreply@fichas.oficina.works"})) |
| 92 | if err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | err = sender.Send(context.Background(), "ana@example.com", |
| 96 | "Sign in", "link") |
| 97 | if err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | if got := <-paths; got != "POST /v1/mail" { |
| 101 | t.Fatalf("the sign-in went to %s, not the mail verb", got) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestBootsOnSMTPAlone(t *testing.T) { |
| 106 | sender, err := boot(t, public(map[string]string{ |
| 107 | "CARLOS_SMTP_HOST": "email-smtp.eu-west-1.amazonaws.com", |
| 108 | "CARLOS_SMTP_FROM": "noreply@fichas.oficina.works"})) |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | if !mail.IsConfigured(sender) { |
| 113 | t.Fatal("SMTP is configured but the sender only logs") |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func TestRefusesWithNeitherChannel(t *testing.T) { |
| 118 | _, err := boot(t, public(nil)) |
| 119 | if !errors.Is(err, ErrMissing) { |
| 120 | t.Fatalf("booted with no mail: %v", err) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func TestRefusesHalfASubmission(t *testing.T) { |
| 125 | // CARLOS_MAIL=submit with no socket: mail.FromCarlos says why. |
| 126 | _, err := boot(t, public(map[string]string{"CARLOS_MAIL": "submit", |
| 127 | "CARLOS_MAIL_FROM": "noreply@fichas.oficina.works"})) |
| 128 | if err == nil { |
| 129 | t.Fatal("booted with submission and no control socket") |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func TestLoggedMailStaysLoopbackOnly(t *testing.T) { |
| 134 | sender, err := boot(t, loopback()) |
| 135 | if err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | if mail.IsConfigured(sender) { |
| 139 | t.Fatal("logged mail on loopback built a real sender") |
| 140 | } |
| 141 | } |
| 142 | |