Skip to content

Commit f56ed8e

Browse files
Merge pull request #222 from matrix-org/neilalexander/restrictedjoin
Use sub-tests for restricted join tests
2 parents 581cb0b + 4f11742 commit f56ed8e

File tree

2 files changed

+100
-78
lines changed

2 files changed

+100
-78
lines changed

internal/must/must.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ func MatchRequest(t *testing.T, req *http.Request, m match.HTTPRequest) []byte {
6666
return body
6767
}
6868

69+
// MatchSuccess consumes the HTTP response and fails if the response is non-2xx.
70+
func MatchSuccess(t *testing.T, res *http.Response) {
71+
if res.StatusCode < 200 || res.StatusCode > 299 {
72+
t.Fatalf("MatchSuccess got status %d instead of a success code", res.StatusCode)
73+
}
74+
}
75+
76+
// MatchFailure consumes the HTTP response and fails if the response is 2xx.
77+
func MatchFailure(t *testing.T, res *http.Response) {
78+
if res.StatusCode >= 200 && res.StatusCode <= 299 {
79+
t.Fatalf("MatchFailure got status %d instead of a failure code", res.StatusCode)
80+
}
81+
}
82+
6983
// MatchResponse consumes the HTTP response and performs HTTP-level assertions on it. Returns the raw response body.
7084
func MatchResponse(t *testing.T, res *http.Response, m match.HTTPResponse) []byte {
7185
t.Helper()

tests/restricted_rooms_test.go

Lines changed: 86 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/matrix-org/complement/internal/must"
1616
)
1717

