Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
92 lines
3.0 KiB
Go
92 lines
3.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"testing"
|
|
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
func TestApprovalRequestIsIdempotentAndGenerationFenced(t *testing.T) {
|
|
db, err := sql.Open("sqlite", ":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
s := &Store{DB: db}
|
|
if err := s.EnsurePublicationApprovalSchema(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hash, body, err := ApprovalSubjectHash(map[string]any{"path": "/docs/a", "revision": "rev-1"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
first, inserted, err := s.CreateApprovalRequestTx(tx, ApprovalRequest{DomainUUID: "d", ScopeKind: "spot", ScopeUUID: "spot-1", SubjectHash: hash, SubjectJSON: body, RequesterUUID: "u", IdempotencyKey: "idem-1"})
|
|
if err != nil || !inserted {
|
|
t.Fatalf("first=%+v inserted=%v err=%v", first, inserted, err)
|
|
}
|
|
second, inserted, err := s.CreateApprovalRequestTx(tx, ApprovalRequest{DomainUUID: "d", ScopeKind: "spot", ScopeUUID: "spot-1", SubjectHash: hash, SubjectJSON: body, RequesterUUID: "u", IdempotencyKey: "idem-1"})
|
|
if err != nil || inserted || second.UUID != first.UUID {
|
|
t.Fatalf("duplicate=%+v inserted=%v err=%v", second, inserted, err)
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.DecideApprovalRequest(context.Background(), first.UUID, "reviewer", "approved", "ok", 2); err == nil {
|
|
t.Fatal("stale generation was accepted")
|
|
}
|
|
if err := s.DecideApprovalRequest(context.Background(), first.UUID, "reviewer", "approved", "ok", 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.DecideApprovalRequest(context.Background(), first.UUID, "reviewer-2", "approved", "replay", 1); err == nil {
|
|
t.Fatal("second decision was accepted")
|
|
}
|
|
}
|
|
|
|
func TestApprovalRequestExpiryIsBounded(t *testing.T) {
|
|
db, err := sql.Open("sqlite", ":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
s := &Store{DB: db}
|
|
if err := s.EnsurePublicationApprovalSchema(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hash, body, err := ApprovalSubjectHash(map[string]string{"path": "/docs/a"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 2; i++ {
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, _, err = s.CreateApprovalRequestTx(tx, ApprovalRequest{UUID: "request-expired-" + string(rune('a'+i)), DomainUUID: "d", ScopeKind: "spot", ScopeUUID: "s", SubjectHash: hash, SubjectJSON: body, RequesterUUID: "u", IdempotencyKey: "expired-" + string(rune('a'+i)), ExpiresAtMS: 10})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := s.ExpireApprovalRequests(context.Background(), 20, 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var pending, expired int
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM publication_requests WHERE state='pending'`).Scan(&pending); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM publication_requests WHERE state='expired'`).Scan(&expired); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if pending != 1 || expired != 1 {
|
|
t.Fatalf("pending=%d expired=%d", pending, expired)
|
|
}
|
|
}
|