refactor: replace APIKey with HasAPIKey in AIConfig and related components
This commit is contained in:
@@ -30,7 +30,7 @@ type AIConfigResponse struct {
|
||||
Name string `json:"name"`
|
||||
Provider enums.AIProvider `json:"provider"`
|
||||
BaseURL string `json:"baseUrl"`
|
||||
APIKey string `json:"apiKey"`
|
||||
HasAPIKey bool `json:"hasApiKey"`
|
||||
ModelType enums.AIModelType `json:"modelType"`
|
||||
ModelName string `json:"modelName"`
|
||||
Dimension int `json:"dimension"`
|
||||
@@ -51,7 +51,7 @@ func BuildAIConfigResponse(item *models.AIConfig) AIConfigResponse {
|
||||
Name: item.Name,
|
||||
Provider: item.Provider,
|
||||
BaseURL: item.BaseURL,
|
||||
APIKey: item.APIKey,
|
||||
HasAPIKey: item.APIKey != "",
|
||||
ModelType: item.ModelType,
|
||||
ModelName: item.ModelName,
|
||||
Dimension: item.Dimension,
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"cs-ai-agent/internal/models"
|
||||
)
|
||||
|
||||
func TestBuildAIConfigResponseOmitsAPIKey(t *testing.T) {
|
||||
payload, err := json.Marshal(BuildAIConfigResponse(&models.AIConfig{
|
||||
ID: 1,
|
||||
Name: "test",
|
||||
APIKey: "sk-secret",
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("marshal response error = %v", err)
|
||||
}
|
||||
|
||||
var decoded map[string]any
|
||||
if err := json.Unmarshal(payload, &decoded); err != nil {
|
||||
t.Fatalf("unmarshal response error = %v", err)
|
||||
}
|
||||
if _, ok := decoded["apiKey"]; ok {
|
||||
t.Fatalf("apiKey should not be exposed: %s", payload)
|
||||
}
|
||||
if got, ok := decoded["hasApiKey"].(bool); !ok || !got {
|
||||
t.Fatalf("hasApiKey = %v, want true: %s", decoded["hasApiKey"], payload)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user