18-
func failJoinRoom(t *testing.T, c *client.CSAPI, roomIDOrAlias string, serverName string, expectedErrorCode int) {
18+
func failJoinRoom(t *testing.T, c *client.CSAPI, roomIDOrAlias string, serverName string) {
1919
t.Helper()
2020

2121
// This is copied from Client.JoinRoom to test a join failure.
@@ -27,9 +27,7 @@ func failJoinRoom(t *testing.T, c *client.CSAPI, roomIDOrAlias string, serverNam
2727
[]string{"_matrix", "client", "r0", "join", roomIDOrAlias},
2828
client.WithQueries(query),
2929
)
30-
must.MatchResponse(t, res, match.HTTPResponse{
31-
StatusCode: expectedErrorCode,
32-
})
30+
must.MatchFailure(t, res)
3331
}
3432

3533
// Creates two rooms on room version 8 and sets the second room to have
@@ -73,88 +71,98 @@ func setupRestrictedRoom(t *testing.T, deployment *docker.Deployment) (*client.C
7371
func checkRestrictedRoom(t *testing.T, alice *client.CSAPI, bob *client.CSAPI, allowed_room string, room string) {
7472
t.Helper()
7573

76-
failJoinRoom(t, bob, room, "hs1", 403)
77-
78-
// Join the allowed room, attempt to join the room again, which now should succeed.
79-
bob.JoinRoom(t, allowed_room, []string{"hs1"})
80-
bob.JoinRoom(t, room, []string{"hs1"})
74+
t.Run("Join should fail initially", func(t *testing.T) {
75+
failJoinRoom(t, bob, room, "hs1")
76+
})
8177

82-
// Joining the same room again should work fine (e.g. to change your display name).
83-
bob.SendEventSynced(
84-
t,
85-
room,
86-
b.Event{
87-
Type: "m.room.member",
88-
Sender: bob.UserID,
89-
StateKey: &bob.UserID,
90-
Content: map[string]interface{}{
91-
"membership": "join",
92-
"displayname": "Bobby",
93-
// This should be ignored since this is a join -> join transition.
94-
"join_authorised_via_users_server": "unused",
78+
t.Run("Join should succeed when joined to allowed room", func(t *testing.T) {
79+
// Join the allowed room, attempt to join the room again, which now should succeed.
80+
bob.JoinRoom(t, allowed_room, []string{"hs1"})
81+
bob.JoinRoom(t, room, []string{"hs1"})
82+
83+
// Joining the same room again should work fine (e.g. to change your display name).
84+
bob.SendEventSynced(
85+
t,
86+
room,
87+
b.Event{
88+
Type: "m.room.member",
89+
Sender: bob.UserID,
90+
StateKey: &bob.UserID,
91+
Content: map[string]interface{}{
92+
"membership": "join",
93+
"displayname": "Bobby",
94+
// This should be ignored since this is a join -> join transition.
95+
"join_authorised_via_users_server": "unused",
96+
},
9597
},
96-
},
97-
)
98+
)
99+
})
98100

99-
// Leaving the room works and the user is unable to re-join.
100-
bob.LeaveRoom(t, room)
101-
bob.LeaveRoom(t, allowed_room)
101+
t.Run("Join should fail when left allowed room", func(t *testing.T) {
102+
// Leaving the room works and the user is unable to re-join.
103+
bob.LeaveRoom(t, room)
104+
bob.LeaveRoom(t, allowed_room)
102105

103-
// Wait until Alice sees Bob leave the allowed room. This ensures that Alice's HS
104-
// has processed the leave before Bob tries rejoining, so that it rejects his
105-
// attempt to join the room.
106-
alice.SyncUntilTimelineHas(t, allowed_room, func(ev gjson.Result) bool {
107-
if ev.Get("type").Str != "m.room.member" || ev.Get("sender").Str != bob.UserID {
108-
return false
109-
}
106+
// Wait until Alice sees Bob leave the allowed room. This ensures that Alice's HS
107+
// has processed the leave before Bob tries rejoining, so that it rejects his
108+
// attempt to join the room.
109+
alice.SyncUntilTimelineHas(t, allowed_room, func(ev gjson.Result) bool {
110+
if ev.Get("type").Str != "m.room.member" || ev.Get("sender").Str != bob.UserID {
111+
return false
112+
}
113+
114+
return ev.Get("content").Get("membership").Str == "leave"
115+
})
110116

111-
return ev.Get("content").Get("membership").Str == "leave"
117+
failJoinRoom(t, bob, room, "hs1")
112118
})
113119

114-
failJoinRoom(t, bob, room, "hs1", 403)
120+
t.Run("Join should succeed when invited", func(t *testing.T) {
121+
// Invite the user and joining should work.
122+
alice.InviteRoom(t, room, bob.UserID)
123+
bob.JoinRoom(t, room, []string{"hs1"})
115124

116-
// Invite the user and joining should work.
117-
alice.InviteRoom(t, room, bob.UserID)
118-
bob.JoinRoom(t, room, []string{"hs1"})
119-
120-
// Leave the room again, and join the allowed room.
121-
bob.LeaveRoom(t, room)
122-
bob.JoinRoom(t, allowed_room, []string{"hs1"})
125+
// Leave the room again, and join the allowed room.
126+
bob.LeaveRoom(t, room)
127+
bob.JoinRoom(t, allowed_room, []string{"hs1"})
128+
})
123129

124-
// Update the room to have bad values in the "allow" field, which should stop
125-
// joining from working properly.
126-
emptyStateKey := ""
127-
alice.SendEventSynced(
128-
t,
129-
room,
130-
b.Event{
131-
Type: "m.room.join_rules",
132-
Sender: alice.UserID,
133-
StateKey: &emptyStateKey,
134-
Content: map[string]interface{}{
135-
"join_rule": "restricted",
136-
"allow": []string{"invalid"},
130+
t.Run("Join should fail with mangled join rules", func(t *testing.T) {
131+
// Update the room to have bad values in the "allow" field, which should stop
132+
// joining from working properly.
133+
emptyStateKey := ""
134+
alice.SendEventSynced(
135+
t,
136+
room,
137+
b.Event{
138+
Type: "m.room.join_rules",
139+
Sender: alice.UserID,
140+
StateKey: &emptyStateKey,
141+
Content: map[string]interface{}{
142+
"join_rule": "restricted",
143+
"allow": []string{"invalid"},
144+
},
137145
},
138-
},
139-
)
140-
// Fails since invalid values get filtered out of allow.
141-
failJoinRoom(t, bob, room, "hs1", 403)
142-
143-
alice.SendEventSynced(
144-
t,
145-
room,
146-
b.Event{
147-
Type: "m.room.join_rules",
148-
Sender: alice.UserID,
149-
StateKey: &emptyStateKey,
150-
Content: map[string]interface{}{
151-
"join_rule": "restricted",
152-
"allow": "invalid",
146+
)
147+
// Fails since invalid values get filtered out of allow.
148+
failJoinRoom(t, bob, room, "hs1")
149+
150+
alice.SendEventSynced(
151+
t,
152+
room,
153+
b.Event{
154+
Type: "m.room.join_rules",
155+
Sender: alice.UserID,
156+
StateKey: &emptyStateKey,
157+
Content: map[string]interface{}{
158+
"join_rule": "restricted",
159+
"allow": "invalid",
160+
},
153161
},
154-
},
155-
)
156-
// Fails since a fully invalid allow key requires an invite.
157-
failJoinRoom(t, bob, room, "hs1", 403)
162+
)
163+
// Fails since a fully invalid allow key requires an invite.
164+
failJoinRoom(t, bob, room, "hs1")
165+
})
158166
}
159167

160168
// Test joining a room with join rules restricted to membership in another room.
@@ -246,7 +254,7 @@ func TestRestrictedRoomsRemoteJoinLocalUser(t *testing.T) {
246254
})
247255

248256
// Bob cannot join the room.
249-
failJoinRoom(t, bob, room, "hs1", 403)
257+
failJoinRoom(t, bob, room, "hs1")
250258

251259
// Join the allowed room via hs2.
252260
bob.JoinRoom(t, allowed_room, []string{"hs2"})
@@ -375,7 +383,7 @@ func TestRestrictedRoomsRemoteJoinFailOver(t *testing.T) {
375383
charlie.JoinRoom(t, allowed_room, []string{"hs1"})
376384

377385
// hs2 doesn't have anyone to invite from, so the join fails.
378-
failJoinRoom(t, charlie, room, "hs2", 502)
386+
failJoinRoom(t, charlie, room, "hs2")
379387

380388
// Including hs1 (and failing over to it) allows the join to succeed.
381389
charlie.JoinRoom(t, room, []string{"hs2", "hs1"})
@@ -429,7 +437,7 @@ func TestRestrictedRoomsRemoteJoinFailOver(t *testing.T) {
429437

430438
// hs2 cannot complete the join since they do not know if Charlie meets the
431439
// requirements (since it is no longer in the allowed room).
432-
failJoinRoom(t, charlie, room, "hs2", 502)
440+
failJoinRoom(t, charlie, room, "hs2")
433441

434442
// Including hs1 (and failing over to it) allows the join to succeed.
435443
charlie.JoinRoom(t, room, []string{"hs2", "hs1"})

0 commit comments

Comments
 (0)