Skip to content

Commit f124baa

Browse files
committed
Add MustGetDisplayName and MustSetDisplayName
1 parent 08ab613 commit f124baa

5 files changed

+23
-44
lines changed

client/client.go

+14
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,20 @@ func (c *CSAPI) MustGenerateOneTimeKeys(t ct.TestLike, otkCount uint) (deviceKey
492492
return deviceKeys, oneTimeKeys
493493
}
494494

495+
// MustSetDisplayName sets the global display name for this account or fails the test.
496+
func (c *CSAPI) MustSetDisplayName(t ct.TestLike, displayname string) {
497+
c.MustDo(t, "PUT", []string{"_matrix", "client", "v3", "profile", c.UserID, "displayname"}, WithJSONBody(t, map[string]any{
498+
"displayname": displayname,
499+
}))
500+
}
501+
502+
// MustGetDisplayName returns the global display name for this user or fails the test.
503+
func (c *CSAPI) MustGetDisplayName(t ct.TestLike, userID string) string {
504+
res := c.MustDo(t, "GET", []string{"_matrix", "client", "v3", "profile", userID, "displayname"})
505+
body := ParseJSON(t, res)
506+
return GetJSONFieldStr(t, body, "displayname")
507+
}
508+
495509
// WithRawBody sets the HTTP request body to `body`
496510
func WithRawBody(body []byte) RequestOpt {
497511
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/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_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)