-
Notifications
You must be signed in to change notification settings - Fork 52
/
room_timestamp_to_event_test.go
427 lines (355 loc) · 17.1 KB
/
room_timestamp_to_event_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//go:build !dendrite_blacklist
// +build !dendrite_blacklist
// This file contains tests for the `/timestamp_to_event` client and federation API
// endpoints (also known as *jump to date*). As defined by MSC3030, which you can read
// here: https://github.com/matrix-org/matrix-doc/pull/3030
package tests
import (
"fmt"
"net/url"
"strconv"
"testing"
"time"
"github.com/matrix-org/complement/internal/b"
"github.com/matrix-org/complement/internal/client"
"github.com/matrix-org/complement/internal/match"
"github.com/matrix-org/complement/internal/must"
"github.com/tidwall/gjson"
)
func TestJumpToDateEndpoint(t *testing.T) {
deployment := Deploy(t, b.BlueprintHSWithApplicationService)
defer deployment.Destroy(t)
// Create the normal user which will send messages in the room
userID := "@alice:hs1"
alice := deployment.Client(t, "hs1", userID)
// Create the federated user which will fetch the messages from a remote homeserver
remoteUserID := "@charlie:hs2"
remoteCharlie := deployment.Client(t, "hs2", remoteUserID)
// Create the application service bridge user that can use the ?ts query parameter
asUserID := "@the-bridge-user:hs1"
as := deployment.Client(t, "hs1", asUserID)
t.Run("parallel", func(t *testing.T) {
t.Run("should find event after given timestmap", func(t *testing.T) {
t.Parallel()
roomID, eventA, _ := createTestRoom(t, alice)
mustCheckEventisReturnedForTime(t, alice, roomID, eventA.BeforeTimestamp, "f", eventA.EventID)
})
t.Run("should find event before given timestmap", func(t *testing.T) {
t.Parallel()
roomID, _, eventB := createTestRoom(t, alice)
mustCheckEventisReturnedForTime(t, alice, roomID, eventB.AfterTimestamp, "b", eventB.EventID)
})
t.Run("should find nothing before the earliest timestmap", func(t *testing.T) {
t.Parallel()
timeBeforeRoomCreation := time.Now()
roomID, _, _ := createTestRoom(t, alice)
mustCheckEventisReturnedForTime(t, alice, roomID, timeBeforeRoomCreation, "b", "")
})
t.Run("should find nothing after the latest timestmap", func(t *testing.T) {
t.Parallel()
roomID, _, eventB := createTestRoom(t, alice)
mustCheckEventisReturnedForTime(t, alice, roomID, eventB.AfterTimestamp, "f", "")
})
t.Run("should find next event topologically before given timestamp when all message timestamps are the same", func(t *testing.T) {
t.Parallel()
roomID, _, _ := createTestRoom(t, alice)
// Join from the application service bridge user so we can use to send
// some messages at a specific time.
as.JoinRoom(t, roomID, []string{"hs1"})
// Send a couple messages with the same timestamp after the other test
// messages in the room.
timeBeforeMessageCreation := time.Now()
sendMessageWithTimestamp(t, as, alice, roomID, timeBeforeMessageCreation, "messageWithSameTime1")
messageIDWithSameTime2 := sendMessageWithTimestamp(t, as, alice, roomID, timeBeforeMessageCreation, "messageWithSameTime2")
// Looking backwards from the time the messages were sent, we should find
// message2. A naive MSC3030 implementation that only sorts by timestamp
// will probably return message1 since it's the first one in the database.
mustCheckEventisReturnedForTime(t, alice, roomID, timeBeforeMessageCreation, "b", messageIDWithSameTime2)
})
t.Run("should find next event topologically after given timestmap when all message timestamps are the same", func(t *testing.T) {
t.Parallel()
roomID, _, _ := createTestRoom(t, alice)
// Join from the application service bridge user so we can use to send
// some messages at a specific time.
as.JoinRoom(t, roomID, []string{"hs1"})
// Send a couple messages with the same timestamp after the other test
// messages in the room.
timeBeforeMessageCreation := time.Now()
messageIDWithSameTime1 := sendMessageWithTimestamp(t, as, alice, roomID, timeBeforeMessageCreation, "messageWithSameTime1")
sendMessageWithTimestamp(t, as, alice, roomID, timeBeforeMessageCreation, "messageWithSameTime2")
// Looking forwards from the time the messages were sent, we should find
// message1.
mustCheckEventisReturnedForTime(t, alice, roomID, timeBeforeMessageCreation, "f", messageIDWithSameTime1)
})
// Just a sanity check that we're not leaking anything from the `/timestamp_to_event` endpoint
t.Run("should not be able to query a private room you are not a member of", func(t *testing.T) {
t.Parallel()
timeBeforeRoomCreation := time.Now()
// Alice will create the private room
roomID := alice.CreateRoom(t, map[string]interface{}{
"preset": "private_chat",
})
// We will use Bob to query the room they're not a member of
nonMemberUser := deployment.Client(t, "hs1", "@bob:hs1")
// Make the `/timestamp_to_event` request from Bob's perspective (non room member)
timestamp := makeTimestampFromTime(timeBeforeRoomCreation)
timestampString := strconv.FormatInt(timestamp, 10)
timestampToEventRes := nonMemberUser.DoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
"ts": []string{timestampString},
"dir": []string{"f"},
}))
// A random user is not allowed to query for events in a private room
// they're not a member of (forbidden).
if timestampToEventRes.StatusCode != 403 {
t.Fatalf("/timestamp_to_event returned %d HTTP status code but expected %d", timestampToEventRes.StatusCode, 403)
}
})
// Just a sanity check that we're not leaking anything from the `/timestamp_to_event` endpoint
t.Run("should not be able to query a public room you are not a member of", func(t *testing.T) {
t.Parallel()
timeBeforeRoomCreation := time.Now()
// Alice will create the public room
roomID := alice.CreateRoom(t, map[string]interface{}{
"preset": "public_chat",
})
// We will use Bob to query the room they're not a member of
nonMemberUser := deployment.Client(t, "hs1", "@bob:hs1")
// Make the `/timestamp_to_event` request from Bob's perspective (non room member)
timestamp := makeTimestampFromTime(timeBeforeRoomCreation)
timestampString := strconv.FormatInt(timestamp, 10)
timestampToEventRes := nonMemberUser.DoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
"ts": []string{timestampString},
"dir": []string{"f"},
}))
// A random user is not allowed to query for events in a public room
// they're not a member of (forbidden).
if timestampToEventRes.StatusCode != 403 {
t.Fatalf("/timestamp_to_event returned %d HTTP status code but expected %d", timestampToEventRes.StatusCode, 403)
}
})
t.Run("federation", func(t *testing.T) {
t.Run("looking forwards, should be able to find event that was sent before we joined", func(t *testing.T) {
t.Parallel()
roomID, eventA, _ := createTestRoom(t, alice)
remoteCharlie.JoinRoom(t, roomID, []string{"hs1"})
mustCheckEventisReturnedForTime(t, remoteCharlie, roomID, eventA.BeforeTimestamp, "f", eventA.EventID)
})
t.Run("looking backwards, should be able to find event that was sent before we joined", func(t *testing.T) {
t.Parallel()
roomID, _, eventB := createTestRoom(t, alice)
remoteCharlie.JoinRoom(t, roomID, []string{"hs1"})
mustCheckEventisReturnedForTime(t, remoteCharlie, roomID, eventB.AfterTimestamp, "b", eventB.EventID)
})
t.Run("when looking backwards before the room was created, should be able to find event that was imported", func(t *testing.T) {
t.Parallel()
timeBeforeRoomCreation := time.Now()
roomID, _, _ := createTestRoom(t, alice)
// Join from the application service bridge user so we can use it to send
// some messages at a specific time.
as.JoinRoom(t, roomID, []string{"hs1"})
// Import a message in the room before the room was created
importTime := time.Date(2022, 01, 03, 0, 0, 0, 0, time.Local)
importedEventID := sendMessageWithTimestamp(t, as, alice, roomID, importTime, "old imported event")
remoteCharlie.JoinRoom(t, roomID, []string{"hs1"})
mustCheckEventisReturnedForTime(t, remoteCharlie, roomID, timeBeforeRoomCreation, "b", importedEventID)
})
t.Run("can paginate after getting remote event from timestamp to event endpoint", func(t *testing.T) {
t.Parallel()
roomID, eventA, eventB := createTestRoom(t, alice)
remoteCharlie.JoinRoom(t, roomID, []string{"hs1"})
mustCheckEventisReturnedForTime(t, remoteCharlie, roomID, eventB.AfterTimestamp, "b", eventB.EventID)
// Get a pagination token from eventB
contextRes := remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "context", eventB.EventID}, client.WithContentType("application/json"), client.WithQueries(url.Values{
"limit": []string{"0"},
}))
contextResResBody := client.ParseJSON(t, contextRes)
paginationToken := client.GetJSONFieldStr(t, contextResResBody, "end")
// Hit `/messages` until `eventA` has been backfilled and replicated across
// workers (the worker persisting events isn't necessarily the same as the worker
// serving `/messages`)
fetchUntilMessagesResponseHas(t, remoteCharlie, roomID, func(ev gjson.Result) bool {
return ev.Get("event_id").Str == eventA.EventID
})
// Paginate backwards from eventB
messagesRes := remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
"dir": []string{"b"},
"limit": []string{"100"},
"from": []string{paginationToken},
}))
// Make sure both messages are visible
must.MatchResponse(t, messagesRes, match.HTTPResponse{
JSON: []match.JSON{
match.JSONCheckOffAllowUnwanted("chunk", []interface{}{eventA.EventID, eventB.EventID}, func(r gjson.Result) interface{} {
return r.Get("event_id").Str
}, nil),
},
})
})
})
})
}
type eventTime struct {
EventID string
BeforeTimestamp time.Time
AfterTimestamp time.Time
}
func createTestRoom(t *testing.T, c *client.CSAPI) (roomID string, eventA, eventB *eventTime) {
t.Helper()
roomID = c.CreateRoom(t, map[string]interface{}{
"preset": "public_chat",
})
timeBeforeEventA := time.Now()
eventAID := c.SendEventSynced(t, roomID, b.Event{
Type: "m.room.message",
Content: map[string]interface{}{
"msgtype": "m.text",
"body": "Message A",
},
})
timeAfterEventA := time.Now()
eventBID := c.SendEventSynced(t, roomID, b.Event{
Type: "m.room.message",
Content: map[string]interface{}{
"msgtype": "m.text",
"body": "Message B",
},
})
timeAfterEventB := time.Now()
eventA = &eventTime{EventID: eventAID, BeforeTimestamp: timeBeforeEventA, AfterTimestamp: timeAfterEventA}
eventB = &eventTime{EventID: eventBID, BeforeTimestamp: timeAfterEventA, AfterTimestamp: timeAfterEventB}
return roomID, eventA, eventB
}
func sendMessageWithTimestamp(t *testing.T, as *client.CSAPI, c *client.CSAPI, roomID string, messageTime time.Time, message string) (messageEventID string) {
t.Helper()
timestamp := makeTimestampFromTime(messageTime)
timestampString := strconv.FormatInt(timestamp, 10)
// We have to use an application service user because they are the only one
// allowed to use the `?ts` query parameter.
//
// We can't use as.SendEventSynced(...) because application services can't use
// the /sync API.
sendRes := as.DoFunc(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", getTxnID("sendMessageWithTimestamp-txn")}, client.WithContentType("application/json"), client.WithJSONBody(t, map[string]interface{}{
"body": message,
"msgtype": "m.text",
}), client.WithQueries(url.Values{
"ts": []string{timestampString},
}))
sendBody := client.ParseJSON(t, sendRes)
messageEventID = client.GetJSONFieldStr(t, sendBody, "event_id")
// Make sure the imported event has reached the homeserver
c.MustSyncUntil(t, client.SyncReq{}, client.SyncTimelineHas(roomID, func(ev gjson.Result) bool {
return ev.Get("event_id").Str == messageEventID
}))
return
}
// Fetch event from /timestamp_to_event and ensure it matches the expectedEventId
func mustCheckEventisReturnedForTime(t *testing.T, c *client.CSAPI, roomID string, givenTime time.Time, direction string, expectedEventId string) {
t.Helper()
givenTimestamp := makeTimestampFromTime(givenTime)
timestampString := strconv.FormatInt(givenTimestamp, 10)
timestampToEventRes := c.DoFunc(t, "GET", []string{"_matrix", "client", "v1", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
"ts": []string{timestampString},
"dir": []string{direction},
}))
timestampToEventResBody := client.ParseJSON(t, timestampToEventRes)
// Only allow a 200 response meaning we found an event or when no `expectedEventId`, a
// 404 meaning we didn't find anything. Other status codes will throw and assumed to
// be application errors.
actualEventId := ""
if timestampToEventRes.StatusCode == 200 {
actualEventId = client.GetJSONFieldStr(t, timestampToEventResBody, "event_id")
} else if timestampToEventRes.StatusCode != 404 || (timestampToEventRes.StatusCode == 404 && expectedEventId != "") {
t.Fatalf("mustCheckEventisReturnedForTime: /timestamp_to_event request failed with status=%d body=%s", timestampToEventRes.StatusCode, string(timestampToEventResBody))
}
if actualEventId != expectedEventId {
debugMessageList := getDebugMessageListFromMessagesResponse(t, c, roomID, expectedEventId, actualEventId, givenTimestamp)
t.Fatalf(
"Want %s given %s but got %s\n%s",
decorateStringWithAnsiColor(expectedEventId, AnsiColorGreen),
decorateStringWithAnsiColor(timestampString, AnsiColorYellow),
decorateStringWithAnsiColor(actualEventId, AnsiColorRed),
debugMessageList,
)
}
}
func getDebugMessageListFromMessagesResponse(t *testing.T, c *client.CSAPI, roomID string, expectedEventId string, actualEventId string, givenTimestamp int64) string {
t.Helper()
messagesRes := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
// The events returned will be from the newest -> oldest since we're going backwards
"dir": []string{"b"},
"limit": []string{"100"},
}))
messsageResBody := client.ParseJSON(t, messagesRes)
wantKey := "chunk"
keyRes := gjson.GetBytes(messsageResBody, wantKey)
if !keyRes.Exists() {
t.Fatalf("missing key '%s'", wantKey)
}
if !keyRes.IsArray() {
t.Fatalf("key '%s' is not an array (was %s)", wantKey, keyRes.Type)
}
// Make the events go from oldest-in-time -> newest-in-time
events := reverseGjsonArray(keyRes.Array())
if len(events) == 0 {
t.Fatalf(
"getDebugMessageListFromMessagesResponse found no messages in the room(%s).",
roomID,
)
}
// We need some padding for some lines to make them all align with the label.
// Pad this out so it equals whatever the longest label is.
paddingString := " "
resultantString := fmt.Sprintf("%s-- oldest events --\n", paddingString)
givenTimestampAlreadyInserted := false
givenTimestampMarker := decorateStringWithAnsiColor(fmt.Sprintf("%s-- givenTimestamp=%s --\n", paddingString, strconv.FormatInt(givenTimestamp, 10)), AnsiColorYellow)
// We're iterating over the events from oldest-in-time -> newest-in-time
for _, ev := range events {
// As we go, keep checking whether the givenTimestamp is
// older(before-in-time) than the current event and insert a timestamp
// marker as soon as we find the spot
if givenTimestamp < ev.Get("origin_server_ts").Int() && !givenTimestampAlreadyInserted {
resultantString += givenTimestampMarker
givenTimestampAlreadyInserted = true
}
eventID := ev.Get("event_id").String()
eventIDString := eventID
labelString := paddingString
if eventID == expectedEventId {
eventIDString = decorateStringWithAnsiColor(eventID, AnsiColorGreen)
labelString = "(want) "
} else if eventID == actualEventId {
eventIDString = decorateStringWithAnsiColor(eventID, AnsiColorRed)
labelString = " (got) "
}
resultantString += fmt.Sprintf(
"%s%s (%s) - %s\n",
labelString,
eventIDString,
strconv.FormatInt(ev.Get("origin_server_ts").Int(), 10),
ev.Get("type").String(),
)
}
// The givenTimestamp could be newer(after-in-time) than any of the other events
if givenTimestamp > events[len(events)-1].Get("origin_server_ts").Int() && !givenTimestampAlreadyInserted {
resultantString += givenTimestampMarker
givenTimestampAlreadyInserted = true
}
resultantString += fmt.Sprintf("%s-- newest events --\n", paddingString)
return resultantString
}
func makeTimestampFromTime(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}
const AnsiColorRed string = "31"
const AnsiColorGreen string = "32"
const AnsiColorYellow string = "33"
func decorateStringWithAnsiColor(inputString, decorationColor string) string {
return fmt.Sprintf("\033[%sm%s\033[0m", decorationColor, inputString)
}
func reverseGjsonArray(in []gjson.Result) []gjson.Result {
out := make([]gjson.Result, len(in))
for i := 0; i < len(in); i++ {
out[i] = in[len(in)-i-1]
}
return out
}