- Pin override precedence, fallback, partials, samples, draft validation and branding - Postgres TestStore/TestSync for index sync, drift hashes and reset - Pact row-state and mail capability method-set pins; attach AbsolutePublicURL cases Co-authored-by: Cursor <cursoragent@cursor.com>
86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package attach
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/compass"
|
|
)
|
|
|
|
func absoluteCfg(t *testing.T, files map[string]string) *compass.Config {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
for name, body := range files {
|
|
path := filepath.Join(dir, name)
|
|
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
cfg, err := compass.Load(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
func TestAbsolutePublicURL(t *testing.T) {
|
|
t.Parallel()
|
|
const key = "a/b.png"
|
|
|
|
t.Run("public_url", func(t *testing.T) {
|
|
got, err := AbsolutePublicURL(absoluteCfg(t, map[string]string{
|
|
"storage.yaml": "uploads:\n public_url: https://cdn.example.test/files\n",
|
|
}), key)
|
|
if err != nil || got != "https://cdn.example.test/files/"+key {
|
|
t.Fatalf("got %q err=%v", got, err)
|
|
}
|
|
})
|
|
|
|
t.Run("public_url_trailing_slash", func(t *testing.T) {
|
|
got, err := AbsolutePublicURL(absoluteCfg(t, map[string]string{
|
|
"storage.yaml": "uploads:\n public_url: https://cdn.example.test/files/\n",
|
|
}), "/"+key)
|
|
if err != nil || got != "https://cdn.example.test/files/"+key {
|
|
t.Fatalf("got %q err=%v", got, err)
|
|
}
|
|
})
|
|
|
|
t.Run("app_url_default_prefix", func(t *testing.T) {
|
|
got, err := AbsolutePublicURL(absoluteCfg(t, map[string]string{
|
|
"app.yaml": "url: https://app.example.test\n",
|
|
}), key)
|
|
if err != nil || got != "https://app.example.test/storage/uploads/"+key {
|
|
t.Fatalf("got %q err=%v", got, err)
|
|
}
|
|
})
|
|
|
|
t.Run("app_url_custom_prefix", func(t *testing.T) {
|
|
got, err := AbsolutePublicURL(absoluteCfg(t, map[string]string{
|
|
"app.yaml": "url: https://app.example.test/site/\n",
|
|
"storage.yaml": "uploads:\n public_path_prefix: /media\n",
|
|
}), key)
|
|
if err != nil || got != "https://app.example.test/site/media/"+key {
|
|
t.Fatalf("got %q err=%v", got, err)
|
|
}
|
|
})
|
|
|
|
t.Run("neither", func(t *testing.T) {
|
|
_, err := AbsolutePublicURL(absoluteCfg(t, map[string]string{"mail.yaml": "driver: memory\n"}), key)
|
|
if !errors.Is(err, ErrNoAbsoluteURL) {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("public_url_with_query_refused", func(t *testing.T) {
|
|
_, err := AbsolutePublicURL(absoluteCfg(t, map[string]string{
|
|
"storage.yaml": "uploads:\n public_url: https://cdn.example.test/files?x=1\n",
|
|
"app.yaml": "url: https://app.example.test\n",
|
|
}), key)
|
|
if err == nil || errors.Is(err, ErrNoAbsoluteURL) {
|
|
t.Fatalf("err = %v, want parse error for query", err)
|
|
}
|
|
})
|
|
}
|