feat(API): media library
This commit is contained in:
1 parent
95ae043d3e
commit
0d99544579
65 files changed
+3992
-1530
No files matched your search
@@ -3,10 +3,37 @@ package test
|
||||
import (
|
||||
imageDomain "RenewCMS/internal/domain/image"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"bytes"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func validBytesFor(t *testing.T, contentType string) []byte {
|
||||
t.Helper()
|
||||
switch contentType {
|
||||
case "image/png":
|
||||
return pngBytes(t, 3, 2)
|
||||
case "image/jpeg":
|
||||
return jpegBytes(t, 3, 2)
|
||||
case "image/webp":
|
||||
return webpBytes(t)
|
||||
default:
|
||||
return []byte(svgFixture)
|
||||
}
|
||||
}
|
||||
|
||||
func paddedTo(data []byte, size int) []byte {
|
||||
padding := byte(0)
|
||||
if bytes.HasPrefix(data, []byte("<svg")) {
|
||||
padding = ' '
|
||||
}
|
||||
return append(append([]byte{}, data...), bytes.Repeat([]byte{padding}, size-len(data))...)
|
||||
}
|
||||
|
||||
func inputOf(content []byte, contentType string) imageDomain.ImageInput {
|
||||
return imageDomain.ImageInput{Content: bytes.NewReader(content), Filename: "upload", ContentType: contentType}
|
||||
}
|
||||
|
||||
func TestImageUploadRules(t *testing.T) {
|
||||
t.Run("CreateImage accepts every allowed content type within the size limit", func(t *testing.T) {
|
||||
for _, contentType := range []string{"image/png", "image/jpeg", "image/webp", "image/svg+xml"} {
|
||||
@@ -14,10 +41,7 @@ func TestImageUploadRules(t *testing.T) {
|
||||
repo := newMockImageRepo()
|
||||
createUc := useCases.NewCreateImageUseCase(repo)
|
||||
|
||||
result, err := createUc.CreateImage(imageDomain.ImageInput{
|
||||
ContentType: contentType,
|
||||
Size: imageDomain.MaxImageSize,
|
||||
})
|
||||
result, err := createUc.CreateImage(inputOf(paddedTo(validBytesFor(t, contentType), int(imageDomain.MaxImageSize)), contentType))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
@@ -36,10 +60,7 @@ func TestImageUploadRules(t *testing.T) {
|
||||
repo := newMockImageRepo()
|
||||
createUc := useCases.NewCreateImageUseCase(repo)
|
||||
|
||||
_, err := createUc.CreateImage(imageDomain.ImageInput{
|
||||
ContentType: "application/pdf",
|
||||
Size: 1024,
|
||||
})
|
||||
_, err := createUc.CreateImage(inputOf([]byte("%PDF"), "application/pdf"))
|
||||
|
||||
if !errors.Is(err, imageDomain.ErrImageTypeNotAllowed) {
|
||||
t.Fatalf("expected ErrImageTypeNotAllowed, got %v", err)
|
||||
@@ -53,10 +74,7 @@ func TestImageUploadRules(t *testing.T) {
|
||||
repo := newMockImageRepo()
|
||||
createUc := useCases.NewCreateImageUseCase(repo)
|
||||
|
||||
_, err := createUc.CreateImage(imageDomain.ImageInput{
|
||||
ContentType: "image/png",
|
||||
Size: imageDomain.MaxImageSize + 1,
|
||||
})
|
||||
_, err := createUc.CreateImage(inputOf(paddedTo(pngBytes(t, 3, 2), int(imageDomain.MaxImageSize)+1), "image/png"))
|
||||
|
||||
if !errors.Is(err, imageDomain.ErrImageTooLarge) {
|
||||
t.Fatalf("expected ErrImageTooLarge, got %v", err)
|
||||
@@ -70,10 +88,7 @@ func TestImageUploadRules(t *testing.T) {
|
||||
repo := newMockImageRepo()
|
||||
createUc := useCases.NewCreateImageUseCase(repo)
|
||||
|
||||
_, err := createUc.CreateImage(imageDomain.ImageInput{
|
||||
ContentType: "image/png",
|
||||
Size: imageDomain.MaxImageSize,
|
||||
})
|
||||
_, err := createUc.CreateImage(inputOf(paddedTo(pngBytes(t, 3, 2), int(imageDomain.MaxImageSize)), "image/png"))
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error at the exact limit, got %v", err)
|
||||
}
|
||||
@@ -81,10 +96,7 @@ func TestImageUploadRules(t *testing.T) {
|
||||
t.Fatalf("expected Create to be called once, got %d calls", len(repo.images))
|
||||
}
|
||||
|
||||
_, err = createUc.CreateImage(imageDomain.ImageInput{
|
||||
ContentType: "image/png",
|
||||
Size: imageDomain.MaxImageSize + 1,
|
||||
})
|
||||
_, err = createUc.CreateImage(inputOf(paddedTo(pngBytes(t, 3, 2), int(imageDomain.MaxImageSize)+1), "image/png"))
|
||||
if !errors.Is(err, imageDomain.ErrImageTooLarge) {
|
||||
t.Fatalf("expected ErrImageTooLarge, got %v", err)
|
||||
}
|
||||
@@ -98,10 +110,7 @@ func TestImageUploadRules(t *testing.T) {
|
||||
repo.fail.failNextWith("Create", errRepositoryFailure)
|
||||
createUc := useCases.NewCreateImageUseCase(repo)
|
||||
|
||||
_, err := createUc.CreateImage(imageDomain.ImageInput{
|
||||
ContentType: "image/png",
|
||||
Size: 1024,
|
||||
})
|
||||
_, err := createUc.CreateImage(inputOf(pngBytes(t, 3, 2), "image/png"))
|
||||
|
||||
if !errors.Is(err, errRepositoryFailure) {
|
||||
t.Fatalf("expected the generic repository error, got %v", err)
|
||||
|
||||
Reference in new issue
Block a user