| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "sync/atomic" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | access "amadan.net/oficina/agent-access.git" |
| 13 | "amadan.net/oficina/home/teammode" |
| 14 | ) |
| 15 | |
| 16 | type calendarLeaseObserver struct { |
| 17 | http.ResponseWriter |
| 18 | budgets chan time.Duration |
| 19 | } |
| 20 | |
| 21 | func (w *calendarLeaseObserver) Unwrap() http.ResponseWriter { return w.ResponseWriter } |
| 22 | func (w *calendarLeaseObserver) SetWriteDeadline(d time.Time) error { |
| 23 | if !d.IsZero() { |
| 24 | w.budgets <- time.Until(d) |
| 25 | } |
| 26 | return http.NewResponseController(w.ResponseWriter).SetWriteDeadline(d) |
| 27 | } |
| 28 | |
| 29 | func TestAgentCalendarSocketDeadlineUsesRosterLease(t *testing.T) { |
| 30 | f := newTeamFixture(t) |
| 31 | subject := teamSubject('a') |
| 32 | f.linkedAlice(subject, "member") |
| 33 | if w := f.get("/calendars"); w.Code != 200 { |
| 34 | t.Fatal(w.Code) |
| 35 | } |
| 36 | team := teamTestBindings().Team |
| 37 | p := access.Principal{Subject: subject, Client: "agent", Grant: "grant", Teams: []string{team}, Scopes: []string{"calendar:read"}} |
| 38 | standing := f.app.team.Access(t.Context(), subject) |
| 39 | if err := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return p, nil }), "https://connect.example.test", team); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | f.dir.SetDown(true) |
| 43 | f.clock.Step(standing.Remaining() - 250*time.Millisecond) |
| 44 | budgets := make(chan time.Duration, 16) |
| 45 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 46 | f.app.agent.ServeHTTP(&calendarLeaseObserver{w, budgets}, r) |
| 47 | }) |
| 48 | code, _, err := calendarLiveCall(t, h, "calendar_list", map[string]any{"team": team}) |
| 49 | if code != 200 || err != nil { |
| 50 | t.Fatal(code, err) |
| 51 | } |
| 52 | close(budgets) |
| 53 | n := 0 |
| 54 | for d := range budgets { |
| 55 | n++ |
| 56 | if d <= 0 || d > 300*time.Millisecond { |
| 57 | t.Fatal("socket deadline exceeds roster lease", d) |
| 58 | } |
| 59 | } |
| 60 | if n == 0 { |
| 61 | t.Fatal("no socket deadline") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestAgentCalendarTransactionStoreKeepsRosterGuard(t *testing.T) { |
| 66 | f := newTeamFixture(t) |
| 67 | subject := teamSubject('a') |
| 68 | person := f.linkedAlice(subject, "member") |
| 69 | if w := f.get("/calendars"); w.Code != 200 { |
| 70 | t.Fatal(w.Code) |
| 71 | } |
| 72 | _, cal := f.calendarsOf(person) |
| 73 | team := teamTestBindings().Team |
| 74 | p := access.Principal{Subject: subject, Client: "agent", Grant: "grant", Teams: []string{team}, Scopes: []string{"calendar:read", "calendar:write"}} |
| 75 | if err := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return p, nil }), "https://connect.example.test", team); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | input := eventAgentInput(cal) |
| 79 | input["team"] = team |
| 80 | w := calendarAgentCall(f.app, "calendar_create_event", input) |
| 81 | if w.Code != 200 { |
| 82 | t.Fatal(w.Code, w.Body.String()) |
| 83 | } |
| 84 | var out struct{ ID int64 } |
| 85 | json.Unmarshal(w.Body.Bytes(), &out) |
| 86 | var e Event |
| 87 | f.app.db.G.First(&e, out.ID) |
| 88 | teamGuard = f.lapsingGuard |
| 89 | defer func() { teamGuard = teammode.Guard }() |
| 90 | update := agentUpdateInput(out.ID, e.Sequence, map[string]any{"title": "must roll back"}) |
| 91 | update["team"] = team |
| 92 | w = calendarAgentCall(f.app, "calendar_update_event", update) |
| 93 | if w.Code != 503 { |
| 94 | t.Fatal("transaction lost roster guard", w.Code, w.Body.String()) |
| 95 | } |
| 96 | var after Event |
| 97 | f.app.db.G.First(&after, out.ID) |
| 98 | if after.Sequence != e.Sequence { |
| 99 | t.Fatal("lapsed transaction committed") |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestAgentEventMutationResponseRequiresWriteGrant(t *testing.T) { |
| 104 | for _, op := range []string{"calendar_create_event", "calendar_update_event"} { |
| 105 | t.Run(op, func(t *testing.T) { |
| 106 | f, p, person, cal := linkedCalendarAgent(t) |
| 107 | var input any = eventAgentInput(cal) |
| 108 | if op == "calendar_update_event" { |
| 109 | e := createdAgentEvent(t, f.app, person, cal) |
| 110 | input = agentUpdateInput(e.ID, e.Sequence, map[string]any{"title": "committed"}) |
| 111 | } |
| 112 | var checks atomic.Int32 |
| 113 | if err := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| 114 | if checks.Add(1) == 4 { |
| 115 | if err := f.app.db.G.Exec("UPDATE grants SET role='viewer' WHERE person_id=?", person.ID).Error; err != nil { |
| 116 | return access.Principal{}, err |
| 117 | } |
| 118 | } |
| 119 | return *p, nil |
| 120 | }), f.app.agentIssuer, "acme"); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | w := calendarAgentCall(f.app, op, input) |
| 124 | if w.Code != 403 || strings.Contains(w.Body.String(), `"id":`) { |
| 125 | t.Fatal("write ack emitted after downgrade", w.Code, w.Body.String()) |
| 126 | } |
| 127 | }) |
| 128 | } |
| 129 | } |
| 130 | |