Files
mtusi2026lab4/internal/chat/attachment_http_test.go
T

126 lines
4.5 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[:], status, id, chatID, owner)
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, "ready"), f.attachment(t, c, f.alice, "ready")
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 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, "ready")
foreign := f.attachment(t, other, f.alice, "ready")
failed := f.attachment(t, c, f.alice, "failed")
pending := f.attachment(t, c, f.alice, "pending")
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},
{"pending", f.alice, pending, http.StatusConflict},
{"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, "same", 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)
}
}