Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case reproducing matrix-org/synapse#5677 for local users #199

Merged
merged 26 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cd709e8
Add test case reproducing matrix-org/synapse#5677
DMRobertson Aug 23, 2021
05d148a
goimports
DMRobertson Aug 23, 2021
877f6a1
Mark MustDo as Deprecated
DMRobertson Aug 23, 2021
8209286
Introduce SyncUntilInvitedTo
DMRobertson Aug 24, 2021
07afbaa
client: Helper function SearchUserDirectory
DMRobertson Aug 24, 2021
75ef397
match: use rs.Exists() in JSONKeyEqual
DMRobertson Aug 24, 2021
68cf89a
match: matcher that seeks an array of a fixed size
DMRobertson Aug 24, 2021
ce8f3a9
Update tests after review
DMRobertson Aug 24, 2021
ed7d66e
Fix format string
DMRobertson Aug 24, 2021
5435622
Make lint accept use of deprecated things
DMRobertson Aug 24, 2021
6ce2fc4
Remove from synapse blacklist
DMRobertson Aug 24, 2021
26aad20
Introduce `AnyOf` matcher
DMRobertson Aug 25, 2021
9f4b9a0
Expand test cases to inspect Bob's behaviour too
DMRobertson Aug 25, 2021
a2e0d06
Tweak expected behaviour
DMRobertson Aug 26, 2021
b10655f
Enforce Displaynames in blueprints
DMRobertson Aug 31, 2021
b52c712
Fix typo
DMRobertson Sep 2, 2021
c95b011
Add case for remote user with per-room nickname
DMRobertson Sep 2, 2021
d650c5a
Ensure pub name, priv name & localpart all differ
DMRobertson Sep 2, 2021
815cf7b
Remove testing comment
DMRobertson Sep 2, 2021
4c95d45
Prefer MustDoFunc; test joining with private name
DMRobertson Sep 6, 2021
ef171f3
Fix PUT call to set displayname
DMRobertson Sep 6, 2021
707b530
Cleanup deployment after test, not after setup!
DMRobertson Sep 6, 2021
2b4609a
aliceId -> aliceUserID
DMRobertson Sep 7, 2021
700e300
Allow remote users' displaynames to be localparts
DMRobertson Sep 7, 2021
56bf3e3
Remove the tests which apply over federation
DMRobertson Nov 10, 2021
c0b3ecd
Blacklist tests for dendrite
DMRobertson Nov 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,6 @@ func (c *CSAPI) InviteRoom(t *testing.T, roomID string, userID string) {
c.MustDo(t, "POST", []string{"_matrix", "client", "r0", "rooms", roomID, "invite"}, body)
}

// SearchUserDirectory makes a request to search the user directory with a given query.
// We don't include an explicit limit on the number of results returned, relying on
// the spec's default of 10.
func (c *CSAPI) SearchUserDirectory(t *testing.T, query string) *http.Response {
t.Helper()
return c.MustDoFunc(
t,
"POST",
[]string{"_matrix", "client", "r0", "user_directory", "search"},
WithJSONBody(t, map[string]interface{}{
"search_term": query,
}),
)
}

