@@ -15,11 +15,13 @@ package tests
15
15
16
16
import (
17
17
"encoding/json"
18
+ "fmt"
18
19
"net/url"
19
20
"testing"
20
21
21
22
"github.com/matrix-org/complement/internal/b"
22
23
"github.com/matrix-org/complement/internal/client"
24
+ "github.com/matrix-org/complement/internal/match"
23
25
"github.com/matrix-org/complement/internal/must"
24
26
"github.com/tidwall/gjson"
25
27
)
@@ -342,3 +344,101 @@ func doInitialSync(t *testing.T, c *client.CSAPI) string {
342
344
since := client .GetJSONFieldStr (t , body , "next_batch" )
343
345
return since
344
346
}
347
+
348
+ // knockRoomsInPublicRoomsDirectoryTest will create a knock room, attempt to publish it to the public rooms directory,
349
+ // and then check that the room appears in the directory. The room's entry should also have a 'join_rule' field
350
+ // representing a knock room. For sanity-checking, this test will also create a public room and ensure it has a
351
+ // 'join_rule' representing a publicly-joinable room.
352
+ func knockRoomsInPublicRoomsDirectoryTest (t * testing.T ) {
353
+ deployment := Deploy (t , "test_knock_rooms_in_public_rooms_directory" , b .BlueprintAlice )
354
+ defer deployment .Destroy (t )
355
+
356
+ // Create a client for a local user
357
+ aliceUserID := "@alice:hs1"
358
+ alice := deployment .Client (t , "hs1" , aliceUserID )
359
+
360
+ // Create an invite-only room with the knock room version
361
+ roomID := alice .CreateRoom (t , struct {
362
+ Preset string `json:"preset"`
363
+ RoomVersion string `json:"room_version"`
364
+ }{
365
+ "private_chat" , // Set to private in order to get an invite-only room
366
+ knockUnstableIdentifier , // Room version required for knocking. TODO: Remove when knocking is in a stable room version
367
+ })
368
+
369
+ // Change the join_rule to allow knocking
370
+ emptyStateKey := ""
371
+ alice .SendEventSynced (t , roomID , b.Event {
372
+ Type : "m.room.join_rules" ,
373
+ Sender : alice .UserID ,
374
+ StateKey : & emptyStateKey ,
375
+ Content : map [string ]interface {}{
376
+ "join_rule" : knockUnstableIdentifier ,
377
+ },
378
+ })
379
+
380
+ // Publish the room to the public room directory and check that the 'join_rule' key is knock
381
+ publishAndCheckRoomJoinRule (t , alice , roomID , knockUnstableIdentifier )
382
+
383
+ // Create a public room
384
+ roomID = alice .CreateRoom (t , struct {
385
+ Preset string `json:"preset"`
386
+ }{
387
+ "public_chat" , // Set to public in order to get a public room
388
+ })
389
+
390
+ // Publish the room, and check that the public room directory presents a 'join_rule' key of public
391
+ publishAndCheckRoomJoinRule (t , alice , roomID , "public" )
392
+ }
393
+
394
+ // publishAndCheckRoomJoinRule will publish a given room ID to the given user's public room directory.
395
+ // It will then query the directory and ensure the room is listed, and has a given 'join_rule' entry
396
+ func publishAndCheckRoomJoinRule (t * testing.T , c * client.CSAPI , roomID , expectedJoinRule string ) {
397
+ // Publish the room to the public room directory
398
+ c .MustDo (
399
+ t ,
400
+ "PUT" ,
401
+ []string {"_matrix" , "client" , "r0" , "directory" , "list" , "room" , roomID },
402
+ struct {
403
+ Visibility string `json:"visibility"`
404
+ }{
405
+ "public" ,
406
+ },
407
+ )
408
+
409
+ // Check that we can see the room in the directory
410
+ res := c .MustDo (
411
+ t ,
412
+ "GET" ,
413
+ []string {"_matrix" , "client" , "r0" , "publicRooms" },
414
+ nil ,
415
+ )
416
+
417
+ roomFound := false
418
+ must .MatchResponse (t , res , match.HTTPResponse {
419
+ JSON : []match.JSON {
420
+ // For each public room directory chunk (representing a single room entry)
421
+ match .JSONArrayEach ("chunk" , func (r gjson.Result ) error {
422
+ // If this is our room
423
+ if r .Get ("room_id" ).Str == roomID {
424
+ roomFound = true
425
+
426
+ // Check that the join_rule key exists and is as we expect
427
+ if roomJoinRule := r .Get ("join_rule" ).Str ; roomJoinRule != expectedJoinRule {
428
+ return fmt .Errorf (
429
+ "'join_rule' key for room in public room chunk is '%s', expected '%s'" ,
430
+ roomJoinRule , expectedJoinRule ,
431
+ )
432
+ }
433
+ }
434
+ return nil
435
+ }),
436
+ },
437
+ })
438
+
439
+ // Check that we did in fact see the room
440
+ if ! roomFound {
441
+ t .Fatalf ("Room was not present in public room directory response" )
442
+ }
443
+
444
+ }
0 commit comments