Settings → Email → Mailing lists connects Listmonk (the Core's, by default) with an API user and its token; Test reads the lists. A person in Review can be added to a list, with their company, title and signal: a double opt-in list asks them to confirm unless they already agreed; a single opt-in list takes them only once they have; a suppressed address is refused. Their card shows the lists they're on (a new list_members table). Every hour, or on Sync now, everyone blocklisted in Listmonk or unsubscribed from one of its lists goes on the suppression list, with the reason; an address already there keeps its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
5.7 KiB
Go
125 lines
5.7 KiB
Go
package web
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"supermarket/internal/prospects"
|
|
"supermarket/internal/secrets"
|
|
)
|
|
|
|
func TestMailingLists(t *testing.T) {
|
|
var added []map[string]any
|
|
lm := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.URL.Path == "/api/lists":
|
|
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"results": []map[string]any{
|
|
{"id": 3, "name": "Newsletter", "optin": "double"}, {"id": 4, "name": "Announcements", "optin": "single"}}}})
|
|
case r.URL.Path == "/api/subscribers" && r.Method == http.MethodPost:
|
|
var b map[string]any
|
|
json.NewDecoder(r.Body).Decode(&b)
|
|
added = append(added, b)
|
|
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"id": 9}})
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer lm.Close()
|
|
s, h := newTestServer(t, nil)
|
|
now := time.Now()
|
|
ann, _, _ := prospects.AddContact(s.DB, prospects.Contact{FirstName: "Ann", LastName: "Lee", Email: "ann@lee.example", Title: "IT lead", Source: prospects.SourceManual}, now)
|
|
bob, _, _ := prospects.AddContact(s.DB, prospects.Contact{FirstName: "Bob", LastName: "Ray", Email: "bob@nope.example", Source: prospects.SourceManual}, now)
|
|
prospects.Suppress(s.DB, "domain", "nope.example", "", now)
|
|
|
|
if body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String(); strings.Contains(body, "Add to a mailing list") {
|
|
t.Error("offered before Listmonk is set up")
|
|
}
|
|
postForm(h, "/settings/email/listmonk", url.Values{"listmonk_url": {lm.URL}, "listmonk_user": {"sm"}, secrets.ListmonkToken: {"tok"}})
|
|
if body := postForm(h, "/settings/email/listmonk/test", nil).Body.String(); !strings.Contains(body, "It works: 2 lists") {
|
|
t.Errorf("test: %s", body)
|
|
}
|
|
body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String()
|
|
if !strings.Contains(body, "Add to a mailing list") || !strings.Contains(body, "Newsletter (double opt-in)") {
|
|
t.Fatalf("review: no list form")
|
|
}
|
|
|
|
add := func(id int64, list string, agreed bool) *httptest.ResponseRecorder {
|
|
v := url.Values{"list": {list}, "tab": {"new"}}
|
|
if agreed {
|
|
v.Set("agreed", "on")
|
|
}
|
|
return postForm(h, "/prospects/contact/"+strconv.FormatInt(id, 10)+"/list", v)
|
|
}
|
|
if rec := add(ann, "4", false); !strings.Contains(rec.Body.String(), "sends its mail without asking first") || len(added) != 0 {
|
|
t.Errorf("single opt-in without agreement: %d %v", rec.Code, added)
|
|
}
|
|
if rec := add(bob, "3", false); !strings.Contains(rec.Body.String(), "suppression list") || len(added) != 0 {
|
|
t.Errorf("suppressed: %d", rec.Code)
|
|
}
|
|
if rec := add(ann, "3", false); rec.Code != http.StatusSeeOther || len(added) != 1 {
|
|
t.Fatalf("add: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
if a := added[0]; a["email"] != "ann@lee.example" || a["preconfirm_subscriptions"] != false || a["attribs"].(map[string]any)["title"] != "IT lead" {
|
|
t.Errorf("sent: %v", a)
|
|
}
|
|
if body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String(); !strings.Contains(body, "<strong>Newsletter</strong> (asked to confirm)") {
|
|
t.Error("the card doesn't say she's on the list")
|
|
}
|
|
if rec := add(ann, "4", true); rec.Code != http.StatusSeeOther || added[1]["preconfirm_subscriptions"] != true {
|
|
t.Errorf("agreed: %d %v", rec.Code, added)
|
|
}
|
|
}
|
|
|
|
func TestOptOutSync(t *testing.T) {
|
|
lm := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
switch {
|
|
case r.URL.Path == "/api/lists":
|
|
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"results": []map[string]any{{"id": 3, "name": "Newsletter", "optin": "double"}}}})
|
|
case q.Get("query") != "":
|
|
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"results": []map[string]any{{"email": "spam@x.example"}, {"email": "kept@x.example"}}, "total": 2}})
|
|
case q.Get("list_id") == "3":
|
|
json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"results": []map[string]any{{"email": "ann@lee.example"}}, "total": 1}})
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer lm.Close()
|
|
s, h := newTestServer(t, nil)
|
|
now := time.Now()
|
|
ann, _, _ := prospects.AddContact(s.DB, prospects.Contact{FirstName: "Ann", LastName: "Lee", Email: "ann@lee.example", Source: prospects.SourceManual}, now)
|
|
prospects.AddedToList(s.DB, ann, 3, "Newsletter", false, now)
|
|
prospects.Suppress(s.DB, "email", "kept@x.example", "My own reason", now)
|
|
postForm(h, "/settings/email/listmonk", url.Values{"listmonk_url": {lm.URL}, "listmonk_user": {"sm"}, secrets.ListmonkToken: {"tok"}})
|
|
|
|
rec := postForm(h, "/settings/email/listmonk/sync", nil)
|
|
if !strings.Contains(rec.Header().Get("Location"), "msg=optouts-synced") || !strings.Contains(rec.Header().Get("Location"), "n1=2") {
|
|
t.Fatalf("sync: %d %s", rec.Code, rec.Header().Get("Location"))
|
|
}
|
|
list, _ := prospects.Suppressions(s.DB)
|
|
reasons := map[string]string{}
|
|
for _, e := range list {
|
|
reasons[e.Value] = e.Reason
|
|
}
|
|
if reasons["spam@x.example"] != "Blocklisted in Listmonk" || reasons["ann@lee.example"] != "Unsubscribed from “Newsletter” in Listmonk" || reasons["kept@x.example"] != "My own reason" {
|
|
t.Errorf("reasons: %v", reasons)
|
|
}
|
|
if on, _ := prospects.ListsOf(s.DB); len(on[ann]) != 0 {
|
|
t.Errorf("Ann still shows on the list: %+v", on[ann])
|
|
}
|
|
if body := do(h, http.MethodGet, "/settings/email", nil, nil).Body.String(); !strings.Contains(body, "3 people don't want mail; 2 new on the suppression list") {
|
|
i := strings.Index(body, "Last synced")
|
|
t.Errorf("the card doesn't show the sync: %s", body[max(0, i):min(len(body), max(0, i)+300)])
|
|
}
|
|
// Again: nothing new.
|
|
if rec := postForm(h, "/settings/email/listmonk/sync", nil); !strings.Contains(rec.Header().Get("Location"), "n1=0") {
|
|
t.Errorf("again: %s", rec.Header().Get("Location"))
|
|
}
|
|
}
|