complement/tests/csapi/invalid_test.go
kegsay a669c750c8
Remove Deployment.Client and change Deploy (#676)
* Remove Deployment.Client and change Deploy

- `Deployment.Client` was used to get pre-registered clients. Now we want tests to register new users for each test, for dirty runs. So swap for `Deployment.Register` everywhere.
- `Deploy` was used to deploy a blueprint. We don't want this to enable dirty runs. So replace it with the number of servers you need e.g `Deploy(t, 2)`.

* Fix up more broken refactoring

* unbreak tests; make user localpart look nicer

* Alice and bob must share a room for presence

* Fix user directory test

* Fix race condition caused by making the room later than before
2023-10-17 18:07:43 +01:00

230 lines
5.4 KiB
Go

package csapi_tests
import (
"strings"
"testing"
"github.com/matrix-org/complement"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/match"
"github.com/matrix-org/complement/must"
"github.com/matrix-org/complement/runtime"
)
func TestJson(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_opts": map[string]interface{}{
"room_version": "6",
},
})
t.Run("Parallel", func(t *testing.T) {
// sytest: Invalid JSON integers
// sytest: Invalid JSON floats
t.Run("Invalid numerical values", func(t *testing.T) {
t.Parallel()
testCases := [][]byte{
[]byte(`{"body": 9007199254740992}`),
[]byte(`{"body": -9007199254740992}`),
[]byte(`{"body": 1.1}`),
}
for _, testCase := range testCases {
res := alice.Do(t, "POST", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "complement.dummy"}, client.WithJSONBody(t, testCase))
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: 400,
JSON: []match.JSON{
match.JSONKeyEqual("errcode", "M_BAD_JSON"),
},
})
}
})
// sytest: Invalid JSON special values
t.Run("Invalid JSON special values", func(t *testing.T) {
t.Parallel()
testCases := [][]byte{
[]byte(`{"body": Infinity}`),
[]byte(`{"body": -Infinity}`),
[]byte(`{"body": NaN}`),
}
for _, testCase := range testCases {
res := alice.Do(t, "POST", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "complement.dummy"}, client.WithJSONBody(t, testCase))
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: 400,
})
}
})
})
}
// small helper function to not bloat the main one
// todo: this should be more exhaustive and up-to-date
// todo: this should be easier to construct
func getFilters() []map[string]interface{} {
const NAO = "not_an_object"
const NAL = "not_a_list"
return []map[string]interface{}{
{
"presence": NAO,
},
{
"room": map[string]interface{}{
"timeline": NAO,
},
},
{
"room": map[string]interface{}{
"state": NAO,
},
},
{
"room": map[string]interface{}{
"ephemeral": NAO,
},
},
{
"room": map[string]interface{}{
"account_data": NAO,
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"rooms": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"not_rooms": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"senders": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"not_senders": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"types": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"not_types": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"types": []int{1},
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"rooms": []string{"not_a_room_id"},
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"senders": []string{"not_a_sender_id"},
},
},
},
}
}
// sytest: Check creating invalid filters returns 4xx
func TestFilter(t *testing.T) {
runtime.SkipIf(t, runtime.Dendrite) // FIXME: https://github.com/matrix-org/dendrite/issues/2067
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
filters := getFilters()
for _, filter := range filters {
res := alice.Do(t, "POST", []string{"_matrix", "client", "v3", "user", alice.UserID, "filter"}, client.WithJSONBody(t, filter))
if res.StatusCode >= 500 || res.StatusCode < 400 {
t.Errorf("Expected 4XX status code, got %d for testing filter %s", res.StatusCode, filter)
}
}
}
// sytest: Event size limits
func TestEvent(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_opts": map[string]interface{}{
"room_version": "6",
},
})
t.Run("Parallel", func(t *testing.T) {
t.Run("Large Event", func(t *testing.T) {
t.Parallel()
event := map[string]interface{}{
"msgtype": "m.text",
"body": strings.Repeat("and they dont stop coming ", 2700), // 2700 * 26 == 70200
}
res := alice.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", "1"}, client.WithJSONBody(t, event))
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: 413,
})
})
t.Run("Large State Event", func(t *testing.T) {
t.Parallel()
stateEvent := map[string]interface{}{
"body": strings.Repeat("Dormammu, I've Come To Bargain.\n", 2200), // 2200 * 32 == 70400
}
res := alice.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "marvel.universe.fate"}, client.WithJSONBody(t, stateEvent))
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: 413,
})
})
})
}