forked from mtusi2026/mtusi2026lab5
Переведите приём и чтение вложений на асинхронный контракт
This commit is contained in:
@@ -25,7 +25,13 @@ func (f fixture) attachment(t *testing.T, chatID, owner uuid.UUID, status string
|
||||
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)
|
||||
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)
|
||||
}
|
||||
@@ -35,7 +41,7 @@ func (f fixture) attachment(t *testing.T, chatID, owner uuid.UUID, status string
|
||||
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")
|
||||
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)
|
||||
@@ -80,14 +86,46 @@ func TestAttachmentHTTPRoundTripReplayAndOrderedConflict(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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, "ready")
|
||||
foreign := f.attachment(t, other, f.alice, "ready")
|
||||
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, "pending")
|
||||
pending := f.attachment(t, c, f.alice, "processing")
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
actor, attachment uuid.UUID
|
||||
@@ -96,13 +134,13 @@ func TestAttachmentHTTPStatusAndChatAccess(t *testing.T) {
|
||||
{"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},
|
||||
{"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, "same", body)
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user