Skip to content

Commit f58adb0

Browse files
Add tests for MSC2716 being backwards compatible with existing room versions (#192)
Synapse changes added in matrix-org/synapse#10566
1 parent 735548e commit f58adb0

File tree

1 file changed

+124
-4
lines changed

1 file changed

+124
-4
lines changed

tests/msc2716_test.go

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ func TestBackfillingHistory(t *testing.T) {
501501
},
502502
}
503503
// We can't use as.SendEventSynced(...) because application services can't use the /sync API
504-
insertionSendRes := as.MustDoFunc(t, "PUT", []string{"_matrix", "client", "r0", "rooms", roomID, "send", insertionEvent.Type, "txn-i123"}, client.WithJSONBody(t, insertionEvent.Content))
504+
txnId := getTxnID("sendInsertionAndEnsureBackfilled-txn")
505+
insertionSendRes := as.MustDoFunc(t, "PUT", []string{"_matrix", "client", "r0", "rooms", roomID, "send", insertionEvent.Type, txnId}, client.WithJSONBody(t, insertionEvent.Content))
505506
insertionSendBody := client.ParseJSON(t, insertionSendRes)
506507
insertionEventID := client.GetJSONFieldStr(t, insertionSendBody, "event_id")
507508
// Make sure the insertion event has reached the homeserver
@@ -726,9 +727,105 @@ func TestBackfillingHistory(t *testing.T) {
726727
},
727728
})
728729
})
730+
731+
t.Run("Existing room versions", func(t *testing.T) {
732+
createUnsupportedMSC2716RoomOpts := map[string]interface{}{
733+
"preset": "public_chat",
734+
"name": "the hangout spot",
735+
// v6 is an existing room version that does not support MSC2716
736+
"room_version": "6",
737+
}
738+
739+
t.Run("Room creator can send MSC2716 events", func(t *testing.T) {
740+
t.Parallel()
741+
742+
roomID := as.CreateRoom(t, createUnsupportedMSC2716RoomOpts)
743+
alice.JoinRoom(t, roomID, nil)
744+
745+
// Create the "live" event we are going to insert our backfilled events next to
746+
eventIDsBefore := createMessagesInRoom(t, alice, roomID, 1)
747+
eventIdBefore := eventIDsBefore[0]
748+
timeAfterEventBefore := time.Now()
749+
750+
// Insert a backfilled event
751+
batchSendRes := batchSendHistoricalMessages(
752+
t,
753+
as,
754+
roomID,
755+
eventIdBefore,
756+
"",
757+
createJoinStateEventsForBackfillRequest([]string{virtualUserID}, timeAfterEventBefore),
758+
createMessageEventsForBackfillRequest([]string{virtualUserID}, timeAfterEventBefore, 1),
759+
// Status
760+
200,
761+
)
762+
batchSendResBody := client.ParseJSON(t, batchSendRes)
763+
historicalEventIDs := getEventsFromBatchSendResponseBody(t, batchSendResBody)
764+
765+
messagesRes := alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
766+
"dir": []string{"b"},
767+
"limit": []string{"100"},
768+
}))
769+
770+
must.MatchResponse(t, messagesRes, match.HTTPResponse{
771+
JSON: []match.JSON{
772+
match.JSONCheckOffAllowUnwanted("chunk", makeInterfaceSlice(historicalEventIDs), func(r gjson.Result) interface{} {
773+
return r.Get("event_id").Str
774+
}, nil),
775+
},
776+
})
777+
})
778+
779+
t.Run("Not allowed to redact MSC2716 insertion, chunk, marker events", func(t *testing.T) {
780+
t.Parallel()
781+
782+
roomID := as.CreateRoom(t, createUnsupportedMSC2716RoomOpts)
783+
alice.JoinRoom(t, roomID, nil)
784+
785+
// Create the "live" event we are going to insert our backfilled events next to
786+
eventIDsBefore := createMessagesInRoom(t, alice, roomID, 1)
787+
eventIdBefore := eventIDsBefore[0]
788+
timeAfterEventBefore := time.Now()
789+
790+
// Insert a backfilled event
791+
batchSendRes := batchSendHistoricalMessages(
792+
t,
793+
as,
794+
roomID,
795+
eventIdBefore,
796+
"",
797+
createJoinStateEventsForBackfillRequest([]string{virtualUserID}, timeAfterEventBefore),
798+
createMessageEventsForBackfillRequest([]string{virtualUserID}, timeAfterEventBefore, 1),
799+
// Status
800+
200,
801+
)
802+
batchSendResBody := client.ParseJSON(t, batchSendRes)
803+
historicalEventIDs := getEventsFromBatchSendResponseBody(t, batchSendResBody)
804+
insertionEventID := historicalEventIDs[0]
805+
chunkEventID := historicalEventIDs[2]
806+
baseInsertionEventID := historicalEventIDs[3]
807+
808+
// Send the marker event
809+
markerEventID := sendMarkerAndEnsureBackfilled(t, as, alice, roomID, baseInsertionEventID)
810+
811+
redactEventID(t, alice, roomID, insertionEventID, 403)
812+
redactEventID(t, alice, roomID, chunkEventID, 403)
813+
redactEventID(t, alice, roomID, markerEventID, 403)
814+
})
815+
})
729816
})
730817
}
731818

