ЛР 6 — БПИ2302 — Нурлыгаянова — на 3

This commit is contained in:
Nurlygayanova Ekaterina
2026-11-13 11:32:45 +03:00
parent 470d9d6f1e
commit 69a190be0e
164 changed files with 25947 additions and 29 deletions
+546
View File
@@ -0,0 +1,546 @@
package http
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"image"
"image/jpeg"
"image/png"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"strings"
"testing"
"github.com/google/uuid"
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/api/http/dto"
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/objectstore"
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/service"
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store"
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/validate"
)
func testPNG(t *testing.T, w, h int) []byte {
t.Helper()
var b bytes.Buffer
if err := png.Encode(&b, image.NewRGBA(image.Rect(0, 0, w, h))); err != nil {
t.Fatal(err)
}
return b.Bytes()
}
func testJPEG(t *testing.T) []byte {
t.Helper()
var b bytes.Buffer
if err := jpeg.Encode(&b, image.NewRGBA(image.Rect(0, 0, 8, 8)), nil); err != nil {
t.Fatal(err)
}
return b.Bytes()
}
func (r *testRig) upload(actor, chatID, filename, contentType string, data []byte) *http.Response {
r.t.Helper()
return r.do(http.MethodPost, "/api/v1/chats/"+chatID+"/attachments?filename="+url.QueryEscape(filename), actor,
map[string]string{"Content-Type": contentType}, bytes.NewReader(data))
}
func (r *testRig) uploadOK(actor, chatID string, data []byte) dto.AttachmentResponse {
r.t.Helper()
resp := r.upload(actor, chatID, "photo.png", "image/png", data)
expectStatus(r.t, resp, http.StatusAccepted)
var att dto.AttachmentResponse
readJSON(r.t, resp, &att)
return att
}
func TestAttachment_QueuedIsDurableAndNotDownloadable(t *testing.T) {
r := newTestRig(t)
alice, bob, eve, chat := bootstrap(r)
data := testPNG(t, 16, 16)
att := r.uploadOK(alice.ID, chat.ID, data)
sum := sha256.Sum256(data)
if att.Status != "queued" || att.SHA256 != hex.EncodeToString(sum[:]) || att.SizeBytes != int64(len(data)) ||
att.OwnerID != alice.ID || att.ChatID != chat.ID || att.ErrorCode != nil || att.Variants == nil {
t.Errorf("metadata = %+v", att)
}
resp := r.do(http.MethodGet, "/api/v1/attachments/"+att.ID+"/content", bob.ID, nil, nil)
expectStatus(t, resp, http.StatusConflict)
resp.Body.Close()
// Queued data is already in durable staging; API has not published final objects.
stored, _, err := r.objects.Get(context.Background(), "staging/"+att.ID)
if err != nil {
t.Fatal(err)
}
defer stored.Close()
got, _ := io.ReadAll(stored)
if !bytes.Equal(got, data) {
t.Fatal("staging bytes differ")
}
for _, suffix := range []string{"", "/content"} {
resp := r.do(http.MethodGet, "/api/v1/attachments/"+att.ID+suffix, eve.ID, nil, nil)
expectStatus(t, resp, http.StatusNotFound)
resp.Body.Close()
}
}
func TestAttachment_ObjectKeyIsServerAssignedAndHidden(t *testing.T) {
r := newTestRig(t)
alice, _, _, chat := bootstrap(r)
resp := r.upload(alice.ID, chat.ID, "evil..name.png", "image/png", testPNG(t, 2, 2))
expectStatus(t, resp, http.StatusAccepted)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
keys := r.objects.Keys()
if len(keys) != 1 || strings.Contains(keys[0], "evil") {
t.Fatalf("object keys = %v", keys)
}
if bytes.Contains(body, []byte(keys[0])) || bytes.Contains(body, []byte("object_key")) {
t.Errorf("response exposes object key: %s", body)
}
}
func TestAttachment_Rejections(t *testing.T) {
r := newTestRig(t)
alice, _, eve, chat := bootstrap(r)
valid := testPNG(t, 4, 4)
oversized := append(bytes.Clone(valid), make([]byte, testMaxUploadBytes)...)
cases := []struct {
name string
actor string
chat string
filename string
contentType string
data []byte
want int
}{
{"outsider", eve.ID, chat.ID, "a.png", "image/png", valid, 404},
{"outsider sees 404 before size check", eve.ID, chat.ID, "a.png", "image/png", oversized, 404},
{"unknown chat", alice.ID, uuid.NewString(), "a.png", "image/png", valid, 404},
{"bad chat id", alice.ID, "nope", "a.png", "image/png", valid, 400},
{"path in filename", alice.ID, chat.ID, "../a.png", "image/png", valid, 400},
{"backslash in filename", alice.ID, chat.ID, `a\b.png`, "image/png", valid, 400},
{"NUL in filename", alice.ID, chat.ID, "a\x00.png", "image/png", valid, 400},
{"empty filename", alice.ID, chat.ID, "", "image/png", valid, 400},
{"too many bytes", alice.ID, chat.ID, "a.png", "image/png", oversized, 413},
{"too many pixels", alice.ID, chat.ID, "a.png", "image/png", testPNG(t, 101, 100), 413},
{"wrong declared type", alice.ID, chat.ID, "a.png", "application/json", valid, 415},
{"no declared type", alice.ID, chat.ID, "a.png", "", valid, 415},
{"declared jpeg, actual png", alice.ID, chat.ID, "a.jpg", "image/jpeg", valid, 415},
{"declared png, actual jpeg", alice.ID, chat.ID, "a.png", "image/png", testJPEG(t), 415},
{"not an image", alice.ID, chat.ID, "a.png", "image/png", []byte("<html>"), 415},
{"truncated", alice.ID, chat.ID, "a.png", "image/png", valid[:len(valid)/2], 415},
{"empty body", alice.ID, chat.ID, "a.png", "image/png", nil, 415},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
resp := r.upload(tc.actor, tc.chat, tc.filename, tc.contentType, tc.data)
defer resp.Body.Close()
if resp.StatusCode != tc.want {
body, _ := io.ReadAll(resp.Body)
t.Fatalf("status = %d, want %d, body = %s", resp.StatusCode, tc.want, body)
}
})
}
if keys := r.objects.Keys(); len(keys) != 0 {
t.Errorf("rejected uploads left objects: %v", keys)
}
}
func TestAttachment_RequiresAuthentication(t *testing.T) {
r := newTestRig(t)
_, _, _, chat := bootstrap(r)
resp := r.upload("", chat.ID, "a.png", "image/png", testPNG(t, 2, 2))
expectStatus(t, resp, http.StatusUnauthorized)
resp.Body.Close()
}
func TestAttachment_S3UnavailableGives503AndKeepsMessagesWorking(t *testing.T) {
r := newTestRig(t)
alice, _, _, chat := bootstrap(r)
data := testPNG(t, 2, 2)
att := r.uploadOK(alice.ID, chat.ID, data)
r.objects.Down.Store(true)
resp := r.upload(alice.ID, chat.ID, "b.png", "image/png", data)
expectStatus(t, resp, http.StatusServiceUnavailable)
if code := readError(t, resp).Error.Code; code != "unavailable" {
t.Errorf("code = %q", code)
}
resp = r.do(http.MethodGet, "/api/v1/attachments/"+att.ID+"/content", alice.ID, nil, nil)
expectStatus(t, resp, http.StatusConflict)
resp.Body.Close()
// Метаданные лежат в БД и остаются доступными.
resp = r.do(http.MethodGet, "/api/v1/attachments/"+att.ID, alice.ID, nil, nil)
expectStatus(t, resp, http.StatusOK)
resp.Body.Close()
if code, _ := r.send(alice, chat.ID, "текст без S3"); code != http.StatusCreated {
t.Errorf("message status = %d", code)
}
r.objects.Down.Store(false)
resp = r.do(http.MethodGet, "/api/v1/attachments/"+att.ID+"/content", alice.ID, nil, nil)
expectStatus(t, resp, http.StatusConflict)
resp.Body.Close()
}
func TestAttachment_UnknownVariant(t *testing.T) {
r := newTestRig(t)
alice, _, _, chat := bootstrap(r)
att := r.uploadOK(alice.ID, chat.ID, testPNG(t, 2, 2))
for query, want := range map[string]int{"?variant=original": 409, "?variant=thumbnail": 409, "?variant=../x": 400, "?variant=": 400} {
resp := r.do(http.MethodGet, "/api/v1/attachments/"+att.ID+"/content"+query, alice.ID, nil, nil)
if resp.StatusCode != want {
t.Errorf("%s: status = %d, want %d", query, resp.StatusCode, want)
}
resp.Body.Close()
}
}
func TestMessage_AttachmentRules(t *testing.T) {
r := newTestRig(t)
alice, bob, _, chat := bootstrap(r)
other := r.createChat(alice, "другой", alice.ID)
data := testPNG(t, 2, 2)
att := r.uploadOK(alice.ID, chat.ID, data)
foreign := r.uploadOK(alice.ID, other.ID, data)
path := "/api/v1/chats/" + chat.ID + "/messages"
post := func(body string) *http.Response {
return r.do(http.MethodPost, path, bob.ID, map[string]string{"Content-Type": "application/json"}, strings.NewReader(body))
}
cases := []struct {
name string
body string
want int
}{
{"empty text with attachment", `{"text":"","attachment_ids":["` + att.ID + `"]}`, 201},
{"spaces with attachment", `{"text":" ","attachment_ids":["` + att.ID + `"]}`, 201},
{"empty attachment list is the old form", `{"text":"x","attachment_ids":[]}`, 201},
{"empty text without attachments", `{"text":"","attachment_ids":[]}`, 400},
{"text is still required", `{"attachment_ids":["` + att.ID + `"]}`, 400},
{"null list", `{"text":"x","attachment_ids":null}`, 400},
{"wrong type", `{"text":"x","attachment_ids":"` + att.ID + `"}`, 400},
{"invalid id", `{"text":"x","attachment_ids":["nope"]}`, 400},
{"duplicate", `{"text":"x","attachment_ids":["` + att.ID + `","` + strings.ToUpper(att.ID) + `"]}`, 400},
{"unknown attachment", `{"text":"x","attachment_ids":["` + uuid.NewString() + `"]}`, 404},
{"attachment of another chat", `{"text":"x","attachment_ids":["` + foreign.ID + `"]}`, 404},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
resp := post(tc.body)
defer resp.Body.Close()
if resp.StatusCode != tc.want {
body, _ := io.ReadAll(resp.Body)
t.Fatalf("status = %d, want %d, body = %s", resp.StatusCode, tc.want, body)
}
})
}
items, _ := r.listMessages(alice, chat.ID, 100)
if len(items) != 3 || len(items[0].AttachmentIDs) != 1 || items[0].AttachmentIDs[0] != att.ID {
t.Errorf("history = %+v", items)
}
if items[2].AttachmentIDs == nil {
t.Error("message without attachments must have [] rather than null")
}
}
func TestMessage_IdempotencyCoversAttachments(t *testing.T) {
r := newTestRig(t)
alice, _, _, chat := bootstrap(r)
a := r.uploadOK(alice.ID, chat.ID, testPNG(t, 2, 2)).ID
b := r.uploadOK(alice.ID, chat.ID, testPNG(t, 3, 3)).ID
key := uuid.NewString()
code, first := r.postKeyed(alice.ID, chat.ID, key, `{"text":"x","attachment_ids":["`+a+`","`+b+`"]}`)
if code != http.StatusCreated {
t.Fatalf("first: %d %s", code, first)
}
code, replay := r.postKeyed(alice.ID, chat.ID, key, `{"attachment_ids":["`+a+`","`+b+`"],"text":"x"}`)
if code != http.StatusCreated || !bytes.Equal(first, replay) {
t.Errorf("replay: %d %s", code, replay)
}
if code, body := r.postKeyed(alice.ID, chat.ID, key, `{"text":"x","attachment_ids":["`+b+`"]}`); code != http.StatusConflict {
t.Errorf("other attachments: %d %s", code, body)
}
// Отсутствующий attachment_ids равен пустому списку.
plain := uuid.NewString()
if code, _ := r.postKeyed(alice.ID, chat.ID, plain, `{"text":"y"}`); code != http.StatusCreated {
t.Fatalf("plain: %d", code)
}
if code, body := r.postKeyed(alice.ID, chat.ID, plain, `{"text":"y","attachment_ids":[]}`); code != http.StatusCreated {
t.Errorf("plain replay: %d %s", code, body)
}
}
// failingMetadata имитирует отказ БД между записью в S3 и записью метаданных.
type failingMetadata struct{ *store.Memory }
func (f failingMetadata) CreateAttachment(context.Context, domain.Attachment) (domain.Attachment, error) {
return domain.Attachment{}, domain.ErrUnavailable
}
func TestAttachment_MetadataFailureRemovesObject(t *testing.T) {
r := newTestRig(t)
alice, _, _, chat := bootstrap(r)
objects := objectstore.NewMemory()
svc := service.NewAttachments(failingMetadata{r.memory}, objects, service.AttachmentLimits{
MaxUploadBytes: testMaxUploadBytes, MaxImagePixels: testMaxImagePixels, MaxConcurrentUploads: 1,
}, slog.New(slog.NewTextHandler(io.Discard, nil)))
data := testPNG(t, 2, 2)
_, err := svc.Upload(context.Background(), service.UploadInput{
CallerID: alice.ID, ChatID: chat.ID, Filename: "a.png", ContentType: "image/png",
ContentLength: int64(len(data)), Body: bytes.NewReader(data),
})
if !errors.Is(err, domain.ErrUnavailable) {
t.Fatalf("err = %v", err)
}
if keys := objects.Keys(); len(keys) != 0 {
t.Errorf("object left without metadata: %v", keys)
}
}
// lostCommit имитирует потерю ответа: строка сохранилась, а INSERT вернул ошибку.
type lostCommit struct{ *store.Memory }
func (l lostCommit) CreateAttachment(ctx context.Context, att domain.Attachment) (domain.Attachment, error) {
if _, err := l.Memory.CreateAttachment(ctx, att); err != nil {
return domain.Attachment{}, err
}
return domain.Attachment{}, domain.ErrUnavailable
}
// metadataDown имитирует БД, недоступную и для записи, и для проверки перед компенсацией.
type metadataDown struct{ failingMetadata }
func (metadataDown) AttachmentExists(context.Context, string) (bool, error) {
return false, domain.ErrUnavailable
}
func uploadVia(t *testing.T, svc *service.Attachments, caller, chatID string, body io.Reader, size int64) error {
t.Helper()
_, err := svc.Upload(context.Background(), service.UploadInput{
CallerID: caller, ChatID: chatID, Filename: "a.png", ContentType: "image/png", ContentLength: size, Body: body,
})
return err
}
// Строка в БД не должна остаться без файла: при потерянном ответе объект удалять нельзя.
func TestAttachment_LostCommitKeepsObject(t *testing.T) {
r := newTestRig(t)
alice, _, _, chat := bootstrap(r)
objects := objectstore.NewMemory()
svc := service.NewAttachments(lostCommit{r.memory}, objects, service.AttachmentLimits{
MaxUploadBytes: testMaxUploadBytes, MaxImagePixels: testMaxImagePixels, MaxConcurrentUploads: 1,
}, slog.New(slog.NewTextHandler(io.Discard, nil)))
data := testPNG(t, 2, 2)
if err := uploadVia(t, svc, alice.ID, chat.ID, bytes.NewReader(data), int64(len(data))); !errors.Is(err, domain.ErrUnavailable) {
t.Fatalf("err = %v", err)
}
if keys := objects.Keys(); len(keys) != 1 {
t.Errorf("object of a committed row was deleted: %v", keys)
}
}
// Если проверить нельзя, объект остаётся очистителю, а не удаляется вслепую.
func TestAttachment_UncheckableMetadataLeavesObjectToJanitor(t *testing.T) {
r := newTestRig(t)
alice, _, _, chat := bootstrap(r)
objects := objectstore.NewMemory()
svc := service.NewAttachments(metadataDown{failingMetadata{r.memory}}, objects, service.AttachmentLimits{
MaxUploadBytes: testMaxUploadBytes, MaxImagePixels: testMaxImagePixels, MaxConcurrentUploads: 1,
}, slog.New(slog.NewTextHandler(io.Discard, nil)))
data := testPNG(t, 2, 2)
if err := uploadVia(t, svc, alice.ID, chat.ID, bytes.NewReader(data), int64(len(data))); !errors.Is(err, domain.ErrUnavailable) {
t.Fatalf("err = %v", err)
}
if keys := objects.Keys(); len(keys) != 1 {
t.Errorf("object deleted without a metadata check: %v", keys)
}
}
// brokenBody отдаёт начало файла и обрывается: так выглядит отключившийся клиент.
type brokenBody struct{ data []byte }
func (b *brokenBody) Read(p []byte) (int, error) {
if len(b.data) == 0 {
return 0, io.ErrUnexpectedEOF
}
n := copy(p, b.data)
b.data = b.data[n:]
return n, nil
}
func spoolFiles(t *testing.T, dir string) []string {
t.Helper()
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
names := make([]string, 0, len(entries))
for _, entry := range entries {
names = append(names, entry.Name())
}
return names
}
func TestAttachment_InterruptedUploadLeavesNothing(t *testing.T) {
dir := t.TempDir()
t.Setenv("TMPDIR", dir)
r := newTestRig(t)
alice, _, _, chat := bootstrap(r)
svc := service.NewAttachments(r.memory, r.objects, service.AttachmentLimits{
MaxUploadBytes: testMaxUploadBytes, MaxImagePixels: testMaxImagePixels, MaxConcurrentUploads: 1,
}, slog.New(slog.NewTextHandler(io.Discard, nil)))
data := testPNG(t, 8, 8)
err := uploadVia(t, svc, alice.ID, chat.ID, &brokenBody{data: bytes.Clone(data[:len(data)/2])}, int64(len(data)))
var invalid *validate.Error
if !errors.As(err, &invalid) {
t.Fatalf("err = %v, want a client error rather than 500", err)
}
if keys := r.objects.Keys(); len(keys) != 0 {
t.Errorf("interrupted upload reached S3: %v", keys)
}
if files := spoolFiles(t, dir); len(files) != 0 {
t.Errorf("temporary files left: %v", files)
}
// Слот освобождён: следующая загрузка проходит при лимите в один файл.
if err := uploadVia(t, svc, alice.ID, chat.ID, bytes.NewReader(data), int64(len(data))); err != nil {
t.Errorf("slot was not released: %v", err)
}
}
func TestAttachment_TemporaryFileIsRemovedWhileUploadIsRunning(t *testing.T) {
dir := t.TempDir()
t.Setenv("TMPDIR", dir)
r := newTestRig(t)
alice, _, _, chat := bootstrap(r)
svc := service.NewAttachments(r.memory, r.objects, service.AttachmentLimits{
MaxUploadBytes: testMaxUploadBytes, MaxImagePixels: testMaxImagePixels, MaxConcurrentUploads: 1,
}, slog.New(slog.NewTextHandler(io.Discard, nil)))
blocked := &blockingReader{release: make(chan struct{}), started: make(chan struct{})}
done := make(chan error, 1)
go func() { done <- uploadVia(t, svc, alice.ID, chat.ID, blocked, -1) }()
<-blocked.started
// Файл уже открыт и убран из каталога: падение процесса в этот момент ничего не оставит.
if files := spoolFiles(t, dir); len(files) != 0 {
t.Errorf("temporary file is visible during upload: %v", files)
}
close(blocked.release)
<-done
}
// Тело без Content-Length не должно обойти лимит размера.
func TestAttachment_ChunkedOverLimitGives413(t *testing.T) {
r := newTestRig(t)
alice, _, _, chat := bootstrap(r)
big := append(testPNG(t, 2, 2), make([]byte, testMaxUploadBytes)...)
req, err := http.NewRequest(http.MethodPost, r.server.URL+"/api/v1/chats/"+chat.ID+"/attachments?filename=a.png", io.MultiReader(bytes.NewReader(big)))
if err != nil {
t.Fatal(err)
}
req.ContentLength = -1
req.Header.Set("Content-Type", "image/png")
r.setAuth(req, alice.ID)
resp, err := r.client.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
expectStatus(t, resp, http.StatusRequestEntityTooLarge)
if keys := r.objects.Keys(); len(keys) != 0 {
t.Errorf("oversized upload reached S3: %v", keys)
}
}
// blockingReader держит загрузку открытой, пока тест не разрешит продолжить.
type blockingReader struct {
release chan struct{}
started chan struct{}
}
func (b *blockingReader) Read([]byte) (int, error) {
close(b.started)
<-b.release
return 0, io.EOF
}
func TestAttachment_ConcurrentUploadLimitGives429(t *testing.T) {
r := newTestRig(t)
alice, _, _, chat := bootstrap(r)
objects := objectstore.NewMemory()
svc := service.NewAttachments(r.memory, objects, service.AttachmentLimits{
MaxUploadBytes: testMaxUploadBytes, MaxImagePixels: testMaxImagePixels, MaxConcurrentUploads: 1,
}, slog.New(slog.NewTextHandler(io.Discard, nil)))
blocked := &blockingReader{release: make(chan struct{}), started: make(chan struct{})}
done := make(chan error, 1)
go func() {
_, err := svc.Upload(context.Background(), service.UploadInput{
CallerID: alice.ID, ChatID: chat.ID, Filename: "a.png", ContentType: "image/png", Body: blocked,
})
done <- err
}()
<-blocked.started
data := testPNG(t, 2, 2)
_, err := svc.Upload(context.Background(), service.UploadInput{
CallerID: alice.ID, ChatID: chat.ID, Filename: "b.png", ContentType: "image/png", Body: bytes.NewReader(data),
})
if !errors.Is(err, domain.ErrRateLimited) {
t.Errorf("second upload: %v", err)
}
close(blocked.release)
<-done
}
// Клиент, который читает ответ только после отправки всего тела, должен получить код, а не обрыв.
func TestAttachment_RejectedBodyIsDrainedSoClientGetsStatus(t *testing.T) {
r := newTestRig(t)
alice, _, eve, chat := bootstrap(r)
big := make([]byte, 8*testMaxUploadBytes)
path := "/api/v1/chats/" + chat.ID + "/attachments?filename=a.png"
cases := []struct {
name string
actor string
want int
}{
{"too large", alice.ID, http.StatusRequestEntityTooLarge},
{"outsider", eve.ID, http.StatusNotFound},
{"no session", "", http.StatusUnauthorized},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
for _, length := range []int64{int64(len(big)), -1} { // с Content-Length и chunked
req, err := http.NewRequest(http.MethodPost, r.server.URL+path, io.MultiReader(bytes.NewReader(big)))
if err != nil {
t.Fatal(err)
}
req.ContentLength = length
req.Header.Set("Content-Type", "image/png")
r.setAuth(req, tc.actor)
resp, err := r.client.Do(req)
if err != nil {
t.Fatalf("length %d: %v", length, err)
}
resp.Body.Close()
if resp.StatusCode != tc.want {
t.Errorf("length %d: status = %d, want %d", length, resp.StatusCode, tc.want)
}
}
})
}
}