forked from mtusi2026/mtusi2026lab4
refactor: isolate attachment lifecycle
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"io"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab4/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.AttachmentReady, result.Status)
|
||||
require.Equal(t, []string{"pending", "put", "ready"}, 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 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 TestUploadReturnsCommittedMetadataAfterAmbiguousReadyUpdate(t *testing.T) {
|
||||
data := pngFixture(t, 2, 2)
|
||||
repository := &repositoryStub{markErr: errors.New("connection lost"), lookupReady: true}
|
||||
objects := &objectStub{}
|
||||
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.AttachmentReady, result.Status)
|
||||
require.False(t, objects.deleted)
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
)
|
||||
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
|
||||
markErr error
|
||||
lookupReady bool
|
||||
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) MarkAttachmentReady(context.Context, string, string) (model.Attachment, error) {
|
||||
if s.markErr != nil {
|
||||
return model.Attachment{}, s.markErr
|
||||
}
|
||||
s.pending.Status = model.AttachmentReady
|
||||
s.record("ready")
|
||||
return s.pending, nil
|
||||
}
|
||||
|
||||
func (s *repositoryStub) DeletePendingAttachment(context.Context, string, string) error {
|
||||
s.pendingDeleted = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *repositoryStub) AttachmentByStorage(context.Context, string, string) (model.Attachment, error) {
|
||||
if s.lookupReady {
|
||||
s.pending.Status = model.AttachmentReady
|
||||
return s.pending, nil
|
||||
}
|
||||
return model.Attachment{}, model.ErrNotFound
|
||||
}
|
||||
|
||||
func (s *repositoryStub) AttachmentForUser(context.Context, string, string) (model.Attachment, error) {
|
||||
return s.pending, 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()
|
||||
}
|
||||
Reference in New Issue
Block a user