|
| 1 | +package csapi_tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/tidwall/gjson" |
| 10 | + |
| 11 | + "github.com/matrix-org/complement/internal/b" |
| 12 | + "github.com/matrix-org/complement/internal/client" |
| 13 | + "github.com/matrix-org/complement/internal/match" |
| 14 | + "github.com/matrix-org/complement/internal/must" |
| 15 | +) |
| 16 | + |
| 17 | +func TestKeyChangesLocal(t *testing.T) { |
| 18 | + deployment := Deploy(t, b.BlueprintAlice) |
| 19 | + defer deployment.Destroy(t) |
| 20 | + |
| 21 | + alice := deployment.Client(t, "hs1", "@alice:hs1") |
| 22 | + password := "$uperSecretPassword" |
| 23 | + bob := deployment.RegisterUser(t, "hs1", "bob", password, false) |
| 24 | + unauthedClient := deployment.Client(t, "hs1", "") |
| 25 | + |
| 26 | + t.Run("New login should create a device_lists.changed entry", func(t *testing.T) { |
| 27 | + mustUploadKeys(t, bob) |
| 28 | + |
| 29 | + roomID := alice.CreateRoom(t, map[string]interface{}{"preset": "public_chat"}) |
| 30 | + bob.JoinRoom(t, roomID, []string{}) |
| 31 | + nextBatch1 := alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID)) |
| 32 | + |
| 33 | + reqBody := client.WithJSONBody(t, map[string]interface{}{ |
| 34 | + "identifier": map[string]interface{}{ |
| 35 | + "type": "m.id.user", |
| 36 | + "user": bob.UserID, |
| 37 | + }, |
| 38 | + "type": "m.login.password", |
| 39 | + "password": password, |
| 40 | + }) |
| 41 | + // Create a new device by logging in |
| 42 | + res := unauthedClient.MustDoFunc(t, "POST", []string{"_matrix", "client", "r0", "login"}, reqBody) |
| 43 | + loginResp := must.ParseJSON(t, res.Body) |
| 44 | + unauthedClient.AccessToken = must.GetJSONFieldStr(t, loginResp, "access_token") |
| 45 | + unauthedClient.DeviceID = must.GetJSONFieldStr(t, loginResp, "device_id") |
| 46 | + unauthedClient.UserID = must.GetJSONFieldStr(t, loginResp, "user_id") |
| 47 | + mustUploadKeys(t, unauthedClient) |
| 48 | + |
| 49 | + // Alice should now see a device list changed entry for Bob |
| 50 | + nextBatch := alice.MustSyncUntil(t, client.SyncReq{Since: nextBatch1}, func(userID string, syncResp gjson.Result) error { |
| 51 | + deviceListsChanged := syncResp.Get("device_lists.changed") |
| 52 | + if !deviceListsChanged.IsArray() { |
| 53 | + return fmt.Errorf("no device_lists.changed entry found: %+v", syncResp.Raw) |
| 54 | + } |
| 55 | + for _, userID := range deviceListsChanged.Array() { |
| 56 | + if userID.String() == bob.UserID { |
| 57 | + return nil |
| 58 | + } |
| 59 | + } |
| 60 | + return fmt.Errorf("no device_lists.changed entry found for %s", bob.UserID) |
| 61 | + }) |
| 62 | + // Verify on /keys/changes that Bob has changes |
| 63 | + queryParams := url.Values{} |
| 64 | + queryParams.Set("from", nextBatch1) |
| 65 | + queryParams.Set("to", nextBatch) |
| 66 | + resp := alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "v3", "keys", "changes"}, client.WithQueries(queryParams)) |
| 67 | + must.MatchResponse(t, resp, match.HTTPResponse{ |
| 68 | + StatusCode: http.StatusOK, |
| 69 | + JSON: []match.JSON{ |
| 70 | + match.JSONKeyEqual("changed.0", bob.UserID), // there should only be one change, so access it directly |
| 71 | + }, |
| 72 | + }) |
| 73 | + |
| 74 | + // Get Bobs keys, there should be two |
| 75 | + queryKeys := client.WithJSONBody(t, map[string]interface{}{ |
| 76 | + "device_keys": map[string][]string{ |
| 77 | + bob.UserID: {}, |
| 78 | + }, |
| 79 | + }) |
| 80 | + resp = alice.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "keys", "query"}, queryKeys) |
| 81 | + keyCount := 0 |
| 82 | + must.MatchResponse(t, resp, match.HTTPResponse{ |
| 83 | + StatusCode: http.StatusOK, |
| 84 | + JSON: []match.JSON{ |
| 85 | + match.JSONMapEach("device_keys."+bob.UserID, func(k, v gjson.Result) error { |
| 86 | + keyCount++ |
| 87 | + return nil |
| 88 | + }), |
| 89 | + }, |
| 90 | + }) |
| 91 | + wantKeyCount := 2 |
| 92 | + if keyCount != wantKeyCount { |
| 93 | + t.Fatalf("unexpected key count: got %d, want %d", keyCount, wantKeyCount) |
| 94 | + } |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +func mustUploadKeys(t *testing.T, user *client.CSAPI) { |
| 99 | + t.Helper() |
| 100 | + deviceKeys, oneTimeKeys := generateKeys(t, user, 5) |
| 101 | + reqBody := client.WithJSONBody(t, map[string]interface{}{ |
| 102 | + "device_keys": deviceKeys, |
| 103 | + "one_time_keys": oneTimeKeys, |
| 104 | + }) |
| 105 | + user.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"}, reqBody) |
| 106 | +} |
0 commit comments