Доработайте публикацию и сверку вложений по итогам ревью

This commit is contained in:
2026-11-18 11:43:27 +03:00
parent 1f06d9f4ab
commit 80c4a56200
27 changed files with 605 additions and 275 deletions
+47 -25
View File
@@ -30,32 +30,54 @@ func TestAttachmentUploadAuthenticatesBeforeParsing(t *testing.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 TestAttachmentMetadataUsesOrdinaryRequestDeadline(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: 40 * time.Millisecond})
var held []*pgxpool.Conn
for range db.Pool.Config().MaxConns {
conn, err := db.Pool.Acquire(context.Background())
if err != nil {
t.Fatal(err)
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
}
held = append(held, conn)
}
defer func() {
for _, conn := range held {
conn.Release()
}
}()
ctx, cancel := context.WithTimeout(context.Background(), 400*time.Millisecond)
defer cancel()
req := httptest.NewRequest("GET", "/api/v1/attachments/11111111-1111-4111-8111-111111111111", nil).WithContext(ctx)
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("metadata exceeded ordinary budget: status%d elapsed%v body%s", rec.Code, time.Since(started), rec.Body.String())
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())
}
})
}
}