-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathpublic_rooms_test.go
More file actions
269 lines (228 loc) · 7.59 KB
/
Copy pathpublic_rooms_test.go
File metadata and controls
269 lines (228 loc) · 7.59 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
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
package csapi_tests
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/tidwall/gjson"
"github.com/matrix-org/complement"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/match"
"github.com/matrix-org/complement/must"
"github.com/matrix-org/complement/should"
)
func TestPublicRooms(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
hostname := deployment.GetFullyQualifiedHomeserverName(t, "hs1")
t.Run("parallel", func(t *testing.T) {
// sytest: Can search public room list
t.Run("Can search public room list", func(t *testing.T) {
t.Parallel()
authedClient := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
roomID := authedClient.MustCreateRoom(t, map[string]any{
"visibility": "public",
"name": "Test Name",
"topic": "Test Topic Wombles",
})
authedClient.MustDo(
t,
"POST",
[]string{"_matrix", "client", "v3", "publicRooms"},
client.WithJSONBody(t, map[string]any{
"filter": map[string]any{
"generic_search_term": "wombles",
},
}),
client.WithRetryUntil(15*time.Second, func(res *http.Response) bool {
body := must.ParseJSON(t, res.Body)
must.MatchGJSON(
t,
body,
match.JSONKeyPresent("chunk"),
match.JSONKeyTypeEqual("chunk", gjson.JSON),
)
chunk := body.Get("chunk")
if !chunk.IsArray() {
t.Logf("chunk is not an array")
return false
}
results := chunk.Array()
if len(results) != 1 {
t.Logf("expected a single search result, got %d", len(results))
return false
}
foundRoomID := results[0].Get("room_id").Str
if foundRoomID != roomID {
t.Logf("expected room_id %s in search results, got %s", roomID, foundRoomID)
return false
}
return true
}),
)
})
// sytest: Name/topic keys are correct
t.Run("Name/topic keys are correct", func(t *testing.T) {
t.Parallel()
authedClient := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
// Define room configurations matching the Sytest
roomConfigs := []struct {
alias string
name string
topic string
}{
{"publicroomalias_no_name", "", ""},
{"publicroomalias_with_name", "name_1", ""},
{"publicroomalias_with_topic", "", "topic_1"},
{"publicroomalias_with_name_topic", "name_2", "topic_2"},
{"publicroom_with_unicode_chars_name", "un nom français", ""},
{"publicroom_with_unicode_chars_topic", "", "un topic à la française"},
{"publicroom_with_unicode_chars_name_topic", "un nom français", "un topic à la française"},
}
// Create all rooms with their configurations
createdRooms := make(map[string]struct {
roomID string
name string
topic string
})
for _, config := range roomConfigs {
roomOptions := map[string]interface{}{
"visibility": "public",
"room_alias_name": config.alias,
}
if config.name != "" {
roomOptions["name"] = config.name
}
if config.topic != "" {
roomOptions["topic"] = config.topic
}
roomID := authedClient.MustCreateRoom(t, roomOptions)
createdRooms[config.alias] = struct {
roomID string
name string
topic string
}{
roomID: roomID,
name: config.name,
topic: config.topic,
}
t.Logf("Created room %s with alias %s", roomID, config.alias)
}
// Poll /publicRooms until all our rooms appear with correct data
authedClient.MustDo(t, "GET", []string{"_matrix", "client", "v3", "publicRooms"},
client.WithRetryUntil(authedClient.SyncUntilTimeout, func(res *http.Response) bool {
body := must.ParseJSON(t, res.Body)
must.MatchGJSON(
t,
body,
match.JSONKeyPresent("chunk"),
match.JSONKeyTypeEqual("chunk", gjson.JSON),
)
chunk := body.Get("chunk")
if !chunk.IsArray() {
t.Logf("chunk is not an array")
return false
}
// Track which rooms we've correctly found
foundRooms := make(map[string]bool)
// Keep track of any rooms that we didn't expect to see.
unexpectedRooms := make([]string, 0)
// Check each room in the public rooms list
for _, roomData := range chunk.Array() {
roomId := roomData.Get("room_id").Str
// Verify required keys are present. This applies to any room we see.
err := should.MatchGJSON(
roomData,
match.JSONKeyPresent("world_readable"),
match.JSONKeyPresent("guest_can_join"),
match.JSONKeyPresent("num_joined_members"),
)
if err != nil {
// This room is missing required keys, log and try again.
t.Logf("Room %s data missing required keys: %s", roomId, err.Error())
return false
}
validationErrors := make([]error, 0)
canonicalAlias := roomData.Get("canonical_alias").Str
name := roomData.Get("name").Str
topic := roomData.Get("topic").Str
numMembers := roomData.Get("num_joined_members").Int()
// Skip rooms that aren't ours
if canonicalAlias == "" {
unexpectedRooms = append(unexpectedRooms, roomId)
continue
}
// Find which of our rooms this matches
var matchedAlias string
for alias := range createdRooms {
expectedAlias := "#" + alias + ":" + string(hostname)
if canonicalAlias == expectedAlias {
matchedAlias = alias
break
}
}
if matchedAlias == "" {
continue // Not one of our rooms
}
roomConfig := createdRooms[matchedAlias]
// Verify member count
if numMembers != 1 {
err = fmt.Errorf("Room %s has %d members, expected 1", matchedAlias, numMembers)
validationErrors = append(validationErrors, err)
}
// Verify name field
if roomConfig.name != "" {
if name != roomConfig.name {
err = fmt.Errorf("Room %s has name '%s', expected '%s'", matchedAlias, name, roomConfig.name)
validationErrors = append(validationErrors, err)
}
} else {
if name != "" {
err = fmt.Errorf("Room %s has unexpected name '%s', expected no name", matchedAlias, name)
validationErrors = append(validationErrors, err)
}
}
// Verify topic field
if roomConfig.topic != "" {
if topic != roomConfig.topic {
err = fmt.Errorf("Room %s has topic '%s', expected '%s'", matchedAlias, topic, roomConfig.topic)
validationErrors = append(validationErrors, err)
}
} else {
if topic != "" {
err = fmt.Errorf("Room %s has unexpected topic '%s', expected no topic", matchedAlias, topic)
validationErrors = append(validationErrors, err)
}
}
if len(validationErrors) > 0 {
for _, e := range validationErrors {
t.Logf("Validation error for room %s: %s", matchedAlias, e.Error())
}
return false
}
// Mark this room as correctly found
foundRooms[matchedAlias] = true
t.Logf("Successfully validated room %s", matchedAlias)
}
// Check if we found all our rooms
if len(foundRooms) != len(createdRooms) {
missing := []string{}
for alias := range createdRooms {
if !foundRooms[alias] {
missing = append(missing, alias)
}
}
t.Logf("Missing rooms in public list: %v (found %d/%d)", missing, len(foundRooms), len(createdRooms))
if len(unexpectedRooms) > 0 {
t.Logf("Also found unexpected rooms: %v", unexpectedRooms)
}
return false
}
t.Logf("All %d rooms found with correct name/topic data", len(foundRooms))
return true
}),
)
})
})
}