164 lines
6.3 KiB
Go
164 lines
6.3 KiB
Go
package chat_test
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"messenger/internal/chat"
|
|
"messenger/internal/platform/ident"
|
|
"messenger/internal/platform/pagination"
|
|
)
|
|
|
|
func (f fixture) attachment(t *testing.T, chatID, owner uuid.UUID, status string) uuid.UUID {
|
|
t.Helper()
|
|
id := ident.New()
|
|
hash := sha256.Sum256([]byte(id.String()))
|
|
_, err := db.Pool.Exec(f.ctx, `
|
|
WITH file AS (
|
|
INSERT INTO files (hash, size_bytes, mime_type, status, created_at)
|
|
VALUES ($1, 7, 'image/png', $2, now()) RETURNING hash)
|
|
INSERT INTO attachments (id, chat_id, owner_id, filename, file_hash, created_at)
|
|
SELECT $3, $4, $5, 'image.png', hash, now() FROM file`, hash[:], "pending", id, chatID, owner)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = db.Pool.Exec(f.ctx, `INSERT INTO attachment_jobs(attachment_id,source_key,state,error_code,lease_token,lease_until)
|
|
VALUES($1,$2,$3,CASE WHEN $3='failed' THEN 'processing_failed' END,CASE WHEN $3='processing' THEN gen_random_uuid() END,
|
|
CASE WHEN $3='processing' THEN now()+interval '1 minute' END)`, id, "staging/"+id.String(), status)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
func TestAttachmentHTTPRoundTripReplayAndOrderedConflict(t *testing.T) {
|
|
f := newFixture(t)
|
|
c := f.newChat(t, f.alice, f.bob)
|
|
a, b := f.attachment(t, c, f.bob, "queued"), f.attachment(t, c, f.alice, "queued")
|
|
r := messageRouter(f, f.alice)
|
|
body := fmt.Sprintf(`{"text":"","attachment_ids":[%q,%q]}`, b, a)
|
|
first := sendJSON(r, c, "attachments", body)
|
|
if first.Code != http.StatusCreated {
|
|
t.Fatalf("attachment-only send: %d %s", first.Code, first.Body)
|
|
}
|
|
var message chat.MessageResponse
|
|
if err := json.Unmarshal(first.Body.Bytes(), &message); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if message.Text != "" || !reflect.DeepEqual(message.AttachmentIDs, []string{b.String(), a.String()}) {
|
|
t.Fatalf("message = %+v", message)
|
|
}
|
|
if again := sendJSON(r, c, "attachments", body); again.Code != http.StatusCreated || again.Body.String() != first.Body.String() {
|
|
t.Fatalf("replay: %d %s, want %s", again.Code, again.Body, first.Body)
|
|
}
|
|
for _, changed := range []string{
|
|
fmt.Sprintf(`{"text":"","attachment_ids":[%q,%q]}`, a, b),
|
|
fmt.Sprintf(`{"text":"","attachment_ids":[%q]}`, b),
|
|
fmt.Sprintf(`{"text":"different","attachment_ids":[%q,%q]}`, b, a),
|
|
} {
|
|
if rec := sendJSON(r, c, "attachments", changed); rec.Code != http.StatusConflict {
|
|
t.Fatalf("changed payload: %d %s", rec.Code, rec.Body)
|
|
}
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
r.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/chats/"+c.String()+"/messages", nil))
|
|
var page pagination.PageResponse[chat.MessageResponse]
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("history: %d %s", rec.Code, rec.Body)
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &page); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(page.Items) != 1 || !reflect.DeepEqual(page.Items[0], message) {
|
|
t.Fatalf("history = %+v, want %+v", page, message)
|
|
}
|
|
without := sendJSON(r, c, "empty", `{"text":"text-only"}`)
|
|
empty := sendJSON(r, c, "empty", `{"attachment_ids":[],"text":"text-only"}`)
|
|
if without.Code != http.StatusCreated || empty.Code != http.StatusCreated || without.Body.String() != empty.Body.String() {
|
|
t.Fatalf("omitted vs empty replay: %d %s; %d %s", without.Code, without.Body, empty.Code, empty.Body)
|
|
}
|
|
}
|
|
|
|
func TestHTTPReplayAfterAttachmentFailsAndMembershipChanges(t *testing.T) {
|
|
f := newFixture(t)
|
|
c := f.newChat(t, f.alice, f.bob)
|
|
a := f.attachment(t, c, f.alice, "queued")
|
|
body := fmt.Sprintf(`{"text":"","attachment_ids":[%q]}`, a)
|
|
router := messageRouter(f, f.alice)
|
|
first := sendJSON(router, c, "key", body)
|
|
if first.Code != http.StatusCreated {
|
|
t.Fatal(first.Code, first.Body)
|
|
}
|
|
if _, err := db.Pool.Exec(f.ctx, `UPDATE attachment_jobs SET state='failed',error_code='source_missing' WHERE attachment_id=$1`, a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
replay := sendJSON(router, c, "key", body)
|
|
if replay.Code != http.StatusCreated || replay.Body.String() != first.Body.String() {
|
|
t.Fatalf("replay=%d %s, want original %s", replay.Code, replay.Body, first.Body)
|
|
}
|
|
changed := fmt.Sprintf(`{"text":"changed","attachment_ids":[%q]}`, ident.New())
|
|
if r := sendJSON(router, c, "key", changed); r.Code != http.StatusConflict {
|
|
t.Fatalf("changed body: %d %s", r.Code, r.Body)
|
|
}
|
|
if r := sendJSON(router, c, "new", body); r.Code != http.StatusConflict {
|
|
t.Fatalf("new message: %d %s", r.Code, r.Body)
|
|
}
|
|
if _, err := db.Pool.Exec(f.ctx, `DELETE FROM chat_members WHERE chat_id=$1 AND user_id=$2`, c, f.alice); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r := sendJSON(router, c, "key", body); r.Code != http.StatusNotFound {
|
|
t.Fatalf("former member: %d %s", r.Code, r.Body)
|
|
}
|
|
}
|
|
|
|
func TestAttachmentHTTPStatusAndChatAccess(t *testing.T) {
|
|
f := newFixture(t)
|
|
c := f.newChat(t, f.alice, f.bob)
|
|
other := f.newChat(t, f.alice)
|
|
ready := f.attachment(t, c, f.bob, "queued")
|
|
foreign := f.attachment(t, other, f.alice, "queued")
|
|
failed := f.attachment(t, c, f.alice, "failed")
|
|
pending := f.attachment(t, c, f.alice, "processing")
|
|
for _, tc := range []struct {
|
|
name string
|
|
actor, attachment uuid.UUID
|
|
want int
|
|
}{
|
|
{"unknown", f.alice, ident.New(), http.StatusNotFound},
|
|
{"foreign", f.alice, foreign, http.StatusNotFound},
|
|
{"failed", f.alice, failed, http.StatusConflict},
|
|
{"processing", f.alice, pending, http.StatusCreated},
|
|
{"outsider", f.eve, ready, http.StatusNotFound},
|
|
{"other-owner", f.alice, ready, http.StatusCreated},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
body := fmt.Sprintf(`{"text":"","attachment_ids":[%q]}`, tc.attachment)
|
|
rec := sendJSON(messageRouter(f, tc.actor), c, tc.name, body)
|
|
if rec.Code != tc.want {
|
|
t.Fatalf("send: %d %s, want %d", rec.Code, rec.Body, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMessageJSONRequiresTextString(t *testing.T) {
|
|
f := newFixture(t)
|
|
c := f.newChat(t, f.alice)
|
|
r := messageRouter(f, f.alice)
|
|
for _, body := range []string{"null", `{}`, `{"text":null}`, `{"text":1}`, `{"text":[]}`, `{"text":"","attachment_ids":[]}`} {
|
|
if rec := sendJSON(r, c, "text", body); rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("send %s: %d %s", body, rec.Code, rec.Body)
|
|
}
|
|
}
|
|
if rec := sendJSON(r, c, "text", `{"text":"valid"}`); rec.Code != http.StatusCreated {
|
|
t.Fatalf("valid payload after rejected requests: %d %s", rec.Code, rec.Body)
|
|
}
|
|
}
|