Skip to content

Commit 755b28a

Browse files
Add 7 history visibility tests (3 sytests) (#261)
* Add 7 history visibility tests (3 sytests) * praise be, the mighty lint roller * add msgtype to m.room.message events * add missing bits to content matching * Update tests/csapi/apidoc_room_history_visibility_test.go * Use MustSyncUntil * Replace SyncUntilInvitedTo as well Co-authored-by: kegsay <[email protected]> Co-authored-by: Kegan Dougal <[email protected]>
1 parent d98c802 commit 755b28a

File tree

1 file changed

+307
-0
lines changed

1 file changed

+307
-0
lines changed
Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
package csapi_tests
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/tidwall/gjson"
8+
9+
"github.com/matrix-org/complement/internal/b"
10+
"github.com/matrix-org/complement/internal/client"
11+
"github.com/matrix-org/complement/internal/match"
12+
"github.com/matrix-org/complement/internal/must"
13+
"github.com/matrix-org/complement/runtime"
14+
)
15+
16+
// TODO most of this can be refactored into data-driven tests
17+
18+
func fetchEvent(t *testing.T, c *client.CSAPI, roomId, eventId string) *http.Response {
19+
return c.DoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomId, "event", eventId})
20+
}
21+
22+
func createRoomWithVisibility(t *testing.T, c *client.CSAPI, visibility string) string {
23+
return c.CreateRoom(t, map[string]interface{}{
24+
"initial_state": []map[string]interface{}{
25+
{
26+
"content": map[string]interface{}{
27+
"history_visibility": visibility,
28+
},
29+
"type": "m.room.history_visibility",
30+
"state_key": "",
31+
},
32+
},
33+
"preset": "public_chat",
34+
})
35+
}
36+
37+
// Fetches an event after join, and succeeds.
38+
// sytest: /event/ on joined room works
39+
func TestFetchEvent(t *testing.T) {
40+
deployment := Deploy(t, b.BlueprintOneToOneRoom)
41+
defer deployment.Destroy(t)
42+
43+
alice := deployment.Client(t, "hs1", "@alice:hs1")
44+
bob := deployment.Client(t, "hs1", "@bob:hs1")
45+
46+
roomID := createRoomWithVisibility(t, alice, "shared")
47+
48+
bob.JoinRoom(t, roomID, nil)
49+
50+
bob.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID))
51+
52+
eventID := alice.SendEventSynced(t, roomID, b.Event{
53+
Type: "m.room.message",
54+
Content: map[string]interface{}{
55+
"msgtype": "m.text",
56+
"body": "Hello world",
57+
},
58+
})
59+
60+
res := fetchEvent(t, bob, roomID, eventID)
61+
62+
must.MatchResponse(t, res, match.HTTPResponse{
63+
StatusCode: 200,
64+
JSON: []match.JSON{
65+
// No harm in checking if the event data is also as expected
66+
match.JSONKeyEqual("content", map[string]interface{}{
67+
"msgtype": "m.text",
68+
"body": "Hello world",
69+
}),
70+
match.JSONKeyEqual("type", "m.room.message"),
71+
72+
// the spec technically doesn't list these following keys, but we're still checking them because sytest did.
73+
// see: https://github.com/matrix-org/matrix-doc/issues/3540
74+
match.JSONKeyEqual("room_id", roomID),
75+
match.JSONKeyEqual("sender", alice.UserID),
76+
match.JSONKeyEqual("event_id", eventID),
77+
match.JSONKeyTypeEqual("origin_server_ts", gjson.Number),
78+
},
79+
})
80+
}
81+
82+
// Tries to fetch an event before join, and fails.
83+
// history_visibility: joined
84+
// sytest: /event/ does not allow access to events before the user joined
85+
func TestFetchHistoricalJoinedEventDenied(t *testing.T) {
86+
deployment := Deploy(t, b.BlueprintOneToOneRoom)
87+
defer deployment.Destroy(t)
88+
89+
alice := deployment.Client(t, "hs1", "@alice:hs1")
90+
bob := deployment.Client(t, "hs1", "@bob:hs1")
91+
92+
roomID := createRoomWithVisibility(t, alice, "joined")
93+
94+
eventID := alice.SendEventSynced(t, roomID, b.Event{
95+
Type: "m.room.message",
96+
Content: map[string]interface{}{
97+
"msgtype": "m.text",
98+
"body": "Hello world",
99+
},
100+
})
101+
102+
bob.JoinRoom(t, roomID, nil)
103+
bob.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID))
104+
105+
res := fetchEvent(t, bob, roomID, eventID)
106+
107+
must.MatchResponse(t, res, match.HTTPResponse{
108+
StatusCode: 404,
109+
})
110+
}
111+
112+
// Tries to fetch an event before join, and succeeds.
113+
// history_visibility: shared
114+
func TestFetchHistoricalSharedEvent(t *testing.T) {
115+
runtime.SkipIf(t, runtime.Dendrite) // FIXME https://github.com/matrix-org/dendrite/issues/617
116+
117+
deployment := Deploy(t, b.BlueprintOneToOneRoom)
118+
defer deployment.Destroy(t)
119+
120+
alice := deployment.Client(t, "hs1", "@alice:hs1")
121+
bob := deployment.Client(t, "hs1", "@bob:hs1")
122+
123+
roomID := createRoomWithVisibility(t, alice, "shared")
124+
125+
eventID := alice.SendEventSynced(t, roomID, b.Event{
126+
Type: "m.room.message",
127+
Content: map[string]interface{}{
128+
"msgtype": "m.text",
129+
"body": "Hello world",
130+
},
131+
})
132+
133+
bob.JoinRoom(t, roomID, nil)
134+
bob.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID))
135+
136+
res := fetchEvent(t, bob, roomID, eventID)
137+
138+
must.MatchResponse(t, res, match.HTTPResponse{
139+
StatusCode: 200,
140+
JSON: []match.JSON{
141+
// No harm in checking if the event data is also as expected
142+
match.JSONKeyEqual("content", map[string]interface{}{
143+
"msgtype": "m.text",
144+
"body": "Hello world",
145+
}),
146+
match.JSONKeyEqual("type", "m.room.message"),
147+
148+
// the spec technically doesn't list these following keys, but we're still checking them because sytest did.
149+
// see: https://github.com/matrix-org/matrix-doc/issues/3540
150+
match.JSONKeyEqual("room_id", roomID),
151+
match.JSONKeyEqual("sender", alice.UserID),
152+
match.JSONKeyEqual("event_id", eventID),
153+
match.JSONKeyTypeEqual("origin_server_ts", gjson.Number),
154+
},
155+
})
156+
}
157+
158+
// Tries to fetch an event between being invited and joined, and succeeds.
159+
// history_visibility: invited
160+
func TestFetchHistoricalInvitedEventFromBetweenInvite(t *testing.T) {
161+
runtime.SkipIf(t, runtime.Dendrite) // FIXME https://github.com/matrix-org/dendrite/issues/617
162+
163+
deployment := Deploy(t, b.BlueprintOneToOneRoom)
164+
defer deployment.Destroy(t)
165+
166+
alice := deployment.Client(t, "hs1", "@alice:hs1")
167+
bob := deployment.Client(t, "hs1", "@bob:hs1")
168+
169+
roomID := createRoomWithVisibility(t, alice, "invited")
170+
171+
alice.InviteRoom(t, roomID, bob.UserID)
172+
bob.MustSyncUntil(t, client.SyncReq{}, client.SyncInvitedTo(bob.UserID, roomID))
173+
174+
eventID := alice.SendEventSynced(t, roomID, b.Event{
175+
Type: "m.room.message",
176+
Content: map[string]interface{}{
177+
"msgtype": "m.text",
178+
"body": "Hello world",
179+
},
180+
})
181+
182+
bob.JoinRoom(t, roomID, nil)
183+
bob.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID))
184+
185+
res := fetchEvent(t, bob, roomID, eventID)
186+
187+
must.MatchResponse(t, res, match.HTTPResponse{
188+
StatusCode: 200,
189+
JSON: []match.JSON{
190+
// No harm in checking if the event data is also as expected
191+
match.JSONKeyEqual("content", map[string]interface{}{
192+
"msgtype": "m.text",
193+
"body": "Hello world",
194+
}),
195+
match.JSONKeyEqual("type", "m.room.message"),
196+
197+
// the spec technically doesn't list these following keys, but we're still checking them because sytest did.
198+
// see: https://github.com/matrix-org/matrix-doc/issues/3540
199+
match.JSONKeyEqual("room_id", roomID),
200+
match.JSONKeyEqual("sender", alice.UserID),
201+
match.JSONKeyEqual("event_id", eventID),
202+
match.JSONKeyTypeEqual("origin_server_ts", gjson.Number),
203+
},
204+
})
205+
}
206+
207+
// Tries to fetch an event before being invited, and fails.
208+
// history_visibility: invited
209+
func TestFetchHistoricalInvitedEventFromBeforeInvite(t *testing.T) {
210+
deployment := Deploy(t, b.BlueprintOneToOneRoom)
211+
defer deployment.Destroy(t)
212+
213+
alice := deployment.Client(t, "hs1", "@alice:hs1")
214+
bob := deployment.Client(t, "hs1", "@bob:hs1")
215+
216+
roomID := createRoomWithVisibility(t, alice, "invited")
217+
218+
eventID := alice.SendEventSynced(t, roomID, b.Event{
219+
Type: "m.room.message",
220+
Content: map[string]interface{}{
221+
"msgtype": "m.text",
222+
"body": "Hello world",
223+
},
224+
})
225+
226+
alice.InviteRoom(t, roomID, bob.UserID)
227+
bob.MustSyncUntil(t, client.SyncReq{}, client.SyncInvitedTo(bob.UserID, roomID))
228+
229+
bob.JoinRoom(t, roomID, nil)
230+
bob.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID))
231+
232+
res := fetchEvent(t, bob, roomID, eventID)
233+
234+
must.MatchResponse(t, res, match.HTTPResponse{
235+
StatusCode: 404,
236+
})
237+
}
238+
239+
// Tries to fetch an event without having joined, and fails.
240+
// history_visibility: shared
241+
// sytest: /event/ on non world readable room does not work
242+
func TestFetchEventNonWorldReadable(t *testing.T) {
243+
deployment := Deploy(t, b.BlueprintOneToOneRoom)
244+
defer deployment.Destroy(t)
245+
246+
alice := deployment.Client(t, "hs1", "@alice:hs1")
247+
bob := deployment.Client(t, "hs1", "@bob:hs1")
248+
249+
roomID := createRoomWithVisibility(t, alice, "shared")
250+
251+
eventID := alice.SendEventSynced(t, roomID, b.Event{
252+
Type: "m.room.message",
253+
Content: map[string]interface{}{
254+
"msgtype": "m.text",
255+
"body": "Hello world",
256+
},
257+
})
258+
259+
res := fetchEvent(t, bob, roomID, eventID)
260+
261+
must.MatchResponse(t, res, match.HTTPResponse{
262+
StatusCode: 404,
263+
})
264+
}
265+
266+
// Tries to fetch an event without having joined, and succeeds.
267+
// history_visibility: world_readable
268+
func TestFetchEventWorldReadable(t *testing.T) {
269+
runtime.SkipIf(t, runtime.Dendrite) // FIXME https://github.com/matrix-org/dendrite/issues/617
270+
271+
deployment := Deploy(t, b.BlueprintOneToOneRoom)
272+
defer deployment.Destroy(t)
273+
274+
alice := deployment.Client(t, "hs1", "@alice:hs1")
275+
bob := deployment.Client(t, "hs1", "@bob:hs1")
276+
277+
roomID := createRoomWithVisibility(t, alice, "world_readable")
278+
279+
eventID := alice.SendEventSynced(t, roomID, b.Event{
280+
Type: "m.room.message",
281+
Content: map[string]interface{}{
282+
"msgtype": "m.text",
283+
"body": "Hello world",
284+
},
285+
})
286+
287+
res := fetchEvent(t, bob, roomID, eventID)
288+
289+
must.MatchResponse(t, res, match.HTTPResponse{
290+
StatusCode: 200,
291+
JSON: []match.JSON{
292+
// No harm in checking if the event data is also as expected
293+
match.JSONKeyEqual("content", map[string]interface{}{
294+
"msgtype": "m.text",
295+
"body": "Hello world",
296+
}),
297+
match.JSONKeyEqual("type", "m.room.message"),
298+
299+
// the spec technically doesn't list these following keys, but we're still checking them because sytest did.
300+
// see: https://github.com/matrix-org/matrix-doc/issues/3540
301+
match.JSONKeyEqual("room_id", roomID),
302+
match.JSONKeyEqual("sender", alice.UserID),
303+
match.JSONKeyEqual("event_id", eventID),
304+
match.JSONKeyTypeEqual("origin_server_ts", gjson.Number),
305+
},
306+
})
307+
}

0 commit comments

Comments
 (0)