221 lines
6.7 KiB
Go
221 lines
6.7 KiB
Go
package attachment
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"io"
|
|
"log/slog"
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
|
|
)
|
|
|
|
func TestUploadLifecycle(t *testing.T) {
|
|
data := pngFixture(t, 2, 2)
|
|
events := make([]string, 0, 3)
|
|
repository := &repositoryStub{events: &events}
|
|
objects := &objectStub{events: &events}
|
|
service := newTestService(t, repository, objects, 1024, 4)
|
|
|
|
result, err := service.Upload(context.Background(), upload(data, "image/png"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, model.AttachmentQueued, result.Status)
|
|
require.Equal(t, []string{"pending", "put", "queued"}, events)
|
|
}
|
|
|
|
func TestUploadValidation(t *testing.T) {
|
|
data := pngFixture(t, 2, 2)
|
|
tests := []struct {
|
|
name string
|
|
contentType string
|
|
data []byte
|
|
maxBytes int64
|
|
maxPixels int64
|
|
want error
|
|
}{
|
|
{name: "spoofed MIME", contentType: "image/jpeg", data: data, maxBytes: 1024, maxPixels: 4, want: model.ErrUnsupportedMedia},
|
|
{name: "too many pixels", contentType: "image/png", data: data, maxBytes: 1024, maxPixels: 3, want: model.ErrPayloadTooLarge},
|
|
{name: "too many bytes", contentType: "image/png", data: data, maxBytes: int64(len(data) - 1), maxPixels: 4, want: model.ErrPayloadTooLarge},
|
|
{name: "damaged", contentType: "image/png", data: data[:16], maxBytes: 1024, maxPixels: 4, want: model.ErrUnsupportedMedia},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
service := newTestService(t, &repositoryStub{}, &objectStub{}, test.maxBytes, test.maxPixels)
|
|
input := upload(test.data, test.contentType)
|
|
_, err := service.Upload(context.Background(), input)
|
|
require.ErrorIs(t, err, test.want)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUploadUsesSeparateBodyAndDecodeBudgets(t *testing.T) {
|
|
data := pngFixture(t, 2, 2)
|
|
service := newTestService(t, &repositoryStub{}, &objectStub{}, 1024, 4)
|
|
require.True(t, service.bodyMemory.TryAcquire(1024))
|
|
_, err := service.Upload(context.Background(), upload(data, "image/png"))
|
|
require.ErrorIs(t, err, model.ErrCapacity)
|
|
service.bodyMemory.Release(1024)
|
|
|
|
require.True(t, service.decodeMemory.TryAcquire(32))
|
|
_, err = service.Upload(context.Background(), upload(data, "image/png"))
|
|
require.ErrorIs(t, err, model.ErrCapacity)
|
|
service.decodeMemory.Release(32)
|
|
}
|
|
|
|
func TestUploadReleasesBodyMemoryAfterOversizedDimensions(t *testing.T) {
|
|
oversized := pngFixture(t, 3, 2)
|
|
valid := pngFixture(t, 2, 2)
|
|
service := newTestService(t, &repositoryStub{}, &objectStub{}, 1024, 4)
|
|
|
|
_, err := service.Upload(context.Background(), upload(oversized, "image/png"))
|
|
require.ErrorIs(t, err, model.ErrPayloadTooLarge)
|
|
_, err = service.Upload(context.Background(), upload(valid, "image/png"))
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestNewServiceRejectsDecodeBudgetOverflow(t *testing.T) {
|
|
_, err := NewService(
|
|
slog.New(slog.NewTextHandler(io.Discard, nil)), &repositoryStub{}, &objectStub{},
|
|
Config{
|
|
MaxBytes: 1024, MaxPixels: math.MaxInt64/8 + 1, BodyMemoryBytes: 1024,
|
|
DecodeMemoryBytes: math.MaxInt64, CleanupTimeout: time.Second, PipelineVersion: 1,
|
|
},
|
|
)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestUploadCleansPendingMetadataWhenObjectWriteFails(t *testing.T) {
|
|
data := pngFixture(t, 2, 2)
|
|
repository := &repositoryStub{}
|
|
objects := &objectStub{putErr: errors.New("s3 unavailable")}
|
|
service := newTestService(t, repository, objects, 1024, 4)
|
|
|
|
_, err := service.Upload(context.Background(), upload(data, "image/png"))
|
|
require.ErrorIs(t, err, model.ErrUnavailable)
|
|
require.True(t, repository.pendingDeleted)
|
|
}
|
|
|
|
func TestUploadKeepsStagingAfterQueueFailure(t *testing.T) {
|
|
data := pngFixture(t, 2, 2)
|
|
repository := &repositoryStub{queueErr: errors.New("connection lost")}
|
|
objects := &objectStub{}
|
|
service := newTestService(t, repository, objects, 1024, 4)
|
|
|
|
_, err := service.Upload(context.Background(), upload(data, "image/png"))
|
|
require.ErrorIs(t, err, model.ErrUnavailable)
|
|
require.False(t, objects.deleted)
|
|
require.False(t, repository.pendingDeleted)
|
|
}
|
|
|
|
func newTestService(t *testing.T, repository Repository, objects ObjectStore, maxBytes, maxPixels int64) *Service {
|
|
t.Helper()
|
|
service, err := NewService(
|
|
slog.New(slog.NewTextHandler(io.Discard, nil)), repository, objects,
|
|
Config{
|
|
MaxBytes: maxBytes, MaxPixels: maxPixels, BodyMemoryBytes: maxBytes,
|
|
DecodeMemoryBytes: maxPixels * 8, CleanupTimeout: time.Second, PipelineVersion: 1,
|
|
},
|
|
)
|
|
require.NoError(t, err)
|
|
return service
|
|
}
|
|
|
|
func upload(data []byte, contentType string) Upload {
|
|
return Upload{
|
|
ChatID: "chat", OwnerID: "owner", Filename: "photo.png", ContentType: contentType,
|
|
ContentLength: int64(len(data)), Body: bytes.NewReader(data),
|
|
}
|
|
}
|
|
|
|
type repositoryStub struct {
|
|
events *[]string
|
|
queueErr error
|
|
pendingDeleted bool
|
|
pending model.Attachment
|
|
}
|
|
|
|
func (*repositoryStub) ChatForUser(context.Context, string, string) (model.Chat, error) {
|
|
return model.Chat{}, nil
|
|
}
|
|
|
|
func (s *repositoryStub) CreatePendingAttachment(_ context.Context, attachment model.Attachment) (model.Attachment, error) {
|
|
attachment.Status = model.AttachmentPending
|
|
s.pending = attachment
|
|
s.record("pending")
|
|
return attachment, nil
|
|
}
|
|
|
|
func (s *repositoryStub) QueueAttachmentWithJobAndOutbox(
|
|
context.Context,
|
|
string,
|
|
string,
|
|
model.AttachmentJob,
|
|
string,
|
|
string,
|
|
) (model.Attachment, error) {
|
|
if s.queueErr != nil {
|
|
return model.Attachment{}, s.queueErr
|
|
}
|
|
s.pending.Status = model.AttachmentQueued
|
|
s.record("queued")
|
|
return s.pending, nil
|
|
}
|
|
|
|
func (s *repositoryStub) DeletePendingAttachment(context.Context, string, string) error {
|
|
s.pendingDeleted = true
|
|
return nil
|
|
}
|
|
|
|
func (s *repositoryStub) AttachmentForUser(context.Context, string, string) (model.Attachment, error) {
|
|
return s.pending, nil
|
|
}
|
|
|
|
func (s *repositoryStub) AttachmentVariantForUser(context.Context, string, string, string) (model.Attachment, model.Variant, error) {
|
|
return s.pending, model.Variant{}, nil
|
|
}
|
|
|
|
func (s *repositoryStub) record(event string) {
|
|
if s.events != nil {
|
|
*s.events = append(*s.events, event)
|
|
}
|
|
}
|
|
|
|
type objectStub struct {
|
|
events *[]string
|
|
putErr error
|
|
deleted bool
|
|
}
|
|
|
|
func (s *objectStub) Put(context.Context, string, string, []byte) error {
|
|
if s.events != nil {
|
|
*s.events = append(*s.events, "put")
|
|
}
|
|
return s.putErr
|
|
}
|
|
|
|
func (*objectStub) Get(context.Context, string) (io.ReadCloser, error) {
|
|
return io.NopCloser(bytes.NewReader(nil)), nil
|
|
}
|
|
|
|
func (s *objectStub) Delete(context.Context, string) error {
|
|
s.deleted = true
|
|
return nil
|
|
}
|
|
|
|
func pngFixture(t *testing.T, width, height int) []byte {
|
|
t.Helper()
|
|
var data bytes.Buffer
|
|
value := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
value.Set(0, 0, color.White)
|
|
require.NoError(t, png.Encode(&data, value))
|
|
return data.Bytes()
|
|
}
|