Bei Antworten steht statt "Jemand" der Handle. Damit er nach einer Umbenennung stimmt, speichert die Benachrichtigung nur die uid (actor) und den Auszug; den Text setzt der Abruf zusammen. Die Tabelle war noch nirgends ausgerollt, daher direkt in neuer Form statt per ALTER. Neu ist der Repost eines eigenen Beitrags (kind bump), in derselben Transaktion wie der Bump. Wer repostet, wird nicht gespeichert, die Meldung sagt daher "Jemand". Eigene Reposts lösen nichts aus. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
120 lines
3.8 KiB
Go
120 lines
3.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type notificationList struct {
|
|
Notifications []notificationJSON `json:"notifications"`
|
|
}
|
|
|
|
func unreadNotifications(t *testing.T, c *http.Client, srv *testServer) int {
|
|
t.Helper()
|
|
var out struct {
|
|
Unread int `json:"unread"`
|
|
}
|
|
if code := getAs(t, c, srv.URL+"/api/notifications/unread", &out); code != http.StatusOK {
|
|
t.Fatalf("/notifications/unread: status %d", code)
|
|
}
|
|
return out.Unread
|
|
}
|
|
|
|
func TestNotifyReply(t *testing.T) {
|
|
t.Parallel()
|
|
srv := newTestServer(t)
|
|
alice := registerAndLogin(t, srv, "alice")
|
|
bob := registerAndLogin(t, srv, "bob")
|
|
|
|
root := createEntry(t, alice, srv, post("frage"))
|
|
createEntry(t, alice, srv, reply(root, "nachtrag")) // an sich selbst: nichts
|
|
long := strings.Repeat("wort ", 20)
|
|
answer := createEntry(t, bob, srv, reply(root, long))
|
|
|
|
if n := unreadNotifications(t, alice, srv); n != 1 {
|
|
t.Fatalf("alice ungesehen: %d, erwartet 1", n)
|
|
}
|
|
if n := unreadNotifications(t, bob, srv); n != 0 {
|
|
t.Errorf("bob ungesehen: %d, erwartet 0", n)
|
|
}
|
|
|
|
var list notificationList
|
|
getAs(t, alice, srv.URL+"/api/notifications", &list)
|
|
if len(list.Notifications) != 1 {
|
|
t.Fatalf("liste: %+v", list)
|
|
}
|
|
n := list.Notifications[0]
|
|
wantMsg := "@bob hat auf deinen Beitrag geantwortet. (" + strings.TrimSpace(long[:50]) + "…)"
|
|
if n.Kind != "reply" || n.Link != fmt.Sprintf("/e/%d", answer) || n.Seen || n.Message != wantMsg {
|
|
t.Errorf("benachrichtigung: %+v", n)
|
|
}
|
|
// Das Ansehen der Liste markiert alles als gesehen.
|
|
if c := unreadNotifications(t, alice, srv); c != 0 {
|
|
t.Errorf("nach dem Ansehen ungesehen: %d", c)
|
|
}
|
|
getAs(t, alice, srv.URL+"/api/notifications", &list)
|
|
if !list.Notifications[0].Seen {
|
|
t.Error("zweiter Abruf: nicht als gesehen markiert")
|
|
}
|
|
|
|
// Bearbeiten zieht den Auszug nach, ein neuer Handle erscheint sofort,
|
|
// Löschen nimmt die Benachrichtigung mit.
|
|
editURL := fmt.Sprintf("%s/api/entry/%d/edit", srv.URL, answer)
|
|
resp := postForm(t, bob, editURL, url.Values{"content": {"neu"}})
|
|
resp.Body.Close()
|
|
resp = postForm(t, bob, srv.URL+"/api/user/handle", url.Values{"handle": {"robert"}})
|
|
resp.Body.Close()
|
|
getAs(t, alice, srv.URL+"/api/notifications", &list)
|
|
if got := list.Notifications[0].Message; got != "@robert hat auf deinen Beitrag geantwortet. (neu)" {
|
|
t.Errorf("nach Bearbeiten und Umbenennen: %q", got)
|
|
}
|
|
resp = postForm(t, bob, fmt.Sprintf("%s/api/entry/%d/delete", srv.URL, answer), nil)
|
|
resp.Body.Close()
|
|
getAs(t, alice, srv.URL+"/api/notifications", &list)
|
|
if len(list.Notifications) != 0 {
|
|
t.Errorf("nach dem Löschen: %+v", list.Notifications)
|
|
}
|
|
}
|
|
|
|
func TestNotifyBump(t *testing.T) {
|
|
t.Parallel()
|
|
srv := newTestServer(t)
|
|
alice := registerAndLogin(t, srv, "alice")
|
|
bob := registerAndLogin(t, srv, "bob")
|
|
mine := createEntry(t, alice, srv, post("mein erster\nbeitrag"))
|
|
own := createEntry(t, alice, srv, post("selbst repostet"))
|
|
|
|
for _, b := range []struct {
|
|
c *http.Client
|
|
pid int64
|
|
}{{bob, mine}, {alice, own}} {
|
|
resp := postForm(t, b.c, fmt.Sprintf("%s/api/entry/%d/bump", srv.URL, b.pid), nil)
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("bump %d: status %d", b.pid, resp.StatusCode)
|
|
}
|
|
}
|
|
var list notificationList
|
|
getAs(t, alice, srv.URL+"/api/notifications", &list)
|
|
if len(list.Notifications) != 1 {
|
|
t.Fatalf("liste: %+v", list.Notifications)
|
|
}
|
|
n := list.Notifications[0]
|
|
// Wer repostet, wird nicht gespeichert: "Jemand", auch wenn es bob war.
|
|
if n.Kind != "bump" || n.Link != fmt.Sprintf("/e/%d", mine) ||
|
|
n.Message != "Jemand hat deinen Beitrag repostet. (mein erster beitrag)" {
|
|
t.Errorf("benachrichtigung: %+v", n)
|
|
}
|
|
}
|
|
|
|
func TestNotificationsNeedLogin(t *testing.T) {
|
|
t.Parallel()
|
|
srv := newTestServer(t)
|
|
if code := getAny(t, srv.URL+"/api/notifications/unread", &struct{}{}); code != http.StatusUnauthorized {
|
|
t.Errorf("ohne Login: status %d, erwartet 401", code)
|
|
}
|
|
}
|