-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathrooms_state_test.go
More file actions
150 lines (120 loc) · 4.46 KB
/
rooms_state_test.go
File metadata and controls
150 lines (120 loc) · 4.46 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
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
//go:build !dendrite_blacklist
// +build !dendrite_blacklist
package csapi_tests
import (
"testing"
"time"
"github.com/tidwall/gjson"
"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/matrix-org/complement/match"
"github.com/matrix-org/complement/must"
)
func TestRoomCreationReportsEventsToMyself(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
bob := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "bob",
Password: "bobpassword",
})
roomID := alice.MustCreateRoom(t, map[string]interface{}{})
t.Run("parallel", func(t *testing.T) {
// sytest: Room creation reports m.room.create to myself
t.Run("Room creation reports m.room.create to myself", func(t *testing.T) {
t.Parallel()
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncTimelineHas(roomID, func(ev gjson.Result) bool {
if ev.Get("type").Str != "m.room.create" {
return false
}
must.Equal(t, ev.Get("sender").Str, alice.UserID, "wrong sender")
must.Equal(t, ev.Get("content").Get("creator").Str, alice.UserID, "wrong content.creator")
return true
}))
})
// sytest: Room creation reports m.room.member to myself
t.Run("Room creation reports m.room.member to myself", func(t *testing.T) {
t.Parallel()
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(alice.UserID, roomID, func(ev gjson.Result) bool {
must.MatchGJSON(t, ev,
match.JSONKeyEqual("sender", alice.UserID),
)
return true
}))
})
// sytest: Setting room topic reports m.room.topic to myself
t.Run("Setting room topic reports m.room.topic to myself", func(t *testing.T) {
t.Parallel()
const roomTopic = "Testing topic for the new room"
alice.SendEventSynced(t, roomID, b.Event{
Type: "m.room.topic",
StateKey: b.Ptr(""),
Content: map[string]interface{}{
"topic": roomTopic,
},
})
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncTimelineHas(roomID, func(ev gjson.Result) bool {
if ev.Get("type").Str != "m.room.topic" {
return false
}
if !ev.Get("state_key").Exists() {
return false
}
must.Equal(t, ev.Get("sender").Str, alice.UserID, "wrong sender")
must.Equal(t, ev.Get("content").Get("topic").Str, roomTopic, "wrong content.topic")
return true
}))
})
// sytest: Setting state twice is idempotent
t.Run("Setting state twice is idempotent", func(t *testing.T) {
t.Parallel()
stateEvent := b.Event{
Type: "a.test.state.type",
StateKey: b.Ptr(""),
Content: map[string]interface{}{
"a_key": "a_value",
},
}
firstID := alice.SendEventSynced(t, roomID, stateEvent)
secondID := alice.SendEventSynced(t, roomID, stateEvent)
if firstID != secondID {
t.Fatalf("Both Event IDs from supposedly-idempotent state-setting differ, %s != %s", firstID, secondID)
}
})
// sytest: Joining room twice is idempotent
t.Run("Joining room twice is idempotent", func(t *testing.T) {
t.Parallel()
roomID := bob.MustCreateRoom(t, map[string]interface{}{
"visibility": "public",
"preset": "public_chat",
})
alice.MustJoinRoom(t, roomID, nil)
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(alice.UserID, roomID))
firstID := *getEventIdForState(t, alice, roomID, "m.room.member", alice.UserID)
alice.MustJoinRoom(t, roomID, nil)
// Unfortunately there is no way to definitively wait
// for a 'potentially false second join event' without
// also failing the test on timeout, with the current APIs.
//
// So we take a conservative estimate of a 5-second sleep delay
// before we check on the server again.
time.Sleep(5 * time.Second)
secondID := *getEventIdForState(t, alice, roomID, "m.room.member", alice.UserID)
if firstID != secondID {
t.Fatalf("Both Event IDs from supposedly-idempotent room joins differ, %s != %s", firstID, secondID)
}
})
})
}
func getEventIdForState(t *testing.T, client *client.CSAPI, roomID, evType, stateKey string) *string {
res := client.MustDo(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state"})
result := must.ParseJSON(t, res.Body)
for _, ev := range result.Array() {
if ev.Get("type").Str == evType && ev.Get("state_key").Str == stateKey {
return b.Ptr(ev.Get("event_id").Str)
}
}
return nil
}