Notify direct parent authors throughout threaded conversations while skipping self-replies, edits, and recipients without email.
This commit is contained in:
@@ -7,7 +7,9 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"plumber/internal/mail"
|
||||
"plumber/internal/pacific"
|
||||
"plumber/internal/store"
|
||||
)
|
||||
@@ -258,6 +260,127 @@ func TestEditPostRoutePermissions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostReplyNotifications(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
recording := &mail.Recording{}
|
||||
srv, mem := newTestServer(t, Config{Mail: recording})
|
||||
handler := srv.Handler()
|
||||
homeowner := seedUser(t, mem, uniq("homeowner"), "hunter22", store.RoleUser)
|
||||
admin := seedUser(t, mem, uniq("admin"), "hunter22", store.RoleAdmin)
|
||||
homeownerCookies := loginUser(t, handler, homeowner.Username, "hunter22")
|
||||
adminCookies := loginUser(t, handler, admin.Username, "hunter22")
|
||||
homeownerCSRF := csrfForCookies(t, handler, homeownerCookies)
|
||||
adminCSRF := csrfForCookies(t, handler, adminCookies)
|
||||
|
||||
rec := postForm(handler, "/posts", url.Values{
|
||||
"_csrf": {homeownerCSRF},
|
||||
"title": {"Leaky sink"},
|
||||
"body": {"Water under the cabinet."},
|
||||
}, homeownerCookies)
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("root create status = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if recording.Len() != 0 {
|
||||
t.Fatalf("root create sent %d notifications", recording.Len())
|
||||
}
|
||||
roots, err := mem.ListRootPosts(context.Background(), pacific.Today(), homeowner.ID)
|
||||
if err != nil || len(roots) != 1 {
|
||||
t.Fatalf("created roots = %+v, %v", roots, err)
|
||||
}
|
||||
root := roots[0]
|
||||
|
||||
rec = postForm(handler, "/posts", url.Values{
|
||||
"_csrf": {adminCSRF},
|
||||
"parent_id": {root.ID},
|
||||
"body": {"Replace the cartridge."},
|
||||
}, adminCookies)
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("admin reply status = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
thread, err := mem.GetPostThread(context.Background(), root.ID)
|
||||
if err != nil || len(thread.Replies) != 1 {
|
||||
t.Fatalf("admin reply thread = %+v, %v", thread, err)
|
||||
}
|
||||
adminReply := thread.Replies[0]
|
||||
msgs := waitForMail(t, recording, 1)
|
||||
if msg := msgs[0]; msg.ToEmail != homeowner.Email ||
|
||||
msg.RootID != root.ID ||
|
||||
msg.RootTitle != root.Title ||
|
||||
msg.ReplyID != adminReply.ID ||
|
||||
msg.ReplyBody != adminReply.Body ||
|
||||
msg.ReplyAuthorName != admin.Name {
|
||||
t.Fatalf("admin reply notification = %+v", msg)
|
||||
}
|
||||
|
||||
rec = postForm(handler, "/posts", url.Values{
|
||||
"_csrf": {homeownerCSRF},
|
||||
"parent_id": {adminReply.ID},
|
||||
"body": {"That fixed the drip."},
|
||||
}, homeownerCookies)
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("homeowner reply status = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
thread, err = mem.GetPostThread(context.Background(), root.ID)
|
||||
if err != nil || len(thread.Replies[0].Replies) != 1 {
|
||||
t.Fatalf("homeowner nested reply thread = %+v, %v", thread, err)
|
||||
}
|
||||
homeownerReply := thread.Replies[0].Replies[0]
|
||||
msgs = waitForMail(t, recording, 2)
|
||||
if msg := msgs[1]; msg.ToEmail != admin.Email ||
|
||||
msg.RootID != root.ID ||
|
||||
msg.ReplyID != homeownerReply.ID ||
|
||||
msg.ReplyAuthorName != homeowner.Name {
|
||||
t.Fatalf("homeowner reply notification = %+v", msg)
|
||||
}
|
||||
|
||||
rec = postForm(handler, "/posts", url.Values{
|
||||
"_csrf": {homeownerCSRF},
|
||||
"parent_id": {root.ID},
|
||||
"body": {"A note to myself."},
|
||||
}, homeownerCookies)
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("self reply status = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
rec = postForm(handler, "/posts/"+adminReply.ID+"/edit", url.Values{
|
||||
"_csrf": {adminCSRF},
|
||||
"body": {"Replace the ceramic cartridge."},
|
||||
}, adminCookies)
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("edit status = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
noEmail := &store.User{
|
||||
Username: uniq("no-email"),
|
||||
PasswordHash: homeowner.PasswordHash,
|
||||
Role: store.RoleUser,
|
||||
}
|
||||
if err := mem.CreateUser(context.Background(), noEmail); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
noEmailRoot := &store.Post{
|
||||
AuthorID: noEmail.ID,
|
||||
Title: "Quiet thread",
|
||||
Body: "No email configured.",
|
||||
PostDate: pacific.Today(),
|
||||
}
|
||||
if err := mem.CreatePost(context.Background(), noEmailRoot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec = postForm(handler, "/posts", url.Values{
|
||||
"_csrf": {adminCSRF},
|
||||
"parent_id": {noEmailRoot.ID},
|
||||
"body": {"This should not send."},
|
||||
}, adminCookies)
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("no-email reply status = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
if recording.Len() != 2 {
|
||||
t.Fatalf("self, edit, or no-email action sent a notification: %+v", recording.Snapshot())
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuestionPageRendersNestedPostControls(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -345,6 +468,19 @@ func TestQuestionPageRendersNestedPostControls(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func waitForMail(t *testing.T, recording *mail.Recording, want int) []mail.PostReply {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if recording.Len() >= want {
|
||||
return recording.Snapshot()
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("recorded %d notifications, want %d", recording.Len(), want)
|
||||
return nil
|
||||
}
|
||||
|
||||
func csrfForCookies(t *testing.T, handler http.Handler, cookies []*http.Cookie) string {
|
||||
t.Helper()
|
||||
req := httptest.NewRequest(http.MethodGet, "/submit", nil)
|
||||
|
||||
Reference in New Issue
Block a user