819+
var txnCounter int = 0
820+
821+
func getTxnID(prefix string) (txnID string) {
822+
txnId := fmt.Sprintf("%s-%d", prefix, txnCounter)
823+
824+
txnCounter++
825+
826+
return txnId
827+
}
828+
732829
func makeInterfaceSlice(slice []string) []interface{} {
733830
interfaceSlice := make([]interface{}, len(slice))
734831
for i := range slice {
@@ -837,7 +934,7 @@ func ensureVirtualUserRegistered(t *testing.T, c *client.CSAPI, virtualUserLocal
837934
}
838935
}
839936

840-
func sendMarkerAndEnsureBackfilled(t *testing.T, as *client.CSAPI, c *client.CSAPI, roomID, insertionEventID string) {
937+
func sendMarkerAndEnsureBackfilled(t *testing.T, as *client.CSAPI, c *client.CSAPI, roomID, insertionEventID string) (markerEventID string) {
841938
t.Helper()
842939

843940
// Send a marker event to let all of the homeservers know about the
@@ -849,9 +946,10 @@ func sendMarkerAndEnsureBackfilled(t *testing.T, as *client.CSAPI, c *client.CSA
849946
},
850947
}
851948
// We can't use as.SendEventSynced(...) because application services can't use the /sync API
852-
markerSendRes := as.MustDoFunc(t, "PUT", []string{"_matrix", "client", "r0", "rooms", roomID, "send", markerEvent.Type, "txn-m123"}, client.WithJSONBody(t, markerEvent.Content))
949+
txnId := getTxnID("sendMarkerAndEnsureBackfilled-txn")
950+
markerSendRes := as.MustDoFunc(t, "PUT", []string{"_matrix", "client", "r0", "rooms", roomID, "send", markerEvent.Type, txnId}, client.WithJSONBody(t, markerEvent.Content))
853951
markerSendBody := client.ParseJSON(t, markerSendRes)
854-
markerEventID := client.GetJSONFieldStr(t, markerSendBody, "event_id")
952+
markerEventID = client.GetJSONFieldStr(t, markerSendBody, "event_id")
855953

856954
// Make sure the marker event has reached the remote homeserver
857955
c.SyncUntilTimelineHas(t, roomID, func(ev gjson.Result) bool {
@@ -867,6 +965,8 @@ func sendMarkerAndEnsureBackfilled(t *testing.T, as *client.CSAPI, c *client.CSA
867965

868966
return false
869967
})
968+
969+
return markerEventID
870970
}
871971

872972
func createMessagesInRoom(t *testing.T, c *client.CSAPI, roomID string, count int) (eventIDs []string) {
@@ -967,6 +1067,26 @@ func createMessageEventsForBackfillRequest(
9671067
return evs
9681068
}
9691069

1070+
func redactEventID(t *testing.T, c *client.CSAPI, roomID, eventID string, expectedStatus int) {
1071+
t.Helper()
1072+
1073+
txnID := getTxnID("redactEventID-txn")
1074+
redactionRes := c.DoFunc(
1075+
t,
1076+
"PUT",
1077+
[]string{"_matrix", "client", "r0", "rooms", roomID, "redact", eventID, txnID},
1078+
client.WithJSONBody(t, map[string]interface{}{"reason": "chaos"}),
1079+
client.WithContentType("application/json"),
1080+
)
1081+
redactionResBody := client.ParseJSON(t, redactionRes)
1082+
redactionResErrcode := client.GetJSONFieldStr(t, redactionResBody, "error")
1083+
redactionResError := client.GetJSONFieldStr(t, redactionResBody, "errcode")
1084+
1085+
if redactionRes.StatusCode != expectedStatus {
1086+
t.Fatalf("msc2716.redactEventID: Expected redaction response to be %d but received %d -> %s: %s", expectedStatus, redactionRes.StatusCode, redactionResErrcode, redactionResError)
1087+
}
1088+
}
1089+
9701090
var chunkCount int64 = 0
9711091

9721092
func batchSendHistoricalMessages(

0 commit comments

Comments
 (0)