forked from mtusi2026/mtusi2026lab4
feat: add minio
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAttachmentImageValidation(t *testing.T) {
|
||||
img := image.NewRGBA(image.Rect(0, 0, 3, 2))
|
||||
var pngData, jpegData bytes.Buffer
|
||||
if err := png.Encode(&pngData, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := jpeg.Encode(&jpegData, img, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{pngData.Bytes(), "image/png"}, {jpegData.Bytes(), "image/jpeg"}} {
|
||||
if actual, err := services.ValidateImage(sample.data, sample.mime); err != nil || actual != sample.mime {
|
||||
t.Fatalf("valid image: %s %v", actual, err)
|
||||
}
|
||||
}
|
||||
huge := append([]byte(nil), pngData.Bytes()...)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 10001)
|
||||
binary.BigEndian.PutUint32(huge[20:24], 4000)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
if _, err := services.ValidateImage(huge, "image/png"); !errors.Is(err, services.ErrPayloadTooLarge) {
|
||||
t.Fatalf("pixel limit: %v", err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{pngData.Bytes(), "image/jpeg"}, {jpegData.Bytes(), "image/png"}, {pngData.Bytes()[:40], "image/png"}, {[]byte("not an image"), "image/png"}, {pngData.Bytes(), "text/plain"}} {
|
||||
if _, err := services.ValidateImage(sample.data, sample.mime); !errors.Is(err, services.ErrUnsupportedMedia) {
|
||||
t.Fatalf("invalid image accepted: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentFilenameValidation(t *testing.T) {
|
||||
for _, name := range []string{"", " ", "../photo.png", `a\b.png`, "photo\x00.png", "photo\n.png"} {
|
||||
if services.ValidateFilename(name) == nil {
|
||||
t.Fatalf("accepted %q", name)
|
||||
}
|
||||
}
|
||||
if err := services.ValidateFilename("Фотография 1.png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentMessageRequiresStringText(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Images", []string{user.ID})
|
||||
id := uuid.NewString()
|
||||
// Repository-level fixture: message validation does not fetch S3 bytes.
|
||||
err := repositories.NewAttachmentStore(f.db).Create(context.Background(), repositories.Attachment{
|
||||
ID: id, ChatID: chat.ID, OwnerID: user.ID, Filename: "photo.png", ObjectKey: "test/" + id,
|
||||
ContentType: "image/png", SizeBytes: 1, SHA256: strings.Repeat("a", 64), CreatedAt: time.Now().UTC(), UpdatedAt: time.Now().UTC(),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, body := range []string{`{"attachment_ids":["` + id + `"]}`, `{"text":null,"attachment_ids":["` + id + `"]}`} {
|
||||
r := httptest.NewRequest(http.MethodPost, "/api/v1/chats/"+chat.ID+"/messages", strings.NewReader(body))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
r.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
authorizeTestRequest(t, f, r, user.ID)
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 0 {
|
||||
t.Fatal("invalid request saved a message")
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package tests
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -44,7 +45,7 @@ func TestIdempotencyConcurrentStoresAndScopes(t *testing.T) {
|
||||
for message := range results {
|
||||
if original.ID == "" {
|
||||
original = message
|
||||
} else if message != original {
|
||||
} else if !reflect.DeepEqual(message, original) {
|
||||
t.Fatalf("replay changed the message: %v != %v", message, original)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +85,13 @@ func newTestFixture(t *testing.T) *testFixture {
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000004_attachments.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = redisClient.Close() })
|
||||
if err := redisClient.Ping(ctx).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user