Files
mtusi2026lab5/internal/app/attachment_test.go
T

264 lines
10 KiB
Go

package app_test
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"image"
"image/jpeg"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"messenger/internal/app"
"messenger/internal/attachment"
"messenger/internal/chat"
"messenger/internal/pipeline"
"messenger/internal/testenv"
)
func TestAttachmentUploadAuthenticatesBeforeParsing(t *testing.T) {
ts := newTester(t)
ts.expect(ts.do(http.MethodPost, "/api/v1/chats/not-a-uuid/attachments?filename=x.png", "", "invalid image", map[string]string{"Content-Type": "image/png"}), http.StatusUnauthorized, "unauthorized")
}
func TestAttachmentRoutesUseConfiguredDeadlines(t *testing.T) {
for _, binary := range []bool{false, true} {
name := "metadata"
requestTimeout := 40 * time.Millisecond
var binaryTimeout time.Duration
if binary {
name = "upload"
requestTimeout = time.Second
binaryTimeout = 40 * time.Millisecond
}
t.Run(name, func(t *testing.T) {
ts := newTester(t)
actor := ts.user("deadline_user", "User")
ts.a = app.New(app.Deps{
Log: slog.New(slog.NewTextHandler(io.Discard, nil)), DB: db.Pool, Sessions: kv.Client,
SessionTTL: time.Hour, MaxBodyBytes: 65536, InstanceID: testInstance,
RequestTimeout: requestTimeout, BinaryRequestTimeout: binaryTimeout,
})
var held []*pgxpool.Conn
for range db.Pool.Config().MaxConns {
conn, err := db.Pool.Acquire(context.Background())
if err != nil {
t.Fatal(err)
}
held = append(held, conn)
}
defer func() {
for _, conn := range held {
conn.Release()
}
}()
ctx, cancel := context.WithTimeout(context.Background(), 400*time.Millisecond)
defer cancel()
path := "/api/v1/attachments/11111111-1111-4111-8111-111111111111"
req := httptest.NewRequest("GET", path, nil).WithContext(ctx)
if binary {
path = "/api/v1/chats/11111111-1111-4111-8111-111111111111/attachments?filename=x.png"
req = httptest.NewRequest("POST", path, strings.NewReader("body")).WithContext(ctx)
req.Header.Set("Content-Type", "image/png")
}
req.Header.Set("Authorization", "Bearer "+actor.token)
rec := httptest.NewRecorder()
started := time.Now()
ts.a.Engine.ServeHTTP(rec, req)
if rec.Code != 503 || time.Since(started) > 200*time.Millisecond {
t.Fatalf("route exceeded configured budget: status%d elapsed%v body%s", rec.Code, time.Since(started), rec.Body.String())
}
})
}
}
func (ts *tester) binary(method, path, token, mime string, data []byte, length int64) response {
ts.t.Helper()
req := httptest.NewRequest(method, path, bytes.NewReader(data))
req.ContentLength = length
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
if mime != "" {
req.Header.Set("Content-Type", mime)
}
rec := httptest.NewRecorder()
ts.a.Engine.ServeHTTP(rec, req)
res := response{status: rec.Code, requestID: rec.Header().Get("X-Request-Id"), challenge: rec.Header().Get("WWW-Authenticate"), raw: rec.Body.Bytes()}
if res.requestID == "" || rec.Header().Get("X-Instance-Id") == "" {
ts.t.Fatal("binary response missing correlation headers")
}
return res
}
func TestAttachmentRealS3RoundTripValidationAndOutage(t *testing.T) {
ctx := context.Background()
s3, err := testenv.StartS3(ctx)
if err != nil {
t.Fatal(err)
}
defer s3.Close()
ts := newTester(t)
ts.a = app.New(app.Deps{Log: slog.New(slog.NewTextHandler(io.Discard, nil)), DB: db.Pool, Sessions: kv.Client, SessionTTL: time.Hour, MaxBodyBytes: 65536, InstanceID: testInstance, Blobs: s3.APIStore})
alice, bob, eve := ts.user("alice", "A"), ts.user("bob", "B"), ts.user("eve", "E")
room := ts.chat(alice.token, "files", alice.ID, bob.ID)
path := "/api/v1/chats/" + room.ID + "/attachments?filename=" + url.QueryEscape("фото.png")
data, err := os.ReadFile("../../tests/fixtures/sample.png")
if err != nil {
t.Fatal(err)
}
res := ts.binary("POST", path, alice.token, "image/png", data, int64(len(data)))
ts.expect(res, 202, "")
a := decode[attachment.Response](t, res)
digest := sha256.Sum256(data)
if a.SHA256 != hex.EncodeToString(digest[:]) || a.SizeBytes != int64(len(data)) || a.Filename != "фото.png" || a.Status != "queued" || a.ErrorCode != nil || len(a.Variants) != 0 {
t.Fatalf("attachment: %+v", a)
}
base := "/api/v1/attachments/" + a.ID
for _, suffix := range []string{"", "/content"} {
ts.expect(ts.do("GET", base+suffix, eve.token, nil, nil), 404, "not_found")
}
ts.expect(ts.do("GET", base, bob.token, nil, nil), 200, "")
got := ts.binary("GET", base+"/content", bob.token, "", nil, 0)
ts.expect(got, 409, "conflict")
ts.expect(ts.do("GET", base+"/content?variant=thumbnail", bob.token, nil, nil), 409, "conflict")
ts.expect(ts.do("GET", base+"/content?variant=unknown", bob.token, nil, nil), 400, "invalid_request")
msg := ts.do("POST", "/api/v1/chats/"+room.ID+"/messages", bob.token, map[string]any{"text": "", "attachment_ids": []string{a.ID}}, freshKey())
ts.expect(msg, 201, "")
if m := decode[chat.MessageResponse](t, msg); len(m.AttachmentIDs) != 1 || m.AttachmentIDs[0] != a.ID {
t.Fatal("lost attachment on message")
}
other := ts.chat(alice.token, "other", alice.ID)
ts.expect(ts.do("POST", "/api/v1/chats/"+other.ID+"/messages", alice.token, map[string]any{"text": "", "attachment_ids": []string{a.ID}}, freshKey()), 404, "not_found")
for _, tc := range []struct {
name, mime string
data []byte
length int64
want int
}{
{"MIME spoof", "image/jpeg", data, int64(len(data)), 415},
{"broken body", "image/png", data[:len(data)/2], int64(len(data) / 2), 415},
{"unsupported", "image/gif", data, int64(len(data)), 415},
{"oversize", "image/png", nil, 10485761, 413},
{"unknown length", "image/png", data, -1, 400},
{"empty", "image/png", nil, 0, 400},
} {
t.Run(tc.name, func(t *testing.T) {
r := ts.binary("POST", path, alice.token, tc.mime, tc.data, tc.length)
if r.status != tc.want {
t.Fatalf("got %d want %d: %s", r.status, tc.want, r.raw)
}
})
}
for _, name := range []string{"../x.png", `a\b.png`, "bad\nname", strings.Repeat("я", 256)} {
ts.expect(ts.binary("POST", "/api/v1/chats/"+room.ID+"/attachments?filename="+url.QueryEscape(name), alice.token, "image/png", data, int64(len(data))), 400, "invalid_request")
}
var j bytes.Buffer
if err := jpeg.Encode(&j, image.NewRGBA(image.Rect(0, 0, 5, 3)), nil); err != nil {
t.Fatal(err)
}
ts.expect(ts.binary("POST", path, bob.token, "image/jpeg", j.Bytes(), int64(j.Len())), 202, "")
ts.expect(ts.binary("POST", path, eve.token, "image/png", data, int64(len(data))), 404, "not_found")
// An interrupted read is never a 500 or a successfully published identity.
before := 0
if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM attachments`).Scan(&before); err != nil {
t.Fatal(err)
}
short := ts.binary("POST", path, alice.token, "image/png", data[:10], int64(len(data)))
if short.status != 400 && short.status != 503 {
t.Fatalf("interrupted body: %d %s", short.status, short.raw)
}
after := 0
if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM attachments`).Scan(&after); err != nil || before != after {
t.Fatalf("interruption published identity: %d -> %d (%v)", before, after, err)
}
// Complete one claimed job with known bytes. Image encoding has its own
// tests; this checks the published version, exact download and ACL on S3.
repo := pipeline.NewStore(db.Pool)
var job pipeline.Job
found, err := repo.PublishOne(ctx, func(_ context.Context, j pipeline.Job) error { job = j; return nil })
if err != nil || !found || job.AttachmentID.String() != a.ID {
t.Fatalf("outbox: %+v %v", job, err)
}
claim, err := repo.Claim(ctx, job, time.Minute)
if err != nil || claim == nil {
t.Fatal("claim", err)
}
meta := decode[attachment.Response](t, ts.do("GET", base, bob.token, nil, nil))
if meta.Status != "processing" || len(meta.Variants) != 0 {
t.Fatal("processing snapshot", meta)
}
if err := s3.Store.Copy(ctx, claim.SourceKey, attachment.BlobKey(claim.Hash)); err != nil {
t.Fatal(err)
}
bodies := map[string][]byte{"original": data}
var outputs []pipeline.Variant
for _, name := range []string{"thumbnail", "crop", "optimized"} {
body := []byte("published-" + name)
mime := "image/jpeg"
if name == "optimized" {
mime = "image/webp"
}
digest := sha256.Sum256(body)
key := pipeline.VariantKey(claim.AttachmentID, claim.PipelineVersion, name, digest[:])
if err := s3.Store.Put(ctx, key, bytes.NewReader(body), int64(len(body)), mime); err != nil {
t.Fatal(err)
}
outputs = append(outputs, pipeline.Variant{Name: name, Key: key, MIMEType: mime, Width: 1, Height: 1, SizeBytes: int64(len(body)), SHA256: digest[:]})
bodies[name] = body
}
if ok, err := repo.Complete(ctx, claim, outputs); err != nil || !ok {
t.Fatal("publish", err)
}
if err := repo.Reprocess(ctx, claim.AttachmentID, 2, func(ctx context.Context, key string) (bool, error) {
_, err := s3.Store.Stat(ctx, key)
if errors.Is(err, attachment.ErrObjectMissing) {
return false, nil
}
return err == nil, err
}); err != nil {
t.Fatal(err)
}
meta = decode[attachment.Response](t, ts.do("GET", base, bob.token, nil, nil))
if meta.Status != "ready" || len(meta.Variants) != 3 || meta.ErrorCode != nil || meta.SHA256 != a.SHA256 {
t.Fatalf("ready snapshot during reprocess: %+v", meta)
}
for _, v := range meta.Variants {
digest := sha256.Sum256(bodies[v.Name])
if v.SHA256 != hex.EncodeToString(digest[:]) || v.SizeBytes != int64(len(bodies[v.Name])) {
t.Fatal("variant checksum", v)
}
}
for name, body := range bodies {
target := base + "/content?variant=" + name
ts.expect(ts.do("GET", target, eve.token, nil, nil), 404, "not_found")
got := ts.binary("GET", target, bob.token, "", nil, 0)
ts.expect(got, 200, "")
if !bytes.Equal(got.raw, body) {
t.Fatalf("wrong published bytes for %s", name)
}
}
if err := s3.Store.Delete(ctx, outputs[0].Key); err != nil {
t.Fatal(err)
}
ts.expect(ts.do("GET", base+"/content?variant="+outputs[0].Name, bob.token, nil, nil), 404, "not_found")
if err := s3.Stop(ctx); err != nil {
t.Fatal(err)
}
ts.expect(ts.binary("POST", path, alice.token, "image/png", data, int64(len(data))), 503, "unavailable")
ts.expect(ts.do("GET", base+"/content", bob.token, nil, nil), 503, "unavailable")
ts.expect(ts.do("GET", "/health/ready", "", nil, nil), 200, "")
ts.message(alice.token, room.ID, fmt.Sprintf("text works with S3 down %d", time.Now().UnixNano()))
}