43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
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.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)
|
|
}
|
|
})
|
|
}
|
|
}
|