Skip to content

Commit 186321f

Browse files
authored
feat: implement all room state tests (#180)
* feat: implement all room state tests Signed-off-by: Meenal Trivedi <[email protected]> * fix Signed-off-by: Meenal Trivedi <[email protected]> * ineff assignment fix Signed-off-by: Meenal Trivedi <[email protected]> * fix Signed-off-by: Meenal Trivedi <[email protected]>
1 parent eb0f034 commit 186321f

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

internal/match/json.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ func JSONCheckOff(wantKey string, wantItems []interface{}, mapper func(gjson.Res
163163
// item calling `fn`. If `fn` returns an error, iterating stops and an error is returned.
164164
func JSONArrayEach(wantKey string, fn func(gjson.Result) error) JSON {
165165
return func(body []byte) error {
166-
res := gjson.GetBytes(body, wantKey)
166+
var res gjson.Result
167+
if wantKey == "" {
168+
res = gjson.ParseBytes(body)
169+
} else {
170+
res = gjson.GetBytes(body, wantKey)
171+
}
172+
167173
if !res.Exists() {
168174
return fmt.Errorf("missing key '%s'", wantKey)
169175
}

tests/csapi/apidoc_room_state_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,130 @@ func TestRoomState(t *testing.T) {
195195
},
196196
})
197197
})
198+
// sytest: POST /rooms/:room_id/state/m.room.name sets name
199+
t.Run("POST /rooms/:room_id/state/m.room.name sets name", func(t *testing.T) {
200+
t.Parallel()
201+
202+
roomID := authedClient.CreateRoom(t, map[string]interface{}{
203+
"visibility": "public",
204+
"preset": "public_chat",
205+
})
206+
207+
reqBody := client.WithJSONBody(t, map[string]interface{}{
208+
"name": "room_test_name",
209+
})
210+
211+
_ = authedClient.MustDoFunc(t, "PUT", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.name"}, reqBody)
212+
213+
res := authedClient.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.name"})
214+
215+
must.MatchResponse(t, res, match.HTTPResponse{
216+
JSON: []match.JSON{
217+
match.JSONKeyPresent("name"),
218+
match.JSONKeyEqual("name", "room_test_name"),
219+
},
220+
})
221+
})
222+
// sytest: GET /rooms/:room_id/state/m.room.topic gets topic
223+
t.Run("GET /rooms/:room_id/state/m.room.topic gets topic", func(t *testing.T) {
224+
t.Parallel()
225+
226+
roomID := authedClient.CreateRoom(t, map[string]interface{}{
227+
"visibility": "public",
228+
"preset": "public_chat",
229+
"topic": "room_topic_test",
230+
})
231+
232+
res := authedClient.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.topic"})
233+
234+
must.MatchResponse(t, res, match.HTTPResponse{
235+
JSON: []match.JSON{
236+
match.JSONKeyPresent("topic"),
237+
match.JSONKeyEqual("topic", "room_topic_test"),
238+
},
239+
})
240+
})
241+
// sytest: POST /rooms/:room_id/state/m.room.topic sets topic
242+
t.Run("PUT /rooms/:room_id/state/m.room.topic sets topic", func(t *testing.T) {
243+
t.Parallel()
244+
245+
roomID := authedClient.CreateRoom(t, map[string]interface{}{
246+
"visibility": "public",
247+
"preset": "public_chat",
248+
})
249+
250+
reqBody := client.WithJSONBody(t, map[string]interface{}{
251+
"topic": "room_test_topic",
252+
})
253+
254+
_ = authedClient.MustDoFunc(t, "PUT", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.topic"}, reqBody)
255+
256+
res := authedClient.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.topic"})
257+
258+
must.MatchResponse(t, res, match.HTTPResponse{
259+
JSON: []match.JSON{
260+
match.JSONKeyPresent("topic"),
261+
match.JSONKeyEqual("topic", "room_test_topic"),
262+
},
263+
})
264+
})
265+
// sytest: GET /rooms/:room_id/state fetches entire room state
266+
t.Run("GET /rooms/:room_id/state fetches entire room state", func(t *testing.T) {
267+
t.Parallel()
268+
269+
roomID := authedClient.CreateRoom(t, map[string]interface{}{
270+
"visibility": "public",
271+
"preset": "public_chat",
272+
"name": "room_test",
273+
"topic": "room_topic_test",
274+
})
275+
276+
wantKeys := map[string]bool{
277+
"m.room.create": true,
278+
"m.room.join_rules": true,
279+
"m.room.name": true,
280+
"m.room.power_levels": true,
281+
"m.room.topic": true,
282+
}
283+
284+
res := authedClient.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "state"})
285+
286+
must.MatchResponse(t, res, match.HTTPResponse{
287+
JSON: []match.JSON{
288+
match.JSONArrayEach("", func(val gjson.Result) error {
289+
gotKey := val.Get("type").Str
290+
if wantKeys[gotKey] {
291+
delete(wantKeys, gotKey)
292+
return nil
293+
}
294+
return nil
295+
}),
296+
},
297+
})
298+
if len(wantKeys) != 0 {
299+
t.Errorf("/state did not return the following expected keys: %v", wantKeys)
300+
}
301+
})
302+
// sytest: POST /createRoom with creation content
303+
t.Run("PUT /createRoom with creation content", func(t *testing.T) {
304+
t.Parallel()
305+
306+
roomID := authedClient.CreateRoom(t, map[string]interface{}{
307+
"visibility": "public",
308+
"preset": "public_chat",
309+
"creation_content": map[string]interface{}{
310+
"m.federate": false,
311+
},
312+
})
313+
314+
res := authedClient.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.create"})
315+
316+
must.MatchResponse(t, res, match.HTTPResponse{
317+
JSON: []match.JSON{
318+
match.JSONKeyPresent("m\\.federate"),
319+
match.JSONKeyEqual("m\\.federate", false),
320+
},
321+
})
322+
})
198323
})
199324
}

0 commit comments

Comments
 (0)