Files
RenewCMS/test/image_upload_rules_test.go
T

111 lines
3.3 KiB
Go

package test
import (
imageDomain "RenewCMS/internal/domain/image"
"RenewCMS/internal/infrastructure/useCases"
"errors"
"testing"
)
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"} {
t.Run(contentType, func(t *testing.T) {
repo := newMockImageRepo()
createUc := useCases.NewCreateImageUseCase(repo)
result, err := createUc.CreateImage(imageDomain.ImageInput{
ContentType: contentType,
Size: imageDomain.MaxImageSize,
})
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result.ID == 0 {
t.Errorf("expected the created image, got zero value")
}
if len(repo.images) != 1 {
t.Errorf("expected Create to be called once, got %d calls", len(repo.images))
}
})
}
})
t.Run("CreateImage rejects a disallowed content type before reaching the repository", func(t *testing.T) {
repo := newMockImageRepo()
createUc := useCases.NewCreateImageUseCase(repo)
_, err := createUc.CreateImage(imageDomain.ImageInput{
ContentType: "application/pdf",
Size: 1024,
})
if !errors.Is(err, imageDomain.ErrImageTypeNotAllowed) {
t.Fatalf("expected ErrImageTypeNotAllowed, got %v", err)
}
if len(repo.images) != 0 {
t.Errorf("expected Create to never be called, got %d calls", len(repo.images))
}
})
t.Run("CreateImage rejects a file that exceeds the size limit", func(t *testing.T) {
repo := newMockImageRepo()
createUc := useCases.NewCreateImageUseCase(repo)
_, err := createUc.CreateImage(imageDomain.ImageInput{
ContentType: "image/png",
Size: imageDomain.MaxImageSize + 1,
})
if !errors.Is(err, imageDomain.ErrImageTooLarge) {
t.Fatalf("expected ErrImageTooLarge, got %v", err)
}
if len(repo.images) != 0 {
t.Errorf("expected Create to never be called, got %d calls", len(repo.images))
}
})
t.Run("A file exactly at the size limit is accepted, one byte over is not", func(t *testing.T) {
repo := newMockImageRepo()
createUc := useCases.NewCreateImageUseCase(repo)
_, err := createUc.CreateImage(imageDomain.ImageInput{
ContentType: "image/png",
Size: imageDomain.MaxImageSize,
})
if err != nil {
t.Fatalf("expected no error at the exact limit, got %v", err)
}
if len(repo.images) != 1 {
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,
})
if !errors.Is(err, imageDomain.ErrImageTooLarge) {
t.Fatalf("expected ErrImageTooLarge, got %v", err)
}
if len(repo.images) != 1 {
t.Errorf("expected Create to still be called exactly once in total, got %d calls", len(repo.images))
}
})
t.Run("A repository failure unrelated to type or size still propagates unchanged", func(t *testing.T) {
repo := newMockImageRepo()
repo.fail.failNextWith("Create", errRepositoryFailure)
createUc := useCases.NewCreateImageUseCase(repo)
_, err := createUc.CreateImage(imageDomain.ImageInput{
ContentType: "image/png",
Size: 1024,
})
if !errors.Is(err, errRepositoryFailure) {
t.Fatalf("expected the generic repository error, got %v", err)
}
})
}