@@ -3,11 +3,13 @@ package tests
33import (
44 "testing"
55
6+ "github.com/matrix-org/gomatrixserverlib"
7+ "github.com/tidwall/gjson"
8+
69 "github.com/matrix-org/complement/internal/b"
710 "github.com/matrix-org/complement/internal/client"
811 "github.com/matrix-org/complement/internal/match"
912 "github.com/matrix-org/complement/internal/must"
10- "github.com/tidwall/gjson"
1113)
1214
1315func TestFederationRoomsInvite (t * testing.T ) {
@@ -23,8 +25,8 @@ func TestFederationRoomsInvite(t *testing.T) {
2325 t .Parallel ()
2426 roomID := alice .CreateRoom (t , map [string ]interface {}{
2527 "preset" : "private_chat" ,
28+ "invite" : []string {bob .UserID },
2629 })
27- alice .InviteRoom (t , roomID , bob .UserID )
2830 bob .MustSyncUntil (t , client.SyncReq {}, client .SyncInvitedTo (bob .UserID , roomID ))
2931 bob .LeaveRoom (t , roomID )
3032 alice .MustSyncUntil (t , client.SyncReq {}, client .SyncLeftFrom (bob .UserID , roomID ))
@@ -49,14 +51,14 @@ func TestFederationRoomsInvite(t *testing.T) {
4951 t .Parallel ()
5052 roomID := alice .CreateRoom (t , map [string ]interface {}{
5153 "preset" : "private_chat" ,
54+ "invite" : []string {bob .UserID },
5255 })
5356 aliceSince := alice .MustSyncUntil (t , client.SyncReq {}, client .SyncJoinedTo (alice .UserID , roomID ))
54- alice .InviteRoom (t , roomID , bob .UserID )
55- charlieSince := bob .MustSyncUntil (t , client.SyncReq {}, client .SyncInvitedTo (bob .UserID , roomID ))
57+ bobSince := bob .MustSyncUntil (t , client.SyncReq {}, client .SyncInvitedTo (bob .UserID , roomID ))
5658 alice .LeaveRoom (t , roomID )
5759 alice .MustSyncUntil (t , client.SyncReq {Since : aliceSince }, client .SyncLeftFrom (alice .UserID , roomID ))
5860 bob .LeaveRoom (t , roomID )
59- bob .MustSyncUntil (t , client.SyncReq {Since : charlieSince }, client .SyncLeftFrom (bob .UserID , roomID ))
61+ bob .MustSyncUntil (t , client.SyncReq {Since : bobSince }, client .SyncLeftFrom (bob .UserID , roomID ))
6062 })
6163
6264 // sytest: Remote invited user can see room metadata
@@ -65,37 +67,72 @@ func TestFederationRoomsInvite(t *testing.T) {
6567 roomID := alice .CreateRoom (t , map [string ]interface {}{
6668 "preset" : "private_chat" ,
6769 "name" : "Invites room" ,
70+ "invite" : []string {bob .UserID },
6871 })
6972
70- alice .InviteRoom (t , roomID , bob .UserID )
73+ wantFields := map [string ]string {
74+ "m.room.join_rules" : "join_rule" ,
75+ "m.room.name" : "name" ,
76+ }
77+ wantValues := map [string ]string {
78+ "m.room.join_rules" : "invite" ,
79+ "m.room.name" : "Invites room" ,
80+ }
81+
7182 bob .MustSyncUntil (t , client.SyncReq {}, client .SyncInvitedTo (bob .UserID , roomID ))
7283 res , _ := bob .MustSync (t , client.SyncReq {})
73- verifyState (t , res , roomID , alice )
84+ verifyState (t , res , wantFields , wantValues , roomID , alice )
85+ })
86+
87+ t .Run ("Invited user has 'is_direct' flag in prev_content after joining" , func (t * testing.T ) {
88+ roomID := alice .CreateRoom (t , map [string ]interface {}{
89+ "preset" : "private_chat" ,
90+ "name" : "Invites room" ,
91+ // invite Bob and make the room a DM, so we can verify m.direct flag is in the prev_content after joining
92+ "invite" : []string {bob .UserID },
93+ "is_direct" : true ,
94+ })
95+ bob .JoinRoom (t , roomID , []string {})
96+ bob .MustSyncUntil (t , client.SyncReq {},
97+ client .SyncTimelineHas (roomID , func (result gjson.Result ) bool {
98+ // We expect a membership event ..
99+ if result .Get ("type" ).Str != gomatrixserverlib .MRoomMember {
100+ return false
101+ }
102+ // .. for Bob
103+ if result .Get ("state_key" ).Str != bob .UserID {
104+ return false
105+ }
106+ // Check that we've got tbe expected is_idrect flag
107+ return result .Get ("unsigned.prev_content.membership" ).Str == "invite" &&
108+ result .Get ("unsigned.prev_content.is_direct" ).Bool () == true &&
109+ result .Get ("unsigned.prev_sender" ).Str == alice .UserID
110+ }),
111+ )
74112 })
75113 })
76114}
77115
78116// verifyState checks that the fields in "wantFields" are present in invite_state.events
79- func verifyState (t * testing.T , res gjson.Result , roomID string , cl * client.CSAPI ) {
80- wantFields := map [ string ] string {
81- "m.room.join_rules" : "join_rule" ,
82- "m.room.name" : "name" ,
117+ func verifyState (t * testing.T , res gjson.Result , wantFields , wantValues map [ string ] string , roomID string , cl * client.CSAPI ) {
118+ inviteEvents := res . Get ( "rooms.invite." + client . GjsonEscape ( roomID ) + ".invite_state.events" )
119+ if ! inviteEvents . Exists () {
120+ t . Errorf ( "expected invite events, but they don't exist" )
83121 }
84-
85- for _ , event := range res .Get ("rooms.invite." + client .GjsonEscape (roomID ) + ".invite_state.events" ).Array () {
122+ for _ , event := range inviteEvents .Array () {
86123 eventType := event .Get ("type" ).Str
87124 field , ok := wantFields [eventType ]
88125 if ! ok {
89126 continue
90127 }
91- eventContent := event . Get ( "content." + field ). Str
128+ wantValue := wantValues [ eventType ]
92129 eventStateKey := event .Get ("state_key" ).Str
93130
94131 res := cl .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "v3" , "rooms" , roomID , "state" , eventType , eventStateKey })
95132
96133 must .MatchResponse (t , res , match.HTTPResponse {
97134 JSON : []match.JSON {
98- match .JSONKeyEqual (field , eventContent ),
135+ match .JSONKeyEqual (field , wantValue ),
99136 },
100137 })
101138 }
0 commit comments