test(19.3-05): build tags, S3 tag guard, DiskExtension and public_url

- manifest_tags_test.go: build.tags load, trim, dedupe and validation
  errors naming build.tags; SaveManifest keeps tags and plugin order;
  goBuildArgs puts one -tags before -o
- s3_test.go: s3:// refused without summercms_s3 naming the tag, opens
  with it (both build modes, any scheme case)
- diskext_test.go: RESEARCH probe rows and table consistency
- url_test.go: public_url base joining, File.URL and thumbnails,
  malformed values refused naming the key
- store_test.go: Postgres regression table through Store
- gate: attach names in both tag modes, Store regression in docker
This commit is contained in:
Jakub Zych
2026-10-15 19:37:35 +02:00
parent 055f51c94f
commit 38f6d65c09
6 changed files with 573 additions and 0 deletions

View File

@@ -362,3 +362,107 @@ func TestBucketAndURLEdges(t *testing.T) {
}
}
}
// TestPublicURLBase (plan 05 of phase 19.3, D-12) builds direct URLs from
// storage.uploads.public_url: trailing slashes on the base and leading
// slashes on the key collapse to one, File.URL and thumbnails use the base,
// and clearing it restores the proxy prefix.
func TestPublicURLBase(t *testing.T) {
t.Cleanup(func() {
setPublicPathPrefix(defaultPublicPathPrefix)
setPublicURLBase("")
})
key := BlobKey("651a2b3c4d5e61234567.png")
for _, tc := range []struct{ base, key, want string }{
{"https://cdn.example.com", key, "https://cdn.example.com/" + key},
{"https://cdn.example.com/", key, "https://cdn.example.com/" + key},
{"https://cdn.example.com/uploads//", "/" + key, "https://cdn.example.com/uploads/" + key},
{"http://bucket.s3.example.com/a/b", "//" + key, "http://bucket.s3.example.com/a/b/" + key},
} {
setPublicURLBase(tc.base)
if got := PublicURL(tc.key); got != tc.want {
t.Errorf("base %q key %q: PublicURL = %q, want %q", tc.base, tc.key, got, tc.want)
}
}
bucket := memblob.OpenBucket(nil)
t.Cleanup(func() { _ = bucket.Close() })
setPublicURLBase("https://cdn.example.com/uploads/")
if PublicURLBase() != "https://cdn.example.com/uploads/" {
t.Fatalf("PublicURLBase = %q", PublicURLBase())
}
f := &File{ID: 31, DiskName: "651a2b3c4d5e61234567.png"}
if got, want := f.URL(), "https://cdn.example.com/uploads/651/a2b/3c4/651a2b3c4d5e61234567.png"; got != want {
t.Fatalf("File.URL = %q, want %q", got, want)
}
if err := bucket.WriteAll(t.Context(), BlobKey(f.DiskName), testJPEG(t), nil); err != nil {
t.Fatal(err)
}
thumb, err := f.Thumb(t.Context(), bucket, 40, 30, "crop")
if err != nil {
t.Fatal(err)
}
if want := "https://cdn.example.com/uploads/651/a2b/3c4/thumb_31_40_30_0_0_crop.png"; thumb != want {
t.Fatalf("thumb = %q, want %q", thumb, want)
}
setPublicURLBase("")
if got := f.URL(); got != "/storage/uploads/651/a2b/3c4/651a2b3c4d5e61234567.png" {
t.Fatalf("without a base File.URL = %q", got)
}
}
// TestPublicURLValidation refuses every malformed storage.uploads.public_url
// at OpenBucket, naming the key and never echoing credentials; an unset or
// blank value keeps the proxy prefix.
func TestPublicURLValidation(t *testing.T) {
t.Cleanup(func() {
setPublicPathPrefix(defaultPublicPathPrefix)
setPublicURLBase("")
})
for _, bad := range []string{
"ftp://cdn.example.com/uploads",
"file:///srv/uploads",
"https://user:secret@cdn.example.com",
"https://token@cdn.example.com",
"https://cdn.example.com/uploads?v=1",
"https://cdn.example.com/uploads?",
"https://cdn.example.com/uploads#top",
"/storage/uploads",
"cdn.example.com/uploads",
"https:///uploads",
"https://cdn.example.com:bad/",
} {
setPublicURLBase("https://stale.example.com")
cfg := openTestConfig(t, "uploads:\n bucket_url: \"mem://\"\n public_url: \""+bad+"\"\n")
bucket, err := OpenBucket(t.Context(), cfg)
if err == nil {
_ = bucket.Close()
t.Fatalf("public_url %q accepted", bad)
}
if !strings.Contains(err.Error(), "storage.uploads.public_url") || strings.Contains(err.Error(), "secret") || strings.Contains(err.Error(), "token@") {
t.Fatalf("public_url %q: error %q", bad, err)
}
if _, perr := parsePublicURL(bad); perr == nil {
t.Fatalf("parsePublicURL(%q) accepted", bad)
}
}
for _, unset := range []string{"", " "} {
setPublicURLBase("https://stale.example.com")
cfg := openTestConfig(t, "uploads:\n bucket_url: \"mem://\"\n public_url: \""+unset+"\"\n")
bucket, err := OpenBucket(t.Context(), cfg)
if err != nil {
t.Fatalf("public_url %q: %v", unset, err)
}
_ = bucket.Close()
if PublicURLBase() != "" || PublicURL("a/b.png") != "/storage/uploads/a/b.png" {
t.Fatalf("public_url %q: base %q URL %q", unset, PublicURLBase(), PublicURL("a/b.png"))
}
}
for _, ok := range []string{"https://cdn.example.com", "HTTP://cdn.example.com:8080/u/", " https://cdn.example.com/uploads "} {
u, err := parsePublicURL(ok)
if err != nil || u == nil {
t.Fatalf("parsePublicURL(%q) = %v, %v", ok, u, err)
}
}
}