package httpapi import ( "bytes" "encoding/base64" "log/slog" "net/http" "net/url" "strconv" "testing" "time" "kver/internal/chat" ) type chatsJSON struct { Chats []chatJSON } type messagesJSON struct { Messages []messageJSON } func msgForm(body []byte) url.Values { return url.Values{"body": {base64.StdEncoding.EncodeToString(body)}} } func TestChats(t *testing.T) { t.Parallel() srv := newTestServer(t) alice := registerAndLogin(t, srv, "alice") bob := registerAndLogin(t, srv, "bob") carol := registerAndLogin(t, srv, "carol") msg := fixture(t, "v6-msg.pgp") for c, kf := range map[*http.Client][2]string{alice: {"v6.pgp", "v6-locked.pgp"}, bob: {"v6-2.pgp", "v6-2-locked.pgp"}} { if st, code := postCode(t, c, srv.URL+"/api/user/key", keyForm(fixture(t, kf[0]), fixture(t, kf[1]))); st != http.StatusCreated { t.Fatalf("schlüssel %s: %d %s", kf[0], st, code) } } toBob, toAlice := srv.URL+"/api/chats/bob", srv.URL+"/api/chats/alice" // Ablehnungen. for _, tc := range []struct { name string c *http.Client url string form url.Values status int code string }{ {"ohne login", newClient(t), toBob, msgForm(msg), 401, "auth.required"}, {"an sich selbst", alice, toAlice, msgForm(msg), 400, "chat.self"}, {"unbekannt", alice, srv.URL + "/api/chats/niemand", msgForm(msg), 404, "not_found"}, {"empfänger ohne schlüssel", alice, srv.URL + "/api/chats/carol", msgForm(msg), 409, "chat.no_key"}, {"absender ohne schlüssel", carol, toBob, msgForm(msg), 409, "chat.no_key"}, {"klartext", alice, toBob, msgForm([]byte("hallo")), 400, "chat.body"}, {"kein base64", alice, toBob, url.Values{"body": {"%%%"}}, 400, "chat.body"}, {"öffentlicher schlüssel", alice, toBob, msgForm(fixture(t, "v6.pgp")), 400, "chat.body"}, } { if st, code := postCode(t, tc.c, tc.url, tc.form); st != tc.status || (tc.code != "not_found" && code != tc.code) { t.Errorf("%s: %d %s, erwartet %d %s", tc.name, st, code, tc.status, tc.code) } } if st, code := postCode(t, alice, toBob, msgForm(msg)); st != http.StatusCreated { t.Fatalf("senden: %d %s", st, code) } var list chatsJSON if getAs(t, bob, srv.URL+"/api/chats", &list); len(list.Chats) != 1 || list.Chats[0].Username != "alice" || list.Chats[0].Unread != 1 { t.Fatalf("bobs chats: %+v", list) } var got messagesJSON if getAs(t, bob, toAlice, &got); len(got.Messages) != 1 || got.Messages[0].Mine || !bytes.Equal(got.Messages[0].Body, msg) { t.Fatalf("bob liest: %+v", got) } // Abgerufen heißt gelesen. if getAs(t, bob, srv.URL+"/api/chats", &list); list.Chats[0].Unread != 0 { t.Fatalf("nach dem lesen: %+v", list) } id := got.Messages[0].ID if getAs(t, alice, toBob, &got); len(got.Messages) != 1 || !got.Messages[0].Mine { t.Fatalf("alice liest: %+v", got) } if getAs(t, alice, toBob+"?after="+strconv.FormatInt(id, 10), &got); len(got.Messages) != 0 { t.Fatalf("after: %+v", got) } // Dritte sehen nichts. if getAs(t, carol, srv.URL+"/api/chats", &list); len(list.Chats) != 0 { t.Fatalf("carols chats: %+v", list) } if getAs(t, carol, toAlice, &got); len(got.Messages) != 0 { t.Fatalf("carol liest alice: %+v", got) } // Abgelaufene Nachrichten: nicht mehr ausgeliefert, dann gelöscht. if _, err := srv.db.Exec(`UPDATE message SET created_at = created_at - $1`, int64(chat.Retention/time.Second)+1); err != nil { t.Fatal(err) } if getAs(t, bob, toAlice, &got); len(got.Messages) != 0 { t.Fatalf("abgelaufen ausgeliefert: %+v", got) } if n, err := chat.NewPurger(srv.db, slog.Default(), time.Now).Purge(t.Context()); err != nil || n != 1 { t.Fatalf("purge: %d %v", n, err) } // Konto löschen nimmt die Nachrichten mit. if st, code := postCode(t, alice, toBob, msgForm(msg)); st != http.StatusCreated { t.Fatalf("senden: %d %s", st, code) } if st := postStatus(t, bob, srv.URL+"/api/user/delete", url.Values{"pass1": {"supersecret1"}}); st != http.StatusOK { t.Fatalf("konto löschen: %d", st) } var n int if err := srv.db.QueryRow(`SELECT COUNT(*) FROM message`).Scan(&n); err != nil || n != 0 { t.Fatalf("nachrichten nach konto-löschung: %d %v", n, err) } }