package http import ( "context" "encoding/json" "io" "net" "net/http" "net/http/cookiejar" "regexp" "strings" "testing" "time" "gitea.federated.computer/Federated_Computer/brain/internal/notify" ) var unsubTokenRx = regexp.MustCompile(`/unsubscribe\?token=([0-9a-f.]+)`) // The unsubscribe surface end to end: footer links + RFC 8058 headers on // owner-addressed mail, the token-credentialed one-click endpoint (no // session), the exact blast radius (email channels + self-addressed agent // email tasks, nothing else), idempotence, and the third-party/transactional // exclusions. func TestUnsubscribeFlow(t *testing.T) { srv, d := testServerDB(t) smtp := newFakeSMTP(t) host, port, _ := net.SplitHostPort(smtp.ln.Addr().String()) t.Setenv("SMTP_HOST", host) t.Setenv("SMTP_PORT", port) t.Setenv("SMTP_FROM", "superbrain@superbrain.test") t.Setenv("NEXTAUTH_URL", "https://superbrain.test") jar, _ := cookiejar.New(nil) alice := apiClient{t, &http.Client{Jar: jar}, srv.URL} alice.request("POST", "/api/signup", map[string]any{"email": "alice@superbrain.test", "password": "a-long-test-password"}, 201) alice.request("POST", "/api/auth/login", map[string]any{"email": "alice@superbrain.test", "password": "a-long-test-password"}, 200) aliceID := meID(alice) hook := newFakeWebhook(t) selfCh := alice.request("POST", "/api/notifications/channels", map[string]any{ "kind": "email", "name": "My inbox", "config": map[string]any{"address": ""}, }, 201) thirdCh := alice.request("POST", "/api/notifications/channels", map[string]any{ "kind": "email", "name": "Bob's inbox", "config": map[string]any{"address": "bob@superbrain.test"}, }, 201) slackCh := alice.request("POST", "/api/notifications/channels", map[string]any{ "kind": "slack", "name": "Team", "config": map[string]any{"webhookUrl": hook.url()}, }, 201) // Signal-condition email tasks need no AI. One addresses the owner, // one a third party: unsubscribe must pause only the former. selfTask := alice.request("POST", "/api/agent/tasks", map[string]any{"name": "Self mail", "spec": map[string]any{ "condition": map[string]any{"type": "signal"}, "action": map[string]any{"type": "email", "to": "alice@superbrain.test"}, }}, 201) thirdTask := alice.request("POST", "/api/agent/tasks", map[string]any{"name": "Bob mail", "spec": map[string]any{ "condition": map[string]any{"type": "signal"}, "action": map[string]any{"type": "email", "to": "bob@superbrain.test"}, }}, 201) // --- owner-addressed delivery carries footer links + one-click headers --- due := time.Now().UTC().Add(6 * time.Hour).Format(time.RFC3339) alice.request("POST", "/api/bookmarks", map[string]any{ "url": "https://example.org/unsub-due", "title": "Unsub due", "dueDate": due, }, 201) alice.request("POST", "/api/notifications/rules", map[string]any{ "name": "Due reminders", "trigger": "bookmark_due", "channelId": selfCh["id"], "config": map[string]any{"leadHours": 24}, "cooldownMinutes": 60, }, 201) if e := notify.RunOnce(context.Background(), d); e != nil { t.Fatal(e) } msg := smtp.waitForMessage(t, 1) if msg[0] != "alice@superbrain.test" { t.Fatalf("envelope recipient = %q", msg[0]) } for _, want := range []string{ "List-Unsubscribe: