Files
go-admin/app/other/apis/upload_test.go
T
zhangwenjian 04c6a081ae fix🐛: source=3 uploaded to aliyun, and neither provider was ever configured
thirdUpload dispatched on the source parameter and then built the same
zero-value ALiYunOSS in both branches, so source=3 could not have
reached qiniu even with credentials.

Neither branch had credentials to use. OXS.Setup is the initialisation
path and nothing in the repository called it, and no configuration field
existed to fill. The store is now taken from extend.fileStore, and a
provider that was not configured says so rather than producing the
provider's own complaint about an empty bucket name.

The two handlers passed errors.New("") to e.Error, discarding what
actually went wrong; they now pass the error.
2026-08-24 13:25:54 +08:00

39 lines
1.2 KiB
Go

package apis
import (
"strings"
"testing"
"go-admin/config"
)
// Both branches used to construct a zero-value ALiYunOSS and call UpLoad on it,
// which panicked; the qiniu branch built the aliyun client, so source=3 could
// not have reached qiniu even with credentials. Unconfigured now reports which
// store is missing.
func TestThirdUploadReportsAnUnconfiguredStore(t *testing.T) {
previous := config.ExtConfig.FileStore
config.ExtConfig.FileStore = config.FileStore{}
t.Cleanup(func() { config.ExtConfig.FileStore = previous })
for source, want := range map[string]string{"2": "AliYunOSS", "3": "QiNiuKodo"} {
err := thirdUpload(source, "x.png", "/tmp/x.png")
if err == nil {
t.Errorf("source=%s: no error from an unconfigured store", source)
continue
}
if !strings.Contains(err.Error(), want) {
t.Errorf("source=%s: error names %q, want it to mention %s", source, err, want)
}
}
}
// source 1 and anything unrecognised keep the local copy and do nothing else.
func TestThirdUploadIgnoresLocalAndUnknownSources(t *testing.T) {
for _, source := range []string{"", "1", "9"} {
if err := thirdUpload(source, "x.png", "/tmp/x.png"); err != nil {
t.Errorf("source=%q returned %v, want nil", source, err)
}
}
}