RezinaLMS-BackEnd/internal/web_server/fileurls/resolver_test.go
2026-10-19 17:59:34 +03:00

80 lines
2.1 KiB
Go

package fileurls
import (
"context"
"strings"
"testing"
"time"
)
type stubURLProvider struct {
bucket string
url string
key string
}
func (s *stubURLProvider) GetURL(_ context.Context, objectKey string, _ time.Duration) (string, error) {
s.key = objectKey
return s.url, nil
}
func (s *stubURLProvider) BucketName() string { return s.bucket }
func TestNeedsResolution(t *testing.T) {
tests := []struct {
in string
want bool
}{
{"minio://image/a.png", true},
{"image/a.png", true},
{"/static/profile_pictures/x.jpg", false},
{"https://player.vimeo.com/video/1", false},
{"https://cdn.example.com/audio.mp3", false},
{"https://minio.example.com/bucket/image/a.png?X-Amz-Algorithm=AWS4-HMAC-SHA256", true},
{"", false},
}
for _, tc := range tests {
if got := NeedsResolution(tc.in); got != tc.want {
t.Fatalf("NeedsResolution(%q) = %v, want %v", tc.in, got, tc.want)
}
}
}
func TestResolveMinIOReference(t *testing.T) {
provider := &stubURLProvider{
bucket: "rezina",
url: "https://minio.example.com/rezina/image/a.png?sig=1",
}
r := New(provider)
got := r.Resolve(context.Background(), "minio://image/a.png")
if got != provider.url {
t.Fatalf("expected presigned url, got %q", got)
}
if provider.key != "image/a.png" {
t.Fatalf("expected object key image/a.png, got %q", provider.key)
}
}
func TestResolveInJSON(t *testing.T) {
provider := &stubURLProvider{
bucket: "rezina",
url: "https://minio.example.com/rezina/image/a.png?sig=1",
}
r := New(provider)
raw := []byte(`{"data":{"profile_picture_url":"minio://image/a.png","reference":"minio://image/a.png","video_url":"https://player.vimeo.com/video/1"}}`)
out, ok := r.ResolveInJSON(context.Background(), raw)
if !ok {
t.Fatal("expected json resolution")
}
body := string(out)
if !strings.Contains(body, provider.url) {
t.Fatalf("expected resolved profile url in %s", body)
}
if !strings.Contains(body, `"reference":"minio://image/a.png"`) {
t.Fatalf("reference should stay canonical in %s", body)
}
if !strings.Contains(body, `"video_url":"https://player.vimeo.com/video/1"`) {
t.Fatalf("video_url should be unchanged in %s", body)
}
}