aboutsummaryrefslogtreecommitdiff
path: root/clientapi/clientapi_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'clientapi/clientapi_test.go')
-rw-r--r--clientapi/clientapi_test.go89
1 files changed, 89 insertions, 0 deletions
diff --git a/clientapi/clientapi_test.go b/clientapi/clientapi_test.go
index fffe4b6b..c550b208 100644
--- a/clientapi/clientapi_test.go
+++ b/clientapi/clientapi_test.go
@@ -2346,3 +2346,92 @@ func TestCreateRoomInvite(t *testing.T) {
}
})
}
+
+func TestReportEvent(t *testing.T) {
+ alice := test.NewUser(t)
+ bob := test.NewUser(t)
+ charlie := test.NewUser(t)
+ room := test.NewRoom(t, alice)
+
+ room.CreateAndInsert(t, charlie, spec.MRoomMember, map[string]interface{}{
+ "membership": "join",
+ }, test.WithStateKey(charlie.ID))
+ eventToReport := room.CreateAndInsert(t, alice, "m.room.message", map[string]interface{}{"body": "hello world"})
+
+ test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
+ cfg, processCtx, close := testrig.CreateConfig(t, dbType)
+ routers := httputil.NewRouters()
+ cm := sqlutil.NewConnectionManager(processCtx, cfg.Global.DatabaseOptions)
+ caches := caching.NewRistrettoCache(128*1024*1024, time.Hour, caching.DisableMetrics)
+ defer close()
+ natsInstance := jetstream.NATSInstance{}
+ jsctx, _ := natsInstance.Prepare(processCtx, &cfg.Global.JetStream)
+ defer jetstream.DeleteAllStreams(jsctx, &cfg.Global.JetStream)
+
+ // Use an actual roomserver for this
+ rsAPI := roomserver.NewInternalAPI(processCtx, cfg, cm, &natsInstance, caches, caching.DisableMetrics)
+ rsAPI.SetFederationAPI(nil, nil)
+ userAPI := userapi.NewInternalAPI(processCtx, cfg, cm, &natsInstance, rsAPI, nil, caching.DisableMetrics, testIsBlacklistedOrBackingOff)
+
+ if err := api.SendEvents(context.Background(), rsAPI, api.KindNew, room.Events(), "test", "test", "test", nil, false); err != nil {
+ t.Fatalf("failed to send events: %v", err)
+ }
+
+ // We mostly need the rsAPI for this test, so nil for other APIs/caches etc.
+ AddPublicRoutes(processCtx, routers, cfg, &natsInstance, nil, rsAPI, nil, nil, nil, userAPI, nil, nil, caching.DisableMetrics)
+
+ accessTokens := map[*test.User]userDevice{
+ alice: {},
+ bob: {},
+ charlie: {},
+ }
+ createAccessTokens(t, accessTokens, userAPI, processCtx.Context(), routers)
+
+ reqBody := map[string]any{
+ "reason": "baaad",
+ "score": -100,
+ }
+ body, err := json.Marshal(reqBody)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ w := httptest.NewRecorder()
+
+ var req *http.Request
+ t.Run("Bob is not joined and should not be able to report the event", func(t *testing.T) {
+ req = httptest.NewRequest(http.MethodPost, fmt.Sprintf("/_matrix/client/v3/rooms/%s/report/%s", room.ID, eventToReport.EventID()), strings.NewReader(string(body)))
+ req.Header.Set("Authorization", "Bearer "+accessTokens[bob].accessToken)
+
+ routers.Client.ServeHTTP(w, req)
+
+ if w.Code != http.StatusNotFound {
+ t.Fatalf("expected report to fail, got HTTP %d instead: %s", w.Code, w.Body.String())
+ }
+ })
+
+ t.Run("Charlie is joined but the event does not exist", func(t *testing.T) {
+ w = httptest.NewRecorder()
+ req = httptest.NewRequest(http.MethodPost, fmt.Sprintf("/_matrix/client/v3/rooms/%s/report/$doesNotExist", room.ID), strings.NewReader(string(body)))
+ req.Header.Set("Authorization", "Bearer "+accessTokens[charlie].accessToken)
+
+ routers.Client.ServeHTTP(w, req)
+
+ if w.Code != http.StatusNotFound {
+ t.Fatalf("expected report to fail, got HTTP %d instead: %s", w.Code, w.Body.String())
+ }
+ })
+
+ t.Run("Charlie is joined and allowed to report the event", func(t *testing.T) {
+ w = httptest.NewRecorder()
+ req = httptest.NewRequest(http.MethodPost, fmt.Sprintf("/_matrix/client/v3/rooms/%s/report/%s", room.ID, eventToReport.EventID()), strings.NewReader(string(body)))
+ req.Header.Set("Authorization", "Bearer "+accessTokens[charlie].accessToken)
+
+ routers.Client.ServeHTTP(w, req)
+
+ if w.Code != http.StatusOK {
+ t.Fatalf("expected report to be successful, got HTTP %d instead: %s", w.Code, w.Body.String())
+ }
+ })
+ })
+}