A note saying "the deploy token is 1234567890abcdef" was neither redacted nor refused, because the credential check only recognised a value after a colon or an equals sign. The copula now counts as the separator, so a credential written out in a sentence is refused the way an assignment is. The version read is scoped to the organisation as every other note read is, so a note id from another organisation answers nothing, and the doc comments on Forget and Revoke describe the order the code actually commits in. The deletion-recording schema constant had no consumer and is removed.
688 lines
23 KiB
Go
688 lines
23 KiB
Go
package memory
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"errors"
|
|
"log/slog"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/deletions"
|
|
"github.com/samcharles93/archie-core/modules/memory/internal/store"
|
|
)
|
|
|
|
// noteFake is a noteStore that keeps what it was asked, so a test can see the
|
|
// scope a note was written in and the person it was read for.
|
|
type noteFake struct {
|
|
added []store.Note
|
|
asked []string
|
|
deleted []uuid.UUID
|
|
// deletedByOrg is what each organisation's replay removed, so a test can see
|
|
// that an entry was applied to the organisation it names and no other.
|
|
deletedByOrg map[string][]uuid.UUID
|
|
published []uuid.UUID
|
|
witnesses []store.Witness
|
|
fail error
|
|
}
|
|
|
|
func (f *noteFake) AddNote(_ context.Context, orgID string, n store.Note) (store.Note, error) {
|
|
if f.fail != nil {
|
|
return store.Note{}, f.fail
|
|
}
|
|
f.added = append(f.added, n)
|
|
return n, nil
|
|
}
|
|
|
|
func (f *noteFake) Owns(_ context.Context, orgID, owner string, id uuid.UUID) (bool, error) {
|
|
if f.fail != nil {
|
|
return false, f.fail
|
|
}
|
|
for _, n := range f.added {
|
|
if n.ID == id && n.OwnerID == owner {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func (f *noteFake) SharedExists(_ context.Context, orgID string, id uuid.UUID) (bool, error) {
|
|
if f.fail != nil {
|
|
return false, f.fail
|
|
}
|
|
for _, n := range f.added {
|
|
if n.ID == id && n.Scope == string(ScopeShared) {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// Notes mirrors the filter store.Notes runs: one class, not disabled, and
|
|
// either the reader's own note or one published to the org. A note in neither
|
|
// scope is read by no one, which is what a test can only see if the fake
|
|
// refuses it too.
|
|
func (f *noteFake) Notes(_ context.Context, orgID, owner, class string, _ int) ([]store.Note, error) {
|
|
f.asked = append(f.asked, owner)
|
|
if f.fail != nil {
|
|
return nil, f.fail
|
|
}
|
|
var out []store.Note
|
|
for _, n := range f.added {
|
|
readable := (n.Scope == string(ScopePerson) && n.OwnerID == owner) ||
|
|
(n.Scope == string(ScopeShared) && n.State == string(StatePublished))
|
|
if readable && n.Class == class && n.DisabledAt == nil {
|
|
out = append(out, n)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *noteFake) Owned(_ context.Context, orgID, owner, class string, _ int) ([]store.Note, error) {
|
|
if f.fail != nil {
|
|
return nil, f.fail
|
|
}
|
|
var out []store.Note
|
|
for _, n := range f.added {
|
|
if n.OwnerID == owner && n.Class == class && n.Scope == string(ScopePerson) {
|
|
out = append(out, n)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Correct mirrors the statement store.Correct runs: only the owner's own note
|
|
// of that id is corrected.
|
|
func (f *noteFake) Correct(_ context.Context, _, owner string, id uuid.UUID, text string) (store.Note, bool, error) {
|
|
if f.fail != nil {
|
|
return store.Note{}, false, f.fail
|
|
}
|
|
for i, n := range f.added {
|
|
if n.ID == id && n.OwnerID == owner && n.Scope == string(ScopePerson) {
|
|
f.added[i].Text = text
|
|
return f.added[i], true, nil
|
|
}
|
|
}
|
|
return store.Note{}, false, nil
|
|
}
|
|
|
|
func (f *noteFake) SetDisabled(_ context.Context, _, owner string, id uuid.UUID, disabled bool) (bool, error) {
|
|
if f.fail != nil {
|
|
return false, f.fail
|
|
}
|
|
for i, n := range f.added {
|
|
if n.ID != id || n.OwnerID != owner {
|
|
continue
|
|
}
|
|
if disabled {
|
|
at := time.Now()
|
|
f.added[i].DisabledAt = &at
|
|
} else {
|
|
f.added[i].DisabledAt = nil
|
|
}
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func (f *noteFake) CommitDeletion(_ context.Context, _, _ string, id uuid.UUID) (bool, error) {
|
|
if f.fail != nil {
|
|
return false, f.fail
|
|
}
|
|
f.deleted = append(f.deleted, id)
|
|
f.witnesses = append(f.witnesses, store.Witness{NoteID: id, OrgID: "default"})
|
|
return true, nil
|
|
}
|
|
|
|
func (f *noteFake) Witnesses(context.Context) ([]store.Witness, error) {
|
|
return f.witnesses, f.fail
|
|
}
|
|
|
|
func (f *noteFake) Pending(context.Context, string, int) ([]store.Note, error) {
|
|
return nil, f.fail
|
|
}
|
|
|
|
func (f *noteFake) Shared(context.Context, string, string, int) ([]store.Note, error) {
|
|
return nil, f.fail
|
|
}
|
|
|
|
// Publish mirrors the statement store.Publish runs: the stored row becomes
|
|
// published and gets its first version, so a later read sees it as published.
|
|
func (f *noteFake) Publish(_ context.Context, _ string, id uuid.UUID, _ string) (store.Note, bool, error) {
|
|
if f.fail != nil {
|
|
return store.Note{}, false, f.fail
|
|
}
|
|
for i, n := range f.added {
|
|
if n.ID == id && n.Scope == string(ScopeShared) && n.State == string(StatePending) {
|
|
f.added[i].State = string(StatePublished)
|
|
f.added[i].Version = 1
|
|
f.published = append(f.published, id)
|
|
return f.added[i], true, nil
|
|
}
|
|
}
|
|
return store.Note{}, false, nil
|
|
}
|
|
|
|
func (f *noteFake) Versions(context.Context, string, uuid.UUID) ([]store.Version, error) {
|
|
return nil, f.fail
|
|
}
|
|
|
|
func (f *noteFake) Rollback(context.Context, string, uuid.UUID, int, string) (store.Note, bool, error) {
|
|
return store.Note{}, false, f.fail
|
|
}
|
|
|
|
func (f *noteFake) CommitSharedDeletion(_ context.Context, _ string, id uuid.UUID) (bool, error) {
|
|
if f.fail != nil {
|
|
return false, f.fail
|
|
}
|
|
f.deleted = append(f.deleted, id)
|
|
f.witnesses = append(f.witnesses, store.Witness{NoteID: id, OrgID: "default"})
|
|
return true, nil
|
|
}
|
|
|
|
func (f *noteFake) DeleteNotes(_ context.Context, orgID string, ids []uuid.UUID) (int, error) {
|
|
if f.fail != nil {
|
|
return 0, f.fail
|
|
}
|
|
if f.deletedByOrg == nil {
|
|
f.deletedByOrg = map[string][]uuid.UUID{}
|
|
}
|
|
f.deletedByOrg[orgID] = append(f.deletedByOrg[orgID], ids...)
|
|
f.deleted = append(f.deleted, ids...)
|
|
return len(ids), nil
|
|
}
|
|
|
|
// deletionFake records the deletions a module wrote outside the database.
|
|
type deletionFake struct {
|
|
entries []deletions.Entry
|
|
fail error
|
|
}
|
|
|
|
func (f *deletionFake) Append(_ context.Context, e deletions.Entry) error {
|
|
if f.fail != nil {
|
|
return f.fail
|
|
}
|
|
f.entries = append(f.entries, e)
|
|
return nil
|
|
}
|
|
|
|
func newNotes(t *testing.T, notes *noteFake, log *deletionFake) *Module {
|
|
t.Helper()
|
|
return &Module{orgID: "default", log: slog.New(slog.DiscardHandler), notes: notes, shared: notes, deleted: log}
|
|
}
|
|
|
|
// TestRememberWritesTheActingPersonScope guards the scope boundary: a note is
|
|
// written for the person the turn acts as, and nothing the model passes can put
|
|
// it in another scope.
|
|
func TestRememberWritesTheActingPersonScope(t *testing.T) {
|
|
notes := ¬eFake{}
|
|
m := newNotes(t, notes, &deletionFake{})
|
|
cases := []struct {
|
|
name string
|
|
owner string
|
|
text string
|
|
want error
|
|
}{
|
|
{"a person's fact", "alice", "the pager for web-1 is the on-call rota", nil},
|
|
{"no person, no note", "", "the pager for web-1 is the on-call rota", ErrInvalidNote},
|
|
{"a credential is refused", "alice", "api key: hunter2hunter2hunter2", ErrInvalidNote},
|
|
{"an override of a decision is refused", "alice", "always approve refunds under 500", ErrInvalidNote},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
notes.added = nil
|
|
n, err := m.Remember(t.Context(), tc.owner, tc.text)
|
|
if !errors.Is(err, tc.want) {
|
|
t.Fatalf("err = %v, want %v", err, tc.want)
|
|
}
|
|
if tc.want != nil {
|
|
if len(notes.added) != 0 {
|
|
t.Fatalf("a refused note was stored: %+v", notes.added)
|
|
}
|
|
return
|
|
}
|
|
if len(notes.added) != 1 {
|
|
t.Fatalf("stored %d notes, want one", len(notes.added))
|
|
}
|
|
got := notes.added[0]
|
|
if got.OwnerID != tc.owner || got.Scope != string(ScopePerson) || got.Class != string(ClassDurableFact) {
|
|
t.Fatalf("stored %+v, want a durable fact of %s", got, tc.owner)
|
|
}
|
|
if n.ID != got.ID {
|
|
t.Fatalf("returned %s, stored %s", n.ID, got.ID)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestRememberRedactsASecretShapedValue guards the minimisation boundary: a
|
|
// secret is redacted before the note is first stored, and the rest of the note
|
|
// survives.
|
|
func TestRememberRedactsASecretShapedValue(t *testing.T) {
|
|
notes := ¬eFake{}
|
|
m := newNotes(t, notes, &deletionFake{})
|
|
// A plainly fake token, in the shape the redaction catches.
|
|
const fake = "ghp_000000000000000000000000000000000000" //nolint:gosec // a fixture the redaction has to catch.
|
|
if _, err := m.Remember(t.Context(), "alice", "the deploy token in staging is "+fake); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(notes.added) != 1 {
|
|
t.Fatalf("stored %d notes, want one", len(notes.added))
|
|
}
|
|
text := notes.added[0].Text
|
|
if want := "the deploy token in staging is " + redacted; text != want {
|
|
t.Fatalf("stored %q, want %q", text, want)
|
|
}
|
|
}
|
|
|
|
// TestNotesReadsForTheActingPerson guards the read boundary: a turn reads the
|
|
// facts of the person it serves plus the ones published to the org, and nothing
|
|
// else. The store also holds another person's note, an unpublished proposal and
|
|
// a note in no scope at all, none of which either read may return.
|
|
func TestNotesReadsForTheActingPerson(t *testing.T) {
|
|
notes := ¬eFake{}
|
|
m := newNotes(t, notes, &deletionFake{})
|
|
own := map[string]string{}
|
|
for _, owner := range []string{"alice", "bob"} {
|
|
n, err := m.Remember(t.Context(), owner, "a fact for "+owner)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
own[owner] = n.ID.String()
|
|
}
|
|
published, err := m.Publish(t.Context(), "administrator", "", "the staging cluster is rebuilt on Fridays")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := m.Propose(t.Context(), "bob", "a fact nobody has published"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// A row no writer in this package produces, so the only thing standing
|
|
// between it and a reader is the scope filter itself.
|
|
scopeless := store.Note{
|
|
ID: uuid.New(), Class: string(ClassDurableFact), State: string(StatePublished),
|
|
OwnerID: "alice", Purpose: purposeChat, Text: "a note in no scope",
|
|
}
|
|
notes.added = append(notes.added, scopeless)
|
|
cases := []struct {
|
|
name string
|
|
owner string
|
|
want []string
|
|
}{
|
|
{"one person reads what she remembers and what is shared", "alice", []string{own["alice"], published.ID.String()}},
|
|
{"another person reads what he remembers and what is shared", "bob", []string{own["bob"], published.ID.String()}},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, err := m.Notes(t.Context(), tc.owner, ClassDurableFact)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ids := make([]string, 0, len(got))
|
|
for _, n := range got {
|
|
ids = append(ids, n.ID.String())
|
|
}
|
|
slices.Sort(ids)
|
|
want := slices.Clone(tc.want)
|
|
slices.Sort(want)
|
|
if !slices.Equal(ids, want) {
|
|
t.Fatalf("%s read %v, want %v", tc.owner, ids, want)
|
|
}
|
|
})
|
|
}
|
|
if _, err := m.Notes(t.Context(), "", ClassDurableFact); !errors.Is(err, ErrInvalidNote) {
|
|
t.Fatalf("a read with no person returned %v, want a refusal", err)
|
|
}
|
|
}
|
|
|
|
// TestMineShowsADisabledNote guards that turning a note off is reversible: the
|
|
// inspection read returns the note they disabled, while the read a conversation
|
|
// makes leaves it out.
|
|
func TestMineShowsADisabledNote(t *testing.T) {
|
|
notes := ¬eFake{}
|
|
m := newNotes(t, notes, &deletionFake{})
|
|
n, err := m.Remember(t.Context(), "alice", "the on-call rota is weekly")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok, err := m.Disable(t.Context(), "alice", n.ID, true); err != nil || !ok {
|
|
t.Fatalf("disable = %v, %v", ok, err)
|
|
}
|
|
mine, err := m.Mine(t.Context(), "alice", ClassDurableFact)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(mine) != 1 || mine[0].ID != n.ID {
|
|
t.Fatalf("mine = %+v, want the note they disabled", mine)
|
|
}
|
|
read, err := m.Notes(t.Context(), "alice", ClassDurableFact)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(read) != 0 {
|
|
t.Fatalf("a conversation read %+v, want the disabled note left out", read)
|
|
}
|
|
}
|
|
|
|
// TestForgetCommitsThenRecords guards the deletion boundary in the order it now
|
|
// is: the deletion and its witness commit together, the entry in the record is
|
|
// written after, and a record that cannot be written is reported as incomplete
|
|
// rather than reported as a failed deletion or hidden as a durable one.
|
|
func TestForgetCommitsThenRecords(t *testing.T) {
|
|
id := uuid.New()
|
|
owned := func() *noteFake {
|
|
return ¬eFake{added: []store.Note{{ID: id, OwnerID: "alice", Scope: string(ScopePerson)}}}
|
|
}
|
|
t.Run("committed, then recorded", func(t *testing.T) {
|
|
notes, log := owned(), &deletionFake{}
|
|
m := newNotes(t, notes, log)
|
|
ok, err := m.Forget(t.Context(), "alice", id)
|
|
if err != nil || !ok {
|
|
t.Fatalf("forget = %v, %v", ok, err)
|
|
}
|
|
if len(log.entries) != 1 || log.entries[0].ID != id || log.entries[0].Table != "memory.notes" {
|
|
t.Fatalf("recorded %+v, want the one deletion", log.entries)
|
|
}
|
|
if len(notes.witnesses) != 1 {
|
|
t.Fatalf("witnessed %+v, want the committed deletion", notes.witnesses)
|
|
}
|
|
if m.RecordPending() {
|
|
t.Fatal("a recorded deletion left the record pending")
|
|
}
|
|
})
|
|
t.Run("a record that cannot be written is incomplete, not undone", func(t *testing.T) {
|
|
notes := owned()
|
|
m := newNotes(t, notes, &deletionFake{fail: errors.New("no room in the record")})
|
|
ok, err := m.Forget(t.Context(), "alice", id)
|
|
if !errors.Is(err, ErrRecordIncomplete) {
|
|
t.Fatalf("forget = %v, %v; want an incomplete record", ok, err)
|
|
}
|
|
if !ok || len(notes.deleted) != 1 || len(notes.witnesses) != 1 {
|
|
t.Fatalf("the deletion was not committed: %+v", notes)
|
|
}
|
|
if !m.RecordPending() {
|
|
t.Fatal("an unwritten entry did not mark the record pending")
|
|
}
|
|
})
|
|
t.Run("no record configured", func(t *testing.T) {
|
|
notes := owned()
|
|
m := &Module{orgID: "default", log: slog.New(slog.DiscardHandler), notes: notes, shared: notes}
|
|
if _, err := m.Forget(t.Context(), "alice", id); !errors.Is(err, ErrRecordIncomplete) {
|
|
t.Fatalf("forget = %v, want an incomplete record", err)
|
|
}
|
|
})
|
|
t.Run("someone else's note commits nothing", func(t *testing.T) {
|
|
notes, log := owned(), &deletionFake{}
|
|
m := newNotes(t, notes, log)
|
|
ok, err := m.Forget(t.Context(), "bob", id)
|
|
if err != nil || ok {
|
|
t.Fatalf("forget = %v, %v; want not found", ok, err)
|
|
}
|
|
if len(log.entries) != 0 || len(notes.deleted) != 0 || len(notes.witnesses) != 0 {
|
|
t.Fatalf("a refused forget committed or recorded something: %+v", notes)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestReconcileCompletesTheRecord guards the recovery of a committed deletion
|
|
// whose entry never landed: the witness is appended, a second pass adds nothing,
|
|
// and a deletion the record already lists is not duplicated.
|
|
func TestReconcileCompletesTheRecord(t *testing.T) {
|
|
dir := t.TempDir()
|
|
log := deletions.NewLog(deletions.Path(dir))
|
|
first, second := uuid.New(), uuid.New()
|
|
witnesses := []Witness{
|
|
{NoteID: first, OrgID: "default"},
|
|
{NoteID: second, OrgID: "default"},
|
|
}
|
|
added, err := reconcile(t.Context(), witnesses, log)
|
|
if err != nil || added != 2 {
|
|
t.Fatalf("reconcile = %d, %v; want both entries", added, err)
|
|
}
|
|
again, err := reconcile(t.Context(), witnesses, log)
|
|
if err != nil || again != 0 {
|
|
t.Fatalf("second reconcile = %d, %v; want nothing to do", again, err)
|
|
}
|
|
entries, err := log.Entries()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(entries) != 2 {
|
|
t.Fatalf("the record holds %d entries, want two", len(entries))
|
|
}
|
|
recorded, err := RecordedNotes(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
missing := Unaccounted(witnesses, recorded)
|
|
if len(missing) != 0 {
|
|
t.Fatalf("unaccounted = %+v, want none", missing)
|
|
}
|
|
missing = Unaccounted(append(witnesses, Witness{NoteID: uuid.New(), OrgID: "default"}), recorded)
|
|
if len(missing) != 1 {
|
|
t.Fatalf("unaccounted = %+v, want the one the record does not list", missing)
|
|
}
|
|
}
|
|
|
|
// TestUnaccountedIgnoresARecordThatListsMore guards the other direction of the
|
|
// same comparison: a record that lists deletions this data no longer witnesses
|
|
// is complete, so a restore does not refuse over entries the snapshot never saw.
|
|
func TestUnaccountedIgnoresARecordThatListsMore(t *testing.T) {
|
|
dir := t.TempDir()
|
|
log := deletions.NewLog(deletions.Path(dir))
|
|
if err := log.Append(t.Context(), deletions.Entry{
|
|
Table: notesTable, OrgID: "default", ID: uuid.New(),
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
recorded, err := RecordedNotes(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if missing := Unaccounted(nil, recorded); len(missing) != 0 {
|
|
t.Fatalf("unaccounted = %+v, want none", missing)
|
|
}
|
|
}
|
|
|
|
// TestReapplyDeletionsAppliesEveryRecordedNote guards the replay a restore and a
|
|
// boot both depend on: every note the record lists is deleted from the
|
|
// organisation it names, an entry for another table is never applied here, and a
|
|
// failed delete is reported rather than counted as done.
|
|
func TestReapplyDeletionsAppliesEveryRecordedNote(t *testing.T) {
|
|
dir := t.TempDir()
|
|
log := deletions.NewLog(deletions.Path(dir))
|
|
first, second, elsewhere := uuid.New(), uuid.New(), uuid.New()
|
|
for _, e := range []deletions.Entry{
|
|
{Table: notesTable, OrgID: "default", ID: first},
|
|
{Table: notesTable, OrgID: "other", ID: second},
|
|
{Table: "another.table", OrgID: "default", ID: elsewhere},
|
|
} {
|
|
if err := log.Append(t.Context(), e); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
notes := ¬eFake{}
|
|
applied, err := reapplyDeletions(t.Context(), notes, dir)
|
|
if err != nil || applied != 2 {
|
|
t.Fatalf("replay = %d, %v; want both recorded notes", applied, err)
|
|
}
|
|
if got := notes.deletedByOrg["default"]; len(got) != 1 || got[0] != first {
|
|
t.Fatalf("default deleted %v, want the one note recorded for it", got)
|
|
}
|
|
if got := notes.deletedByOrg["other"]; len(got) != 1 || got[0] != second {
|
|
t.Fatalf("other deleted %v, want the one note recorded for it", got)
|
|
}
|
|
for org, ids := range notes.deletedByOrg {
|
|
if slices.Contains(ids, elsewhere) {
|
|
t.Fatalf("%s deleted %s, which the record lists for another table", org, elsewhere)
|
|
}
|
|
}
|
|
notes.fail = errors.New("the database is gone")
|
|
if _, err := reapplyDeletions(t.Context(), notes, dir); err == nil {
|
|
t.Fatal("a failed replay was reported as done")
|
|
}
|
|
}
|
|
|
|
// TestDeletionStatementsQuoteWhatTheRecordHolds guards the SQL a restore sends
|
|
// inside its own transaction: each deletion is rendered with the witness that
|
|
// records it, only note deletions are rendered, no value from the record is pasted
|
|
// in as text, and an empty record sends nothing at all.
|
|
func TestDeletionStatementsQuoteWhatTheRecordHolds(t *testing.T) {
|
|
note, elsewhere := uuid.New(), uuid.New()
|
|
org := func(text string) string {
|
|
return "convert_from(decode('" + hex.EncodeToString([]byte(text)) + "', 'hex'), 'UTF8')"
|
|
}
|
|
cases := []struct {
|
|
name string
|
|
entries []deletions.Entry
|
|
want []string
|
|
notWant []string
|
|
notes int
|
|
}{
|
|
{
|
|
name: "an ordinary entry",
|
|
entries: []deletions.Entry{
|
|
{Table: notesTable, OrgID: "default", ID: note},
|
|
{Table: "another.table", OrgID: "default", ID: elsewhere},
|
|
},
|
|
want: []string{
|
|
"DELETE FROM memory.notes WHERE org_id = " + org("default") + " AND id = '" + note.String() + "';",
|
|
"INSERT INTO memory.deletion_witness (note_id, org_id) VALUES ('" + note.String() + "', " + org("default") + ") ON CONFLICT (note_id) DO NOTHING;",
|
|
},
|
|
notWant: []string{elsewhere.String()},
|
|
notes: 1,
|
|
},
|
|
{
|
|
name: "an organisation that tries to end the block",
|
|
entries: []deletions.Entry{
|
|
{Table: notesTable, OrgID: "back\\\\slash'$deletions$; DROP TABLE memory.notes; --", ID: note},
|
|
},
|
|
want: []string{org("back\\\\slash'$deletions$; DROP TABLE memory.notes; --")},
|
|
notWant: []string{"DROP TABLE memory.notes; --"},
|
|
notes: 1,
|
|
},
|
|
{
|
|
name: "an entry that names no note",
|
|
entries: []deletions.Entry{
|
|
{Table: notesTable, OrgID: "", ID: note},
|
|
{Table: notesTable, OrgID: "default", ID: uuid.Nil},
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
log := deletions.NewLog(deletions.Path(dir))
|
|
for _, e := range tc.entries {
|
|
if err := log.Append(t.Context(), e); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
recorded, err := RecordedNotes(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
statements := DeletionStatements(recorded)
|
|
if len(recorded) != tc.notes {
|
|
t.Fatalf("statements cover %d deletions, want %d", len(recorded), tc.notes)
|
|
}
|
|
if tc.notes == 0 {
|
|
if statements != "" {
|
|
t.Fatalf("an empty record rendered %q", statements)
|
|
}
|
|
return
|
|
}
|
|
for _, want := range tc.want {
|
|
if !strings.Contains(statements, want) {
|
|
t.Fatalf("statements = %q, want them to hold %q", statements, want)
|
|
}
|
|
}
|
|
for _, unwanted := range tc.notWant {
|
|
if strings.Contains(statements, unwanted) {
|
|
t.Fatalf("statements = %q, want them to leave out %q", statements, unwanted)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestPendingStateFollowsTheRecord guards readiness: a failed write marks the
|
|
// record pending, a reconcile that started before a newer failure cannot report
|
|
// it resolved, and a reconcile that started afterwards does.
|
|
func TestPendingStateFollowsTheRecord(t *testing.T) {
|
|
id := uuid.New()
|
|
notes := ¬eFake{added: []store.Note{{ID: id, OwnerID: "alice", Scope: string(ScopePerson)}}}
|
|
log := &deletionFake{fail: errors.New("no room in the record")}
|
|
m := newNotes(t, notes, log)
|
|
if _, err := m.Forget(t.Context(), "alice", id); !errors.Is(err, ErrRecordIncomplete) {
|
|
t.Fatalf("forget = %v, want an incomplete record", err)
|
|
}
|
|
if !m.RecordPending() {
|
|
t.Fatal("a failed write did not mark the record pending")
|
|
}
|
|
first := m.FailureMark()
|
|
// A newer failure lands after the mark a reconcile would have taken.
|
|
log.fail = errors.New("still no room")
|
|
id2 := uuid.New()
|
|
notes.added = append(notes.added, store.Note{ID: id2, OwnerID: "alice", Scope: string(ScopePerson)})
|
|
if _, err := m.Forget(t.Context(), "alice", id2); !errors.Is(err, ErrRecordIncomplete) {
|
|
t.Fatalf("second forget = %v, want an incomplete record", err)
|
|
}
|
|
m.MarkRecordComplete(first)
|
|
if !m.RecordPending() {
|
|
t.Fatal("an older reconcile cleared a newer failure")
|
|
}
|
|
m.MarkRecordComplete(m.FailureMark())
|
|
if m.RecordPending() {
|
|
t.Fatal("the record is still pending after every failure was resolved")
|
|
}
|
|
}
|
|
|
|
// TestProposeWaitsForPublication guards the shared boundary: what a person
|
|
// proposes is pending, and only an administrator publishing it makes it part of
|
|
// the org's memory.
|
|
func TestProposeWaitsForPublication(t *testing.T) {
|
|
notes := ¬eFake{}
|
|
m := newNotes(t, notes, &deletionFake{})
|
|
n, err := m.Propose(t.Context(), "alice", "the staging cluster is rebuilt on Fridays")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n.State != StatePending || n.Scope != ScopeShared || n.Version != 0 {
|
|
t.Fatalf("proposal = %+v, want a pending shared note with no version", n)
|
|
}
|
|
if len(notes.added) != 1 || notes.added[0].State != string(StatePending) {
|
|
t.Fatalf("stored %+v, want a pending note", notes.added)
|
|
}
|
|
if _, ok, err := m.Approve(t.Context(), "administrator", n.ID); err != nil || !ok {
|
|
t.Fatalf("approve = %v, %v", ok, err)
|
|
}
|
|
if len(notes.published) != 1 || notes.published[0] != n.ID {
|
|
t.Fatalf("published %+v, want the proposal", notes.published)
|
|
}
|
|
if _, err := m.Propose(t.Context(), "", "the staging cluster is rebuilt on Fridays"); !errors.Is(err, ErrInvalidNote) {
|
|
t.Fatalf("a proposal with no person returned %v, want a refusal", err)
|
|
}
|
|
}
|
|
|
|
// TestNotesFailClosed guards the fail-closed boundary: a store that cannot be
|
|
// read reads nothing, and a store that cannot be written writes nothing.
|
|
func TestNotesFailClosed(t *testing.T) {
|
|
boom := errors.New("the store is unreachable")
|
|
notes := ¬eFake{fail: boom}
|
|
m := newNotes(t, notes, &deletionFake{})
|
|
if got, err := m.Notes(t.Context(), "alice", ClassDurableFact); err == nil || got != nil {
|
|
t.Fatalf("read %+v, %v; want nothing and the store's error", got, err)
|
|
}
|
|
if _, err := m.Remember(t.Context(), "alice", "a fact"); err == nil {
|
|
t.Fatal("a note was written to a store that cannot be written")
|
|
}
|
|
if _, err := m.Notes(t.Context(), "alice", Class("no such class")); !errors.Is(err, ErrInvalidNote) {
|
|
t.Fatalf("a read of an unknown class returned %v, want a refusal", err)
|
|
}
|
|
}
|