@@ -1336,3 +1336,140 @@ func TestAdminQueryEventReports(t *testing.T) {
1336
1336
})
1337
1337
})
1338
1338
}
1339
+
1340
+ func TestEventReportsGetDelete (t * testing.T ) {
1341
+ alice := test .NewUser (t , test .WithAccountType (uapi .AccountTypeAdmin ))
1342
+ bob := test .NewUser (t )
1343
+ room := test .NewRoom (t , alice )
1344
+
1345
+ // Add a name and alias
1346
+ roomName := "Testing"
1347
+ alias := "#testing"
1348
+ room .CreateAndInsert (t , alice , spec .MRoomName , map [string ]string {"name" : roomName }, test .WithStateKey ("" ))
1349
+ room .CreateAndInsert (t , alice , spec .MRoomCanonicalAlias , map [string ]string {"alias" : alias }, test .WithStateKey ("" ))
1350
+
1351
+ // Join the rooms with Bob
1352
+ room .CreateAndInsert (t , bob , spec .MRoomMember , map [string ]interface {}{
1353
+ "membership" : "join" ,
1354
+ }, test .WithStateKey (bob .ID ))
1355
+
1356
+ // Create a few events to report
1357
+
1358
+ eventIDToReport := room .CreateAndInsert (t , alice , "m.room.message" , map [string ]interface {}{"body" : "hello world" })
1359
+
1360
+ test .WithAllDatabases (t , func (t * testing.T , dbType test.DBType ) {
1361
+ cfg , processCtx , close := testrig .CreateConfig (t , dbType )
1362
+ routers := httputil .NewRouters ()
1363
+ cm := sqlutil .NewConnectionManager (processCtx , cfg .Global .DatabaseOptions )
1364
+ caches := caching .NewRistrettoCache (128 * 1024 * 1024 , time .Hour , caching .DisableMetrics )
1365
+ defer close ()
1366
+ natsInstance := jetstream.NATSInstance {}
1367
+ jsctx , _ := natsInstance .Prepare (processCtx , & cfg .Global .JetStream )
1368
+ defer jetstream .DeleteAllStreams (jsctx , & cfg .Global .JetStream )
1369
+
1370
+ // Use an actual roomserver for this
1371
+ rsAPI := roomserver .NewInternalAPI (processCtx , cfg , cm , & natsInstance , caches , caching .DisableMetrics )
1372
+ rsAPI .SetFederationAPI (nil , nil )
1373
+ userAPI := userapi .NewInternalAPI (processCtx , cfg , cm , & natsInstance , rsAPI , nil , caching .DisableMetrics , testIsBlacklistedOrBackingOff )
1374
+
1375
+ if err := api .SendEvents (context .Background (), rsAPI , api .KindNew , room .Events (), "test" , "test" , "test" , nil , false ); err != nil {
1376
+ t .Fatalf ("failed to send events: %v" , err )
1377
+ }
1378
+
1379
+ // We mostly need the rsAPI for this test, so nil for other APIs/caches etc.
1380
+ AddPublicRoutes (processCtx , routers , cfg , & natsInstance , nil , rsAPI , nil , nil , nil , userAPI , nil , nil , caching .DisableMetrics )
1381
+
1382
+ accessTokens := map [* test.User ]userDevice {
1383
+ alice : {},
1384
+ bob : {},
1385
+ }
1386
+ createAccessTokens (t , accessTokens , userAPI , processCtx .Context (), routers )
1387
+
1388
+ reqBody := map [string ]any {
1389
+ "reason" : "baaad" ,
1390
+ "score" : - 100 ,
1391
+ }
1392
+ body , err := json .Marshal (reqBody )
1393
+ if err != nil {
1394
+ t .Fatal (err )
1395
+ }
1396
+
1397
+ w := httptest .NewRecorder ()
1398
+
1399
+ var req * http.Request
1400
+ // Report the event
1401
+ req = httptest .NewRequest (http .MethodPost , fmt .Sprintf ("/_matrix/client/v3/rooms/%s/report/%s" , room .ID , eventIDToReport .EventID ()), strings .NewReader (string (body )))
1402
+ req .Header .Set ("Authorization" , "Bearer " + accessTokens [bob ].accessToken )
1403
+
1404
+ routers .Client .ServeHTTP (w , req )
1405
+
1406
+ if w .Code != http .StatusOK {
1407
+ t .Fatalf ("expected report to succeed, got HTTP %d instead: %s" , w .Code , w .Body .String ())
1408
+ }
1409
+
1410
+ t .Run ("Can not query with invalid ID" , func (t * testing.T ) {
1411
+ w = httptest .NewRecorder ()
1412
+ req = httptest .NewRequest (http .MethodGet , "/_synapse/admin/v1/event_reports/abc" , strings .NewReader (string (body )))
1413
+ req .Header .Set ("Authorization" , "Bearer " + accessTokens [alice ].accessToken )
1414
+
1415
+ routers .SynapseAdmin .ServeHTTP (w , req )
1416
+
1417
+ if w .Code != http .StatusBadRequest {
1418
+ t .Fatalf ("expected getting report to fail, got HTTP %d instead: %s" , w .Code , w .Body .String ())
1419
+ }
1420
+ })
1421
+
1422
+ t .Run ("Can query with valid ID" , func (t * testing.T ) {
1423
+ w = httptest .NewRecorder ()
1424
+ req = httptest .NewRequest (http .MethodGet , "/_synapse/admin/v1/event_reports/1" , strings .NewReader (string (body )))
1425
+ req .Header .Set ("Authorization" , "Bearer " + accessTokens [alice ].accessToken )
1426
+
1427
+ routers .SynapseAdmin .ServeHTTP (w , req )
1428
+
1429
+ if w .Code != http .StatusOK {
1430
+ t .Fatalf ("expected getting report to fail, got HTTP %d instead: %s" , w .Code , w .Body .String ())
1431
+ }
1432
+ resp := api.QueryAdminEventReportResponse {}
1433
+ if err = json .Unmarshal (w .Body .Bytes (), & resp ); err != nil {
1434
+ t .Fatal (err )
1435
+ }
1436
+ // test a few things
1437
+ if resp .EventID != eventIDToReport .EventID () {
1438
+ t .Fatalf ("expected eventID to be %s, got %s instead" , eventIDToReport .EventID (), resp .EventID )
1439
+ }
1440
+ if resp .RoomName != roomName {
1441
+ t .Fatalf ("expected roomName to be %s, got %s instead" , roomName , resp .RoomName )
1442
+ }
1443
+ if resp .CanonicalAlias != alias {
1444
+ t .Fatalf ("expected alias to be %s, got %s instead" , alias , resp .CanonicalAlias )
1445
+ }
1446
+ if reflect .DeepEqual (resp .EventJSON , eventIDToReport .JSON ()) {
1447
+ t .Fatal ("mismatching eventJSON" )
1448
+ }
1449
+ })
1450
+
1451
+ t .Run ("Can delete with a valid ID" , func (t * testing.T ) {
1452
+ w = httptest .NewRecorder ()
1453
+ req = httptest .NewRequest (http .MethodDelete , "/_synapse/admin/v1/event_reports/1" , strings .NewReader (string (body )))
1454
+ req .Header .Set ("Authorization" , "Bearer " + accessTokens [alice ].accessToken )
1455
+
1456
+ routers .SynapseAdmin .ServeHTTP (w , req )
1457
+
1458
+ if w .Code != http .StatusOK {
1459
+ t .Fatalf ("expected getting report to fail, got HTTP %d instead: %s" , w .Code , w .Body .String ())
1460
+ }
1461
+ })
1462
+
1463
+ t .Run ("Can not query deleted report" , func (t * testing.T ) {
1464
+ w = httptest .NewRecorder ()
1465
+ req = httptest .NewRequest (http .MethodGet , "/_synapse/admin/v1/event_reports/1" , strings .NewReader (string (body )))
1466
+ req .Header .Set ("Authorization" , "Bearer " + accessTokens [alice ].accessToken )
1467
+
1468
+ routers .SynapseAdmin .ServeHTTP (w , req )
1469
+
1470
+ if w .Code == http .StatusOK {
1471
+ t .Fatalf ("expected getting report to fail, got HTTP %d instead: %s" , w .Code , w .Body .String ())
1472
+ }
1473
+ })
1474
+ })
1475
+ }
0 commit comments