Skip to content

Commit 57edac7

Browse files
committed
Add (Must)GetStateEventContent
It's used in enough places to warrant being a helper function.
1 parent 55095cf commit 57edac7

8 files changed

Lines changed: 88 additions & 168 deletions

client/client.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,21 @@ func (c *CSAPI) SendRedaction(t ct.TestLike, roomID string, content map[string]i
355355
return c.Do(t, "PUT", paths, WithJSONBody(t, content))
356356
}
357357

358+
// MustGetStateEvent returns the event content for the given state event. Fails the test if the state event does not exist.
359+
func (c *CSAPI) MustGetStateEventContent(t ct.TestLike, roomID, eventType, stateKey string) (content gjson.Result) {
360+
t.Helper()
361+
res := c.GetStateEventContent(t, roomID, eventType, stateKey)
362+
mustRespond2xx(t, res)
363+
body := ParseJSON(t, res)
364+
return gjson.ParseBytes(body)
365+
}
366+
367+
// GetStateEvent returns the event content for the given state event. Use this form to detect absence via 404.
368+
func (c *CSAPI) GetStateEventContent(t ct.TestLike, roomID, eventType, stateKey string) *http.Response {
369+
t.Helper()
370+
return c.Do(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", eventType, stateKey})
371+
}
372+
358373
// MustSendTyping marks this user as typing until the timeout is reached. If isTyping is false, timeout is ignored.
359374
func (c *CSAPI) MustSendTyping(t ct.TestLike, roomID string, isTyping bool, timeoutMillis int) {
360375
res := c.SendTyping(t, roomID, isTyping, timeoutMillis)
@@ -822,6 +837,7 @@ func (c *CSAPI) SendToDeviceMessages(t ct.TestLike, evType string, messages map[
822837
}
823838

824839
func mustRespond2xx(t ct.TestLike, res *http.Response) {
840+
t.Helper()
825841
if res.StatusCode >= 200 && res.StatusCode < 300 {
826842
return // 2xx
827843
}

tests/csapi/apidoc_room_create_test.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,8 @@ func TestRoomCreate(t *testing.T) {
5959
"topic": "Test Room",
6060
"preset": "public_chat",
6161
})
62-
res := alice.MustDo(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.topic"})
63-
must.MatchResponse(t, res, match.HTTPResponse{
64-
StatusCode: 200,
65-
JSON: []match.JSON{
66-
match.JSONKeyEqual("topic", "Test Room"),
67-
},
68-
})
62+
content := alice.MustGetStateEventContent(t, roomID, "m.room.topic", "")
63+
must.MatchGJSON(t, content, match.JSONKeyEqual("topic", "Test Room"))
6964
})
7065
// sytest: POST /createRoom makes a room with a name
7166
t.Run("POST /createRoom makes a room with a name", func(t *testing.T) {
@@ -74,13 +69,8 @@ func TestRoomCreate(t *testing.T) {
7469
"name": "Test Room",
7570
"preset": "public_chat",
7671
})
77-
res := alice.MustDo(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.name"})
78-
must.MatchResponse(t, res, match.HTTPResponse{
79-
StatusCode: 200,
80-
JSON: []match.JSON{
81-
match.JSONKeyEqual("name", "Test Room"),
82-
},
83-
})
72+
content := alice.MustGetStateEventContent(t, roomID, "m.room.name", "")
73+
must.MatchGJSON(t, content, match.JSONKeyEqual("name", "Test Room"))
8474
})
8575
// sytest: POST /createRoom creates a room with the given version
8676
t.Run("POST /createRoom creates a room with the given version", func(t *testing.T) {
@@ -89,13 +79,8 @@ func TestRoomCreate(t *testing.T) {
8979
"room_version": "2",
9080
"preset": "public_chat",
9181
})
92-
res := alice.MustDo(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.create"})
93-
must.MatchResponse(t, res, match.HTTPResponse{
94-
StatusCode: 200,
95-
JSON: []match.JSON{
96-
match.JSONKeyEqual("room_version", "2"),
97-
},
98-
})
82+
content := alice.MustGetStateEventContent(t, roomID, "m.room.create", "")
83+
must.MatchGJSON(t, content, match.JSONKeyEqual("room_version", "2"))
9984
})
10085
// sytest: POST /createRoom makes a private room with invites
10186
t.Run("POST /createRoom makes a private room with invites", func(t *testing.T) {

tests/csapi/apidoc_room_members_test.go

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,8 @@ func TestRoomMembers(t *testing.T) {
157157
})
158158

159159
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID))
160-
res = alice.Do(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.member", bob.UserID})
161-
162-
must.MatchResponse(t, res, match.HTTPResponse{
163-
JSON: []match.JSON{
164-
match.JSONKeyEqual("foo", "bar"),
165-
match.JSONKeyEqual("membership", "join"),
166-
},
167-
})
160+
content := alice.MustGetStateEventContent(t, roomID, "m.room.member", bob.UserID)
161+
must.MatchGJSON(t, content, match.JSONKeyEqual("membership", "join"), match.JSONKeyEqual("foo", "bar"))
168162
})
169163
// sytest: POST /join/:room_alias can join a room with custom content
170164
t.Run("POST /join/:room_alias can join a room with custom content", func(t *testing.T) {
@@ -186,14 +180,8 @@ func TestRoomMembers(t *testing.T) {
186180
})
187181

188182
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID))
189-
res = alice.Do(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.member", bob.UserID})
190-
191-
must.MatchResponse(t, res, match.HTTPResponse{
192-
JSON: []match.JSON{
193-
match.JSONKeyEqual("foo", "bar"),
194-
match.JSONKeyEqual("membership", "join"),
195-
},
196-
})
183+
content := alice.MustGetStateEventContent(t, roomID, "m.room.member", bob.UserID)
184+
must.MatchGJSON(t, content, match.JSONKeyEqual("membership", "join"), match.JSONKeyEqual("foo", "bar"))
197185
})
198186

