|
| 1 | +package csapi_tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/matrix-org/complement/internal/b" |
| 8 | + "github.com/matrix-org/complement/internal/client" |
| 9 | + |
| 10 | + "github.com/tidwall/gjson" |
| 11 | +) |
| 12 | + |
| 13 | +// TestLocalUsersReceiveDeviceListUpdates tests that users on the same |
| 14 | +// homeserver receive device list updates from other local users, as |
| 15 | +// long as they share a room. |
| 16 | +func TestLocalUsersReceiveDeviceListUpdates(t *testing.T) { |
| 17 | + // Create a homeserver with two users that share a room |
| 18 | + deployment := Deploy(t, b.BlueprintOneToOneRoom) |
| 19 | + defer deployment.Destroy(t) |
| 20 | + |
| 21 | + // Get a reference to the already logged-in CS API clients for each user |
| 22 | + alice := deployment.Client(t, "hs1", "@alice:hs1") |
| 23 | + bob := deployment.Client(t, "hs1", "@bob:hs1") |
| 24 | + |
| 25 | + // Deduce alice's device ID |
| 26 | + resp := alice.MustDoFunc( |
| 27 | + t, |
| 28 | + "GET", |
| 29 | + []string{"_matrix", "client", "v3", "account", "whoami"}, |
| 30 | + ) |
| 31 | + responseBodyBytes := client.ParseJSON(t, resp) |
| 32 | + aliceDeviceID := gjson.GetBytes(responseBodyBytes, "device_id").Str |
| 33 | + |
| 34 | + // Bob performs an initial sync |
| 35 | + _, bobNextBatch := bob.MustSync(t, client.SyncReq{}) |
| 36 | + |
| 37 | + // Alice then updates their device list by renaming their current device |
| 38 | + alice.MustDoFunc( |
| 39 | + t, |
| 40 | + "PUT", |
| 41 | + []string{"_matrix", "client", "v3", "devices", aliceDeviceID}, |
| 42 | + client.WithJSONBody( |
| 43 | + t, |
| 44 | + map[string]interface{}{ |
| 45 | + "display_name": "A New Device Name", |
| 46 | + }, |
| 47 | + ), |
| 48 | + ) |
| 49 | + |
| 50 | + // Check that Bob received a device list update from Alice |
| 51 | + bob.MustSyncUntil( |
| 52 | + t, |
| 53 | + client.SyncReq{ |
| 54 | + Since: bobNextBatch, |
| 55 | + }, func(clientUserID string, topLevelSyncJSON gjson.Result) error { |
| 56 | + // Ensure that Bob sees that Alice has updated their device list |
| 57 | + usersWithChangedDeviceListsArray := topLevelSyncJSON.Get("device_lists.changed").Array() |
| 58 | + for _, userID := range usersWithChangedDeviceListsArray { |
| 59 | + if userID.Str == alice.UserID { |
| 60 | + return nil |
| 61 | + } |
| 62 | + } |
| 63 | + return fmt.Errorf("missing device list update for Alice") |
| 64 | + }, |
| 65 | + ) |
| 66 | +} |
0 commit comments