| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "sync/atomic" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | access "amadan.net/oficina/agent-access.git" |
| 11 | "amadan.net/oficina/home/roster" |
| 12 | ) |
| 13 | |
| 14 | func TestAgentCalendarRosterAdmission(t *testing.T) { |
| 15 | for _, mode := range []string{"member", "removed", "unknown", "runtime", "home-only", "team-mismatch"} { |
| 16 | t.Run(mode, func(t *testing.T) { |
| 17 | f := newTeamFixture(t) |
| 18 | subject := teamSubject('a') |
| 19 | var person Person |
| 20 | if mode == "home-only" { |
| 21 | person = f.member(subject, "member") |
| 22 | } else { |
| 23 | person = f.linkedAlice(subject, "member") |
| 24 | if w := f.get("/calendars"); w.Code != 200 { |
| 25 | t.Fatal(w.Code) |
| 26 | } |
| 27 | } |
| 28 | _, cal := f.calendarsOf(person) |
| 29 | p := access.Principal{Subject: subject, Client: "agent", Grant: "grant", Teams: []string{teamTestBindings().Team}, Scopes: []string{"calendar:read", "calendar:write"}} |
| 30 | team := teamTestBindings().Team |
| 31 | if mode == "team-mismatch" { |
| 32 | team = "wrong" |
| 33 | } |
| 34 | err := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return p, nil }), "https://connect.example.test", team) |
| 35 | if mode == "team-mismatch" { |
| 36 | if err == nil { |
| 37 | t.Fatal("mismatched roster team accepted") |
| 38 | } |
| 39 | return |
| 40 | } |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | switch mode { |
| 45 | case "removed": |
| 46 | f.dir.Set(subject, "") |
| 47 | f.refreshed() |
| 48 | case "unknown": |
| 49 | f.expired() |
| 50 | case "runtime": |
| 51 | f.app.team = nil |
| 52 | } |
| 53 | in := eventAgentInput(cal) |
| 54 | in["team"] = team |
| 55 | want := 200 |
| 56 | switch mode { |
| 57 | case "removed", "home-only": |
| 58 | want = 403 |
| 59 | case "unknown", "runtime": |
| 60 | want = 503 |
| 61 | } |
| 62 | for _, op := range []string{"calendar_list", "calendar_create_event"} { |
| 63 | input := any(in) |
| 64 | if op == "calendar_list" { |
| 65 | input = map[string]any{"team": team} |
| 66 | } |
| 67 | w := calendarAgentCall(f.app, op, input) |
| 68 | if w.Code != want { |
| 69 | t.Fatalf("%s: %d want %d: %s", op, w.Code, want, w.Body.String()) |
| 70 | } |
| 71 | } |
| 72 | var n int64 |
| 73 | f.app.db.G.Model(&Event{}).Count(&n) |
| 74 | if want != 200 && n != 0 { |
| 75 | t.Fatal("refused create wrote event") |
| 76 | } |
| 77 | }) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestAgentCalendarRosterEditLeaseAndReminderChannel(t *testing.T) { |
| 82 | for _, mode := range []string{"valid", "final-expiry", "response-expiry"} { |
| 83 | t.Run(mode, func(t *testing.T) { |
| 84 | f := newTeamFixture(t) |
| 85 | subject := teamSubject('a') |
| 86 | person := f.linkedAlice(subject, "member") |
| 87 | if w := f.get("/calendars"); w.Code != 200 { |
| 88 | t.Fatal(w.Code) |
| 89 | } |
| 90 | _, cal := f.calendarsOf(person) |
| 91 | if err := f.app.db.G.Model(&Person{}).Where("id=?", person.ID).Update("reminder_channel", "inapp").Error; err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | team := teamTestBindings().Team |
| 95 | p := access.Principal{Subject: subject, Client: "agent", Grant: "grant", Teams: []string{team}, Scopes: []string{"calendar:read", "calendar:write"}} |
| 96 | var checks atomic.Int32 |
| 97 | expireAt := int32(0) |
| 98 | if err := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| 99 | if n := checks.Add(1); expireAt > 0 && n == expireAt { |
| 100 | f.clock.Step(roster.MaximumAge + time.Second) |
| 101 | } |
| 102 | return p, nil |
| 103 | }), "https://connect.example.test", team); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | in := eventAgentInput(cal) |
| 107 | in["team"] = team |
| 108 | w := calendarAgentCall(f.app, "calendar_create_event", in) |
| 109 | if w.Code != 200 { |
| 110 | t.Fatal(w.Code, w.Body.String()) |
| 111 | } |
| 112 | var out struct{ ID int64 } |
| 113 | json.Unmarshal(w.Body.Bytes(), &out) |
| 114 | var rule Reminder |
| 115 | if err := f.app.db.G.Where("event_id=?", out.ID).First(&rule).Error; err != nil || rule.Channel != "inapp" { |
| 116 | t.Fatal("default channel", rule.Channel, err) |
| 117 | } |
| 118 | var before Event |
| 119 | f.app.db.G.First(&before, out.ID) |
| 120 | edit := agentUpdateInput(out.ID, before.Sequence, map[string]any{"title": "changed"}) |
| 121 | edit["team"] = team |
| 122 | checks.Store(0) |
| 123 | switch mode { |
| 124 | case "final-expiry": |
| 125 | expireAt = 3 |
| 126 | case "response-expiry": |
| 127 | expireAt = 4 |
| 128 | } |
| 129 | w = calendarAgentCall(f.app, "calendar_update_event", edit) |
| 130 | want := 200 |
| 131 | if mode != "valid" { |
| 132 | want = 503 |
| 133 | } |
| 134 | if w.Code != want { |
| 135 | t.Fatal(w.Code, want, w.Body.String()) |
| 136 | } |
| 137 | var after Event |
| 138 | f.app.db.G.First(&after, out.ID) |
| 139 | if mode == "final-expiry" && after.Sequence != before.Sequence { |
| 140 | t.Fatal("expired mutation committed") |
| 141 | } |
| 142 | if mode != "final-expiry" && after.Sequence != before.Sequence+1 { |
| 143 | t.Fatal("valid mutation did not commit") |
| 144 | } |
| 145 | }) |
| 146 | } |
| 147 | } |
| 148 | |