| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | access "amadan.net/oficina/agent-access.git" |
| 5 | "bytes" |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "github.com/google/uuid" |
| 9 | "net/http/httptest" |
| 10 | "sync/atomic" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | type slideAgentFixture struct { |
| 16 | a *slideAgent |
| 17 | u User |
| 18 | p access.Principal |
| 19 | checks atomic.Int64 |
| 20 | revokeAt atomic.Int64 |
| 21 | } |
| 22 | type agentDeadlineRecorder struct { |
| 23 | *httptest.ResponseRecorder |
| 24 | afterWrite func() |
| 25 | } |
| 26 | |
| 27 | func (*agentDeadlineRecorder) SetWriteDeadline(time.Time) error { return nil } |
| 28 | func (w *agentDeadlineRecorder) Write(b []byte) (int, error) { |
| 29 | n, e := w.ResponseRecorder.Write(b) |
| 30 | if w.afterWrite != nil { |
| 31 | f := w.afterWrite |
| 32 | w.afterWrite = nil |
| 33 | f() |
| 34 | } |
| 35 | return n, e |
| 36 | } |
| 37 | func newSlideAgentFixture(t *testing.T, issuers ...string) *slideAgentFixture { |
| 38 | _, app, u := homeFixture(t) |
| 39 | homeLinked(t, app, u) |
| 40 | issuer := "https://connect.example.com" |
| 41 | if len(issuers) > 0 { |
| 42 | issuer = issuers[0] |
| 43 | } |
| 44 | a := &slideAgent{app: app, issuer: issuer, identityIssuer: app.cfg.Home.Issuer, team: "blue", key: bytes.Repeat([]byte{8}, 32)} |
| 45 | f := &slideAgentFixture{a: a, u: u, p: access.Principal{Subject: "home-person", Client: "test-client", Grant: "grant-one", Teams: []string{"blue"}, Scopes: []string{"slides:read", "slides:write"}}} |
| 46 | f.install(t) |
| 47 | return f |
| 48 | } |
| 49 | func (f *slideAgentFixture) install(t *testing.T) { |
| 50 | t.Helper() |
| 51 | e := f.a.app.mountAgent(f.a, access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| 52 | n := f.checks.Add(1) |
| 53 | if at := f.revokeAt.Load(); at > 0 && n >= at { |
| 54 | return access.Principal{}, access.Fail("unauthenticated", "Revoked.") |
| 55 | } |
| 56 | return f.p, nil |
| 57 | })) |
| 58 | if e != nil { |
| 59 | t.Fatal(e) |
| 60 | } |
| 61 | } |
| 62 | func (f *slideAgentFixture) raw(t *testing.T, name string, raw []byte) *httptest.ResponseRecorder { |
| 63 | t.Helper() |
| 64 | r := httptest.NewRequest("POST", homeTestOrigin+"/api/v1/operations/"+name, bytes.NewReader(raw)) |
| 65 | r.Header.Set("Authorization", "Bearer test-token") |
| 66 | r.Header.Set("Content-Type", "application/json") |
| 67 | w := &agentDeadlineRecorder{ResponseRecorder: httptest.NewRecorder()} |
| 68 | f.a.app.agent.ServeHTTP(w, r) |
| 69 | return w.ResponseRecorder |
| 70 | } |
| 71 | func (f *slideAgentFixture) call(t *testing.T, name string, input any) *httptest.ResponseRecorder { |
| 72 | t.Helper() |
| 73 | b, e := json.Marshal(input) |
| 74 | if e != nil { |
| 75 | t.Fatal(e) |
| 76 | } |
| 77 | return f.raw(t, name, b) |
| 78 | } |
| 79 | func initialSlideAgentInput() slideAgentCreate { |
| 80 | return slideAgentCreate{Team: "blue", ClientID: uuid.NewString(), Title: "Deck ✓", Slides: []slideAgentInitial{{Heading: "Welcome", Body: "**REAL_DECK_CANARY**", Notes: "Speaker notes"}}} |
| 81 | } |
| 82 | func slideCreated(t *testing.T, r *httptest.ResponseRecorder) string { |
| 83 | t.Helper() |
| 84 | if r.Code != 200 { |
| 85 | t.Fatal(r.Code, r.Body.String()) |
| 86 | } |
| 87 | var v struct { |
| 88 | Sqid string `json:"sqid"` |
| 89 | } |
| 90 | if e := json.Unmarshal(r.Body.Bytes(), &v); e != nil || v.Sqid == "" { |
| 91 | t.Fatal(e, r.Body.String()) |
| 92 | } |
| 93 | return v.Sqid |
| 94 | } |
| 95 | |