test(web/config): dedupe web search API key preservation tests
This commit is contained in:
parent
60499def99
commit
935d5f8660
1 changed files with 34 additions and 86 deletions
|
|
@ -143,74 +143,64 @@ func TestHandlePatchConfig_AllowsInvalidExecRegexPatternsWhenExecDisabled(t *tes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandlePatchConfig_PreservesWebSearchAPIKeysWhenOmitted(t *testing.T) {
|
func runConfigRequest(t *testing.T, configPath, method, body string) {
|
||||||
configPath, cleanup := setupOAuthTestEnv(t)
|
t.Helper()
|
||||||
defer cleanup()
|
|
||||||
seedWebSearchAPIKeys(t, configPath, "glm-existing-key", "baidu-existing-key")
|
|
||||||
|
|
||||||
h := NewHandler(configPath)
|
h := NewHandler(configPath)
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
h.RegisterRoutes(mux)
|
h.RegisterRoutes(mux)
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodPatch, "/api/config", bytes.NewBufferString(`{
|
req := httptest.NewRequest(method, "/api/config", bytes.NewBufferString(body))
|
||||||
"gateway": {
|
|
||||||
"log_level": "info"
|
|
||||||
}
|
|
||||||
}`))
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
mux.ServeHTTP(rec, req)
|
mux.ServeHTTP(rec, req)
|
||||||
if rec.Code != http.StatusOK {
|
if rec.Code != http.StatusOK {
|
||||||
t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
t.Fatalf("%s /api/config status = %d, want %d, body=%s", method, rec.Code, http.StatusOK, rec.Body.String())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertWebSearchAPIKeys(t *testing.T, configPath, wantGLMAPIKey, wantBaiduAPIKey string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
cfg, err := config.LoadConfig(configPath)
|
cfg, err := config.LoadConfig(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("LoadConfig() error = %v", err)
|
t.Fatalf("LoadConfig() error = %v", err)
|
||||||
}
|
}
|
||||||
if got := cfg.Tools.Web.GLMSearch.APIKey(); got != "glm-existing-key" {
|
if got := cfg.Tools.Web.GLMSearch.APIKey(); got != wantGLMAPIKey {
|
||||||
t.Fatalf("tools.web.glm_search.api_key = %q, want %q", got, "glm-existing-key")
|
t.Fatalf("tools.web.glm_search.api_key = %q, want %q", got, wantGLMAPIKey)
|
||||||
}
|
}
|
||||||
if got := cfg.Tools.Web.BaiduSearch.APIKey(); got != "baidu-existing-key" {
|
if got := cfg.Tools.Web.BaiduSearch.APIKey(); got != wantBaiduAPIKey {
|
||||||
t.Fatalf("tools.web.baidu_search.api_key = %q, want %q", got, "baidu-existing-key")
|
t.Fatalf("tools.web.baidu_search.api_key = %q, want %q", got, wantBaiduAPIKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandlePatchConfig_PreservesWebSearchAPIKeysWhenOmitted(t *testing.T) {
|
||||||
|
configPath, cleanup := setupOAuthTestEnv(t)
|
||||||
|
defer cleanup()
|
||||||
|
seedWebSearchAPIKeys(t, configPath, "glm-existing-key", "baidu-existing-key")
|
||||||
|
|
||||||
|
runConfigRequest(t, configPath, http.MethodPatch, `{
|
||||||
|
"gateway": {
|
||||||
|
"log_level": "info"
|
||||||
|
}
|
||||||
|
}`)
|
||||||
|
assertWebSearchAPIKeys(t, configPath, "glm-existing-key", "baidu-existing-key")
|
||||||
|
}
|
||||||
|
|
||||||
func TestHandlePatchConfig_UpdatesWebSearchAPIKeys(t *testing.T) {
|
func TestHandlePatchConfig_UpdatesWebSearchAPIKeys(t *testing.T) {
|
||||||
configPath, cleanup := setupOAuthTestEnv(t)
|
configPath, cleanup := setupOAuthTestEnv(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
h := NewHandler(configPath)
|
runConfigRequest(t, configPath, http.MethodPatch, `{
|
||||||
mux := http.NewServeMux()
|
|
||||||
h.RegisterRoutes(mux)
|
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodPatch, "/api/config", bytes.NewBufferString(`{
|
|
||||||
"tools": {
|
"tools": {
|
||||||
"web": {
|
"web": {
|
||||||
"glm_search": { "api_key": "glm-updated-key" },
|
"glm_search": { "api_key": "glm-updated-key" },
|
||||||
"baidu_search": { "api_key": "baidu-updated-key" }
|
"baidu_search": { "api_key": "baidu-updated-key" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}`))
|
}`)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
assertWebSearchAPIKeys(t, configPath, "glm-updated-key", "baidu-updated-key")
|
||||||
|
|
||||||
rec := httptest.NewRecorder()
|
|
||||||
mux.ServeHTTP(rec, req)
|
|
||||||
if rec.Code != http.StatusOK {
|
|
||||||
t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg, err := config.LoadConfig(configPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("LoadConfig() error = %v", err)
|
|
||||||
}
|
|
||||||
if got := cfg.Tools.Web.GLMSearch.APIKey(); got != "glm-updated-key" {
|
|
||||||
t.Fatalf("tools.web.glm_search.api_key = %q, want %q", got, "glm-updated-key")
|
|
||||||
}
|
|
||||||
if got := cfg.Tools.Web.BaiduSearch.APIKey(); got != "baidu-updated-key" {
|
|
||||||
t.Fatalf("tools.web.baidu_search.api_key = %q, want %q", got, "baidu-updated-key")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleUpdateConfig_PreservesWebSearchAPIKeysWhenOmitted(t *testing.T) {
|
func TestHandleUpdateConfig_PreservesWebSearchAPIKeysWhenOmitted(t *testing.T) {
|
||||||
|
|
@ -218,11 +208,7 @@ func TestHandleUpdateConfig_PreservesWebSearchAPIKeysWhenOmitted(t *testing.T) {
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
seedWebSearchAPIKeys(t, configPath, "glm-existing-key-via-put", "baidu-existing-key-via-put")
|
seedWebSearchAPIKeys(t, configPath, "glm-existing-key-via-put", "baidu-existing-key-via-put")
|
||||||
|
|
||||||
h := NewHandler(configPath)
|
runConfigRequest(t, configPath, http.MethodPut, `{
|
||||||
mux := http.NewServeMux()
|
|
||||||
h.RegisterRoutes(mux)
|
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodPut, "/api/config", bytes.NewBufferString(`{
|
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"agents": {
|
"agents": {
|
||||||
"defaults": {
|
"defaults": {
|
||||||
|
|
@ -237,36 +223,15 @@ func TestHandleUpdateConfig_PreservesWebSearchAPIKeysWhenOmitted(t *testing.T) {
|
||||||
"api_keys": ["sk-default"]
|
"api_keys": ["sk-default"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}`))
|
}`)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
assertWebSearchAPIKeys(t, configPath, "glm-existing-key-via-put", "baidu-existing-key-via-put")
|
||||||
|
|
||||||
rec := httptest.NewRecorder()
|
|
||||||
mux.ServeHTTP(rec, req)
|
|
||||||
if rec.Code != http.StatusOK {
|
|
||||||
t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg, err := config.LoadConfig(configPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("LoadConfig() error = %v", err)
|
|
||||||
}
|
|
||||||
if got := cfg.Tools.Web.GLMSearch.APIKey(); got != "glm-existing-key-via-put" {
|
|
||||||
t.Fatalf("tools.web.glm_search.api_key = %q, want %q", got, "glm-existing-key-via-put")
|
|
||||||
}
|
|
||||||
if got := cfg.Tools.Web.BaiduSearch.APIKey(); got != "baidu-existing-key-via-put" {
|
|
||||||
t.Fatalf("tools.web.baidu_search.api_key = %q, want %q", got, "baidu-existing-key-via-put")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleUpdateConfig_UpdatesWebSearchAPIKeys(t *testing.T) {
|
func TestHandleUpdateConfig_UpdatesWebSearchAPIKeys(t *testing.T) {
|
||||||
configPath, cleanup := setupOAuthTestEnv(t)
|
configPath, cleanup := setupOAuthTestEnv(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
h := NewHandler(configPath)
|
runConfigRequest(t, configPath, http.MethodPut, `{
|
||||||
mux := http.NewServeMux()
|
|
||||||
h.RegisterRoutes(mux)
|
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodPut, "/api/config", bytes.NewBufferString(`{
|
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"agents": {
|
"agents": {
|
||||||
"defaults": {
|
"defaults": {
|
||||||
|
|
@ -287,25 +252,8 @@ func TestHandleUpdateConfig_UpdatesWebSearchAPIKeys(t *testing.T) {
|
||||||
"baidu_search": { "api_key": "baidu-updated-key-via-put" }
|
"baidu_search": { "api_key": "baidu-updated-key-via-put" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}`))
|
}`)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
assertWebSearchAPIKeys(t, configPath, "glm-updated-key-via-put", "baidu-updated-key-via-put")
|
||||||
|
|
||||||
rec := httptest.NewRecorder()
|
|
||||||
mux.ServeHTTP(rec, req)
|
|
||||||
if rec.Code != http.StatusOK {
|
|
||||||
t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg, err := config.LoadConfig(configPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("LoadConfig() error = %v", err)
|
|
||||||
}
|
|
||||||
if got := cfg.Tools.Web.GLMSearch.APIKey(); got != "glm-updated-key-via-put" {
|
|
||||||
t.Fatalf("tools.web.glm_search.api_key = %q, want %q", got, "glm-updated-key-via-put")
|
|
||||||
}
|
|
||||||
if got := cfg.Tools.Web.BaiduSearch.APIKey(); got != "baidu-updated-key-via-put" {
|
|
||||||
t.Fatalf("tools.web.baidu_search.api_key = %q, want %q", got, "baidu-updated-key-via-put")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func seedWebSearchAPIKeys(t *testing.T, configPath, glmAPIKey, baiduAPIKey string) {
|
func seedWebSearchAPIKeys(t *testing.T, configPath, glmAPIKey, baiduAPIKey string) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue