refac: allowed image types and the size limit are a domain rule checked by the use case, not by the persistence layer

This commit is contained in:
fsylvain committed 2026-09-24 00:03:29 +02:00
1 parent 8a8a24434a
commit 5992c8598b
7 files changed
+249 -17

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
package test
import (
imageDomain "RenewCMS/internal/domain/image"
"RenewCMS/internal/infrastructure/persistence/models"
persistence "RenewCMS/internal/infrastructure/persistence/repositories"
"path/filepath"
"reflect"
"strings"
"testing"
)
func TestImageRepositoryHasAnExtensionForEveryAllowedContentType(t *testing.T) {
db := openInMemoryDb(t)
if err := db.AutoMigrate(&models.Image{}); err != nil {
t.Fatalf("failed to migrate: %v", err)
}
repo := persistence.NewImageRepository(db)
t.Setenv("UPLOAD_DIR", t.TempDir())
allowedContentTypes := imageDomain.AllowedContentTypes()
expectedContentTypes := []string{"image/jpeg", "image/png", "image/svg+xml", "image/webp"}
if !reflect.DeepEqual(allowedContentTypes, expectedContentTypes) {
t.Fatalf("expected AllowedContentTypes() to be %v, got %v", expectedContentTypes, allowedContentTypes)
}
for _, contentType := range allowedContentTypes {
t.Run(contentType, func(t *testing.T) {
result, err := repo.Create(imageDomain.ImageInput{
Content: strings.NewReader("fake-image-data"),
Filename: "upload",
ContentType: contentType,
})
if err != nil {
t.Fatalf("Create failed: %v", err)
}
extension := filepath.Ext(result.Path)
if extension == "" {
t.Errorf("expected a non-empty extension for %q, got path %q", contentType, result.Path)
}
})
}
}