55 "testing"
66 "time"
77
8+ "github.com/tidwall/gjson"
9+
810 "github.com/matrix-org/complement/internal/b"
911 "github.com/matrix-org/complement/internal/client"
1012 "github.com/matrix-org/complement/internal/match"
@@ -29,7 +31,7 @@ func listRoomAliasesResp(t *testing.T, c *client.CSAPI, roomID string) *http.Res
2931 return c .DoFunc (t , "GET" , []string {"_matrix" , "client" , "v3" , "rooms" , roomID , "aliases" })
3032}
3133
32- func setCanonicalAlias (t * testing.T , c * client.CSAPI , roomID string , roomAlias string , altAliases * []string ) * http.Response {
34+ func setCanonicalAliasResp (t * testing.T , c * client.CSAPI , roomID string , roomAlias string , altAliases * []string ) * http.Response {
3335 content := map [string ]interface {}{
3436 "alias" : roomAlias ,
3537 }
@@ -40,6 +42,21 @@ func setCanonicalAlias(t *testing.T, c *client.CSAPI, roomID string, roomAlias s
4042 return c .DoFunc (t , "PUT" , []string {"_matrix" , "client" , "v3" , "rooms" , roomID , "state" , "m.room.canonical_alias" }, client .WithJSONBody (t , content ))
4143}
4244
45+ func mustSetCanonicalAlias (t * testing.T , c * client.CSAPI , roomID string , roomAlias string , altAliases * []string ) string {
46+ content := map [string ]interface {}{
47+ "alias" : roomAlias ,
48+ }
49+ if altAliases != nil {
50+ content ["alt_aliases" ] = altAliases
51+ }
52+
53+ return c .SendEventSynced (t , roomID , b.Event {
54+ Type : "m.room.canonical_alias" ,
55+ StateKey : b .Ptr ("" ),
56+ Content : content ,
57+ })
58+ }
59+
4360func TestRoomAlias (t * testing.T ) {
4461 deployment := Deploy (t , b .BlueprintOneToOneRoom )
4562 defer deployment .Destroy (t )
@@ -141,6 +158,28 @@ func TestRoomAlias(t *testing.T) {
141158 StatusCode : 403 ,
142159 })
143160 })
161+
162+ // sytest: Room aliases can contain Unicode
163+ t .Run ("Room aliases can contain Unicode" , func (t * testing.T ) {
164+ t .Parallel ()
165+
166+ const unicodeAlias = "#老虎£я🤨👉ඞ:hs1"
167+
168+ roomID := alice .CreateRoom (t , map [string ]interface {}{})
169+
170+ res := setRoomAliasResp (t , alice , roomID , unicodeAlias )
171+ must .MatchResponse (t , res , match.HTTPResponse {
172+ StatusCode : 200 ,
173+ })
174+
175+ res = getRoomAliasResp (t , alice , unicodeAlias )
176+ must .MatchResponse (t , res , match.HTTPResponse {
177+ StatusCode : 200 ,
178+ JSON : []match.JSON {
179+ match .JSONKeyEqual ("room_id" , roomID ),
180+ },
181+ })
182+ })
144183 })
145184}
146185
@@ -209,14 +248,7 @@ func TestRoomDeleteAlias(t *testing.T) {
209248 },
210249 })
211250
212- res = setCanonicalAlias (t , alice , roomID , roomAlias , nil )
213-
214- must .MatchResponse (t , res , match.HTTPResponse {
215- StatusCode : 200 ,
216- JSON : []match.JSON {
217- match .JSONKeyPresent ("event_id" ),
218- },
219- })
251+ mustSetCanonicalAlias (t , alice , roomID , roomAlias , nil )
220252
221253 res = deleteRoomAliasResp (t , bob , roomAlias )
222254 must .MatchResponse (t , res , match.HTTPResponse {
@@ -238,6 +270,187 @@ func TestRoomDeleteAlias(t *testing.T) {
238270 },
239271 })
240272 })
273+
274+ // sytest: Can delete canonical alias
275+ t .Run ("Can delete canonical alias" , func (t * testing.T ) {
276+ t .Parallel ()
277+
278+ roomID := alice .CreateRoom (t , map [string ]interface {}{})
279+
280+ roomAlias := "#random_alias:hs1"
281+
282+ res := setRoomAliasResp (t , alice , roomID , roomAlias )
283+ must .MatchResponse (t , res , match.HTTPResponse {
284+ StatusCode : 200 ,
285+ })
286+
287+ mustSetCanonicalAlias (t , alice , roomID , roomAlias , nil )
288+
289+ _ , sinceToken := alice .MustSync (t , client.SyncReq {TimeoutMillis : "0" })
290+
291+ res = deleteRoomAliasResp (t , alice , roomAlias )
292+ must .MatchResponse (t , res , match.HTTPResponse {
293+ StatusCode : 200 ,
294+ })
295+
296+ alice .MustSyncUntil (t , client.SyncReq {Since : sinceToken }, client .SyncTimelineHas (roomID , func (ev gjson.Result ) bool {
297+ if ev .Get ("type" ).Str == "m.room.canonical_alias" &&
298+ ev .Get ("content" ).IsObject () &&
299+ len (ev .Get ("content" ).Map ()) == 0 {
300+ return true
301+ }
302+
303+ return false
304+ }))
305+ })
306+
307+ // sytest: Regular users can add and delete aliases in the default room configuration
308+ t .Run ("Regular users can add and delete aliases in the default room configuration" , func (t * testing.T ) {
309+ t .Parallel ()
310+
311+ roomID := alice .CreateRoom (t , map [string ]interface {}{})
312+
313+ randomAlias := "#random_alias_2:hs1"
314+
315+ alice .InviteRoom (t , roomID , bob .UserID )
316+ bob .JoinRoom (t , roomID , nil )
317+ bob .MustSyncUntil (t , client.SyncReq {}, client .SyncJoinedTo (bob .UserID , roomID ))
318+
319+ res := setRoomAliasResp (t , bob , roomID , randomAlias )
320+ must .MatchResponse (t , res , match.HTTPResponse {
321+ StatusCode : 200 ,
322+ })
323+
324+ res = getRoomAliasResp (t , alice , randomAlias )
325+ must .MatchResponse (t , res , match.HTTPResponse {
326+ StatusCode : 200 ,
327+ JSON : []match.JSON {
328+ match .JSONKeyEqual ("room_id" , roomID ),
329+ },
330+ })
331+
332+ res = deleteRoomAliasResp (t , bob , randomAlias )
333+ must .MatchResponse (t , res , match.HTTPResponse {
334+ StatusCode : 200 ,
335+ })
336+ })
337+
338+ // sytest: Regular users can add and delete aliases when m.room.aliases is restricted
339+ t .Run ("Regular users can add and delete aliases when m.room.aliases is restricted" , func (t * testing.T ) {
340+ t .Parallel ()
341+
342+ roomID := alice .CreateRoom (t , map [string ]interface {}{})
343+
344+ randomAlias := "#random_alias_3:hs1"
345+
346+ alice .InviteRoom (t , roomID , bob .UserID )
347+ bob .JoinRoom (t , roomID , nil )
348+
349+ alice .SendEventSynced (t , roomID , b.Event {
350+ Type : "m.room.power_levels" ,
351+ StateKey : b .Ptr ("" ),
352+ Content : map [string ]interface {}{
353+ "users" : map [string ]int64 {
354+ alice .UserID : 100 ,
355+ },
356+ "events" : map [string ]int64 {
357+ "m.room.aliases" : 50 ,
358+ },
359+ },
360+ })
361+
362+ res := setRoomAliasResp (t , bob , roomID , randomAlias )
363+ must .MatchResponse (t , res , match.HTTPResponse {
364+ StatusCode : 200 ,
365+ })
366+
367+ res = getRoomAliasResp (t , alice , randomAlias )
368+ must .MatchResponse (t , res , match.HTTPResponse {
369+ StatusCode : 200 ,
370+ JSON : []match.JSON {
371+ match .JSONKeyEqual ("room_id" , roomID ),
372+ },
373+ })
374+
375+ res = deleteRoomAliasResp (t , bob , randomAlias )
376+ must .MatchResponse (t , res , match.HTTPResponse {
377+ StatusCode : 200 ,
378+ })
379+ })
380+
381+ // sytest: Users can't delete other's aliases
382+ t .Run ("Users can't delete other's aliases" , func (t * testing.T ) {
383+ t .Parallel ()
384+
385+ roomID := alice .CreateRoom (t , map [string ]interface {}{})
386+
387+ randomAlias := "#random_alias_4:hs1"
388+
389+ alice .InviteRoom (t , roomID , bob .UserID )
390+ bob .JoinRoom (t , roomID , nil )
391+
392+ res := setRoomAliasResp (t , alice , roomID , randomAlias )
393+ must .MatchResponse (t , res , match.HTTPResponse {
394+ StatusCode : 200 ,
395+ })
396+
397+ res = getRoomAliasResp (t , bob , randomAlias )
398+ must .MatchResponse (t , res , match.HTTPResponse {
399+ StatusCode : 200 ,
400+ JSON : []match.JSON {
401+ match .JSONKeyEqual ("room_id" , roomID ),
402+ },
403+ })
404+
405+ res = deleteRoomAliasResp (t , bob , randomAlias )
406+ must .MatchResponse (t , res , match.HTTPResponse {
407+ StatusCode : 403 ,
408+ JSON : []match.JSON {
409+ match .JSONKeyEqual ("errcode" , "M_FORBIDDEN" ),
410+ },
411+ })
412+ })
413+
414+ // sytest: Users with sufficient power-level can delete other's aliases
415+ t .Run ("Users with sufficient power-level can delete other's aliases" , func (t * testing.T ) {
416+ t .Parallel ()
417+
418+ roomID := alice .CreateRoom (t , map [string ]interface {}{})
419+
420+ randomAlias := "#random_alias_5:hs1"
421+
422+ alice .InviteRoom (t , roomID , bob .UserID )
423+ bob .JoinRoom (t , roomID , nil )
424+
425+ alice .SendEventSynced (t , roomID , b.Event {
426+ Type : "m.room.power_levels" ,
427+ StateKey : b .Ptr ("" ),
428+ Content : map [string ]interface {}{
429+ "users" : map [string ]int64 {
430+ alice .UserID : 100 ,
431+ bob .UserID : 100 ,
432+ },
433+ },
434+ })
435+
436+ res := setRoomAliasResp (t , alice , roomID , randomAlias )
437+ must .MatchResponse (t , res , match.HTTPResponse {
438+ StatusCode : 200 ,
439+ })
440+
441+ res = getRoomAliasResp (t , bob , randomAlias )
442+ must .MatchResponse (t , res , match.HTTPResponse {
443+ StatusCode : 200 ,
444+ JSON : []match.JSON {
445+ match .JSONKeyEqual ("room_id" , roomID ),
446+ },
447+ })
448+
449+ res = deleteRoomAliasResp (t , bob , randomAlias )
450+ must .MatchResponse (t , res , match.HTTPResponse {
451+ StatusCode : 200 ,
452+ })
453+ })
241454 })
242455}
243456
@@ -263,14 +476,7 @@ func TestRoomCanonicalAlias(t *testing.T) {
263476 StatusCode : 200 ,
264477 })
265478
266- res = setCanonicalAlias (t , alice , roomID , roomAlias , nil )
267-
268- must .MatchResponse (t , res , match.HTTPResponse {
269- StatusCode : 200 ,
270- JSON : []match.JSON {
271- match .JSONKeyPresent ("event_id" ),
272- },
273- })
479+ mustSetCanonicalAlias (t , alice , roomID , roomAlias , nil )
274480 })
275481
276482 // part of "Canonical alias can be set"
@@ -282,7 +488,7 @@ func TestRoomCanonicalAlias(t *testing.T) {
282488
283489 roomAlias := "#rejects_missing:hs1"
284490
285- res := setCanonicalAlias (t , alice , roomID , roomAlias , nil )
491+ res := setCanonicalAliasResp (t , alice , roomID , roomAlias , nil )
286492
287493 must .MatchResponse (t , res , match.HTTPResponse {
288494 StatusCode : 400 ,
@@ -301,7 +507,7 @@ func TestRoomCanonicalAlias(t *testing.T) {
301507
302508 roomAlias := "%invalid_aliases:hs1"
303509
304- res := setCanonicalAlias (t , alice , roomID , roomAlias , nil )
510+ res := setCanonicalAliasResp (t , alice , roomID , roomAlias , nil )
305511
306512 must .MatchResponse (t , res , match.HTTPResponse {
307513 StatusCode : 400 ,
@@ -335,7 +541,7 @@ func TestRoomCanonicalAlias(t *testing.T) {
335541 StatusCode : 404 ,
336542 })
337543
338- res = setCanonicalAlias (t , alice , roomID , roomAlias , nil )
544+ res = setCanonicalAliasResp (t , alice , roomID , roomAlias , nil )
339545
340546 must .MatchResponse (t , res , match.HTTPResponse {
341547 StatusCode : 400 ,
@@ -359,7 +565,7 @@ func TestRoomCanonicalAlias(t *testing.T) {
359565 StatusCode : 200 ,
360566 })
361567
362- res = setCanonicalAlias (t , alice , room2 , roomAlias , nil )
568+ res = setCanonicalAliasResp (t , alice , room2 , roomAlias , nil )
363569
364570 must .MatchResponse (t , res , match.HTTPResponse {
365571 StatusCode : 400 ,
@@ -383,14 +589,7 @@ func TestRoomCanonicalAlias(t *testing.T) {
383589 StatusCode : 200 ,
384590 })
385591
386- res = setCanonicalAlias (t , alice , roomID , roomAlias , & []string {roomAlias })
387-
388- must .MatchResponse (t , res , match.HTTPResponse {
389- StatusCode : 200 ,
390- JSON : []match.JSON {
391- match .JSONKeyPresent ("event_id" ),
392- },
393- })
592+ mustSetCanonicalAlias (t , alice , roomID , roomAlias , & []string {roomAlias })
394593 })
395594
396595 // part of "Canonical alias can include alt_aliases"
@@ -407,7 +606,7 @@ func TestRoomCanonicalAlias(t *testing.T) {
407606 StatusCode : 200 ,
408607 })
409608
410- res = setCanonicalAlias (t , alice , roomID , roomAlias , & []string {wrongRoomAlias })
609+ res = setCanonicalAliasResp (t , alice , roomID , roomAlias , & []string {wrongRoomAlias })
411610
412611 must .MatchResponse (t , res , match.HTTPResponse {
413612 StatusCode : 400 ,
@@ -431,7 +630,7 @@ func TestRoomCanonicalAlias(t *testing.T) {
431630 StatusCode : 200 ,
432631 })
433632
434- res = setCanonicalAlias (t , alice , roomID , roomAlias , & []string {wrongRoomAlias })
633+ res = setCanonicalAliasResp (t , alice , roomID , roomAlias , & []string {wrongRoomAlias })
435634
436635 must .MatchResponse (t , res , match.HTTPResponse {
437636 StatusCode : 400 ,
@@ -462,7 +661,7 @@ func TestRoomCanonicalAlias(t *testing.T) {
462661 StatusCode : 200 ,
463662 })
464663
465- res = setCanonicalAlias (t , alice , room2 , room2Alias , & []string {room1Alias })
664+ res = setCanonicalAliasResp (t , alice , room2 , room2Alias , & []string {room1Alias })
466665
467666 must .MatchResponse (t , res , match.HTTPResponse {
468667 StatusCode : 400 ,
0 commit comments