Files
hfb_sys/backend/internal/modules/file/storage_test.go
T

79 lines
2.3 KiB
Go

package file
import (
"strings"
"testing"
"github.com/minio/minio-go/v7"
)
func TestSanitizeObjectMetadataEscapesNonASCII(t *testing.T) {
got := sanitizeObjectMetadata(map[string]string{
"original-filename": "游戏ID截图 1.png",
})
if got["original-filename"] != "%E6%B8%B8%E6%88%8FID%E6%88%AA%E5%9B%BE+1.png" {
t.Fatalf("metadata filename = %q", got["original-filename"])
}
}
func TestParseBucketLookup(t *testing.T) {
tests := []struct {
name string
raw string
want minio.BucketLookupType
}{
{name: "auto", raw: "auto", want: minio.BucketLookupAuto},
{name: "dns", raw: "dns", want: minio.BucketLookupDNS},
{name: "path", raw: "path", want: minio.BucketLookupPath},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseBucketLookup(tt.raw)
if err != nil || got != tt.want {
t.Fatalf("parseBucketLookup(%q) = %v, %v", tt.raw, got, err)
}
})
}
if _, err := parseBucketLookup("invalid"); err == nil {
t.Fatal("invalid bucket lookup should fail")
}
}
func TestAnnouncementSceneUsesPublicFileURL(t *testing.T) {
key := "announcement/2026/06/15/example.webp"
got := fileURLForScene("announcement", key)
if !strings.HasPrefix(got, "/api/public/files/object?") {
t.Fatalf("announcement file url = %q, want public file endpoint", got)
}
}
func TestNormalizeSceneAllowsAnnouncement(t *testing.T) {
if got := normalizeScene("announcement"); got != "announcement" {
t.Fatalf("normalize announcement scene = %q", got)
}
}
func TestAWRecycleSceneUsesPublicFileURL(t *testing.T) {
if got := normalizeScene("aw-recycle"); got != "aw-recycle" {
t.Fatalf("normalize aw-recycle scene = %q", got)
}
key := "aw-recycle/2026/07/19/example.webp"
got := fileURLForScene("aw-recycle", key)
if !strings.HasPrefix(got, "/api/public/files/object?") {
t.Fatalf("aw-recycle file url = %q, want public file endpoint", got)
}
}
func TestCooperationFeedbackSceneUsesPublicFileURL(t *testing.T) {
if got := normalizeScene("cooperation-feedback"); got != "cooperation-feedback" {
t.Fatalf("normalize cooperation-feedback scene = %q", got)
}
key := "cooperation-feedback/2026/07/19/example.webp"
got := fileURLForScene("cooperation-feedback", key)
if !strings.HasPrefix(got, "/api/public/files/object?") {
t.Fatalf("cooperation-feedback file url = %q, want public file endpoint", got)
}
}