-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathapidoc_room_receipts_test.go
More file actions
105 lines (84 loc) · 3.58 KB
/
apidoc_room_receipts_test.go
File metadata and controls
105 lines (84 loc) · 3.58 KB
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
package csapi_tests
import (
"testing"
"github.com/matrix-org/complement"
"github.com/matrix-org/complement/b"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/helpers"
"github.com/tidwall/gjson"
)
// tests/10apidoc/37room-receipts.pl
func createRoomForReadReceipts(t *testing.T, c *client.CSAPI) (string, string) {
roomID := c.MustCreateRoom(t, map[string]interface{}{"preset": "public_chat"})
c.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(c.UserID, roomID))
eventID := sendMessageIntoRoom(t, c, roomID)
return roomID, eventID
}
func sendMessageIntoRoom(t *testing.T, c *client.CSAPI, roomID string) string {
return c.SendEventSynced(t, roomID, b.Event{
Type: "m.room.message",
Content: map[string]interface{}{
"msgtype": "m.text",
"body": "Hello world!",
},
})
}
func syncHasReadReceipt(roomID, userID, eventID string) client.SyncCheckOpt {
return client.SyncEphemeralHas(roomID, func(result gjson.Result) bool {
return result.Get("type").Str == "m.receipt" &&
result.Get("content").Get(eventID).Get(`m\.read`).Get(userID).Exists()
})
}
// sytest: POST /rooms/:room_id/receipt can create receipts
func TestRoomReceipts(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
roomID, eventID := createRoomForReadReceipts(t, alice)
alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "rooms", roomID, "receipt", "m.read", eventID}, client.WithJSONBody(t, struct{}{}))
// Make sure the read receipt shows up in sync.
sinceToken := alice.MustSyncUntil(t, client.SyncReq{}, syncHasReadReceipt(roomID, alice.UserID, eventID))
// Receipt events include a `room_id` field over federation, but they should
// not do so down `/sync` to clients. Ensure homeservers strip that field out.
t.Run("Receipts DO NOT include a `room_id` field", func(t *testing.T) {
// Send another event to read.
eventID2 := sendMessageIntoRoom(t, alice, roomID)
// Send a read receipt for the event.
alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "rooms", roomID, "receipt", "m.read", eventID2}, client.WithJSONBody(t, struct{}{}))
alice.MustSyncUntil(
t,
client.SyncReq{Since: sinceToken},
client.SyncEphemeralHas(roomID, func(r gjson.Result) bool {
if r.Get("type").Str != "m.read" {
return false
}
// Ensure that the `room_id` field does NOT exist.
if r.Get("room_id").Exists() {
t.Fatalf("Read receipt included `room_id` field down sync: %s", r.Raw)
}
// Exit the /sync loop.
return true;
}),
)
})
}
// sytest: POST /rooms/:room_id/read_markers can create read marker
func TestRoomReadMarkers(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
roomID, eventID := createRoomForReadReceipts(t, alice)
reqBody := client.WithJSONBody(t, map[string]interface{}{
"m.fully_read": eventID,
"m.read": eventID,
})
alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "rooms", roomID, "read_markers"}, reqBody)
// Make sure the read receipt shows up in sync.
alice.MustSyncUntil(t, client.SyncReq{}, syncHasReadReceipt(roomID, alice.UserID, eventID))
// Make sure that the fully_read receipt shows up in account data via sync.
// Use the same token as above to replay the syncs.
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncRoomAccountDataHas(roomID, func(result gjson.Result) bool {
return result.Get("type").Str == "m.fully_read" &&
result.Get("content.event_id").Str == eventID
}))
}