Skip to content

Commit 6dbf2bd

Browse files
authoredJan 27, 2025··
Update knock test utilities to be more generic/useful (#758)
They now match the style of the other utilities under `client`: `mustKnockOnRoomSynced`/`syncKnockedOn` This is spawning from trying to write some knock tests that also stressed element-hq/synapse#18075 but they didn't pan out. I thought this refactor and update was still useful to upstream in any case.
1 parent e21a6fb commit 6dbf2bd

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed
 

‎tests/knocking_test.go

+30-16
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ func knockingBetweenTwoUsersTest(t *testing.T, roomID string, inRoomUser, knocki
117117
})
118118

119119
t.Run("Knocking on a room with join rule 'knock' should succeed", func(t *testing.T) {
120-
knockOnRoomSynced(t, knockingUser, roomID, testKnockReason, []string{"hs1"})
120+
mustKnockOnRoomSynced(t, knockingUser, roomID, testKnockReason, []string{"hs1"})
121121
})
122122

123123
t.Run("A user that has already knocked is allowed to knock again on the same room", func(t *testing.T) {
124-
knockOnRoomSynced(t, knockingUser, roomID, "I really like knock knock jokes", []string{"hs1"})
124+
mustKnockOnRoomSynced(t, knockingUser, roomID, "I really like knock knock jokes", []string{"hs1"})
125125
})
126126

127127
t.Run("Users in the room see a user's membership update when they knock", func(t *testing.T) {
@@ -183,7 +183,7 @@ func knockingBetweenTwoUsersTest(t *testing.T, roomID string, inRoomUser, knocki
183183
})
184184

185185
// Knock again to return us to the knocked state
186-
knockOnRoomSynced(t, knockingUser, roomID, "Let me in... again?", []string{"hs1"})
186+
mustKnockOnRoomSynced(t, knockingUser, roomID, "Let me in... again?", []string{"hs1"})
187187
})
188188
}
189189

@@ -211,7 +211,7 @@ func knockingBetweenTwoUsersTest(t *testing.T, roomID string, inRoomUser, knocki
211211
}))
212212

213213
// Knock again
214-
knockOnRoomSynced(t, knockingUser, roomID, "Pleeease let me in?", []string{"hs1"})
214+
mustKnockOnRoomSynced(t, knockingUser, roomID, "Pleeease let me in?", []string{"hs1"})
215215
})
216216

217217
t.Run("A user can knock on a room without a reason", func(t *testing.T) {
@@ -227,7 +227,7 @@ func knockingBetweenTwoUsersTest(t *testing.T, roomID string, inRoomUser, knocki
227227
)
228228

229229
// Knock again, this time without a reason
230-
knockOnRoomSynced(t, knockingUser, roomID, "", []string{"hs1"})
230+
mustKnockOnRoomSynced(t, knockingUser, roomID, "", []string{"hs1"})
231231
})
232232

233233
t.Run("A user in the room can accept a knock", func(t *testing.T) {
@@ -279,22 +279,36 @@ func knockingBetweenTwoUsersTest(t *testing.T, roomID string, inRoomUser, knocki
279279
})
280280
}
281281

282-
// knockOnRoomSynced will knock on a given room on the behalf of a user, and block until the knock has persisted.
282+
func syncKnockedOn(userID, roomID string) client.SyncCheckOpt {
283+
return func(clientUserID string, topLevelSyncJSON gjson.Result) error {
284+
// two forms which depend on what the client user is:
285+
// - passively viewing a membership for a room you're joined in
286+
// - actively leaving the room
287+
if clientUserID == userID {
288+
events := topLevelSyncJSON.Get("rooms.knock." + client.GjsonEscape(roomID) + ".knock_state.events")
289+
if events.Exists() && events.IsArray() {
290+
// We don't currently define any required state event types to be sent.
291+
// If we've reached this point, then an entry for this room was found
292+
return nil
293+
}
294+
return fmt.Errorf("no knock section for room %s", roomID)
295+
}
296+
297+
// passive
298+
return client.SyncTimelineHas(roomID, func(ev gjson.Result) bool {
299+
return ev.Get("type").Str == "m.room.member" && ev.Get("state_key").Str == userID && ev.Get("content.membership").Str == "knock"
300+
})(clientUserID, topLevelSyncJSON)
301+
}
302+
}
303+
304+
// mustKnockOnRoomSynced will knock on a given room on the behalf of a user, and block until the knock has persisted.
283305
// serverNames should be populated if knocking on a room that the user's homeserver isn't currently a part of.
284306
// Fails the test if the knock response does not return a 200 status code.
285-
func knockOnRoomSynced(t *testing.T, c *client.CSAPI, roomID, reason string, serverNames []string) {
307+
func mustKnockOnRoomSynced(t *testing.T, c *client.CSAPI, roomID, reason string, serverNames []string) {
286308
knockOnRoomWithStatus(t, c, roomID, reason, serverNames, 200)
287309

288310
// The knock should have succeeded. Block until we see the knock appear down sync
289-
c.MustSyncUntil(t, client.SyncReq{}, func(clientUserID string, topLevelSyncJSON gjson.Result) error {
290-
events := topLevelSyncJSON.Get("rooms.knock." + client.GjsonEscape(roomID) + ".knock_state.events")
291-
if events.Exists() && events.IsArray() {
292-
// We don't currently define any required state event types to be sent.
293-
// If we've reached this point, then an entry for this room was found
294-
return nil
295-
}
296-
return fmt.Errorf("no knock section for room %s", roomID)
297-
})
311+
c.MustSyncUntil(t, client.SyncReq{}, syncKnockedOn(c.UserID, roomID))
298312
}
299313

300314
// knockOnRoomWithStatus will knock on a given room on the behalf of a user.

0 commit comments

Comments
 (0)
Please sign in to comment.