Skip to content

Commit 0c19e2d

Browse files
authored
Add Must[Get|Set]DisplayName and use MustUploadKeys in more places (#755)
* Add MustGetDisplayName and MustSetDisplayName * Use MustUploadKeys more
1 parent 08ab613 commit 0c19e2d

8 files changed

+41
-70
lines changed

client/client.go

+24-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"`
@@ -492,6 +498,20 @@ func (c *CSAPI) MustGenerateOneTimeKeys(t ct.TestLike, otkCount uint) (deviceKey
492498
return deviceKeys, oneTimeKeys
493499
}
494500

501+
// MustSetDisplayName sets the global display name for this account or fails the test.
502+
func (c *CSAPI) MustSetDisplayName(t ct.TestLike, displayname string) {
503+
c.MustDo(t, "PUT", []string{"_matrix", "client", "v3", "profile", c.UserID, "displayname"}, WithJSONBody(t, map[string]any{
504+
"displayname": displayname,
505+
}))
506+
}
507+
508+
// MustGetDisplayName returns the global display name for this user or fails the test.
509+
func (c *CSAPI) MustGetDisplayName(t ct.TestLike, userID string) string {
510+
res := c.MustDo(t, "GET", []string{"_matrix", "client", "v3", "profile", userID, "displayname"})
511+
body := ParseJSON(t, res)
512+
return GetJSONFieldStr(t, body, "displayname")
513+
}
514+
495515
// WithRawBody sets the HTTP request body to `body`
496516
func WithRawBody(body []byte) RequestOpt {
497517
return func(req *http.Request) {

tests/csapi/apidoc_profile_displayname_test.go

+3-13
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"testing"
55

66
"github.com/matrix-org/complement"
7-
"github.com/matrix-org/complement/client"
87
"github.com/matrix-org/complement/helpers"
9-
"github.com/matrix-org/complement/match"
108
"github.com/matrix-org/complement/must"
119
)
1210

@@ -18,19 +16,11 @@ func TestProfileDisplayName(t *testing.T) {
1816
displayName := "my_display_name"
1917
// sytest: PUT /profile/:user_id/displayname sets my name
2018
t.Run("PUT /profile/:user_id/displayname sets my name", func(t *testing.T) {
21-
reqBody := client.WithJSONBody(t, map[string]interface{}{
22-
"displayname": displayName,
23-
})
24-
_ = authedClient.MustDo(t, "PUT", []string{"_matrix", "client", "v3", "profile", authedClient.UserID, "displayname"}, reqBody)
19+
authedClient.MustSetDisplayName(t, displayName)
2520
})
2621
// sytest: GET /profile/:user_id/displayname publicly accessible
2722
t.Run("GET /profile/:user_id/displayname publicly accessible", func(t *testing.T) {
28-
res := unauthedClient.Do(t, "GET", []string{"_matrix", "client", "v3", "profile", authedClient.UserID, "displayname"})
29-
must.MatchResponse(t, res, match.HTTPResponse{
30-
StatusCode: 200,
31-
JSON: []match.JSON{
32-
match.JSONKeyEqual("displayname", displayName),
33-
},
34-
})
23+
gotDisplayName := unauthedClient.MustGetDisplayName(t, authedClient.UserID)
24+
must.Equal(t, gotDisplayName, displayName, "display name mismatch")
3525
})
3626
}

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-
}

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{}{

tests/csapi/user_directory_display_names_test.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,7 @@ func setupUsers(t *testing.T) (*client.CSAPI, *client.CSAPI, *client.CSAPI, func
6161
// Alice sets her profile displayname. This ensures that her
6262
// public name, private name and userid localpart are all
6363
// distinguishable, even case-insensitively.
64-
alice.MustDo(
65-
t,
66-
"PUT",
67-
[]string{"_matrix", "client", "v3", "profile", alice.UserID, "displayname"},
68-
client.WithJSONBody(t, map[string]interface{}{
69-
"displayname": alicePublicName,
70-
}),
71-
)
64+
alice.MustSetDisplayName(t, alicePublicName)
7265

7366
// Alice creates a public room (so when Eve searches, she can see that Alice exists)
7467
alice.MustCreateRoom(t, map[string]interface{}{"visibility": "public"})

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)

tests/federation_query_profile_test.go

+4-16
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import (
1010
"github.com/matrix-org/gomatrixserverlib/fclient"
1111
"github.com/matrix-org/gomatrixserverlib/spec"
1212

13-
"github.com/matrix-org/complement/client"
14-
"github.com/matrix-org/complement/helpers"
1513
"github.com/matrix-org/complement/federation"
14+
"github.com/matrix-org/complement/helpers"
1615
"github.com/matrix-org/complement/match"
1716
"github.com/matrix-org/complement/must"
1817
)
@@ -61,12 +60,8 @@ func TestOutboundFederationProfile(t *testing.T) {
6160

6261
// query the display name which should do an outbound federation hit
6362
unauthedClient := deployment.UnauthenticatedClient(t, "hs1")
64-
res := unauthedClient.MustDo(t, "GET", []string{"_matrix", "client", "v3", "profile", remoteUserID, "displayname"})
65-
must.MatchResponse(t, res, match.HTTPResponse{
66-
JSON: []match.JSON{
67-
match.JSONKeyEqual("displayname", remoteDisplayName),
68-
},
69-
})
63+
gotDisplayName := unauthedClient.MustGetDisplayName(t, remoteUserID)
64+
must.Equal(t, gotDisplayName, remoteDisplayName, "display name mismatch")
7065
})
7166
}
7267

@@ -107,14 +102,7 @@ func TestInboundFederationProfile(t *testing.T) {
107102
t.Run("Inbound federation can query profile data", func(t *testing.T) {
108103
const alicePublicName = "Alice Cooper"
109104

110-
alice.MustDo(
111-
t,
112-
"PUT",
113-
[]string{"_matrix", "client", "v3", "profile", alice.UserID, "displayname"},
114-
client.WithJSONBody(t, map[string]interface{}{
115-
"displayname": alicePublicName,
116-
}),
117-
)
105+
alice.MustSetDisplayName(t, alicePublicName)
118106

119107
fedReq := fclient.NewFederationRequest(
120108
"GET",

tests/msc3902/federation_room_join_partial_state_test.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -3597,13 +3597,7 @@ func TestPartialStateJoin(t *testing.T) {
35973597
)
35983598
defer removePDUHandler()
35993599

3600-
alice.MustDo(t,
3601-
"PUT",
3602-
[]string{"_matrix", "client", "v3", "profile", alice.UserID, "displayname"},
3603-
client.WithJSONBody(t, map[string]interface{}{
3604-
"displayname": "alice 2",
3605-
}),
3606-
)
3600+
alice.MustSetDisplayName(t, "alice 2")
36073601
t.Logf("Alice changed display name")
36083602

36093603
select {

0 commit comments

Comments
 (0)