199187
// sytest: POST /rooms/:room_id/ban can ban a user
@@ -221,12 +209,8 @@ func TestRoomMembers(t *testing.T) {
221209
return ev.Get("content.membership").Str == "ban"
222210
}))
223211
// verify bob is banned
224-
res = alice.Do(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.member", bob.UserID})
225-
must.MatchResponse(t, res, match.HTTPResponse{
226-
JSON: []match.JSON{
227-
match.JSONKeyEqual("membership", "ban"),
228-
},
229-
})
212+
content := alice.MustGetStateEventContent(t, roomID, "m.room.member", bob.UserID)
213+
must.MatchGJSON(t, content, match.JSONKeyEqual("membership", "ban"))
230214
})
231215

232216
// sytest: POST /rooms/:room_id/invite can send an invite
@@ -235,12 +219,8 @@ func TestRoomMembers(t *testing.T) {
235219
roomID := alice.MustCreateRoom(t, map[string]interface{}{})
236220
alice.MustInviteRoom(t, roomID, bob.UserID)
237221
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncInvitedTo(bob.UserID, roomID))
238-
res := alice.Do(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.member", bob.UserID})
239-
must.MatchResponse(t, res, match.HTTPResponse{
240-
JSON: []match.JSON{
241-
match.JSONKeyEqual("membership", "invite"),
242-
},
243-
})
222+
content := alice.MustGetStateEventContent(t, roomID, "m.room.member", bob.UserID)
223+
must.MatchGJSON(t, content, match.JSONKeyEqual("membership", "invite"))
244224
})
245225

246226
// sytest: POST /rooms/:room_id/leave can leave a room
@@ -253,13 +233,8 @@ func TestRoomMembers(t *testing.T) {
253233
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID))
254234
bob.MustLeaveRoom(t, roomID)
255235
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncLeftFrom(bob.UserID, roomID))
256-
257-
res := alice.Do(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.member", bob.UserID})
258-
must.MatchResponse(t, res, match.HTTPResponse{
259-
JSON: []match.JSON{
260-
match.JSONKeyEqual("membership", "leave"),
261-
},
262-
})
236+
content := alice.MustGetStateEventContent(t, roomID, "m.room.member", bob.UserID)
237+
must.MatchGJSON(t, content, match.JSONKeyEqual("membership", "leave"))
263238
})
264239
})
265240
}

