Files
knowledge-governance/internal/httpapi/reconciliation_test.go
zheromiku 839b4ca2cf
build / build (push) Successful in 1m57s
fix(reconciliation): 处置视图列名限定,非法 document_id 返回 400
QA 在候选上实测出两条:
- P0 处置接口 500:LoadReconciliationActionView 的 SELECT 用了未限定的
  reconciliationFindingColumns,而语句 JOIN 了 reconciliation_runs 与
  test_publications(两者也有 id/title/path/source),PostgreSQL 直接报
  column reference "id" is ambiguous,writeback/requeue/remove 全部 500。
  改为按别名限定列(qualifiedFindingColumns),补真库集成用例。
- R-102 契约偏差:空的 document_id 被多段路由规范化成 307、含未编码斜杠的 id
  落到 404,契约要求 400。补子树兜底路由,并在鉴权之后加 guard 把 // 路径拦成 400
  (未登录仍是 401),补接口用例。
2026-12-01 20:36:57 +08:00

147 lines
6.5 KiB
Go

package httpapi
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"knowledge-governance/internal/governance"
"knowledge-governance/internal/reconciliation"
)
type reconciliationServiceStub struct {
created bool
progress governance.ReconciliationProgress
err error
}
func (s *reconciliationServiceStub) CreateRun(context.Context, string, string) (governance.ReconciliationRun, error) {
s.created = true
return governance.ReconciliationRun{ID: "run-1", KBID: "kb-1", State: "running"}, s.err
}
func (s *reconciliationServiceStub) ListRuns(context.Context, governance.ReconciliationRunFilter) (governance.ReconciliationRunPage, error) {
return governance.ReconciliationRunPage{Items: []governance.ReconciliationRun{}}, s.err
}
func (s *reconciliationServiceStub) GetRun(context.Context, string) (governance.ReconciliationRun, error) {
return governance.ReconciliationRun{ID: "run-1"}, s.err
}
func (s *reconciliationServiceStub) ListFindings(context.Context, governance.ReconciliationFindingFilter) (governance.ReconciliationFindingPage, error) {
return governance.ReconciliationFindingPage{Items: []governance.ReconciliationFinding{}}, s.err
}
func (s *reconciliationServiceStub) Writeback(context.Context, string, string, string) (governance.ReconciliationFinding, error) {
return governance.ReconciliationFinding{ID: "f1", ActionState: "done"}, s.err
}
func (s *reconciliationServiceStub) Requeue(context.Context, string, string, string) (governance.ReconciliationFinding, error) {
return governance.ReconciliationFinding{ID: "f1", ActionState: "done"}, s.err
}
func (s *reconciliationServiceStub) Remove(context.Context, string, string, string) (governance.ReconciliationFinding, error) {
return governance.ReconciliationFinding{ID: "f1", ActionState: "done"}, s.err
}
func (s *reconciliationServiceStub) Progress(context.Context, string) (governance.ReconciliationProgress, error) {
return s.progress, s.err
}
func reconciliationRequest(method, path, role, body string) *http.Request {
request := httptest.NewRequest(method, path, strings.NewReader(body))
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: role}))
return request
}
func TestReconciliationRoutesEnforceRolesAndWriteIntent(t *testing.T) {
service := &reconciliationServiceStub{}
handler, err := NewHandler(Config{Store: &fakeJobStore{}, Reconciliation: service})
if err != nil {
t.Fatal(err)
}
viewer := reconciliationRequest(http.MethodPost, "/api/v1/reconciliation/runs", roleViewer, `{"kb_id":"kb-1"}`)
viewer.Header.Set("X-Governance-Action", "reconciliation-action")
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, viewer)
if recorder.Code != http.StatusForbidden || service.created {
t.Fatalf("viewer create = %d created=%t", recorder.Code, service.created)
}
engineer := reconciliationRequest(http.MethodPost, "/api/v1/reconciliation/runs", roleEngineer, `{"kb_id":"kb-1"}`)
engineer.Header.Set("X-Governance-Action", "reconciliation-action")
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, engineer)
if recorder.Code != http.StatusAccepted || !service.created {
t.Fatalf("engineer create = %d created=%t %s", recorder.Code, service.created, recorder.Body.String())
}
missingIntent := reconciliationRequest(http.MethodPost, "/api/v1/reconciliation/findings/f1/requeue", roleEngineer, "")
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, missingIntent)
if recorder.Code != http.StatusForbidden {
t.Fatalf("missing write intent = %d", recorder.Code)
}
engineerRemove := reconciliationRequest(http.MethodPost, "/api/v1/reconciliation/findings/f1/remove", roleEngineer, "")
engineerRemove.Header.Set("X-Governance-Action", "reconciliation-action")
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, engineerRemove)
if recorder.Code != http.StatusForbidden {
t.Fatalf("engineer remove = %d", recorder.Code)
}
adminRemove := reconciliationRequest(http.MethodPost, "/api/v1/reconciliation/findings/f1/remove", roleAdmin, "")
adminRemove.Header.Set("X-Governance-Action", "reconciliation-action")
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, adminRemove)
if recorder.Code != http.StatusOK {
t.Fatalf("admin remove = %d %s", recorder.Code, recorder.Body.String())
}
}
func TestReconciliationProgress503AndUnwiredRoute(t *testing.T) {
service := &reconciliationServiceStub{err: reconciliation.ErrRuntimeUnavailable}
handler, err := NewHandler(Config{Store: &fakeJobStore{}, Reconciliation: service})
if err != nil {
t.Fatal(err)
}
request := reconciliationRequest(http.MethodGet, "/api/v1/reconciliation/documents/doc-1/progress", roleViewer, "")
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusServiceUnavailable || !strings.Contains(recorder.Body.String(), "runtime_read_unavailable") {
t.Fatalf("progress = %d %s", recorder.Code, recorder.Body.String())
}
unwired, err := NewHandler(Config{Store: &fakeJobStore{}})
if err != nil {
t.Fatal(err)
}
recorder = httptest.NewRecorder()
unwired.ServeHTTP(recorder, reconciliationRequest(http.MethodGet, "/api/v1/reconciliation/runs", roleViewer, ""))
if recorder.Code != http.StatusServiceUnavailable {
t.Fatalf("unwired runs = %d", recorder.Code)
}
}
func TestReconciliationFindingTypeValidation(t *testing.T) {
handler, err := NewHandler(Config{Store: &fakeJobStore{}, Reconciliation: &reconciliationServiceStub{}})
if err != nil {
t.Fatal(err)
}
request := reconciliationRequest(http.MethodGet, "/api/v1/reconciliation/runs/run-1/findings?type=R9", roleViewer, "")
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusBadRequest {
t.Fatalf("invalid type = %d", recorder.Code)
}
}
func TestReconciliationProgressRejectsMalformedDocumentIDWith400(t *testing.T) {
service := &reconciliationServiceStub{}
handler, err := NewHandler(Config{Store: &fakeJobStore{}, Reconciliation: service})
if err != nil {
t.Fatal(err)
}
for _, path := range []string{
"/api/v1/reconciliation/documents//progress",
"/api/v1/reconciliation/documents/doc-1/extra/progress",
} {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, reconciliationRequest(http.MethodGet, path, roleViewer, ""))
if recorder.Code != http.StatusBadRequest || !strings.Contains(recorder.Body.String(), "invalid document id") {
t.Fatalf("%s = %d %s, want 400 invalid document id", path, recorder.Code, recorder.Body.String())
}
}
}