Files
knowledge-governance/internal/httpapi/library_restore_test.go
T
姚章浩 a2d051f6b3
build / build (push) Successful in 1m25s
feat(library): 按版本恢复入口与失败作业续作
为什么:恢复此前只有"失败自动回滚",没有手动按版本恢复;失败作业也只能重发封存。
- 迁移 000068 允许 kind=restore;POST /library/restores(管理员 + 该快照恢复授权 + 闸口)。
- POST /library/jobs/{id}/resume 对失败/取消的同步/恢复作业按原参数续作。
- worker 统一 runLibraryRelease,同步与恢复共用链路;成功后登记 production_effective 版本。
2026-10-28 16:27:46 +08:00

88 lines
3.6 KiB
Go

package httpapi
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"knowledge-governance/internal/governance"
)
func restoreRequest(body, role string) *http.Request {
request := httptest.NewRequest(http.MethodPost, "/api/v1/library/restores", strings.NewReader(body))
request.Header.Set("Content-Type", "application/json")
ctx := context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: role})
return request.WithContext(ctx)
}
// 恢复是写生产:非管理员直接拒绝,任何授权都不会被创建。
func TestLibraryRestoreRequiresAdmin(t *testing.T) {
store := &fakeLibraryStore{}
recorder := httptest.NewRecorder()
libraryRestoreEnqueue(recorder, restoreRequest(`{"scope":"internal","snapshot_id":"snap-1"}`, roleEngineer), Config{Store: store})
if recorder.Code != http.StatusForbidden {
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
}
if store.createN != 0 {
t.Fatal("non-admin must not create a restore job")
}
}
// 闸口关闭 / 缺恢复演练证据 / 缺该快照授权:逐条拒绝且不建作业。
func TestLibraryRestoreRefusesWithoutGatesAndAuthorization(t *testing.T) {
store := &fakeLibraryStore{}
recorder := httptest.NewRecorder()
libraryRestoreEnqueue(recorder, restoreRequest(`{"scope":"internal","snapshot_id":"snap-1"}`, roleAdmin), Config{Store: store})
if recorder.Code != http.StatusServiceUnavailable {
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
}
body := recorder.Body.String()
for _, want := range []string{"生产写入闸口关闭", "恢复演练证据", "恢复授权"} {
if !strings.Contains(body, want) {
t.Fatalf("refusal must name %q: %s", want, body)
}
}
if store.createN != 0 {
t.Fatal("must not create a restore job while blocked")
}
}
// 授权必须绑定"恢复到这个快照":同步授权不能顶替。
func TestLibraryRestoreRequiresSnapshotBoundGrant(t *testing.T) {
store := &fakeLibraryStore{}
syncDigest, err := librarySyncParamsDigest("internal", "snap-1")
if err != nil {
t.Fatal(err)
}
now := time.Now().UTC()
store.grant = &governance.LibraryReleaseAuthorization{ID: "a", Scope: "internal", SnapshotID: "snap-1",
ParamsDigest: syncDigest, ExpiresAt: now.Add(time.Hour), CreatedAt: now}
config := Config{Store: store, ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: true}
recorder := httptest.NewRecorder()
libraryRestoreEnqueue(recorder, restoreRequest(`{"scope":"internal","snapshot_id":"snap-1"}`, roleAdmin), config)
if recorder.Code != http.StatusServiceUnavailable || !strings.Contains(recorder.Body.String(), "恢复授权") {
t.Fatalf("sync grant must not authorize a restore: %d %s", recorder.Code, recorder.Body.String())
}
if store.createN != 0 {
t.Fatal("must not create a restore job with a sync-only grant")
}
}
// 续作只对失败/取消的同步与恢复作业开放;封存作业走封存入口。
func TestLibraryJobResumeRejectsNonFailedJobs(t *testing.T) {
store := &fakeLibraryStore{jobs: []governance.LibraryJob{{ID: "job-1", Scope: "internal",
Kind: governance.LibraryJobSync, State: governance.LibraryJobSucceeded}}}
request := httptest.NewRequest(http.MethodPost, "/api/v1/library/jobs/job-1/resume", nil)
request.SetPathValue("id", "job-1")
request = request.WithContext(context.WithValue(request.Context(), sessionKey{},
sessionIdentity{Username: "eng", Role: roleEngineer}))
recorder := httptest.NewRecorder()
libraryJobResume(recorder, request, Config{Store: store})
if recorder.Code != http.StatusConflict {
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
}
}