@@ -2148,3 +2148,130 @@ func TestKeyBackup(t *testing.T) {
2148
2148
}
2149
2149
})
2150
2150
}
2151
+
2152
+ func TestGetMembership (t * testing.T ) {
2153
+ alice := test .NewUser (t )
2154
+ bob := test .NewUser (t )
2155
+
2156
+ testCases := []struct {
2157
+ name string
2158
+ roomID string
2159
+ user * test.User
2160
+ additionalEvents func (t * testing.T , room * test.Room )
2161
+ request func (t * testing.T , room * test.Room , accessToken string ) * http.Request
2162
+ wantOK bool
2163
+ wantMemberCount int
2164
+ }{
2165
+
2166
+ {
2167
+ name : "/joined_members - Bob never joined" ,
2168
+ user : bob ,
2169
+ request : func (t * testing.T , room * test.Room , accessToken string ) * http.Request {
2170
+ return test .NewRequest (t , "GET" , fmt .Sprintf ("/_matrix/client/v3/rooms/%s/joined_members" , room .ID ), test .WithQueryParams (map [string ]string {
2171
+ "access_token" : accessToken ,
2172
+ }))
2173
+ },
2174
+ wantOK : false ,
2175
+ },
2176
+ {
2177
+ name : "/joined_members - Alice joined" ,
2178
+ user : alice ,
2179
+ request : func (t * testing.T , room * test.Room , accessToken string ) * http.Request {
2180
+ return test .NewRequest (t , "GET" , fmt .Sprintf ("/_matrix/client/v3/rooms/%s/joined_members" , room .ID ), test .WithQueryParams (map [string ]string {
2181
+ "access_token" : accessToken ,
2182
+ }))
2183
+ },
2184
+ wantOK : true ,
2185
+ wantMemberCount : 1 ,
2186
+ },
2187
+ {
2188
+ name : "/joined_members - Alice leaves, shouldn't be able to see members " ,
2189
+ user : alice ,
2190
+ request : func (t * testing.T , room * test.Room , accessToken string ) * http.Request {
2191
+ return test .NewRequest (t , "GET" , fmt .Sprintf ("/_matrix/client/v3/rooms/%s/joined_members" , room .ID ), test .WithQueryParams (map [string ]string {
2192
+ "access_token" : accessToken ,
2193
+ }))
2194
+ },
2195
+ additionalEvents : func (t * testing.T , room * test.Room ) {
2196
+ room .CreateAndInsert (t , alice , spec .MRoomMember , map [string ]interface {}{
2197
+ "membership" : "leave" ,
2198
+ }, test .WithStateKey (alice .ID ))
2199
+ },
2200
+ wantOK : false ,
2201
+ },
2202
+ {
2203
+ name : "/joined_members - Bob joins, Alice sees two members" ,
2204
+ user : alice ,
2205
+ request : func (t * testing.T , room * test.Room , accessToken string ) * http.Request {
2206
+ return test .NewRequest (t , "GET" , fmt .Sprintf ("/_matrix/client/v3/rooms/%s/joined_members" , room .ID ), test .WithQueryParams (map [string ]string {
2207
+ "access_token" : accessToken ,
2208
+ }))
2209
+ },
2210
+ additionalEvents : func (t * testing.T , room * test.Room ) {
2211
+ room .CreateAndInsert (t , bob , spec .MRoomMember , map [string ]interface {}{
2212
+ "membership" : "join" ,
2213
+ }, test .WithStateKey (bob .ID ))
2214
+ },
2215
+ wantOK : true ,
2216
+ wantMemberCount : 2 ,
2217
+ },
2218
+ }
2219
+
2220
+ test .WithAllDatabases (t , func (t * testing.T , dbType test.DBType ) {
2221
+
2222
+ cfg , processCtx , close := testrig .CreateConfig (t , dbType )
2223
+ routers := httputil .NewRouters ()
2224
+ cm := sqlutil .NewConnectionManager (processCtx , cfg .Global .DatabaseOptions )
2225
+ caches := caching .NewRistrettoCache (128 * 1024 * 1024 , time .Hour , caching .DisableMetrics )
2226
+ defer close ()
2227
+ natsInstance := jetstream.NATSInstance {}
2228
+ jsctx , _ := natsInstance .Prepare (processCtx , & cfg .Global .JetStream )
2229
+ defer jetstream .DeleteAllStreams (jsctx , & cfg .Global .JetStream )
2230
+
2231
+ // Use an actual roomserver for this
2232
+ rsAPI := roomserver .NewInternalAPI (processCtx , cfg , cm , & natsInstance , caches , caching .DisableMetrics )
2233
+ rsAPI .SetFederationAPI (nil , nil )
2234
+ userAPI := userapi .NewInternalAPI (processCtx , cfg , cm , & natsInstance , rsAPI , nil , caching .DisableMetrics , testIsBlacklistedOrBackingOff )
2235
+
2236
+ // We mostly need the rsAPI for this test, so nil for other APIs/caches etc.
2237
+ AddPublicRoutes (processCtx , routers , cfg , & natsInstance , nil , rsAPI , nil , nil , nil , userAPI , nil , nil , caching .DisableMetrics )
2238
+
2239
+ accessTokens := map [* test.User ]userDevice {
2240
+ alice : {},
2241
+ bob : {},
2242
+ }
2243
+ createAccessTokens (t , accessTokens , userAPI , processCtx .Context (), routers )
2244
+
2245
+ for _ , tc := range testCases {
2246
+ t .Run (tc .name , func (t * testing.T ) {
2247
+ room := test .NewRoom (t , alice )
2248
+ t .Cleanup (func () {
2249
+ t .Logf ("running cleanup for %s" , tc .name )
2250
+ })
2251
+ // inject additional events
2252
+ if tc .additionalEvents != nil {
2253
+ tc .additionalEvents (t , room )
2254
+ }
2255
+ if err := api .SendEvents (context .Background (), rsAPI , api .KindNew , room .Events (), "test" , "test" , "test" , nil , false ); err != nil {
2256
+ t .Fatalf ("failed to send events: %v" , err )
2257
+ }
2258
+
2259
+ w := httptest .NewRecorder ()
2260
+ routers .Client .ServeHTTP (w , tc .request (t , room , accessTokens [tc .user ].accessToken ))
2261
+ if w .Code != 200 && tc .wantOK {
2262
+ t .Logf ("%s" , w .Body .String ())
2263
+ t .Fatalf ("got HTTP %d want %d" , w .Code , 200 )
2264
+ }
2265
+ t .Logf ("[%s] Resp: %s" , tc .name , w .Body .String ())
2266
+
2267
+ // check we got the expected events
2268
+ if tc .wantOK {
2269
+ memberCount := len (gjson .GetBytes (w .Body .Bytes (), "joined" ).Map ())
2270
+ if memberCount != tc .wantMemberCount {
2271
+ t .Fatalf ("expected %d members, got %d" , tc .wantMemberCount , memberCount )
2272
+ }
2273
+ }
2274
+ })
2275
+ }
2276
+ })
2277
+ }
0 commit comments