tests/csapi/power_levels_test.go

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -60,52 +60,47 @@ func TestPowerLevels(t *testing.T) {
6060
// sytest: GET /rooms/:room_id/state/m.room.power_levels can fetch levels
6161
t.Run("GET /rooms/:room_id/state/m.room.power_levels can fetch levels", func(t *testing.T) {
6262
// Test if the old state still exists
63-
res := alice.MustDo(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.power_levels"})
64-
6563
// note: before v10 we technically cannot assume that powerlevel integers are json numbers,
6664
// as they can be both strings and numbers.
6765
// However, for this test, we control the test environment,
6866
// and we will assume the server is sane and give us powerlevels as numbers,
6967
// and if it doesn't, that's an offense worthy of a frown.
68+
content := alice.MustGetStateEventContent(t, roomID, "m.room.power_levels", "")
69+
must.MatchGJSON(t, content,
70+
match.JSONKeyTypeEqual("ban", gjson.Number),
71+
match.JSONKeyTypeEqual("kick", gjson.Number),
72+
match.JSONKeyTypeEqual("redact", gjson.Number),
73+
match.JSONKeyTypeEqual("state_default", gjson.Number),
74+
match.JSONKeyTypeEqual("events_default", gjson.Number),
75+
match.JSONKeyTypeEqual("users_default", gjson.Number),
76+
77+
match.JSONMapEach("events", func(k, v gjson.Result) error {
78+
if v.Type != gjson.Number {
79+
return fmt.Errorf("key %s is not a number", k.Str)
80+
} else {
81+
return nil
82+
}
83+
}),
7084

71-
must.MatchResponse(t, res, match.HTTPResponse{
72-
StatusCode: 200,
73-
JSON: []match.JSON{
74-
match.JSONKeyTypeEqual("ban", gjson.Number),
75-
match.JSONKeyTypeEqual("kick", gjson.Number),
76-
match.JSONKeyTypeEqual("redact", gjson.Number),
77-
match.JSONKeyTypeEqual("state_default", gjson.Number),
78-
match.JSONKeyTypeEqual("events_default", gjson.Number),
79-
match.JSONKeyTypeEqual("users_default", gjson.Number),
80-
81-
match.JSONMapEach("events", func(k, v gjson.Result) error {
82-
if v.Type != gjson.Number {
83-
return fmt.Errorf("key %s is not a number", k.Str)
84-
} else {
85-
return nil
86-
}
87-
}),
88-
89-
match.JSONMapEach("users", func(k, v gjson.Result) error {
90-
if v.Type != gjson.Number {
91-
return fmt.Errorf("key %s is not a number", k.Str)
92-
} else {
93-
return nil
94-
}
95-
}),
96-
97-
func(body gjson.Result) error {
98-
userDefault := int(body.Get("users_default").Num)
99-
thisUser := int(body.Get("users." + client.GjsonEscape(alice.UserID)).Num)
100-
101-
if thisUser > userDefault {
102-
return nil
103-
} else {
104-
return fmt.Errorf("expected room creator (%d) to have a higher-than-default powerlevel (which is %d)", thisUser, userDefault)
105-
}
106-
},
85+
match.JSONMapEach("users", func(k, v gjson.Result) error {
86+
if v.Type != gjson.Number {
87+
return fmt.Errorf("key %s is not a number", k.Str)
88+
} else {
89+
return nil
90+
}
91+
}),
92+
93+
func(body gjson.Result) error {
94+
userDefault := int(body.Get("users_default").Num)
95+
thisUser := int(body.Get("users." + client.GjsonEscape(alice.UserID)).Num)
96+
97+
if thisUser > userDefault {
98+
return nil
99+
} else {
100+
return fmt.Errorf("expected room creator (%d) to have a higher-than-default powerlevel (which is %d)", thisUser, userDefault)
101+
}
107102
},
108-
})
103+
)
109104
})
110105

111106
// sytest: PUT /rooms/:room_id/state/m.room.power_levels can set levels
@@ -172,13 +167,7 @@ func TestPowerLevels(t *testing.T) {
172167
})
173168

174169
// Test if the old state still exists
175-
res = alice.MustDo(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.power_levels"})
176-
177-
must.MatchResponse(t, res, match.HTTPResponse{
178-
StatusCode: 200,
179-
JSON: []match.JSON{
180-
match.JSONKeyMissing("users"),
181-
},
182-
})
170+
content := alice.MustGetStateEventContent(t, roomID, "m.room.power_levels", "")
171+
must.MatchGJSON(t, content, match.JSONKeyMissing("users"))
183172
})
184173
}

