| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | access "amadan.net/oficina/agent-access.git" |
| 12 | "amadan.net/oficina/home/teammode" |
| 13 | ) |
| 14 | |
| 15 | func newTeamSlideAgent(t *testing.T) (*teamFixture, *slideAgentFixture) { |
| 16 | t.Helper() |
| 17 | f := newTeamFixture(t, "writer@example.com") |
| 18 | u := f.localUser("writer@example.com") |
| 19 | subject := teamSubject('a') |
| 20 | homeLinkedSubject(t, f.a, u, subject) |
| 21 | f.dir.Set(subject, "member") |
| 22 | f.refreshed() |
| 23 | team := f.a.team.Bindings().Team |
| 24 | a := &slideAgent{app: f.a, issuer: "https://connect.example.com", identityIssuer: teamTestIssuer, team: team, key: bytes.Repeat([]byte{8}, 32)} |
| 25 | af := &slideAgentFixture{a: a, u: u, p: access.Principal{Subject: subject, Client: "test-client", Grant: "grant-one", Teams: []string{team}, Scopes: []string{"slides:read", "slides:write"}}} |
| 26 | af.install(t) |
| 27 | return f, af |
| 28 | } |
| 29 | func teamSlideInput(af *slideAgentFixture) slideAgentCreate { |
| 30 | in := initialSlideAgentInput() |
| 31 | in.Team = af.a.team |
| 32 | return in |
| 33 | } |
| 34 | func teamSlideCounts(t *testing.T, f *teamFixture) []int64 { |
| 35 | t.Helper() |
| 36 | out := []int64{} |
| 37 | for _, table := range []string{"decks", "deck_updates", "slides_agent_receipts", "slides_connections"} { |
| 38 | var n int64 |
| 39 | if e := f.d.G.Table(table).Count(&n).Error; e != nil { |
| 40 | t.Fatal(e) |
| 41 | } |
| 42 | out = append(out, n) |
| 43 | } |
| 44 | return out |
| 45 | } |
| 46 | func TestAgentTeamSlidesMembershipAndIndependentShares(t *testing.T) { |
| 47 | f, af := newTeamSlideAgent(t) |
| 48 | f.a.allowed = allowlist{} |
| 49 | mine := slideCreated(t, af.call(t, "slides_create_deck", teamSlideInput(af))) |
| 50 | other := f.localUser("colleague@example.com") |
| 51 | shared := f.deck(other, "Independent deck", rungPrivate) |
| 52 | teamDeck := f.deck(other, "Team-only deck", rungTeam) |
| 53 | f.grant(shared, af.u, other) |
| 54 | f.dir.Set(af.p.Subject, "") |
| 55 | f.refreshed() |
| 56 | if w := af.call(t, "slides_create_deck", teamSlideInput(af)); w.Code != 403 { |
| 57 | t.Fatal("removed creator", w.Code, w.Body.String()) |
| 58 | } |
| 59 | for _, sqid := range []string{mine, teamDeck.Sqid} { |
| 60 | if w := af.call(t, "slides_get_deck", map[string]any{"team": af.a.team, "sqid": sqid}); w.Code != 404 { |
| 61 | t.Fatal("team rung survived removal", w.Code, w.Body.String()) |
| 62 | } |
| 63 | } |
| 64 | if w := af.call(t, "slides_get_deck", map[string]any{"team": af.a.team, "sqid": shared.Sqid}); w.Code != 200 { |
| 65 | t.Fatal("share lost", w.Code, w.Body.String()) |
| 66 | } |
| 67 | if w := af.call(t, "slides_list_decks", map[string]any{"team": af.a.team, "limit": 1}); w.Code != 200 || !strings.Contains(w.Body.String(), "Independent deck") { |
| 68 | t.Fatal("list grants", w.Code, w.Body.String()) |
| 69 | } |
| 70 | f.expired() |
| 71 | if w := af.call(t, "slides_get_deck", map[string]any{"team": af.a.team, "sqid": shared.Sqid}); w.Code != 200 { |
| 72 | t.Fatal("Unknown share", w.Code, w.Body.String()) |
| 73 | } |
| 74 | if w := af.call(t, "slides_get_deck", map[string]any{"team": af.a.team, "sqid": mine}); w.Code != 503 { |
| 75 | t.Fatal("Unknown own", w.Code, w.Body.String()) |
| 76 | } |
| 77 | if w := af.call(t, "slides_list_decks", map[string]any{"team": af.a.team}); w.Code != 503 { |
| 78 | t.Fatal("partial list", w.Code, w.Body.String()) |
| 79 | } |
| 80 | if w := af.call(t, "slides_create_deck", teamSlideInput(af)); w.Code != 503 { |
| 81 | t.Fatal("Unknown create", w.Code, w.Body.String()) |
| 82 | } |
| 83 | } |
| 84 | func TestAgentTeamSlidesWriterExpiryAndCompletedRetry(t *testing.T) { |
| 85 | for _, retry := range []bool{false, true} { |
| 86 | t.Run(strconv.FormatBool(retry), func(t *testing.T) { |
| 87 | f, af := newTeamSlideAgent(t) |
| 88 | in := teamSlideInput(af) |
| 89 | if retry { |
| 90 | slideCreated(t, af.call(t, "slides_create_deck", in)) |
| 91 | } |
| 92 | before, _ := json.Marshal(teamSlideCounts(t, f)) |
| 93 | u, err := af.a.member(t.Context(), af.p, true) |
| 94 | if err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | n := 0 |
| 98 | state := &slideAgentContext{member: u, slides: map[int64]bool{}, verify: func(context.Context) error { |
| 99 | n++ |
| 100 | if n == 2 { |
| 101 | f.expired() |
| 102 | } |
| 103 | return nil |
| 104 | }} |
| 105 | ctx := teammode.WithAccess(context.WithValue(t.Context(), slideAgentKey{}, state), u.team.Access) |
| 106 | if _, err = af.a.create(ctx, af.p, u, in); err == nil { |
| 107 | t.Fatal("expired writer accepted") |
| 108 | } |
| 109 | after, _ := json.Marshal(teamSlideCounts(t, f)) |
| 110 | if !bytes.Equal(before, after) { |
| 111 | t.Fatal("expired write committed", string(before), string(after)) |
| 112 | } |
| 113 | }) |
| 114 | } |
| 115 | } |
| 116 | func TestAgentTeamSlidesHomeOnlyAndBindingRefused(t *testing.T) { |
| 117 | f, af := newTeamSlideAgent(t) |
| 118 | subject := teamSubject('b') |
| 119 | f.dir.Set(subject, "member") |
| 120 | f.refreshed() |
| 121 | f.freshHomeSignIn(subject) |
| 122 | var before, after int64 |
| 123 | f.d.G.Model(&User{}).Count(&before) |
| 124 | af.p.Subject = subject |
| 125 | if w := af.call(t, "slides_list_decks", map[string]any{"team": af.a.team}); w.Code != 403 { |
| 126 | t.Fatal("HomeOnly accepted", w.Code, w.Body.String()) |
| 127 | } |
| 128 | f.d.G.Model(&User{}).Count(&after) |
| 129 | if before != after { |
| 130 | t.Fatal("allocated identity") |
| 131 | } |
| 132 | fresh := newTeamFixture(t) |
| 133 | bad := &slideAgent{app: fresh.a, issuer: "https://connect.example.com", identityIssuer: teamTestIssuer, team: "foreign", key: bytes.Repeat([]byte{8}, 32)} |
| 134 | if err := fresh.a.mountAgent(bad, access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return af.p, nil })); err == nil { |
| 135 | t.Fatal("foreign binding accepted") |
| 136 | } |
| 137 | var n int64 |
| 138 | if err := fresh.d.G.Raw("SELECT count(*) FROM sqlite_master WHERE name LIKE 'slides_agent_%'").Scan(&n).Error; err != nil || n != 0 { |
| 139 | t.Fatal("foreign binding installed agent schema", n, err) |
| 140 | } |
| 141 | } |
| 142 | func TestAgentTeamSlidesLateUnknownListWithShares(t *testing.T) { |
| 143 | f, af := newTeamSlideAgent(t) |
| 144 | other := f.localUser("other@example.com") |
| 145 | shared := f.deck(other, "Independent deck", rungPrivate) |
| 146 | f.grant(shared, af.u, other) |
| 147 | n := 0 |
| 148 | h, err := af.a.httpHandler(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| 149 | n++ |
| 150 | if n == 2 { |
| 151 | f.expired() |
| 152 | } |
| 153 | return af.p, nil |
| 154 | })) |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | f.a.agent = h |
| 159 | if w := af.call(t, "slides_list_decks", map[string]any{"team": af.a.team}); w.Code != 503 || strings.Contains(w.Body.String(), "Independent deck") { |
| 160 | t.Fatal("late partial list escaped", w.Code, w.Body.String()) |
| 161 | } |
| 162 | } |
| 163 | |