test(backend): setupAPI points BooksDir at os.TempDir and resets shared loginrl key
This commit is contained in:
@@ -7,8 +7,11 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
|
||||||
"booklib/internal/auth"
|
"booklib/internal/auth"
|
||||||
"booklib/internal/db"
|
"booklib/internal/db"
|
||||||
"booklib/internal/redispkg"
|
"booklib/internal/redispkg"
|
||||||
@@ -38,7 +41,16 @@ func setupAPI(t *testing.T) (*store.Store, http.Handler) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
cfg := testCfg()
|
cfg := testCfg()
|
||||||
|
cfg.BooksDir = filepath.Clean(os.TempDir())
|
||||||
|
cfg.CacheDir = t.TempDir()
|
||||||
r := NewRouter(cfg, st, redispkg.New(os.Getenv("REDIS_URL")))
|
r := NewRouter(cfg, st, redispkg.New(os.Getenv("REDIS_URL")))
|
||||||
|
if u := os.Getenv("REDIS_URL"); u != "" { // 测试卫生: 共享 redis 上重置登录限流桶, 防跨测试累计 429
|
||||||
|
if opt, e := redis.ParseURL(u); e == nil {
|
||||||
|
rc := redis.NewClient(opt)
|
||||||
|
rc.Del(ctx, "loginrl:192.0.2.1")
|
||||||
|
rc.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
return st, r
|
return st, r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user