tests/csapi/room_leave_test.go

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,12 @@ func TestLeftRoomFixture(t *testing.T) {
128128
// sytest: Can get rooms/{roomId}/state for a departed room (SPEC-216)
129129
t.Run("Can get rooms/{roomId}/state for a departed room", func(t *testing.T) {
130130
// Bob gets the old state
131-
resp := bob.MustDo(
132-
t,
133-
"GET",
134-
[]string{"_matrix", "client", "v3", "rooms", roomID, "state", madeUpStateKey},
135-
)
136-
must.MatchResponse(t, resp, match.HTTPResponse{
137-
JSON: []match.JSON{
138-
match.JSONKeyEqual("body", beforeMadeUpState),
139-
},
140-
})
131+
content := bob.MustGetStateEventContent(t, roomID, madeUpStateKey, "")
132+
must.MatchGJSON(t, content, match.JSONKeyEqual("body", beforeMadeUpState))
141133

142134
// ...While Alice gets the new state
143-
resp = alice.MustDo(
144-
t,
145-
"GET",
146-
[]string{"_matrix", "client", "v3", "rooms", roomID, "state", madeUpStateKey},
147-
)
148-
must.MatchResponse(t, resp, match.HTTPResponse{
149-
JSON: []match.JSON{
150-
match.JSONKeyEqual("body", afterMadeUpState),
151-
},
152-
})
135+
content = alice.MustGetStateEventContent(t, roomID, madeUpStateKey, "")
136+
must.MatchGJSON(t, content, match.JSONKeyEqual("body", afterMadeUpState))
153137
})
154138

155139
// sytest: Can get rooms/{roomId}/members for a departed room (SPEC-216)
@@ -205,28 +189,12 @@ func TestLeftRoomFixture(t *testing.T) {
205189
// sytest: Can get 'm.room.name' state for a departed room (SPEC-216)
206190
t.Run("Can get 'm.room.name' state for a departed room", func(t *testing.T) {
207191
// Bob gets the old name
208-
resp := bob.MustDo(
209-
t,
210-
"GET",
211-
[]string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.name"},
212-
)
213-
must.MatchResponse(t, resp, match.HTTPResponse{
214-
JSON: []match.JSON{
215-
match.JSONKeyEqual("name", beforeRoomName),
216-
},
217-
})
192+
content := bob.MustGetStateEventContent(t, roomID, "m.room.name", "")
193+
must.MatchGJSON(t, content, match.JSONKeyEqual("name", beforeRoomName))
218194

219195
// ...While Alice gets the new name
220-
resp = alice.MustDo(
221-
t,
222-
"GET",
223-
[]string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.name"},
224-
)
225-
must.MatchResponse(t, resp, match.HTTPResponse{
226-
JSON: []match.JSON{
227-
match.JSONKeyEqual("name", afterRoomName),
228-
},
229-
})
196+
content = alice.MustGetStateEventContent(t, roomID, "m.room.name", "")
197+
must.MatchGJSON(t, content, match.JSONKeyEqual("name", afterRoomName))
230198
})
231199

232200
// sytest: Getting messages going forward is limited for a departed room (SPEC-216)

tests/csapi/rooms_invite_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,7 @@ func verifyState(t *testing.T, res gjson.Result, roomID string, cl *client.CSAPI
175175
eventContent := event.Get("content." + field).Str
176176
eventStateKey := event.Get("state_key").Str
177177

178-
res := cl.MustDo(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", eventType, eventStateKey})
179-
180-
must.MatchResponse(t, res, match.HTTPResponse{
181-
JSON: []match.JSON{
182-
match.JSONKeyEqual(field, eventContent),
183-
},
184-
})
178+
content := cl.MustGetStateEventContent(t, roomID, eventType, eventStateKey)
179+
must.MatchGJSON(t, content, match.JSONKeyEqual(field, eventContent))
185180
}
186181
}

tests/federation_acl_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,11 @@ func TestACLs(t *testing.T) {
9999
must.ContainSubset(t, events, []string{sentinelEventID})
100100

101101
// Validate the ACL event is actually in the rooms state
102-
res := user.Do(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.server_acl"})
103-
must.MatchResponse(t, res, match.HTTPResponse{
104-
StatusCode: 200,
105-
JSON: []match.JSON{
106-
match.JSONKeyEqual("allow", []string{"*"}),
107-
match.JSONKeyEqual("deny", []string{"hs2"}),
108-
match.JSONKeyEqual("allow_ip_literals", true),
109-
},
110-
})
102+
content := user.MustGetStateEventContent(t, roomID, "m.room.server_acl", "")
103+
must.MatchGJSON(t, content,
104+
match.JSONKeyEqual("allow", []string{"*"}),
105+
match.JSONKeyEqual("deny", []string{"hs2"}),
106+
match.JSONKeyEqual("allow_ip_literals", true),
107+
)
111108
}
112109
}

tests/federation_room_join_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222

2323
"github.com/matrix-org/complement/b"
2424
"github.com/matrix-org/complement/client"
25-
"github.com/matrix-org/complement/helpers"
2625
"github.com/matrix-org/complement/federation"
26+
"github.com/matrix-org/complement/helpers"
2727
"github.com/matrix-org/complement/match"
2828
"github.com/matrix-org/complement/must"
2929
"github.com/matrix-org/complement/runtime"
@@ -352,15 +352,10 @@ func TestBannedUserCannotSendJoin(t *testing.T) {
352352
}
353353

354354
// Alice checks the room state to check that charlie isn't a member
355-
res := alice.MustDo(
356-
t,
357-
"GET",
358-
[]string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.member", charlie},
355+
content := alice.MustGetStateEventContent(t, roomID, "m.room.member", charlie)
356+
must.MatchGJSON(t, content,
357+
match.JSONKeyEqual("membership", "ban"),
359358
)
360-
stateResp := must.ParseJSON(t, res.Body)
361-
res.Body.Close()
362-
membership := must.GetJSONFieldStr(t, stateResp, "membership")
363-
must.Equal(t, membership, "ban", "membership of charlie")
364359
}
365360

366361
// This test checks that we cannot submit anything via /v1/send_join except a join.

0 commit comments

Comments
 (0)