| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | access "amadan.net/oficina/agent-access.git" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "gorm.io/gorm" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "sync" |
| 13 | "testing" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | func scheduleReservation() map[string]any { |
| 18 | return map[string]any{"team": "acme", "client_id": "3395dd82-455b-46ae-a758-a7fb1484f3e6", "fingerprint": strings.Repeat("a", 64)} |
| 19 | } |
| 20 | func TestDelegatedScheduleReservation(t *testing.T) { |
| 21 | f, p := linkedDelegated(t) |
| 22 | input := scheduleReservation() |
| 23 | var wg sync.WaitGroup |
| 24 | for range 5 { |
| 25 | wg.Add(1) |
| 26 | go func() { |
| 27 | defer wg.Done() |
| 28 | w := delegatedCall(f.app, "calls_reserve_schedule", input) |
| 29 | if w.Code != 200 { |
| 30 | t.Errorf("reserve %d %s", w.Code, w.Body.String()) |
| 31 | } |
| 32 | }() |
| 33 | } |
| 34 | wg.Wait() |
| 35 | var count int |
| 36 | if err := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM calls_agent_schedules").Scan(&count); err != nil || count != 1 { |
| 37 | t.Fatal(count, err) |
| 38 | } |
| 39 | var coordinate, content string |
| 40 | if err := f.app.db.Writer().QueryRow("SELECT coordinate,content FROM calls_agent_schedules").Scan(&coordinate, &content); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if strings.Contains(coordinate, input["client_id"].(string)) || content == input["fingerprint"] { |
| 44 | t.Fatal("unkeyed workflow storage") |
| 45 | } |
| 46 | for _, table := range []string{"meetings", "calls_connections", "calls_agent_meetings", "meeting_links"} { |
| 47 | if err := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM " + table).Scan(&count); err != nil || count != 0 { |
| 48 | t.Fatal(table, count, err) |
| 49 | } |
| 50 | } |
| 51 | input["fingerprint"] = strings.Repeat("b", 64) |
| 52 | if w := delegatedCall(f.app, "calls_reserve_schedule", input); w.Code != 409 { |
| 53 | t.Fatal("changed workflow accepted", w.Code) |
| 54 | } |
| 55 | p.Client = "different-client" |
| 56 | if w := delegatedCall(f.app, "calls_reserve_schedule", input); w.Code != 200 { |
| 57 | t.Fatal("client not isolated", w.Code) |
| 58 | } |
| 59 | if err := f.app.db.G.Exec("DELETE FROM calls_agent_state").Error; err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if err := f.app.db.G.Transaction(func(tx *gorm.DB) error { return f.app.bindDelegated(tx) }); err == nil { |
| 63 | t.Fatal("orphaned reservation adopted") |
| 64 | } |
| 65 | } |
| 66 | func TestDelegatedScheduleReservationFinalAuthority(t *testing.T) { |
| 67 | f, _ := linkedDelegated(t) |
| 68 | if err := f.app.db.G.Exec("CREATE TRIGGER revoke_schedule AFTER INSERT ON calls_agent_schedules BEGIN DELETE FROM home_identity_links; END").Error; err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | if w := delegatedCall(f.app, "calls_reserve_schedule", scheduleReservation()); w.Code != 403 { |
| 72 | t.Fatal("revoked reservation committed", w.Code, w.Body.String()) |
| 73 | } |
| 74 | var count int64 |
| 75 | if err := f.app.db.G.Table("calls_agent_schedules").Count(&count).Error; err != nil || count != 0 { |
| 76 | t.Fatal(count, err) |
| 77 | } |
| 78 | } |
| 79 | func TestSuiteInteropFixture(t *testing.T) { |
| 80 | dir := os.Getenv("OFICINA_INTEROP_DIR") |
| 81 | if dir == "" { |
| 82 | t.Skip("cross-app harness only") |
| 83 | } |
| 84 | f, p := linkedDelegated(t) |
| 85 | p.Client = "oficina-interop" |
| 86 | p.Grant = "interop-grant" |
| 87 | server := httptest.NewTLSServer(f.app.delegated) |
| 88 | defer server.Close() |
| 89 | b, _ := json.Marshal(map[string]any{"url": server.URL, "origin": f.app.cfg.Origin}) |
| 90 | if err := os.WriteFile(filepath.Join(dir, "calls.json"), b, 0600); err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | deadline := time.NewTimer(2 * time.Minute) |
| 94 | defer deadline.Stop() |
| 95 | tick := time.NewTicker(100 * time.Millisecond) |
| 96 | defer tick.Stop() |
| 97 | for { |
| 98 | select { |
| 99 | case <-deadline.C: |
| 100 | t.Fatal("cross-app harness timed out") |
| 101 | case <-t.Context().Done(): |
| 102 | t.Fatal(t.Context().Err()) |
| 103 | case <-tick.C: |
| 104 | if _, err := os.Stat(filepath.Join(dir, "stop")); err == nil { |
| 105 | return |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | func TestDelegatedScheduleTeamStanding(t *testing.T) { |
| 111 | for _, mode := range []string{"member", "removed", "expired"} { |
| 112 | t.Run(mode, func(t *testing.T) { |
| 113 | f := newTeamFixture(t) |
| 114 | subject := teamSubject('a') |
| 115 | f.dir.Set(subject, "member") |
| 116 | f.refreshed() |
| 117 | f.local(t) |
| 118 | if w := f.get("/account/home"); w.Code != 200 { |
| 119 | t.Fatal(w.Code) |
| 120 | } |
| 121 | start := homeRedirect(t, f.post("/account/home", "")) |
| 122 | homeRedirect(t, f.exchange(t, start, subject)) |
| 123 | homeRedirect(t, f.post("/account/home/confirm", "")) |
| 124 | p := access.Principal{Subject: subject, Client: "workflow", Grant: "grant", Teams: []string{f.cfg.Team.TeamID}, Scopes: []string{"calls:read", "calls:write", "calls:share"}} |
| 125 | if err := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return p, nil }), "https://connect.example.test", f.cfg.Team.TeamID); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | if mode == "removed" { |
| 129 | f.removed(subject) |
| 130 | } |
| 131 | if mode == "expired" { |
| 132 | f.expired() |
| 133 | } |
| 134 | in := scheduleReservation() |
| 135 | in["team"] = f.cfg.Team.TeamID |
| 136 | w := delegatedCall(f.app, "calls_reserve_schedule", in) |
| 137 | want := 200 |
| 138 | if mode == "removed" { |
| 139 | want = 403 |
| 140 | } |
| 141 | if mode == "expired" { |
| 142 | want = 503 |
| 143 | } |
| 144 | if w.Code != want { |
| 145 | t.Fatal(w.Code, w.Body.String()) |
| 146 | } |
| 147 | if mode == "member" { |
| 148 | in := delegatedInputForTest() |
| 149 | in["team"] = f.cfg.Team.TeamID |
| 150 | w := delegatedCall(f.app, "calls_create_meeting", in) |
| 151 | if w.Code != 200 { |
| 152 | t.Fatal(w.Code, w.Body.String()) |
| 153 | } |
| 154 | var out struct{ Sqid string } |
| 155 | if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | w = delegatedCall(f.app, "calls_get_join_link", map[string]any{"team": f.cfg.Team.TeamID, "sqid": out.Sqid}) |
| 159 | if w.Code != 200 { |
| 160 | t.Fatal("connection operator host not resolved", w.Code, w.Body.String()) |
| 161 | } |
| 162 | } |
| 163 | }) |
| 164 | } |
| 165 | } |
| 166 | |