mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-22 02:27:57 +00:00
Each implementation keeps its provider client in an interface{} field that
Setup assigns, so an unconfigured store holds nil - and asserting nil to
the provider's client type panics:
panic: interface conversion: interface {} is nil, not *oss.Client
The upload endpoint reaches that path for any request naming a provider
the deployment never configured.
Three more things were wrong in the same files. OXS.Setup printed a
failure and returned the store anyway, handing back exactly the broken
object that panics. HuaWeiOBS.UpLoad printed the provider's error and
returned nil, so a failed upload reported success. Both it and
QiNiuKODO.UpLoad asserted the local path was a string without checking.
The tests asked the reader to paste their own credentials, so they failed
for everyone who did not. They now cover the guards and skip the part
that needs a provider unless credentials are in the environment.
71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package file_store
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// The three implementations keep their provider client in an interface{} field
|
|
// that Setup assigns, so an unconfigured store holds nil. Asserting nil to the
|
|
// provider's type panics, and the upload endpoint reaches that path whenever a
|
|
// request names a provider the deployment never configured.
|
|
func TestUnconfiguredStoresReportItInsteadOfPanicking(t *testing.T) {
|
|
stores := map[DriverType]FileStoreType{
|
|
AliYunOSS: new(ALiYunOSS),
|
|
HuaweiOBS: new(HuaWeiOBS),
|
|
QiNiuKodo: new(QiNiuKODO),
|
|
}
|
|
|
|
for driver, store := range stores {
|
|
t.Run(string(driver), func(t *testing.T) {
|
|
err := store.UpLoad("img/x.png", "/tmp/x.png")
|
|
if err == nil {
|
|
t.Fatal("upload on an unconfigured store returned no error")
|
|
}
|
|
var notCfg *ErrNotConfigured
|
|
if !errors.As(err, ¬Cfg) {
|
|
t.Fatalf("want ErrNotConfigured, got %v", err)
|
|
}
|
|
if notCfg.Driver != driver {
|
|
t.Errorf("error names %s, want %s", notCfg.Driver, driver)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUnconfiguredTokenReportsItToo(t *testing.T) {
|
|
if _, err := new(QiNiuKODO).GetTempToken(); err == nil {
|
|
t.Fatal("token from an unconfigured store returned no error")
|
|
}
|
|
}
|
|
|
|
func TestSetupRejectsAnUnknownDriver(t *testing.T) {
|
|
if _, err := (&OXS{}).Setup("NoSuchCloud"); err == nil {
|
|
t.Fatal("unknown driver was accepted")
|
|
}
|
|
}
|
|
|
|
// Setup reaching the provider needs credentials, so it runs only when they are
|
|
// supplied. Previously the test carried a comment telling the reader to paste
|
|
// their own, which meant it failed for everyone who did not.
|
|
func TestSetupWithRealCredentials(t *testing.T) {
|
|
endpoint := os.Getenv("GOADMIN_OSS_ENDPOINT")
|
|
if endpoint == "" {
|
|
t.Skip("set GOADMIN_OSS_ENDPOINT, GOADMIN_OSS_AK, GOADMIN_OSS_SK, GOADMIN_OSS_BUCKET to run")
|
|
}
|
|
oxs := OXS{
|
|
Endpoint: endpoint,
|
|
AccessKeyID: os.Getenv("GOADMIN_OSS_AK"),
|
|
AccessKeySecret: os.Getenv("GOADMIN_OSS_SK"),
|
|
BucketName: os.Getenv("GOADMIN_OSS_BUCKET"),
|
|
}
|
|
store, err := oxs.Setup(AliYunOSS)
|
|
if err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
if store == nil {
|
|
t.Fatal("setup returned no store and no error")
|
|
}
|
|
}
|