Skip to content

Commit 12210e3

Browse files
Add redcation test (#848)
Part of element-hq/synapse-rust-apps#430 (tracking tests we want to make sure work with the Synapse Pro Event Cache)
1 parent 35b1745 commit 12210e3

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

client/client.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func (c *CSAPI) SendRedaction(t ct.TestLike, roomID string, content map[string]i
459459
return c.Do(t, "PUT", paths, WithJSONBody(t, content))
460460
}
461461

462-
// MustGetStateEvent returns the event content for the given state event. Fails the test if the state event does not exist.
462+
// MustGetStateEventContent returns the event content for the given state event. Fails the test if the state event does not exist.
463463
func (c *CSAPI) MustGetStateEventContent(t ct.TestLike, roomID, eventType, stateKey string) (content gjson.Result) {
464464
t.Helper()
465465
res := c.GetStateEventContent(t, roomID, eventType, stateKey)
@@ -468,12 +468,27 @@ func (c *CSAPI) MustGetStateEventContent(t ct.TestLike, roomID, eventType, state
468468
return gjson.ParseBytes(body)
469469
}
470470

471-
// GetStateEvent returns the event content for the given state event. Use this form to detect absence via 404.
471+
// GetStateEventContent returns the event content for the given state event. Use this form to detect absence via 404.
472472
func (c *CSAPI) GetStateEventContent(t ct.TestLike, roomID, eventType, stateKey string) *http.Response {
473473
t.Helper()
474474
return c.Do(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", eventType, stateKey})
475475
}
476476

477+
// MustGetEvent returns the event content for the given state event. Fails the test if the state event does not exist.
478+
func (c *CSAPI) MustGetEvent(t ct.TestLike, roomID, eventID string) (eventJson gjson.Result) {
479+
t.Helper()
480+
res := c.GetEvent(t, roomID, eventID)
481+
mustRespond2xx(t, res)
482+
body := ParseJSON(t, res)
483+
return gjson.ParseBytes(body)
484+
}
485+
486+
// GetEvent returns the event JSON. Use this form to detect absence via 404.
487+
func (c *CSAPI) GetEvent(t ct.TestLike, roomID, eventID string) *http.Response {
488+
t.Helper()
489+
return c.Do(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "event", eventID})
490+
}
491+
477492
// MustSendTyping marks this user as typing until the timeout is reached. If isTyping is false, timeout is ignored.
478493
func (c *CSAPI) MustSendTyping(t ct.TestLike, roomID string, isTyping bool, timeoutMillis int) {
479494
res := c.SendTyping(t, roomID, isTyping, timeoutMillis)

tests/csapi/redact_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package csapi_tests
2+
3+
import (
4+
"testing"
5+
6+
"github.com/matrix-org/complement"
7+
"github.com/matrix-org/complement/b"
8+
"github.com/matrix-org/complement/helpers"
9+
"github.com/matrix-org/complement/match"
10+
"github.com/matrix-org/complement/must"
11+
)
12+
13+
// Test `PUT /_matrix/client/v3/rooms/{roomId}/redact/{eventId}/{txnId} ` (redactions)
14+
func TestRedact(t *testing.T) {
15+
deployment := complement.Deploy(t, 1)
16+
defer deployment.Destroy(t)
17+
18+
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{
19+
LocalpartSuffix: "alice",
20+
})
21+
bob := deployment.Register(t, "hs1", helpers.RegistrationOpts{
22+
LocalpartSuffix: "bob",
23+
})
24+
25+
// Alice creates the room
26+
roomID := alice.MustCreateRoom(t, map[string]interface{}{
27+
// Just making it easier for everyone to join
28+
"preset": "public_chat",
29+
})
30+
// Bob joins the room
31+
bob.MustJoinRoom(t, roomID, nil)
32+
33+
t.Run("Event content is redacted", func(t *testing.T) {
34+
// Alice creates an event
35+
expectedEventContent := map[string]interface{}{
36+
"msgtype": "m.text",
37+
"body": "expected message body",
38+
}
39+
eventIDToRedact := alice.SendEventSynced(t, roomID, b.Event{
40+
Type: "m.room.message",
41+
Content:expectedEventContent,
42+
})
43+
44+
// Bob can see the event content
45+
eventJsonBefore := bob.MustGetEvent(t, roomID, eventIDToRedact)
46+
must.MatchGJSON(t, eventJsonBefore, match.JSONKeyEqual("content", expectedEventContent))
47+
48+
// Alice redacts the event
49+
alice.MustSendRedaction(t, roomID, map[string]interface{}{
50+
"reason": "reasons...",
51+
}, eventIDToRedact)
52+
53+
// Bob can no longer see the event content
54+
eventJsonAfter := bob.MustGetEvent(t, roomID, eventIDToRedact)
55+
must.MatchGJSON(t, eventJsonAfter, match.JSONKeyEqual("content", map[string]interface{}{
56+
// no content
57+
}))
58+
})
59+
60+
}

0 commit comments

Comments
 (0)