Initial test case

This commit is contained in:
David Robertson 2023-03-31 15:28:03 +01:00
parent cef0570738
commit 11ca86f085
No known key found for this signature in database
GPG Key ID: 903ECE108A39DEDD
2 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,95 @@
package syncv3_test
import (
"bytes"
"encoding/json"
"fmt"
"github.com/matrix-org/sliding-sync/sync3"
"github.com/matrix-org/sliding-sync/sync3/extensions"
"github.com/matrix-org/sliding-sync/testutils"
"github.com/matrix-org/sliding-sync/testutils/m"
"testing"
)
func TestAccountDataRespectsExtensionScope(t *testing.T) {
alice := registerNewUser(t)
var syncResp *sync3.Response
// Want at least one test of the initial sync behaviour (which hits `ProcessInitial`)
// separate to the incremental sync behaviour (hits `AppendLive`)
t.Log("Alice creates rooms 1 and 2.")
room1 := alice.CreateRoom(t, map[string]interface{}{"preset": "public_chat", "name": "room 1"})
room2 := alice.CreateRoom(t, map[string]interface{}{"preset": "public_chat", "name": "room 2"})
t.Logf("room1=%s room2=%s", room1, room2)
t.Log("Alice uploads account data for both rooms, plus global account data.")
globalAccountDataEvent := putGlobalAccountData(
t,
alice,
"com.example.global",
map[string]interface{}{"global": "GLOBAL!"},
)
putRoomAccountData(
t,
alice,
room1,
"com.example.room",
map[string]interface{}{"room": 1},
)
putRoomAccountData(
t,
alice,
room2,
"com.example.room",
map[string]interface{}{"room": 2},
)
t.Log("Alice makes an initial sync request, requesting global account data only.")
syncResp = alice.SlidingSync(t, sync3.Request{
Extensions: extensions.Request{
AccountData: &extensions.AccountDataRequest{
Core: extensions.Core{Enabled: &boolTrue, Lists: []string{}, Rooms: []string{}},
},
},
Lists: map[string]sync3.RequestList{
"window": {
Ranges: sync3.SliceRanges{{0, 20}},
},
},
})
t.Log("Alice should see her global account data only.")
m.MatchResponse(
t,
syncResp,
func(res *sync3.Response) error {
for _, msg := range res.Extensions.AccountData.Global {
if bytes.Equal(msg, globalAccountDataEvent) {
return nil
}
}
return fmt.Errorf("could not find the global account data that Alice PUT earlier")
},
m.MatchNoRoomAccountData([]string{room1, room2}),
)
}
// putAccountData is a wrapper around SetGlobalAccountData. It returns the account data
// event as a json.RawMessage, automatically including top-level `type` and `content`
// fields. This is useful because it can be compared with account data events in a
// sync response with bytes.Equal.
func putGlobalAccountData(t *testing.T, client *CSAPI, eventType string, content map[string]interface{}) json.RawMessage {
t.Helper()
client.SetGlobalAccountData(t, eventType, content)
serialised := testutils.NewAccountData(t, eventType, content)
return serialised
}
// putRoomAccountData is like putGlobalAccountData, but for room-specific account data.
func putRoomAccountData(t *testing.T, client *CSAPI, roomID, eventType string, content map[string]interface{}) json.RawMessage {
t.Helper()
client.SetRoomAccountData(t, roomID, eventType, content)
serialised := testutils.NewAccountData(t, eventType, content)
return serialised
}

View File

@ -539,6 +539,21 @@ func MatchAccountData(globals []json.RawMessage, rooms map[string][]json.RawMess
}
}
// MatchNoRoomAccountData builds a matcher which asserts that none of the given roomIDs
// have room account data in a sync response.
func MatchNoRoomAccountData(roomIDs []string) RespMatcher {
return func(res *sync3.Response) error {
for _, roomID := range roomIDs {
// quick and dirty: complain the first time we see something we shouldn't
roomData := res.Extensions.AccountData.Rooms[roomID]
if roomData != nil {
return fmt.Errorf("MatchNoRoomAccountData: got account data for %s, but expected it to be missing", roomID)
}
}
return nil
}
}
func CheckList(listKey string, res sync3.ResponseList, matchers ...ListMatcher) error {
for _, m := range matchers {
if err := m(res); err != nil {