2026-05-30 22:41:21 +08:00
|
|
|
package response
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-08-21 00:41:07 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
2026-05-30 22:41:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
if _, ok := decoded["api_key"]; ok {
|
|
|
|
|
t.Fatalf("api_key should not be exposed: %s", payload)
|
2026-05-30 22:41:21 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
if got, ok := decoded["has_api_key"].(bool); !ok || !got {
|
|
|
|
|
t.Fatalf("has_api_key = %v, want true: %s", decoded["has_api_key"], payload)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBuildAIConfigDetailResponseIncludesAPIKey(t *testing.T) {
|
|
|
|
|
payload, err := json.Marshal(BuildAIConfigDetailResponse(&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 got := decoded["api_key"]; got != "sk-secret" {
|
|
|
|
|
t.Fatalf("api_key = %v, want sk-secret: %s", got, payload)
|
2026-05-30 22:41:21 +08:00
|
|
|
}
|
|
|
|
|
}
|