feat(settings): 知识库绑定按环境一次保存连接与内外库
原「知识库映射 → WeKnora 连接」把地址、文件和绑定分散在多处,页面无法输入密钥, 也看不到候选实例真实可读的库。现在测试 090 / 生产 090 各自一个工作区:草稿填地址 与 API Key,服务器真实分页列库并区分空库/无权限/不可达,选内外库后一次保存连接与 绑定,回读真实生效值。 服务端以修订号 + 行锁 + 单事务写连接、绑定与待入库重排;旧部署无新行时继续使用文件 凭据/默认绑定。密钥只存 AES-256-GCM 密文,主密钥在独立文件;换地址必须重录,旧密钥 不转发、不跨环境取用。消费端(readiness/上传轮询/列库/探活/生产列库)统一从该存储 解析当前连接,避免只保存了页面数据。 验证:go test ./...;前端 606 项;隔离 PostgreSQL 集成用例覆盖保存/冲突/重排/拒错; make verify 在本提交上执行(结果见交付说明)。
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
package kbbinding
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type emptyDB struct{}
|
||||
|
||||
func (emptyDB) QueryRow(context.Context, string, ...any) pgx.Row { return emptyRow{} }
|
||||
func (emptyDB) Begin(context.Context) (pgx.Tx, error) {
|
||||
return nil, errors.New("no transaction in unit test")
|
||||
}
|
||||
|
||||
type emptyRow struct{}
|
||||
|
||||
func (emptyRow) Scan(...any) error { return pgx.ErrNoRows }
|
||||
|
||||
// fakeWeKnora serves the two reads the candidate validation makes: one page of
|
||||
// knowledge bases and a single base by id.
|
||||
func fakeWeKnora(t *testing.T, wantKey string, bases []struct{ id, name string }, total int64) *httptest.Server {
|
||||
t.Helper()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if got := r.Header.Get("X-API-Key"); got != wantKey {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/api/v1/knowledge-bases" {
|
||||
items := make([]string, 0, len(bases))
|
||||
for _, base := range bases {
|
||||
items = append(items, `{"id":"`+base.id+`","name":"`+base.name+`","knowledge_count":2}`)
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"success":true,"data":[` + strings.Join(items, ",") + `],"total":` + strconv.FormatInt(total, 10) + `}`))
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, "/api/v1/knowledge-bases/") {
|
||||
id := strings.TrimPrefix(r.URL.Path, "/api/v1/knowledge-bases/")
|
||||
for _, base := range bases {
|
||||
if base.id == id {
|
||||
_, _ = w.Write([]byte(`{"success":true,"data":{"id":"` + base.id + `","name":"` + base.name + `"}}`))
|
||||
return
|
||||
}
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
return server
|
||||
}
|
||||
|
||||
func TestCatalogDistinguishesEmptyFromPermissionFromUnreachable(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := New(emptyDB{}, Options{Timeout: 2 * time.Second, AllowDockerHTTP: map[string]bool{EnvTest: true}})
|
||||
|
||||
empty := fakeWeKnora(t, "key-1", nil, 0)
|
||||
catalog, err := store.Catalog(ctx, EnvTest, CatalogInput{
|
||||
BaseURL: empty.URL, KeyAction: "replace", APIKey: "key-1", Page: 1, PageSize: 20,
|
||||
})
|
||||
if err != nil || catalog.Status != statusEmpty || catalog.Total != 0 {
|
||||
t.Fatalf("empty catalog = %#v, %v", catalog, err)
|
||||
}
|
||||
|
||||
denied := fakeWeKnora(t, "another-key", nil, 0)
|
||||
catalog, err = store.Catalog(ctx, EnvTest, CatalogInput{
|
||||
BaseURL: denied.URL, KeyAction: "replace", APIKey: "key-1", Page: 1, PageSize: 20,
|
||||
})
|
||||
if err != nil || catalog.Status != statusUnauthorized {
|
||||
t.Fatalf("denied catalog = %#v, %v", catalog, err)
|
||||
}
|
||||
|
||||
down := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||
downURL := down.URL
|
||||
down.Close()
|
||||
catalog, err = store.Catalog(ctx, EnvTest, CatalogInput{
|
||||
BaseURL: downURL, KeyAction: "replace", APIKey: "key-1", Page: 1, PageSize: 20,
|
||||
})
|
||||
if err != nil || catalog.Status != statusUnreachable {
|
||||
t.Fatalf("unreachable catalog = %#v, %v", catalog, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCatalogPaginatesAndReturnsNamesWithShortIDsAreStable(t *testing.T) {
|
||||
server := fakeWeKnora(t, "key-1", []struct{ id, name string }{{"kb-1", "技术资料"}}, 1)
|
||||
store := New(emptyDB{}, Options{Timeout: 2 * time.Second, AllowDockerHTTP: map[string]bool{EnvTest: true}})
|
||||
catalog, err := store.Catalog(context.Background(), EnvTest, CatalogInput{
|
||||
BaseURL: server.URL, KeyAction: "replace", APIKey: "key-1", Page: 1, PageSize: 20,
|
||||
})
|
||||
if err != nil || catalog.Status != statusOK || len(catalog.Options) != 1 || catalog.Options[0].Name != "技术资料" {
|
||||
t.Fatalf("catalog = %#v, %v", catalog, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeyActionRulesRejectForwardingAndInvalidInput(t *testing.T) {
|
||||
store := New(emptyDB{}, Options{})
|
||||
saved := stored{Environment: EnvTest, Revision: 1, BaseURL: "https://old.example", KeyCiphertext: []byte("sealed")}
|
||||
if _, err := store.keyFor(context.Background(), EnvTest, "keep", "https://new.example", "", saved, true); !errors.Is(err, ErrKeyForwarding) {
|
||||
t.Fatalf("keep with a new address = %v, want ErrKeyForwarding", err)
|
||||
}
|
||||
if _, err := store.keyFor(context.Background(), EnvTest, "keep", "https://old.example", "typed", saved, true); !errors.Is(err, ErrInvalid) {
|
||||
t.Fatalf("keep with a typed key = %v, want ErrInvalid", err)
|
||||
}
|
||||
if _, err := store.keyFor(context.Background(), EnvTest, "replace", "https://new.example", " ", saved, true); !errors.Is(err, ErrInvalid) {
|
||||
t.Fatalf("replace with blank key = %v, want ErrInvalid", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveInputValidationRejectsDuplicateAndBadIDs(t *testing.T) {
|
||||
now := time.Date(2026, 11, 19, 8, 0, 0, 0, time.UTC)
|
||||
store := New(emptyDB{}, Options{Now: func() time.Time { return now }})
|
||||
for _, input := range []SaveInput{
|
||||
{BaseURL: "https://x.example", KeyAction: "replace", APIKey: "k", InternalKBID: "kb", ExternalKBID: "kb"},
|
||||
{BaseURL: "https://x.example", KeyAction: "replace", APIKey: "k", InternalKBID: "kb/1"},
|
||||
{BaseURL: "https://x.example?token=1", KeyAction: "replace", APIKey: "k"},
|
||||
} {
|
||||
if _, err := store.Save(context.Background(), EnvTest, input, "admin", now); !errors.Is(err, ErrInvalid) {
|
||||
t.Fatalf("input %#v error = %v, want ErrInvalid", input, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user