oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "context"
5 "net"
6 "net/http"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "amadan.net/rastrillo/rastrillo/mail"
13)
14
15// The boots CARLOS platform mail submission needs (carlos plan Task 9):
16// on submission with the agent's socket, on SMTP alone, and with
17// neither, which refuses on a public origin.
18
19// switchedEnv is a switched instance: the five CARLOS_SMTP_* have left
20// the bundle, and the agent has set CARLOS_MAIL=submit beside its
21// socket, with the platform's CARLOS_MAIL_FROM.
22func switchedEnv(socket string) map[string]string {
23 env := goodEnv()
24 delete(env, "CARLOS_SMTP_HOST")
25 delete(env, "CARLOS_SMTP_FROM")
26 env["CARLOS_MAIL"] = "submit"
27 env["CARLOS_CONTROL_SOCKET"] = socket
28 env["CARLOS_ADMIN_TOKEN"] = "token"
29 env["CARLOS_MAIL_FROM"] = "noreply@sheets.example.com"
30 return env
31}
32
33// mailVerb stands in for the agent's control socket, answering POST
34// /v1/mail and reporting each request's method and path.
35func mailVerb(t *testing.T) (string, chan string) {
36 t.Helper()
37 // A short directory: a unix socket path has a 104-byte limit.
38 dir, err := os.MkdirTemp("/tmp", "sheets-mail")
39 if err != nil {
40 t.Fatal(err)
41 }
42 t.Cleanup(func() { os.RemoveAll(dir) })
43 socket := filepath.Join(dir, "control.sock")
44 listener, err := net.Listen("unix", socket)
45 if err != nil {
46 t.Fatal(err)
47 }
48 paths := make(chan string, 1)
49 server := &http.Server{Handler: http.HandlerFunc(
50 func(w http.ResponseWriter, r *http.Request) {
51 paths <- r.Method + " " + r.URL.Path
52 w.Write([]byte(`{"message_id":"m-1"}`))
53 })}
54 go server.Serve(listener)
55 t.Cleanup(func() { server.Close() })
56 return socket, paths
57}
58
59func TestLoadAuthEnvBootsOnMailSubmission(t *testing.T) {
60 socket, paths := mailVerb(t)
61 setEnv(t, switchedEnv(socket))
62 env, err := LoadAuthEnv(quietLogger())
63 if err != nil {
64 t.Fatalf("a switched Sheets refused to boot: %v", err)
65 }
66 if err := env.Mailer.Send(context.Background(), "ana@example.com",
67 "Sign in", "link"); err != nil {
68 t.Fatal(err)
69 }
70 if got := <-paths; got != "POST /v1/mail" {
71 t.Fatalf("the sign-in went to %s, not the mail verb", got)
72 }
73}
74
75func TestLoadAuthEnvBootsOnSMTPAlone(t *testing.T) {
76 setEnv(t, goodEnv())
77 env, err := LoadAuthEnv(quietLogger())
78 if err != nil {
79 t.Fatal(err)
80 }
81 if !mail.IsConfigured(env.Mailer) {
82 t.Fatal("SMTP is configured but the sender only logs")
83 }
84}
85
86func TestLoadAuthEnvRefusesWithNeitherChannel(t *testing.T) {
87 env := goodEnv()
88 delete(env, "CARLOS_SMTP_HOST")
89 delete(env, "CARLOS_SMTP_FROM")
90 setEnv(t, env)
91 _, err := LoadAuthEnv(quietLogger())
92 if err == nil {
93 t.Fatal("booted on a public origin with no mail channel")
94 }
95 for _, want := range []string{"CARLOS_MAIL=submit", "CARLOS_SMTP_HOST"} {
96 if !strings.Contains(err.Error(), want) {
97 t.Errorf("refusal %q does not name %s", err, want)
98 }
99 }
100}
101
102// A submission missing anything the agent delivers refuses at boot,
103// even on loopback: it is never logged mail in its place.
104func TestLoadAuthEnvRefusesHalfASubmission(t *testing.T) {
105 socket, _ := mailVerb(t)
106 for _, missing := range []string{"CARLOS_CONTROL_SOCKET",
107 "CARLOS_ADMIN_TOKEN", "CARLOS_MAIL_FROM"} {
108 for _, origin := range []string{"https://sheets.example.com",
109 "http://localhost:8080"} {
110 t.Run(missing+" "+origin, func(t *testing.T) {
111 env := switchedEnv(socket)
112 env["SHEETS_ORIGIN"] = origin
113 delete(env, missing)
114 setEnv(t, env)
115 _, err := LoadAuthEnv(quietLogger())
116 if err == nil {
117 t.Fatalf("booted on submission with no %s", missing)
118 }
119 if !strings.Contains(err.Error(), missing) {
120 t.Errorf("refusal %q does not name %s", err, missing)
121 }
122 })
123 }
124 }
125}
126