From f06fb7ba8d30289615ec179926fca9038f025993 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 2 Apr 2021 08:10:19 -0400 Subject: [PATCH 1/6] Test MSC3083 - joining a restricted room. --- dockerfiles/synapse/homeserver.yaml | 2 + tests/msc3083_test.go | 78 +++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tests/msc3083_test.go diff --git a/dockerfiles/synapse/homeserver.yaml b/dockerfiles/synapse/homeserver.yaml index af4d5e59..b2415afc 100644 --- a/dockerfiles/synapse/homeserver.yaml +++ b/dockerfiles/synapse/homeserver.yaml @@ -105,3 +105,5 @@ AS_REGISTRATION_FILES experimental_features: # Enable knocking support msc2403_enabled: true + # Enable spaces support + spaces_enabled: true diff --git a/tests/msc3083_test.go b/tests/msc3083_test.go new file mode 100644 index 00000000..3d531384 --- /dev/null +++ b/tests/msc3083_test.go @@ -0,0 +1,78 @@ +// +build msc3083 + +// Tests MSC3083, an experimental feature for joining restricted rooms based on +// membership in a space. + +package tests + +import ( + "net/url" + "testing" + + "github.com/matrix-org/complement/internal/b" +) + +var ( + spaceChildEventType = "org.matrix.msc1772.space.child" + spaceParentEventType = "org.matrix.msc1772.space.parent" +) + +// Test joining a room with join rules restricted to membership in a space. +func TestRestrictedRoomsLocalJoin(t *testing.T) { + deployment := Deploy(t, "msc3083", b.BlueprintOneToOneRoom) + defer deployment.Destroy(t) + + // Create the space and put a room in it. + alice := deployment.Client(t, "hs1", "@alice:hs1") + space := alice.CreateRoom(t, map[string]interface{}{ + "preset": "public_chat", + "name": "Space", + }) + // The room is an unstable room version which supports the restricted join_rule. + room := alice.CreateRoom(t, map[string]interface{}{ + "preset": "public_chat", + "name": "Room", + "room_version": "org.matrix.msc3083", + "initial_state": []map[string]interface{}{ + { + "type": "m.room.join_rules", + "state_key": "", + "content": map[string]interface{}{ + "join_rule": "restricted", + "allow": []map[string]interface{}{ + { + "space": &space, + "via": []string{"hs1"}, + }, + }, + }, + }, + }, + }) + alice.SendEventSynced(t, space, b.Event{ + Type: spaceChildEventType, + StateKey: &room, + Content: map[string]interface{}{ + "via": []string{"hs1"}, + }, + }) + + // Create a second user and attempt to join the room, it should fail. + bob := deployment.Client(t, "hs1", "@bob:hs1") + // This is copied from Client.JoinRoom to test a join failure. + query := make(url.Values, 1) + query.Set("server_name", "hs1") + bob.MustDoWithStatusRaw( + t, + "POST", + []string{"_matrix", "client", "r0", "join", room}, + nil, + "application/json", + query, + 403, + ) + + // Join the space, attempt to join the room again, which now should succeed. + bob.JoinRoom(t, space, []string{"hs1"}) + bob.JoinRoom(t, room, []string{"hs1"}) +} From 158fd187cc73155d2fee5ce0764e3a18fe46ff92 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 2 Apr 2021 09:38:33 -0400 Subject: [PATCH 2/6] Expand tests to handle invites. --- internal/client/client.go | 17 +++++++++++++++++ tests/msc3083_test.go | 38 ++++++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index 28ef89d5..ca86be68 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -84,6 +84,23 @@ func (c *CSAPI) JoinRoom(t *testing.T, roomIDOrAlias string, serverNames []strin return GetJSONFieldStr(t, body, "room_id") } +// LeaveRoom joins the room ID, else fails the test. +func (c *CSAPI) LeaveRoom(t *testing.T, roomID string) { + t.Helper() + // leave the room + c.MustDoRaw(t, "POST", []string{"_matrix", "client", "r0", "rooms", roomID, "leave"}, nil, "application/json", nil) +} + +// InviteRoom joins the room ID, else fails the test. +func (c *CSAPI) InviteRoom(t *testing.T, roomID string, userID string) { + t.Helper() + // Invite the user to the room + body := map[string]interface{}{ + "user_id": userID, + } + c.MustDo(t, "POST", []string{"_matrix", "client", "r0", "rooms", roomID, "invite"}, body) +} + // SendEventSynced sends `e` into the room and waits for its event ID to come down /sync. // Returns the event ID of the sent event. func (c *CSAPI) SendEventSynced(t *testing.T, roomID string, e b.Event) string { diff --git a/tests/msc3083_test.go b/tests/msc3083_test.go index 3d531384..475221c1 100644 --- a/tests/msc3083_test.go +++ b/tests/msc3083_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/matrix-org/complement/internal/b" + "github.com/matrix-org/complement/internal/client" ) var ( @@ -17,6 +18,21 @@ var ( spaceParentEventType = "org.matrix.msc1772.space.parent" ) +func FailJoinRoom(c *client.CSAPI, t *testing.T, roomIDOrAlias string, serverName string) { + // This is copied from Client.JoinRoom to test a join failure. + query := make(url.Values, 1) + query.Set("server_name", serverName) + c.MustDoWithStatusRaw( + t, + "POST", + []string{"_matrix", "client", "r0", "join", roomIDOrAlias}, + nil, + "application/json", + query, + 403, + ) +} + // Test joining a room with join rules restricted to membership in a space. func TestRestrictedRoomsLocalJoin(t *testing.T) { deployment := Deploy(t, "msc3083", b.BlueprintOneToOneRoom) @@ -59,20 +75,18 @@ func TestRestrictedRoomsLocalJoin(t *testing.T) { // Create a second user and attempt to join the room, it should fail. bob := deployment.Client(t, "hs1", "@bob:hs1") - // This is copied from Client.JoinRoom to test a join failure. - query := make(url.Values, 1) - query.Set("server_name", "hs1") - bob.MustDoWithStatusRaw( - t, - "POST", - []string{"_matrix", "client", "r0", "join", room}, - nil, - "application/json", - query, - 403, - ) + FailJoinRoom(bob, t, room, "hs1") // Join the space, attempt to join the room again, which now should succeed. bob.JoinRoom(t, space, []string{"hs1"}) bob.JoinRoom(t, room, []string{"hs1"}) + + // Leaving the room works and the user is unable to re-join. + bob.LeaveRoom(t, room) + bob.LeaveRoom(t, space) + FailJoinRoom(bob, t, room, "hs1") + + // Invite the user and joining shuold work. + alice.InviteRoom(t, room, "@bob:hs1") + bob.JoinRoom(t, room, []string{"hs1"}) } From d6a6652fed7fbb1782b33e870f1fe328b98c0954 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 2 Apr 2021 11:14:02 -0400 Subject: [PATCH 3/6] Lint --- internal/client/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/client/client.go b/internal/client/client.go index ca86be68..69369421 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -96,7 +96,7 @@ func (c *CSAPI) InviteRoom(t *testing.T, roomID string, userID string) { t.Helper() // Invite the user to the room body := map[string]interface{}{ - "user_id": userID, + "user_id": userID, } c.MustDo(t, "POST", []string{"_matrix", "client", "r0", "rooms", roomID, "invite"}, body) } From 51cc9b4bb217ac746ec8f809e25993356c6a91ea Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 2 Apr 2021 11:50:32 -0400 Subject: [PATCH 4/6] Add edge cases of bad data. --- tests/msc3083_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/msc3083_test.go b/tests/msc3083_test.go index 475221c1..7686106e 100644 --- a/tests/msc3083_test.go +++ b/tests/msc3083_test.go @@ -89,4 +89,44 @@ func TestRestrictedRoomsLocalJoin(t *testing.T) { // Invite the user and joining shuold work. alice.InviteRoom(t, room, "@bob:hs1") bob.JoinRoom(t, room, []string{"hs1"}) + + // Leave the room again, and join the space. + bob.LeaveRoom(t, room) + bob.JoinRoom(t, space, []string{"hs1"}) + + // Update the room to have bad values in the "allow" field, which should stop + // joining from working properly. + emptyStateKey := "" + alice.SendEventSynced( + t, + room, + b.Event{ + Type: "m.room.join_rules", + Sender: alice.UserID, + StateKey: &emptyStateKey, + Content: map[string]interface{}{ + "join_rule": "restricted", + "allow": []string{"invalid"}, + }, + }, + ) + // Fails since invalid values get filtered out of allow. + FailJoinRoom(bob, t, room, "hs1") + + alice.SendEventSynced( + t, + room, + b.Event{ + Type: "m.room.join_rules", + Sender: alice.UserID, + StateKey: &emptyStateKey, + Content: map[string]interface{}{ + "join_rule": "restricted", + "allow": "invalid", + }, + }, + ) + // Succeeds since a fully invalid allow key is ignored (and the room is + // treated as public). + bob.JoinRoom(t, room, []string{"hs1"}) } From e12cf88a1821573cfa3e84a467e278a777802a01 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 7 Apr 2021 09:06:43 -0400 Subject: [PATCH 5/6] Clarify comments. Co-authored-by: Kegsay --- internal/client/client.go | 2 +- tests/msc3083_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index 69369421..d7e42709 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -91,7 +91,7 @@ func (c *CSAPI) LeaveRoom(t *testing.T, roomID string) { c.MustDoRaw(t, "POST", []string{"_matrix", "client", "r0", "rooms", roomID, "leave"}, nil, "application/json", nil) } -// InviteRoom joins the room ID, else fails the test. +// InviteRoom invites userID to the room ID, else fails the test. func (c *CSAPI) InviteRoom(t *testing.T, roomID string, userID string) { t.Helper() // Invite the user to the room diff --git a/tests/msc3083_test.go b/tests/msc3083_test.go index 7686106e..2d2d4999 100644 --- a/tests/msc3083_test.go +++ b/tests/msc3083_test.go @@ -86,7 +86,7 @@ func TestRestrictedRoomsLocalJoin(t *testing.T) { bob.LeaveRoom(t, space) FailJoinRoom(bob, t, room, "hs1") - // Invite the user and joining shuold work. + // Invite the user and joining should work. alice.InviteRoom(t, room, "@bob:hs1") bob.JoinRoom(t, room, []string{"hs1"}) From 3eaf13992351db482065d91f0a7e7723ecb1c86a Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 7 Apr 2021 12:06:46 -0400 Subject: [PATCH 6/6] Updates to match the behavior of bad data. --- tests/msc3083_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/msc3083_test.go b/tests/msc3083_test.go index 2d2d4999..86b51327 100644 --- a/tests/msc3083_test.go +++ b/tests/msc3083_test.go @@ -126,7 +126,6 @@ func TestRestrictedRoomsLocalJoin(t *testing.T) { }, }, ) - // Succeeds since a fully invalid allow key is ignored (and the room is - // treated as public). - bob.JoinRoom(t, room, []string{"hs1"}) + // Fails since a fully invalid allow key rquires an invite. + FailJoinRoom(bob, t, room, "hs1") }