oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1package calls
2
3import (
4 "encoding/json"
5 "net/http/httptest"
6 "strings"
7 "testing"
8)
9
10func TestDelegatedPaginationAndClientIsolation(t *testing.T) {
11 f, p := linkedDelegated(t)
12 input := delegatedInputForTest()
13 for _, client := range []string{"one", "two", "three"} {
14 p.Client = client
15 if w := delegatedCall(f.app, "calls_create_meeting", input); w.Code != 200 {
16 t.Fatal(w.Code, w.Body.String())
17 }
18 }
19 var after int64
20 seen := map[string]bool{}
21 for i := 0; i < 3; i++ {
22 w := delegatedCall(f.app, "calls_list_meetings", map[string]any{"team": "acme", "limit": 1, "after": after})
23 if w.Code != 200 {
24 t.Fatal(w.Code, w.Body.String())
25 }
26 var out struct {
27 Meetings []struct {
28 Sqid string `json:"sqid"`
29 } `json:"meetings"`
30 Next int64 `json:"next_after"`
31 More bool `json:"has_more"`
32 }
33 if json.Unmarshal(w.Body.Bytes(), &out) != nil || len(out.Meetings) != 1 || out.More != (i < 2) || out.Next <= after || seen[out.Meetings[0].Sqid] {
34 t.Fatal("bad pagination", w.Body.String())
35 }
36 seen[out.Meetings[0].Sqid] = true
37 after = out.Next
38 }
39 if w := delegatedCall(f.app, "calls_get_meeting", map[string]any{"team": "acme", "sqid": "23456789ab"}); w.Code != 404 {
40 t.Fatal("missing meeting", w.Code)
41 }
42 for id := range seen {
43 w := delegatedCall(f.app, "calls_get_meeting", map[string]any{"team": "acme", "sqid": id})
44 if w.Code != 200 || !strings.Contains(w.Body.String(), input["title"].(string)) {
45 t.Fatal("read missing content", w.Code, w.Body.String())
46 }
47 }
48}
49func TestDelegatedRateAndStorageFailure(t *testing.T) {
50 f, _ := linkedDelegated(t)
51 rate := false
52 for i := 0; i < 65; i++ {
53 w := delegatedCall(f.app, "calls_list_meetings", map[string]any{"team": "acme"})
54 if w.Code == 429 {
55 rate = true
56 break
57 }
58 if w.Code != 200 {
59 t.Fatal(w.Code)
60 }
61 }
62 if !rate {
63 t.Fatal("no rate limit")
64 }
65 if err := f.app.db.Close(); err != nil {
66 t.Fatal(err)
67 }
68 if w := delegatedCall(f.app, "calls_list_meetings", map[string]any{"team": "acme"}); w.Code != 503 {
69 t.Fatal("storage failure misreported", w.Code)
70 }
71}
72func TestDelegatedProductionRoutesStayOutsideBrowserAndLegacyAuth(t *testing.T) {
73 f := newHomeRouterFixture(t)
74 cfg := f.app.cfg.Config
75 cfg.AgentIssuer = "https://connect.example.test"
76 cfg.AgentTeam = "acme"
77 mux, _, err := buildForTest(t, f.app.db, cfg)
78 if err != nil {
79 t.Fatal(err)
80 }
81 for _, method := range []string{"GET", "POST"} {
82 path := "/api/v1/operations"
83 if method == "POST" {
84 path += "/calls_create_meeting"
85 }
86 r := httptest.NewRequest(method, homeTestOrigin+path, strings.NewReader(`{}`))
87 w := httptest.NewRecorder()
88 mux.ServeHTTP(responseRecorder{w}, r)
89 if w.Code != 401 || !strings.Contains(w.Body.String(), `"code":"unauthenticated"`) {
90 t.Fatal("delegated route intercepted", w.Code, w.Body.String())
91 }
92 }
93 r := httptest.NewRequest("GET", homeTestOrigin+"/api/v1/meetings", nil)
94 w := httptest.NewRecorder()
95 mux.ServeHTTP(responseRecorder{w}, r)
96 if w.Code != 401 || !strings.Contains(w.Body.String(), `"error":"unauthorized"`) {
97 t.Fatal("legacy route changed", w.Code, w.Body.String())
98 }
99 cfg.AgentTeam = ""
100 if _, err := resolve(cfg); err == nil {
101 t.Fatal("partial config accepted")
102 }
103}
104
105func TestDelegatedCursorRejectsIntegerOverflow(t *testing.T) {
106 f, _ := linkedDelegated(t)
107 w := delegatedCall(f.app, "calls_list_meetings", map[string]any{"team": "acme", "after": json.Number("9223372036854775808")})
108 if w.Code != 422 {
109 t.Fatal("overflow silently reset cursor", w.Code, w.Body.String())
110 }
111}
112func TestDelegatedCreationRechecksOperatorInTransaction(t *testing.T) {
113 f, _ := linkedDelegated(t)
114 if _, err := f.app.db.Writer().Exec("CREATE TRIGGER change_operator BEFORE INSERT ON calls_agent_meetings BEGIN UPDATE persons SET anonymous=1; END"); err != nil {
115 t.Fatal(err)
116 }
117 if w := delegatedCall(f.app, "calls_create_meeting", delegatedInputForTest()); w.Code != 403 {
118 t.Fatal("stale operator admitted", w.Code, w.Body.String())
119 }
120 for _, table := range []string{"meetings", "calls_agent_meetings", "calls_connections"} {
121 var n int
122 if err := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM " + table).Scan(&n); err != nil || n != 0 {
123 t.Fatal("partial effect", table, n, err)
124 }
125 }
126 var anon bool
127 if err := f.app.db.Writer().QueryRow("SELECT anonymous FROM persons").Scan(&anon); err != nil || anon {
128 t.Fatal("transaction not rolled back", err)
129 }
130}
131func TestDelegatedJoinLinkRechecksCurrentInvitation(t *testing.T) {
132 f, _ := linkedDelegated(t)
133 w := delegatedCall(f.app, "calls_create_meeting", delegatedInputForTest())
134 if w.Code != 200 {
135 t.Fatal(w.Code)
136 }
137 var m struct {
138 Sqid string `json:"sqid"`
139 }
140 _ = json.Unmarshal(w.Body.Bytes(), &m)
141 input := map[string]any{"team": "acme", "sqid": m.Sqid}
142 if _, err := f.app.db.Writer().Exec("UPDATE meeting_links SET revoked_at=CURRENT_TIMESTAMP"); err != nil {
143 t.Fatal(err)
144 }
145 if w := delegatedCall(f.app, "calls_get_join_link", input); w.Code != 404 {
146 t.Fatal("revoked invitation disclosed", w.Code, w.Body.String())
147 }
148 if _, err := f.app.db.Writer().Exec("UPDATE meeting_links SET revoked_at=NULL,token_sealed=NULL"); err != nil {
149 t.Fatal(err)
150 }
151 if w := delegatedCall(f.app, "calls_get_join_link", input); w.Code != 404 {
152 t.Fatal("hash-only invitation fabricated", w.Code, w.Body.String())
153 }
154 var count int
155 if err := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM meeting_links").Scan(&count); err != nil || count != 1 {
156 t.Fatal("read created invitation", count, err)
157 }
158}
159