-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test case reproducing matrix-org/synapse#5677 for local users (#199)
* Mark MustDo as Deprecated * Introduce SyncUntilInvitedTo * match: use rs.Exists() in JSONKeyEqual, for consistency with other matchers * match: matcher that seeks an array of a fixed size * Introduce `AnyOf` matcher * Fix PUT call to set displayname
- Loading branch information
David Robertson
authored
Nov 15, 2021
1 parent
f56ed8e
commit 83b4571
Showing
5 changed files
with
282 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
// +build !dendrite_blacklist | ||
|
||
// Rationale for being included in Dendrite's blacklist: https://github.com/matrix-org/complement/pull/199#issuecomment-904852233 | ||
package csapi_tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/matrix-org/complement/internal/b" | ||
"github.com/matrix-org/complement/internal/client" | ||
"github.com/matrix-org/complement/internal/match" | ||
"github.com/matrix-org/complement/internal/must" | ||
) | ||
|
||
const aliceUserID = "@alice:hs1" | ||
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", aliceUserID), | ||
} | ||
|
||
var noResults = []match.JSON{ | ||
match.JSONKeyArrayOfSize("results", 0), | ||
} | ||
|
||
func setupUsers(t *testing.T) (*client.CSAPI, *client.CSAPI, *client.CSAPI, func(*testing.T)) { | ||
// Originally written to reproduce https://github.com/matrix-org/synapse/issues/5677 | ||
// In that bug report, | ||
// - Bob knows about Alice, and | ||
// - Alice has revealed a private name to another friend X, | ||
// - Bob can see that private name when he shouldn't be able to. | ||
// | ||
// 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 via the directory. | ||
deployment := Deploy(t, b.BlueprintAlice) | ||
cleanup := func(t *testing.T) { | ||
deployment.Destroy(t) | ||
} | ||
|
||
alice := deployment.Client(t, "hs1", aliceUserID) | ||
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. | ||
alice.MustDoFunc( | ||
t, | ||
"PUT", | ||
[]string{"_matrix", "client", "r0", "profile", alice.UserID, "displayname"}, | ||
client.WithJSONBody(t, map[string]interface{}{ | ||
"displayname": alicePublicName, | ||
}), | ||
) | ||
|
||
// 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, cleanup | ||
} | ||
|
||
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": aliceUserID, | ||
}), | ||
) | ||
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": aliceUserID, | ||
}), | ||
) | ||
must.MatchResponse(t, res, match.HTTPResponse{ | ||
JSON: justAliceByPublicName, | ||
}) | ||
}) | ||
} | ||
|
||
func TestRoomSpecificUsernameChange(t *testing.T) { | ||
alice, bob, eve, cleanup := setupUsers(t) | ||
defer cleanup(t) | ||
|
||
// Bob creates a new room and invites Alice. | ||
privateRoom := bob.CreateRoom(t, map[string]interface{}{ | ||
"visibility": "private", | ||
"invite": []string{alice.UserID}, | ||
}) | ||
|
||
// Alice waits until she sees the invite, then accepts. | ||
alice.SyncUntilInvitedTo(t, privateRoom) | ||
alice.JoinRoom(t, privateRoom, nil) | ||
|
||
// 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", | ||
}), | ||
) | ||
|
||
checkExpectations(t, bob, eve) | ||
} | ||
|
||
func TestRoomSpecificUsernameAtJoin(t *testing.T) { | ||
alice, bob, eve, cleanup := setupUsers(t) | ||
defer cleanup(t) | ||
|
||
// Bob creates a new room and invites Alice. | ||
privateRoom := bob.CreateRoom(t, map[string]interface{}{ | ||
"visibility": "private", | ||
"invite": []string{alice.UserID}, | ||
}) | ||
|
||
// 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) | ||
|
||
// 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", | ||
}), | ||
) | ||
|
||
checkExpectations(t, bob, eve) | ||
} |