79a3358810
web/handlers/window.go: GET /window (list), GET /window/new (form),
POST /window (open), GET /window/{id} (detail+lifecycle+audit), POST
/window/{id}/activate|revoke|expire (lifecycle transitions). Store
extensions: OpenWindow/ActivateWindow/RevokeWindow/ExpireWindow/
ListWindows/GetWindow/GetAuditLog — all call the REAL x/window/types
Window.Activate/Revoke/Expire methods (not reimplementation). Revoke on
Expired is a no-op (v0.2 terminal-state contract). AuditEntry appended on
each transition. 3 Window templates. window_test.go: lifecycle
correctness (asserts real methods invoked), idempotent revoke, revoke-on-
expired no-op, G-026 error lexicon checks. Coverage: store 99.2%,
handlers 87.7%.
---ci---
project: oy
phase: 3
milestone: v0.6
status: execute
---/ci---
437 lines
13 KiB
Go
437 lines
13 KiB
Go
package store
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
|
|
identitytypes "github.com/oy/openyield/x/identity/types"
|
|
stashtypes "github.com/oy/openyield/x/stash/types"
|
|
windowtypes "github.com/oy/openyield/x/window/types"
|
|
)
|
|
|
|
func TestNewStoreSeedsFixtures(t *testing.T) {
|
|
s := NewStore()
|
|
reaches := s.ListReaches()
|
|
if len(reaches) < 2 {
|
|
t.Fatalf("NewStore seeded %d reaches, want >=2", len(reaches))
|
|
}
|
|
// Both seeded reaches must be Nomads (IsNomad=true).
|
|
for _, r := range reaches {
|
|
if !r.IsNomad {
|
|
t.Errorf("seeded reach %q: IsNomad=false, want true", r.HolderID)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCreateReachAtomicReachAndStash(t *testing.T) {
|
|
s := NewStore()
|
|
reach, stash, err := s.CreateReach("holder-test1", "pk-test1")
|
|
if err != nil {
|
|
t.Fatalf("CreateReach: %v", err)
|
|
}
|
|
// D-071: Reach must be IsNomad=true.
|
|
if !reach.IsNomad {
|
|
t.Errorf("reach.IsNomad = false, want true (D-071)")
|
|
}
|
|
if reach.HolderID != "holder-test1" {
|
|
t.Errorf("reach.HolderID = %q, want holder-test1", reach.HolderID)
|
|
}
|
|
// D-071: Stash must have matching HolderID + seeded BalanceGrain.
|
|
if stash.HolderID != reach.HolderID {
|
|
t.Errorf("stash.HolderID = %q, want %q (D-071 atomic)", stash.HolderID, reach.HolderID)
|
|
}
|
|
if stash.BalanceGrain != seedBalanceGrain {
|
|
t.Errorf("stash.BalanceGrain = %d, want %d", stash.BalanceGrain, seedBalanceGrain)
|
|
}
|
|
// Both must be retrievable after the atomic call.
|
|
if _, ok := s.GetReach("holder-test1"); !ok {
|
|
t.Errorf("GetReach miss after CreateReach (atomicity broken)")
|
|
}
|
|
if _, ok := s.GetStash("holder-test1"); !ok {
|
|
t.Errorf("GetStash miss after CreateReach (atomicity broken)")
|
|
}
|
|
if _, ok := s.GetStashActivity(stash.StashID); !ok {
|
|
t.Errorf("GetStashActivity miss after CreateReach (atomicity broken)")
|
|
}
|
|
}
|
|
|
|
func TestCreateReachDuplicateRejected(t *testing.T) {
|
|
s := NewStore()
|
|
if _, _, err := s.CreateReach("holder-alia", "pk-dupe"); err == nil {
|
|
t.Errorf("CreateReach duplicate holder-alia: expected error, got nil")
|
|
}
|
|
}
|
|
|
|
func TestCreateReachValidationG027(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
holderID string
|
|
publicKey string
|
|
wantErr bool
|
|
}{
|
|
{"empty holder", "", "pk", true},
|
|
{"empty pubkey", "h", "", true},
|
|
{"holder too long", stringOf('x', 129), "pk", true},
|
|
{"pubkey too long", "h", stringOf('y', 129), true},
|
|
{"holder with slash", "h/x", "pk", true},
|
|
{"holder with backslash", "h\\x", "pk", true},
|
|
{"holder with template syntax", "h{{", "pk", true},
|
|
{"pubkey with slash", "h", "p/x", true},
|
|
{"valid minimal", "h", "p", false},
|
|
{"valid typical", "holder-oka", "pk-oka-7", false},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
s := NewStore()
|
|
_, _, err := s.CreateReach(c.holderID, c.publicKey)
|
|
if c.wantErr && err == nil {
|
|
t.Errorf("expected error, got nil")
|
|
}
|
|
if !c.wantErr && err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetReachHitMiss(t *testing.T) {
|
|
s := NewStore()
|
|
if _, ok := s.GetReach("holder-alia"); !ok {
|
|
t.Errorf("GetReach(holder-alia) miss, want hit (seeded)")
|
|
}
|
|
if _, ok := s.GetReach("nobody"); ok {
|
|
t.Errorf("GetReach(nobody) hit, want miss")
|
|
}
|
|
}
|
|
|
|
func TestGetStashHitMiss(t *testing.T) {
|
|
s := NewStore()
|
|
if _, ok := s.GetStash("holder-alia"); !ok {
|
|
t.Errorf("GetStash(holder-alia) miss, want hit (seeded)")
|
|
}
|
|
if _, ok := s.GetStash("nobody"); ok {
|
|
t.Errorf("GetStash(nobody) hit, want miss")
|
|
}
|
|
}
|
|
|
|
func TestGetStashActivityHitMiss(t *testing.T) {
|
|
s := NewStore()
|
|
stash, ok := s.GetStash("holder-alia")
|
|
if !ok {
|
|
t.Fatal("seeded stash holder-alia missing")
|
|
}
|
|
if _, ok := s.GetStashActivity(stash.StashID); !ok {
|
|
t.Errorf("GetStashActivity(%q) miss, want hit", stash.StashID)
|
|
}
|
|
if _, ok := s.GetStashActivity("stash-nobody"); ok {
|
|
t.Errorf("GetStashActivity(stash-nobody) hit, want miss")
|
|
}
|
|
}
|
|
|
|
func TestCreateReachConcurrentNoRace(t *testing.T) {
|
|
s := NewStore()
|
|
const n = 50
|
|
var wg sync.WaitGroup
|
|
wg.Add(n)
|
|
for i := 0; i < n; i++ {
|
|
go func(i int) {
|
|
defer wg.Done()
|
|
holder := "holder-concurrent-" + itoa(i)
|
|
_, _, _ = s.CreateReach(holder, "pk")
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
// All n concurrent creates with distinct holder IDs must be present.
|
|
for i := 0; i < n; i++ {
|
|
if _, ok := s.GetReach("holder-concurrent-" + itoa(i)); !ok {
|
|
t.Errorf("concurrent reach %d missing after wg.Wait", i)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSeededMatureVsImmature(t *testing.T) {
|
|
s := NewStore()
|
|
// holder-alia: ActiveDays=92, MaxGapDays=10 -> mature.
|
|
aliaStash, ok := s.GetStash("holder-alia")
|
|
if !ok {
|
|
t.Fatal("seeded holder-alia missing")
|
|
}
|
|
aliaAct, ok := s.GetStashActivity(aliaStash.StashID)
|
|
if !ok {
|
|
t.Fatal("seeded alia activity missing")
|
|
}
|
|
if !aliaAct.IsMature() {
|
|
t.Errorf("holder-alia IsMature=false, want true (ActiveDays=%d, MaxGap=%d)",
|
|
aliaAct.ActiveDays, aliaAct.MaxGapDays)
|
|
}
|
|
// holder-bryn: ActiveDays=45, MaxGapDays=5 -> not mature.
|
|
brynStash, ok := s.GetStash("holder-bryn")
|
|
if !ok {
|
|
t.Fatal("seeded holder-bryn missing")
|
|
}
|
|
brynAct, ok := s.GetStashActivity(brynStash.StashID)
|
|
if !ok {
|
|
t.Fatal("seeded bryn activity missing")
|
|
}
|
|
if brynAct.IsMature() {
|
|
t.Errorf("holder-bryn IsMature=true, want false (ActiveDays=%d, MaxGap=%d)",
|
|
brynAct.ActiveDays, brynAct.MaxGapDays)
|
|
}
|
|
}
|
|
|
|
// Compile-time assertions that the types are the real x/*/types structs
|
|
// (D-067: the mock store grounds the UI in the real Go type definitions).
|
|
var _ identitytypes.Reach
|
|
var _ stashtypes.Stash
|
|
|
|
// itoa is a tiny strconv.Itoa without the import (keeps store_test.go deps
|
|
// to just sync + testing + the two x/*/types packages).
|
|
func itoa(n int) string {
|
|
if n == 0 {
|
|
return "0"
|
|
}
|
|
neg := n < 0
|
|
if neg {
|
|
n = -n
|
|
}
|
|
var buf [20]byte
|
|
i := len(buf)
|
|
for n > 0 {
|
|
i--
|
|
buf[i] = byte('0' + n%10)
|
|
n /= 10
|
|
}
|
|
if neg {
|
|
i--
|
|
buf[i] = '-'
|
|
}
|
|
return string(buf[i:])
|
|
}
|
|
|
|
func stringOf(r rune, n int) string {
|
|
b := make([]byte, n)
|
|
for i := range b {
|
|
b[i] = byte(r)
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
// --- Window tests (P3) ---
|
|
|
|
func TestOpenWindowCreatesStatusOpenWithInitialAudit(t *testing.T) {
|
|
s := NewStore()
|
|
scope := windowtypes.Scope{Kind: windowtypes.ScopeReadStash, ResourceID: "stash-x"}
|
|
rl := windowtypes.RateLimit{MaxActions: 5, PerDurationSeconds: 3600}
|
|
w, err := s.OpenWindow("holder-alia", "service-1", scope, 1000, 2000, rl)
|
|
if err != nil {
|
|
t.Fatalf("OpenWindow: %v", err)
|
|
}
|
|
if w.Status != windowtypes.StatusOpen {
|
|
t.Errorf("OpenWindow status %q, want Open", w.Status)
|
|
}
|
|
if w.WindowID == "" {
|
|
t.Error("OpenWindow: empty WindowID")
|
|
}
|
|
audit := s.GetAuditLog(w.WindowID)
|
|
if len(audit) != 1 {
|
|
t.Errorf("OpenWindow: audit log len %d, want 1", len(audit))
|
|
}
|
|
if audit[0].Action != "open" {
|
|
t.Errorf("OpenWindow: audit[0].Action %q, want open", audit[0].Action)
|
|
}
|
|
}
|
|
|
|
func TestOpenWindowValidation(t *testing.T) {
|
|
scope := windowtypes.Scope{Kind: windowtypes.ScopeReadStash}
|
|
rl := windowtypes.RateLimit{MaxActions: 1}
|
|
cases := []struct {
|
|
name, grantor, grantee string
|
|
wantErr bool
|
|
}{
|
|
{"empty grantor", "", "g", true},
|
|
{"empty grantee", "h", "", true},
|
|
{"valid", "h", "g", false},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
s := NewStore()
|
|
_, err := s.OpenWindow(c.grantor, c.grantee, scope, 1, 2, rl)
|
|
if c.wantErr && err == nil {
|
|
t.Errorf("expected error, got nil")
|
|
}
|
|
if !c.wantErr && err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestActivateWindowTransitionsToActive(t *testing.T) {
|
|
s := NewStore()
|
|
scope := windowtypes.Scope{Kind: windowtypes.ScopeReadStash}
|
|
rl := windowtypes.RateLimit{MaxActions: 1}
|
|
w, _ := s.OpenWindow("holder-alia", "svc", scope, 1, 2, rl)
|
|
if err := s.ActivateWindow(w.WindowID); err != nil {
|
|
t.Fatalf("ActivateWindow: %v", err)
|
|
}
|
|
updated, _ := s.GetWindow(w.WindowID)
|
|
if updated.Status != windowtypes.StatusActive {
|
|
t.Errorf("after activate: %q, want Active", updated.Status)
|
|
}
|
|
audit := s.GetAuditLog(w.WindowID)
|
|
if len(audit) != 2 {
|
|
t.Errorf("after activate: audit len %d, want 2", len(audit))
|
|
}
|
|
}
|
|
|
|
func TestActivateWindowNotFound(t *testing.T) {
|
|
s := NewStore()
|
|
if err := s.ActivateWindow("window-nobody"); err == nil {
|
|
t.Error("ActivateWindow(nobody): expected error, got nil")
|
|
}
|
|
}
|
|
|
|
func TestActivateWindowOnActiveFails(t *testing.T) {
|
|
s := NewStore()
|
|
scope := windowtypes.Scope{Kind: windowtypes.ScopeReadStash}
|
|
rl := windowtypes.RateLimit{MaxActions: 1}
|
|
w, _ := s.OpenWindow("holder-alia", "svc", scope, 1, 2, rl)
|
|
_ = s.ActivateWindow(w.WindowID)
|
|
// Activate again should fail (can only activate Open windows).
|
|
if err := s.ActivateWindow(w.WindowID); err == nil {
|
|
t.Error("activate on Active: expected error, got nil (Window.Activate rejects non-Open)")
|
|
}
|
|
}
|
|
|
|
func TestRevokeWindowTransitionsToRevoked(t *testing.T) {
|
|
s := NewStore()
|
|
scope := windowtypes.Scope{Kind: windowtypes.ScopeReadStash}
|
|
rl := windowtypes.RateLimit{MaxActions: 1}
|
|
w, _ := s.OpenWindow("holder-alia", "svc", scope, 1, 2, rl)
|
|
if err := s.RevokeWindow(w.WindowID); err != nil {
|
|
t.Fatalf("RevokeWindow: %v", err)
|
|
}
|
|
updated, _ := s.GetWindow(w.WindowID)
|
|
if updated.Status != windowtypes.StatusRevoked {
|
|
t.Errorf("after revoke: %q, want Revoked", updated.Status)
|
|
}
|
|
if !updated.Revoked {
|
|
t.Error("after revoke: Revoked flag false, want true")
|
|
}
|
|
}
|
|
|
|
func TestRevokeWindowIdempotent(t *testing.T) {
|
|
s := NewStore()
|
|
scope := windowtypes.Scope{Kind: windowtypes.ScopeReadStash}
|
|
rl := windowtypes.RateLimit{MaxActions: 1}
|
|
w, _ := s.OpenWindow("holder-alia", "svc", scope, 1, 2, rl)
|
|
_ = s.RevokeWindow(w.WindowID)
|
|
before := len(s.GetAuditLog(w.WindowID))
|
|
_ = s.RevokeWindow(w.WindowID)
|
|
after := len(s.GetAuditLog(w.WindowID))
|
|
if after != before {
|
|
t.Errorf("idempotent revoke: audit grew %d -> %d", before, after)
|
|
}
|
|
}
|
|
|
|
func TestRevokeWindowOnExpiredIsNoOp(t *testing.T) {
|
|
s := NewStore()
|
|
scope := windowtypes.Scope{Kind: windowtypes.ScopeReadStash}
|
|
rl := windowtypes.RateLimit{MaxActions: 1}
|
|
w, _ := s.OpenWindow("holder-alia", "svc", scope, 1, 2, rl)
|
|
_ = s.ExpireWindow(w.WindowID)
|
|
before := len(s.GetAuditLog(w.WindowID))
|
|
_ = s.RevokeWindow(w.WindowID)
|
|
updated, _ := s.GetWindow(w.WindowID)
|
|
if updated.Status != windowtypes.StatusExpired {
|
|
t.Errorf("revoke-on-expired: %q, want Expired (terminal wins)", updated.Status)
|
|
}
|
|
after := len(s.GetAuditLog(w.WindowID))
|
|
if after != before {
|
|
t.Errorf("revoke-on-expired: audit grew %d -> %d (no-op)", before, after)
|
|
}
|
|
}
|
|
|
|
func TestRevokeWindowNotFound(t *testing.T) {
|
|
s := NewStore()
|
|
if err := s.RevokeWindow("window-nobody"); err == nil {
|
|
t.Error("RevokeWindow(nobody): expected error, got nil")
|
|
}
|
|
}
|
|
|
|
func TestExpireWindowTransitionsToExpired(t *testing.T) {
|
|
s := NewStore()
|
|
scope := windowtypes.Scope{Kind: windowtypes.ScopeReadStash}
|
|
rl := windowtypes.RateLimit{MaxActions: 1}
|
|
w, _ := s.OpenWindow("holder-alia", "svc", scope, 1, 2, rl)
|
|
if err := s.ExpireWindow(w.WindowID); err != nil {
|
|
t.Fatalf("ExpireWindow: %v", err)
|
|
}
|
|
updated, _ := s.GetWindow(w.WindowID)
|
|
if updated.Status != windowtypes.StatusExpired {
|
|
t.Errorf("after expire: %q, want Expired", updated.Status)
|
|
}
|
|
}
|
|
|
|
func TestExpireWindowNotFound(t *testing.T) {
|
|
s := NewStore()
|
|
if err := s.ExpireWindow("window-nobody"); err == nil {
|
|
t.Error("ExpireWindow(nobody): expected error, got nil")
|
|
}
|
|
}
|
|
|
|
func TestExpireWindowIdempotent(t *testing.T) {
|
|
s := NewStore()
|
|
scope := windowtypes.Scope{Kind: windowtypes.ScopeReadStash}
|
|
rl := windowtypes.RateLimit{MaxActions: 1}
|
|
w, _ := s.OpenWindow("holder-alia", "svc", scope, 1, 2, rl)
|
|
_ = s.ExpireWindow(w.WindowID)
|
|
before := len(s.GetAuditLog(w.WindowID))
|
|
_ = s.ExpireWindow(w.WindowID)
|
|
after := len(s.GetAuditLog(w.WindowID))
|
|
if after != before {
|
|
t.Errorf("idempotent expire: audit grew %d -> %d", before, after)
|
|
}
|
|
}
|
|
|
|
func TestListWindowsFiltersByGrantor(t *testing.T) {
|
|
s := NewStore()
|
|
scope := windowtypes.Scope{Kind: windowtypes.ScopeReadStash}
|
|
rl := windowtypes.RateLimit{MaxActions: 1}
|
|
_, _ = s.OpenWindow("holder-alia", "svc1", scope, 1, 2, rl)
|
|
_, _ = s.OpenWindow("holder-alia", "svc2", scope, 1, 2, rl)
|
|
_, _ = s.OpenWindow("holder-bryn", "svc3", scope, 1, 2, rl)
|
|
alia := s.ListWindows("holder-alia")
|
|
if len(alia) != 2 {
|
|
t.Errorf("ListWindows(holder-alia) = %d, want 2", len(alia))
|
|
}
|
|
bryn := s.ListWindows("holder-bryn")
|
|
if len(bryn) != 1 {
|
|
t.Errorf("ListWindows(holder-bryn) = %d, want 1", len(bryn))
|
|
}
|
|
nobody := s.ListWindows("nobody")
|
|
if len(nobody) != 0 {
|
|
t.Errorf("ListWindows(nobody) = %d, want 0", len(nobody))
|
|
}
|
|
}
|
|
|
|
func TestGetWindowHitMiss(t *testing.T) {
|
|
s := NewStore()
|
|
scope := windowtypes.Scope{Kind: windowtypes.ScopeReadStash}
|
|
rl := windowtypes.RateLimit{MaxActions: 1}
|
|
w, _ := s.OpenWindow("holder-alia", "svc", scope, 1, 2, rl)
|
|
if _, ok := s.GetWindow(w.WindowID); !ok {
|
|
t.Errorf("GetWindow(%q) miss, want hit", w.WindowID)
|
|
}
|
|
if _, ok := s.GetWindow("window-nobody"); ok {
|
|
t.Error("GetWindow(nobody) hit, want miss")
|
|
}
|
|
}
|
|
|
|
func TestGetAuditLogEmptyForMissing(t *testing.T) {
|
|
s := NewStore()
|
|
if logs := s.GetAuditLog("window-nobody"); logs != nil {
|
|
t.Errorf("GetAuditLog(nobody) = %v, want nil", logs)
|
|
}
|
|
}
|