| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "net/http/httptest" |
| 8 | "testing" |
| 9 | |
| 10 | access "amadan.net/oficina/agent-access.git" |
| 11 | ) |
| 12 | |
| 13 | func TestAgentDeckConfigurationAndRouter(t *testing.T) { |
| 14 | _, base, _ := homeFixture(t) |
| 15 | for _, mode := range []string{"issuer-only", "team-only", "no-home", "same-issuer"} { |
| 16 | t.Run(mode, func(t *testing.T) { |
| 17 | cfg := base.cfg |
| 18 | cfg.AgentIssuer = "https://connect.example.com" |
| 19 | cfg.AgentTeam = "blue" |
| 20 | switch mode { |
| 21 | case "issuer-only": |
| 22 | cfg.AgentTeam = "" |
| 23 | case "team-only": |
| 24 | cfg.AgentIssuer = "" |
| 25 | case "no-home": |
| 26 | cfg.Home = nil |
| 27 | case "same-issuer": |
| 28 | cfg.AgentIssuer = cfg.Home.Issuer |
| 29 | } |
| 30 | d := testDB(t) |
| 31 | if _, a, e := New(d, cfg); e == nil { |
| 32 | a.Close() |
| 33 | t.Fatal("partial authority accepted") |
| 34 | } |
| 35 | var n int |
| 36 | if e := d.Writer().QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='decks'").Scan(&n); e != nil || n != 0 { |
| 37 | t.Fatal("mutated database on invalid config", n, e) |
| 38 | } |
| 39 | }) |
| 40 | } |
| 41 | cfg := base.cfg |
| 42 | cfg.AgentIssuer = "https://connect.example.com" |
| 43 | cfg.AgentTeam = "blue" |
| 44 | mux, a, e := New(testDB(t), cfg) |
| 45 | if e != nil { |
| 46 | t.Fatal(e) |
| 47 | } |
| 48 | defer a.Close() |
| 49 | u := User{ID: 72, Email: "owner@example.com"} |
| 50 | if e = a.db.G.Create(&u).Error; e != nil { |
| 51 | t.Fatal(e) |
| 52 | } |
| 53 | homeLinked(t, a, u) |
| 54 | if e = a.mountAgent(a.agentAccess, access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| 55 | return access.Principal{Subject: "home-person", Client: "fixture", Grant: "grant", Teams: []string{"blue"}, Scopes: []string{"slides:read", "slides:write"}}, nil |
| 56 | })); e != nil { |
| 57 | t.Fatal(e) |
| 58 | } |
| 59 | // The production mux captured the startup handler; use an introspection server |
| 60 | // test separately for actual remote verification. The specific operation paths |
| 61 | // must nevertheless be outside browser CSRF and reject an absent bearer as JSON. |
| 62 | r := httptest.NewRequest("POST", homeTestOrigin+"/api/v1/operations/slides_create_deck", bytes.NewReader([]byte(`{}`))) |
| 63 | w := httptest.NewRecorder() |
| 64 | mux.ServeHTTP(w, r) |
| 65 | if w.Code != 401 || !json.Valid(w.Body.Bytes()) { |
| 66 | t.Fatal("bearer route hidden by browser CSRF", w.Code, w.Body.String()) |
| 67 | } |
| 68 | disabled, app, e := New(testDB(t), base.cfg) |
| 69 | if e != nil { |
| 70 | t.Fatal(e) |
| 71 | } |
| 72 | defer app.Close() |
| 73 | w = httptest.NewRecorder() |
| 74 | disabled.ServeHTTP(w, r) |
| 75 | if app.agent != nil || w.Code == 200 { |
| 76 | t.Fatal("disabled connection active", w.Code) |
| 77 | } |
| 78 | } |
| 79 | func TestAgentDeckHistoricalAttributionAndSealBinding(t *testing.T) { |
| 80 | f := newSlideAgentFixture(t) |
| 81 | slideCreated(t, f.call(t, "slides_create_deck", initialSlideAgentInput())) |
| 82 | var update DeckUpdate |
| 83 | f.a.app.db.G.Take(&update) |
| 84 | f.a.app.agentAccess = nil |
| 85 | f.a.app.agent = nil |
| 86 | if label := f.a.app.actorLabels(t.Context(), []string{update.Actor})[update.Actor]; label == "" { |
| 87 | t.Fatal("disabled connection erased history") |
| 88 | } |
| 89 | if e := f.a.app.db.G.Exec("UPDATE slides_connections SET coordinate='other'").Error; e != nil { |
| 90 | t.Fatal(e) |
| 91 | } |
| 92 | if label := f.a.app.actorLabels(t.Context(), []string{"connection:other"})["connection:other"]; label != "" { |
| 93 | t.Fatal("ciphertext moved to unrelated actor", label) |
| 94 | } |
| 95 | } |
| 96 | |