oficina / calendar Public

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

Plain git — no account needed to clone.

Download

Download this file

1package calendar
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "net/http/httptest"
8 "strings"
9 "sync/atomic"
10 "testing"
11
12 access "amadan.net/oficina/agent-access.git"
13 "amadan.net/oficina/calendar/internal/calendar/seal"
14 "gorm.io/gorm"
15)
16
17func TestAgentEventPrecommitAuthority(t *testing.T) {
18 for _, mode := range []string{"reserved-revoke", "final-revoke", "binding", "unlink", "grant"} {
19 t.Run(mode, func(t *testing.T) {
20 f, p, _, cal := linkedCalendarAgent(t)
21 var checks atomic.Int64
22 if e := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) {
23 n := checks.Add(1)
24 if mode == "reserved-revoke" && n >= 2 || mode == "final-revoke" && n >= 3 {
25 return access.Principal{}, access.Fail("unauthenticated", "Revoked.")
26 }
27 return *p, nil
28 }), f.app.agentIssuer, "acme"); e != nil {
29 t.Fatal(e)
30 }
31 if mode == "reserved-revoke" {
32 if _, e := f.app.db.Writer().Exec("CREATE TRIGGER body_must_not_run BEFORE INSERT ON events BEGIN SELECT RAISE(ABORT,'body attempted'); END"); e != nil {
33 t.Fatal(e)
34 }
35 }
36 change := ""
37 if mode == "binding" {
38 change = "UPDATE home_identity_links SET person_binding='broken'"
39 }
40 if mode == "unlink" {
41 change = "DELETE FROM home_identity_links"
42 }
43 if mode == "grant" {
44 change = "UPDATE grants SET role='viewer'"
45 }
46 if change != "" {
47 if _, e := f.app.db.Writer().Exec("CREATE TRIGGER lose_agent_authority AFTER UPDATE OF event_id ON calendar_agent_events BEGIN " + change + "; END"); e != nil {
48 t.Fatal(e)
49 }
50 }
51 rec := calendarAgentCall(f.app, "calendar_create_event", eventAgentInput(cal))
52 if rec.Code != 401 && rec.Code != 403 {
53 t.Fatal("lost authority committed", mode, rec.Code, rec.Body.String())
54 }
55 for _, table := range []string{"events", "reminders", "reminder_queues", "calendar_agent_events"} {
56 var n int
57 if e := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM " + table).Scan(&n); e != nil || n != 0 {
58 t.Fatal("partial event", table, n, e)
59 }
60 }
61 })
62 }
63}
64func TestAgentEventAuthorityBindingRestart(t *testing.T) {
65 f, p, _, cal := linkedCalendarAgent(t)
66 input := eventAgentInput(cal)
67 first := calendarAgentCall(f.app, "calendar_create_event", input)
68 if first.Code != 200 {
69 t.Fatal(first.Code)
70 }
71 cfg := f.app.cfg
72 cfg.AgentIssuer = f.app.agentIssuer
73 cfg.AgentTeam = "acme"
74 if e := f.app.Close(); e != nil {
75 t.Fatal(e)
76 }
77 restarted, e := New(cfg)
78 if e != nil {
79 t.Fatal(e)
80 }
81 defer restarted.Close()
82 if e = restarted.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return *p, nil }), cfg.AgentIssuer, cfg.AgentTeam); e != nil {
83 t.Fatal(e)
84 }
85 rec := calendarAgentCall(restarted, "calendar_create_event", input)
86 if rec.Code != 200 || rec.Body.String() != first.Body.String() {
87 t.Fatal("restart changed receipt", rec.Code, rec.Body.String())
88 }
89 for _, kind := range []string{"issuer", "home", "team", "key"} {
90 t.Run(kind, func(t *testing.T) {
91 oldIssuer, oldHome, oldTeam, oldSeal := restarted.agentIssuer, restarted.cfg.Home.Issuer, restarted.agentTeam, restarted.store.seal
92 defer func() {
93 restarted.agentIssuer = oldIssuer
94 restarted.cfg.Home.Issuer = oldHome
95 restarted.agentTeam = oldTeam
96 restarted.store.seal = oldSeal
97 }()
98 switch kind {
99 case "issuer":
100 restarted.agentIssuer = "https://other-connect.test"
101 case "home":
102 restarted.cfg.Home.Issuer = "https://other-home.test"
103 case "team":
104 restarted.agentTeam = "green"
105 case "key":
106 restarted.store.seal, _ = seal.New(bytes.Repeat([]byte{29}, 32))
107 }
108 if e := restarted.db.G.Transaction(func(tx *gorm.DB) error { return restarted.bindAgent(tx) }); e == nil {
109 t.Fatal("authority change accepted")
110 }
111 })
112 }
113 if _, e = restarted.db.Writer().Exec("DELETE FROM calendar_agent_state"); e != nil {
114 t.Fatal(e)
115 }
116 if e = restarted.db.G.Transaction(func(tx *gorm.DB) error { return restarted.bindAgent(tx) }); e == nil {
117 t.Fatal("existing receipts adopted without marker")
118 }
119}
120func TestAgentEventRejectsMalformedUnicode(t *testing.T) {
121 f, _, _, cal := linkedCalendarAgent(t)
122 input := eventAgentInput(cal)
123 input["title"] = "REPLACEME"
124 raw, _ := json.Marshal(input)
125 for _, bad := range []string{`\ud800`, string([]byte{255})} {
126 // Use the actual mounted handler; do not predecode with encoding/json.
127 rec := calendarRawAgentCall(f.app, strings.Replace(string(raw), "REPLACEME", bad, 1))
128 if rec.Code != 422 {
129 t.Fatal("malformed event Unicode", rec.Code, rec.Body.String())
130 }
131 }
132}
133
134func calendarRawAgentCall(a *App, raw string) *httptest.ResponseRecorder {
135 r := httptest.NewRequest("POST", homeTestOrigin+"/api/v1/operations/calendar_create_event", strings.NewReader(raw))
136 r.Header.Set("Content-Type", "application/json")
137 r.Header.Set("Authorization", "Bearer fixture")
138 w := httptest.NewRecorder()
139 a.agent.ServeHTTP(responseRecorder{w}, r)
140 return w
141}
142