Skip to content

Commit 0383afe

Browse files
committed
Use MustUploadKeys more
1 parent f124baa commit 0383afe

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

Diff for: client/client.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,18 @@ func (c *CSAPI) GetDefaultRoomVersion(t ct.TestLike) gomatrixserverlib.RoomVersi
396396
return gomatrixserverlib.RoomVersion(defaultVersion.Str)
397397
}
398398

399+
// MustUploadKeys uploads device and/or one time keys to the server, returning the current OTK counts.
400+
// Both device keys and one time keys are optional. Fails the test if the upload fails.
399401
func (c *CSAPI) MustUploadKeys(t ct.TestLike, deviceKeys map[string]interface{}, oneTimeKeys map[string]interface{}) (otkCounts map[string]int) {
400402
t.Helper()
401-
res := c.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"}, WithJSONBody(t, map[string]interface{}{
402-
"device_keys": deviceKeys,
403-
"one_time_keys": oneTimeKeys,
404-
}))
403+
reqBody := make(map[string]interface{})
404+
if deviceKeys != nil {
405+
reqBody["device_keys"] = deviceKeys
406+
}
407+
if oneTimeKeys != nil {
408+
reqBody["one_time_keys"] = oneTimeKeys
409+
}
410+
res := c.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"}, WithJSONBody(t, reqBody))
405411
bodyBytes := ParseJSON(t, res)
406412
s := struct {
407413
OTKCounts map[string]int `json:"one_time_key_counts"`

Diff for: tests/csapi/keychanges_test.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ func TestKeyChangesLocal(t *testing.T) {
2828
unauthedClient := deployment.UnauthenticatedClient(t, "hs1")
2929

3030
t.Run("New login should create a device_lists.changed entry", func(t *testing.T) {
31-
mustUploadKeys(t, bob)
31+
bobDeviceKeys, bobOTKs := bob.MustGenerateOneTimeKeys(t, 1)
32+
bob.MustUploadKeys(t, bobDeviceKeys, bobOTKs)
3233

3334
roomID := alice.MustCreateRoom(t, map[string]interface{}{"preset": "public_chat"})
3435
bob.MustJoinRoom(t, roomID, []string{})
@@ -48,7 +49,8 @@ func TestKeyChangesLocal(t *testing.T) {
4849
unauthedClient.AccessToken = must.GetJSONFieldStr(t, loginResp, "access_token")
4950
unauthedClient.DeviceID = must.GetJSONFieldStr(t, loginResp, "device_id")
5051
unauthedClient.UserID = must.GetJSONFieldStr(t, loginResp, "user_id")
51-
mustUploadKeys(t, unauthedClient)
52+
unauthedKeys, unauthedOTKs := unauthedClient.MustGenerateOneTimeKeys(t, 1)
53+
unauthedClient.MustUploadKeys(t, unauthedKeys, unauthedOTKs)
5254

5355
// Alice should now see a device list changed entry for Bob
5456
nextBatch := alice.MustSyncUntil(t, client.SyncReq{Since: nextBatch1}, func(userID string, syncResp gjson.Result) error {
@@ -98,13 +100,3 @@ func TestKeyChangesLocal(t *testing.T) {
98100
}
99101
})
100102
}
101-
102-
func mustUploadKeys(t *testing.T, user *client.CSAPI) {
103-
t.Helper()
104-
deviceKeys, oneTimeKeys := user.MustGenerateOneTimeKeys(t, 5)
105-
reqBody := client.WithJSONBody(t, map[string]interface{}{
106-
"device_keys": deviceKeys,
107-
"one_time_keys": oneTimeKeys,
108-
})
109-
user.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"}, reqBody)
110-
}

Diff for: tests/csapi/upload_keys_test.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,12 @@ func TestKeyClaimOrdering(t *testing.T) {
184184

185185
// first upload key 1, sleep a bit, then upload key 0.
186186
otk1 := map[string]interface{}{"signed_curve25519:1": oneTimeKeys["signed_curve25519:1"]}
187-
alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"},
188-
client.WithJSONBody(t, map[string]interface{}{"one_time_keys": otk1}))
189-
190-
// Ensure that there is a difference in timestamp between the two upload requests.
187+
alice.MustUploadKeys(t, nil, otk1)
188+
// Ensure that there is a difference in timestamp between the two upload requests.
191189
time.Sleep(1 * time.Second)
192190

193191
otk0 := map[string]interface{}{"signed_curve25519:0": oneTimeKeys["signed_curve25519:0"]}
194-
alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"},
195-
client.WithJSONBody(t, map[string]interface{}{"one_time_keys": otk0}))
192+
alice.MustUploadKeys(t, nil, otk0)
196193

197194
// Now claim the keys, and check they come back in the right order
198195
reqBody := client.WithJSONBody(t, map[string]interface{}{

Diff for: tests/federation_device_list_update_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ func TestDeviceListsUpdateOverFederation(t *testing.T) {
142142
Password: "this is alices password",
143143
})
144144
deviceKeys, oneTimeKeys := alice2.MustGenerateOneTimeKeys(t, 1)
145-
alice2.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"}, client.WithJSONBody(t, map[string]interface{}{
146-
"device_keys": deviceKeys,
147-
"one_time_keys": oneTimeKeys,
148-
}))
145+
alice2.MustUploadKeys(t, deviceKeys, oneTimeKeys)
149146

150147
// now federation comes back online
151148
tc.makeReachable(t)

0 commit comments

Comments
 (0)