From 1a8c6c1728d8e91ba75240f4ef91f7cd07756bbd Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 10 Feb 2022 18:21:54 +0000 Subject: [PATCH 1/2] Add a test for device list updates of local users coming down /sync --- tests/csapi/device_lists_test.go | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/csapi/device_lists_test.go diff --git a/tests/csapi/device_lists_test.go b/tests/csapi/device_lists_test.go new file mode 100644 index 00000000..b80ac156 --- /dev/null +++ b/tests/csapi/device_lists_test.go @@ -0,0 +1,66 @@ +package csapi_tests + +import ( + "fmt" + "testing" + + "github.com/matrix-org/complement/internal/b" + "github.com/matrix-org/complement/internal/client" + + "github.com/tidwall/gjson" +) + +// TestLocalUsersReceiveDeviceListUpdates tests that users on the same +// homeserver receive device list updates from other local users, as +// long as they share a room. +func TestLocalUsersReceiveDeviceListUpdates(t *testing.T) { + // Create a homeserver with two users that share a room + deployment := Deploy(t, b.BlueprintOneToOneRoom) + defer deployment.Destroy(t) + + // Get a reference to the already logged-in CS API clients for each user + alice := deployment.Client(t, "hs1", "@alice:hs1") + bob := deployment.Client(t, "hs1", "@bob:hs1") + + // Deduce alice's device ID + resp := alice.MustDoFunc( + t, + "GET", + []string{"_matrix", "client", "v3", "devices"}, + ) + responseBodyBytes := client.ParseJSON(t, resp) + aliceDeviceID := gjson.GetBytes(responseBodyBytes, "devices.0.device_id").Str + + // Bob performs an initial sync + _, bobNextBatch := bob.MustSync(t, client.SyncReq{}) + + // Alice then updates their device list by renaming their current device + alice.MustDoFunc( + t, + "PUT", + []string{"_matrix", "client", "v3", "devices", aliceDeviceID}, + client.WithJSONBody( + t, + map[string]interface{}{ + "display_name": "A New Device Name", + }, + ), + ) + + // Check that Bob received a device list update from Alice + bob.MustSyncUntil( + t, + client.SyncReq{ + Since: bobNextBatch, + }, func(clientUserID string, topLevelSyncJSON gjson.Result) error { + // Ensure that Bob sees that Alice has updated their device list + usersWithChangedDeviceListsArray := topLevelSyncJSON.Get("device_lists.changed").Array() + for _, userID := range usersWithChangedDeviceListsArray { + if userID.Str == alice.UserID { + return nil + } + } + return fmt.Errorf("missing device list update for Alice") + }, + ) +} From 2d6ec9e236428cc008cf42913fea1c21ee32ff5c Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 15 Feb 2022 11:17:11 +0000 Subject: [PATCH 2/2] Switch to /whoami endpoint for device ID discovery --- tests/csapi/device_lists_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/csapi/device_lists_test.go b/tests/csapi/device_lists_test.go index b80ac156..91fdb546 100644 --- a/tests/csapi/device_lists_test.go +++ b/tests/csapi/device_lists_test.go @@ -26,10 +26,10 @@ func TestLocalUsersReceiveDeviceListUpdates(t *testing.T) { resp := alice.MustDoFunc( t, "GET", - []string{"_matrix", "client", "v3", "devices"}, + []string{"_matrix", "client", "v3", "account", "whoami"}, ) responseBodyBytes := client.ParseJSON(t, resp) - aliceDeviceID := gjson.GetBytes(responseBodyBytes, "devices.0.device_id").Str + aliceDeviceID := gjson.GetBytes(responseBodyBytes, "device_id").Str // Bob performs an initial sync _, bobNextBatch := bob.MustSync(t, client.SyncReq{})