// SendEventSynced sends `e` into the room and waits for its event ID to come down /sync.
// Returns the event ID of the sent event.
func (c *CSAPI) SendEventSynced(t *testing.T, roomID string, e b.Event) string {
Expand Down
160 changes: 115 additions & 45 deletions tests/csapi/user_directory_display_names_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@ import (
"github.com/matrix-org/complement/internal/must"
)

func TestRoomSpecificUsernameHandling(t *testing.T) {
const aliceId = "@alice:hs1"
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
const alicePublicName = "Alice Cooper"
const alicePrivateName = "Freddy"

var justAliceByPublicName = []match.JSON{
match.JSONKeyArrayOfSize("results", 1),
match.JSONKeyEqual("results.0.display_name", alicePublicName),
match.JSONKeyEqual("results.0.user_id", aliceId),
}

var noResults = []match.JSON{
match.JSONKeyArrayOfSize("results", 0),
}

func setupUsers(t *testing.T) (*client.CSAPI, *client.CSAPI, *client.CSAPI) {
// Originally written to reproduce https://github.com/matrix-org/synapse/issues/5677
// In that bug report,
// - Bob knows about Alice, and
Expand All @@ -19,18 +33,17 @@ func TestRoomSpecificUsernameHandling(t *testing.T) {
// I've tweaked the names to be more traditional:
// - Eve knows about Alice,
// - Alice reveals a private name to another friend Bob
// - Eve shouldn't be able to see that private name.
// - Eve shouldn't be able to see that private name via the directory.
deployment := Deploy(t, b.BlueprintAlice)
defer deployment.Destroy(t)

alice := deployment.Client(t, "hs1", "@alice:hs1")
alice := deployment.Client(t, "hs1", aliceId)
bob := deployment.RegisterUser(t, "hs1", "bob", "bob-has-a-very-secret-pw")
eve := deployment.RegisterUser(t, "hs1", "eve", "eve-has-a-very-secret-pw")

// Alice sets her profile displayname. This ensures that her
// public name, private name and userid localpart are all
// distinguishable, even case-insensitively.
const alicePublicName = "Alice Cooper"
alice.MustDoFunc(
t,
"PUT",
Expand All @@ -40,9 +53,81 @@ func TestRoomSpecificUsernameHandling(t *testing.T) {
}),
)

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

return alice, bob, eve
}

func checkExpectations(t *testing.T, bob, eve *client.CSAPI) {
t.Run("Eve can find Alice by profile display name", func(t *testing.T) {
res := eve.MustDoFunc(
t,
"POST",
[]string{"_matrix", "client", "r0", "user_directory", "search"},
client.WithJSONBody(t, map[string]interface{}{
"search_term": alicePublicName,
}),
)
must.MatchResponse(t, res, match.HTTPResponse{JSON: justAliceByPublicName})
})

t.Run("Eve can find Alice by mxid", func(t *testing.T) {
res := eve.MustDoFunc(
t,
"POST",
[]string{"_matrix", "client", "r0", "user_directory", "search"},
client.WithJSONBody(t, map[string]interface{}{
"search_term": aliceId,
}),
)
must.MatchResponse(t, res, match.HTTPResponse{JSON: justAliceByPublicName})
})

t.Run("Eve cannot find Alice by room-specific name that Eve is not privy to", func(t *testing.T) {
res := eve.MustDoFunc(
t,
"POST",
[]string{"_matrix", "client", "r0", "user_directory", "search"},
client.WithJSONBody(t, map[string]interface{}{
"search_term": alicePrivateName,
}),
)
must.MatchResponse(t, res, match.HTTPResponse{JSON: noResults})
})

t.Run("Bob can find Alice by profile display name", func(t *testing.T) {
res := bob.MustDoFunc(
t,
"POST",
[]string{"_matrix", "client", "r0", "user_directory", "search"},
client.WithJSONBody(t, map[string]interface{}{
"search_term": alicePublicName,
}),
)
must.MatchResponse(t, res, match.HTTPResponse{
JSON: justAliceByPublicName,
})
})

t.Run("Bob can find Alice by mxid", func(t *testing.T) {
res := bob.MustDoFunc(
t,
"POST",
[]string{"_matrix", "client", "r0", "user_directory", "search"},
client.WithJSONBody(t, map[string]interface{}{
"search_term": aliceId,
}),
)
must.MatchResponse(t, res, match.HTTPResponse{
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
JSON: justAliceByPublicName,
})
})
}

func TestRoomSpecificUsernameChange(t *testing.T) {
alice, bob, eve := setupUsers(t)

// Bob creates a new room and invites Alice.
privateRoom := bob.CreateRoom(t, map[string]interface{}{
"visibility": "private",
Expand All @@ -54,58 +139,43 @@ func TestRoomSpecificUsernameHandling(t *testing.T) {
alice.JoinRoom(t, privateRoom, nil)

// Alice reveals her private name to Bob
const alicePrivateName = "Freddy"
alice.MustDoFunc(
t,
"PUT",
[]string{"_matrix", "client", "r0", "rooms", privateRoom, "state", "m.room.member", "@alice:hs1"},
[]string{"_matrix", "client", "r0", "rooms", privateRoom, "state", "m.room.member", alice.UserID},
client.WithJSONBody(t, map[string]interface{}{
"displayname": alicePrivateName,
"membership": "join",
}),
)

justAliceByPublicName := []match.JSON{
match.JSONKeyArrayOfSize("results", 1),
match.JSONKeyEqual("results.0.display_name", alicePublicName),
match.JSONKeyEqual("results.0.user_id", alice.UserID),
}

t.Run("Eve can find Alice by profile display name",
func(t *testing.T) {
res := eve.SearchUserDirectory(t, alicePublicName)
must.MatchResponse(t, res, match.HTTPResponse{JSON: justAliceByPublicName})
})
checkExpectations(t, bob, eve)
}

t.Run("Eve can find Alice by mxid",
func(t *testing.T) {
res := eve.SearchUserDirectory(t, alice.UserID)
must.MatchResponse(t, res, match.HTTPResponse{JSON: justAliceByPublicName})
})
func TestRoomSpecificUsernameAtJoin(t *testing.T) {
alice, bob, eve := setupUsers(t)

noResults := []match.JSON{
match.JSONKeyArrayOfSize("results", 0),
}
// Bob creates a new room and invites Alice.
privateRoom := bob.CreateRoom(t, map[string]interface{}{
"visibility": "private",
"invite": []string{alice.UserID},
})

t.Run("Eve cannot find Alice by room-specific name that Eve is not privy to",
func(t *testing.T) {
res := eve.SearchUserDirectory(t, alicePrivateName)
must.MatchResponse(t, res, match.HTTPResponse{JSON: noResults})
})
// Alice waits until she sees the invite, then accepts.
// When she accepts, she does so with a specific displayname.
alice.SyncUntilInvitedTo(t, privateRoom)
alice.JoinRoom(t, privateRoom, nil)

t.Run("Bob can find Alice by profile display name",
func(t *testing.T) {
res := bob.SearchUserDirectory(t, alicePublicName)
must.MatchResponse(t, res, match.HTTPResponse{
JSON: justAliceByPublicName,
})
})
// Alice reveals her private name to Bob
alice.MustDoFunc(
t,
"PUT",
[]string{"_matrix", "client", "r0", "rooms", privateRoom, "state", "m.room.member", alice.UserID},
client.WithJSONBody(t, map[string]interface{}{
"displayname": alicePrivateName,
"membership": "join",
}),
)

t.Run("Bob can find Alice by mxid",
func(t *testing.T) {
res := bob.SearchUserDirectory(t, alice.UserID)
must.MatchResponse(t, res, match.HTTPResponse{
JSON: justAliceByPublicName,
})
})
checkExpectations(t, bob, eve)
}
77 changes: 54 additions & 23 deletions tests/user_directory_federated_display_names_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,41 +88,72 @@ func TestRoomSpecificUsernameHandlingOverFederation(t *testing.T) {
match.JSONKeyEqual("results.0.user_id", remoteCharlie.UserID),
}

t.Run("Eve can find Charlie by profile display name",
func(t *testing.T) {
res := eve.SearchUserDirectory(t, charliePublicName)
must.MatchResponse(t, res, match.HTTPResponse{JSON: justCharlieByPublicNameOrMxid})
})
t.Run("Eve can find Charlie by profile display name", func(t *testing.T) {
res := eve.MustDoFunc(
t,
"POST",
[]string{"_matrix", "client", "r0", "user_directory", "search"},
client.WithJSONBody(t, map[string]interface{}{
"search_term": charliePublicName,
}),
)
must.MatchResponse(t, res, match.HTTPResponse{JSON: justCharlieByPublicNameOrMxid})
})

t.Run("Eve can find Charlie by mxid",
func(t *testing.T) {
res := eve.SearchUserDirectory(t, remoteCharlie.UserID)
must.MatchResponse(t, res, match.HTTPResponse{JSON: justCharlieByPublicNameOrMxid})
})
t.Run("Eve can find Charlie by mxid", func(t *testing.T) {
res := eve.MustDoFunc(
t,
"POST",
[]string{"_matrix", "client", "r0", "user_directory", "search"},
client.WithJSONBody(t, map[string]interface{}{
"search_term": remoteCharlie.UserID,
}),
)
must.MatchResponse(t, res, match.HTTPResponse{JSON: justCharlieByPublicNameOrMxid})
})

noResults := []match.JSON{
match.JSONKeyArrayOfSize("results", 0),
}

t.Run("Eve cannot find Charlie by room-specific name that Eve is not privy to",
func(t *testing.T) {
res := eve.SearchUserDirectory(t, charliePrivateName)
res := eve.MustDoFunc(
t,
"POST",
[]string{"_matrix", "client", "r0", "user_directory", "search"},
client.WithJSONBody(t, map[string]interface{}{
"search_term": charliePrivateName,
}),
)
must.MatchResponse(t, res, match.HTTPResponse{JSON: noResults})
})

t.Run("Bob can find Charlie by profile display name",
func(t *testing.T) {
res := bob.SearchUserDirectory(t, charliePublicName)
must.MatchResponse(t, res, match.HTTPResponse{
JSON: justCharlieByPublicNameOrMxid,
})
t.Run("Bob can find Charlie by profile display name", func(t *testing.T) {
res := bob.MustDoFunc(
t,
"POST",
[]string{"_matrix", "client", "r0", "user_directory", "search"},
client.WithJSONBody(t, map[string]interface{}{
"search_term": charliePublicName,
}),
)
must.MatchResponse(t, res, match.HTTPResponse{
JSON: justCharlieByPublicNameOrMxid,
})
})

t.Run("Bob can find Charlie by mxid",
func(t *testing.T) {
res := bob.SearchUserDirectory(t, remoteCharlie.UserID)
must.MatchResponse(t, res, match.HTTPResponse{
JSON: justCharlieByPublicNameOrMxid,
})
t.Run("Bob can find Charlie by mxid", func(t *testing.T) {
res := bob.MustDoFunc(
t,
"POST",
[]string{"_matrix", "client", "r0", "user_directory", "search"},
client.WithJSONBody(t, map[string]interface{}{
"search_term": remoteCharlie.UserID,
}),
)
must.MatchResponse(t, res, match.HTTPResponse{
JSON: justCharlieByPublicNameOrMxid,
})
})
}