oficina / fichas Public

Clone
git clone https://amadan.net/oficina/fichas

Plain git — no account needed to clone.

Download

Download this file

1package fichastest
2
3import (
4 "context"
5 "io"
6 "log/slog"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 fichas "amadan.net/oficina/fichas/internal/fichas"
13 "amadan.net/rastrillo/rastrillo/db"
14)
15
16// canaries are plaintexts the app seals. None may appear in any file it
17// writes.
18//
19// The first version of this list had seven entries and excluded "Customer"
20// with a comment claiming the opaque key `t_customer` "legitimately
21// contains it". **That was simply false** — strings.Contains is
22// case-sensitive and `t_customer` does not contain `Customer` — so the
23// exclusion weakened the gate for no reason and taught the next reader that
24// weakening it was normal. It is back.
25var canaries = []string{
26 // Card type and field labels, sealed by Seed.
27 "Customer", "Call log", "Name", "Email", "Phone", "Stage",
28 "Next action", "Notes", "When", "What was said", "Duration",
29 // Sealed enum options.
30 "Lead", "Contacted", "Proposal", "Won", "Lost",
31 // Values written through the store below.
32 "Cnoc Teoranta", "ana@example.com", "renewal is due in March",
33 "talked about the renewal",
34 // The change payload's own JSON keys, which are sealed with it.
35 `"before"`, `"after"`,
36}
37
38// scan reads every file in dir and returns the (file, canary) pairs found.
39// Shared by the gate and by the test that proves the gate can fail.
40func scan(t *testing.T, dir string, canaries []string) []string {
41 t.Helper()
42 entries, err := os.ReadDir(dir)
43 if err != nil {
44 t.Fatalf("reading %s: %v", dir, err)
45 }
46 var hits []string
47 read := 0
48 for _, e := range entries {
49 if e.IsDir() {
50 continue
51 }
52 raw, err := os.ReadFile(filepath.Join(dir, e.Name()))
53 if err != nil {
54 t.Fatalf("reading %s: %v", e.Name(), err)
55 }
56 read++
57 for _, c := range canaries {
58 if strings.Contains(string(raw), c) {
59 hits = append(hits, e.Name()+": "+c)
60 }
61 }
62 }
63 if read == 0 {
64 t.Fatal("no files were read: this scan would pass vacuously")
65 }
66 return hits
67}
68
69// TestPlaintextNeverReachesTheDatabase is the gate this app exists to pass.
70// memoria has one of the same shape; Sheets has the schema-side twin.
71//
72// It boots the real app, **writes through the real store path**, then reads
73// every byte on disk — database AND write-ahead log, because a value can
74// sit in the WAL for a long time before it is checkpointed, and a gate that
75// greps only the .db passes while the plaintext is on disk beside it.
76//
77// The earlier version only booted and seeded, so it never saw a single
78// value a person typed — in the app whose entire premise is what happens to
79// those.
80func TestPlaintextNeverReachesTheDatabase(t *testing.T) {
81 dir := t.TempDir()
82 logger := slog.New(slog.NewTextHandler(io.Discard, nil))
83
84 d, err := db.Open(filepath.Join(dir, "fichas.db"), logger)
85 if err != nil {
86 t.Fatalf("db.Open: %v", err)
87 }
88 if _, err := fichas.AppForTest(d, testConfig(t), logger, TestOwner); err != nil {
89 t.Fatalf("App: %v", err)
90 }
91
92 s := testSealer(t)
93 pr, err := s.Print("f_member", []byte("ana@example.com"))
94 if err != nil {
95 t.Fatalf("print: %v", err)
96 }
97 if err := d.G.Create(&fichas.Member{AddressPrint: pr}).Error; err != nil {
98 t.Fatalf("creating a member: %v", err)
99 }
100
101 card, err := fichas.CreateCard(context.Background(), d.G, s, fichas.TypeCustomer, fichas.Person(1),
102 map[string]string{
103 fichas.FieldName: "Cnoc Teoranta",
104 fichas.FieldEmail: "ana@example.com",
105 fichas.FieldStage: "Lead",
106 })
107 if err != nil {
108 t.Fatalf("CreateCard: %v", err)
109 }
110 if err := fichas.UpdateCard(context.Background(), d.G, s, card.ID, fichas.Person(1),
111 map[string]string{fichas.FieldNotes: "renewal is due in March"}); err != nil {
112 t.Fatalf("UpdateCard: %v", err)
113 }
114 if _, err := fichas.AddEntry(context.Background(), d.G, s, card.ID, fichas.Person(1),
115 fichas.TypeCallLog, "manual",
116 map[string]string{fichas.FieldBody: "talked about the renewal"}); err != nil {
117 t.Fatalf("AddEntry: %v", err)
118 }
119
120 // Close so the WAL is checkpointed and every byte is on disk to read.
121 if err := d.Close(); err != nil {
122 t.Fatalf("Close: %v", err)
123 }
124
125 for _, hit := range scan(t, dir, canaries) {
126 t.Errorf("plaintext on disk — %s", hit)
127 }
128}
129
130// The gate is only worth something if it can fail, and it must be THIS
131// gate that fails, not a hand-rolled grep standing in for it. So: plant
132// every canary in a file in a directory and assert scan finds them all.
133func TestTheGateCanFail(t *testing.T) {
134 dir := t.TempDir()
135 planted := "header " + strings.Join(canaries, " ") + " footer"
136 if err := os.WriteFile(filepath.Join(dir, "planted.db"), []byte(planted), 0o600); err != nil {
137 t.Fatalf("WriteFile: %v", err)
138 }
139 if hits := scan(t, dir, canaries); len(hits) != len(canaries) {
140 t.Fatalf("scan found %d of %d planted canaries; the gate does not look for what it claims",
141 len(hits), len(canaries))
142 }
143}
144