|  | 
|  | 1 | +package csapi_tests | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"strings" | 
|  | 5 | +	"testing" | 
|  | 6 | + | 
|  | 7 | +	"github.com/matrix-org/complement/internal/b" | 
|  | 8 | +	"github.com/matrix-org/complement/internal/client" | 
|  | 9 | +	"github.com/matrix-org/complement/internal/match" | 
|  | 10 | +	"github.com/matrix-org/complement/internal/must" | 
|  | 11 | +	"github.com/matrix-org/complement/runtime" | 
|  | 12 | +) | 
|  | 13 | + | 
|  | 14 | +func TestJson(t *testing.T) { | 
|  | 15 | +	deployment := Deploy(t, b.BlueprintAlice) | 
|  | 16 | +	defer deployment.Destroy(t) | 
|  | 17 | +	alice := deployment.Client(t, "hs1", "@alice:hs1") | 
|  | 18 | +	roomID := alice.CreateRoom(t, map[string]interface{}{ | 
|  | 19 | +		"room_opts": map[string]interface{}{ | 
|  | 20 | +			"room_version": "6", | 
|  | 21 | +		}, | 
|  | 22 | +	}) | 
|  | 23 | + | 
|  | 24 | +	t.Run("Parallel", func(t *testing.T) { | 
|  | 25 | +		// sytest: Invalid JSON integers | 
|  | 26 | +		// sytest: Invalid JSON floats | 
|  | 27 | +		t.Run("Invalid numerical values", func(t *testing.T) { | 
|  | 28 | +			t.Parallel() | 
|  | 29 | + | 
|  | 30 | +			testCases := [][]byte{ | 
|  | 31 | +				[]byte(`{"body": 9007199254740992}`), | 
|  | 32 | +				[]byte(`{"body": -9007199254740992}`), | 
|  | 33 | +				[]byte(`{"body": 1.1}`), | 
|  | 34 | +			} | 
|  | 35 | + | 
|  | 36 | +			for _, testCase := range testCases { | 
|  | 37 | +				res := alice.DoFunc(t, "POST", []string{"_matrix", "client", "r0", "rooms", roomID, "send", "complement.dummy"}, client.WithJSONBody(t, testCase)) | 
|  | 38 | + | 
|  | 39 | +				must.MatchResponse(t, res, match.HTTPResponse{ | 
|  | 40 | +					StatusCode: 400, | 
|  | 41 | +					JSON: []match.JSON{ | 
|  | 42 | +						match.JSONKeyEqual("errcode", "M_BAD_JSON"), | 
|  | 43 | +					}, | 
|  | 44 | +				}) | 
|  | 45 | +			} | 
|  | 46 | +		}) | 
|  | 47 | + | 
|  | 48 | +		// sytest: Invalid JSON special values | 
|  | 49 | +		t.Run("Invalid JSON special values", func(t *testing.T) { | 
|  | 50 | +			t.Parallel() | 
|  | 51 | + | 
|  | 52 | +			testCases := [][]byte{ | 
|  | 53 | +				[]byte(`{"body": Infinity}`), | 
|  | 54 | +				[]byte(`{"body": -Infinity}`), | 
|  | 55 | +				[]byte(`{"body": NaN}`), | 
|  | 56 | +			} | 
|  | 57 | + | 
|  | 58 | +			for _, testCase := range testCases { | 
|  | 59 | +				res := alice.DoFunc(t, "POST", []string{"_matrix", "client", "r0", "rooms", roomID, "send", "complement.dummy"}, client.WithJSONBody(t, testCase)) | 
|  | 60 | + | 
|  | 61 | +				must.MatchResponse(t, res, match.HTTPResponse{ | 
|  | 62 | +					StatusCode: 400, | 
|  | 63 | +				}) | 
|  | 64 | +			} | 
|  | 65 | +		}) | 
|  | 66 | +	}) | 
|  | 67 | +} | 
|  | 68 | + | 
|  | 69 | +// small helper function to not bloat the main one | 
|  | 70 | +// todo: this should be more exhaustive and up-to-date | 
|  | 71 | +// todo: this should be easier to construct | 
|  | 72 | +func getFilters() []map[string]interface{} { | 
|  | 73 | +	const NAO = "not_an_object" | 
|  | 74 | +	const NAL = "not_a_list" | 
|  | 75 | + | 
|  | 76 | +	return []map[string]interface{}{ | 
|  | 77 | +		{ | 
|  | 78 | +			"presence": NAO, | 
|  | 79 | +		}, | 
|  | 80 | + | 
|  | 81 | +		{ | 
|  | 82 | +			"room": map[string]interface{}{ | 
|  | 83 | +				"timeline": NAO, | 
|  | 84 | +			}, | 
|  | 85 | +		}, | 
|  | 86 | +		{ | 
|  | 87 | +			"room": map[string]interface{}{ | 
|  | 88 | +				"state": NAO, | 
|  | 89 | +			}, | 
|  | 90 | +		}, | 
|  | 91 | +		{ | 
|  | 92 | +			"room": map[string]interface{}{ | 
|  | 93 | +				"ephemeral": NAO, | 
|  | 94 | +			}, | 
|  | 95 | +		}, | 
|  | 96 | +		{ | 
|  | 97 | +			"room": map[string]interface{}{ | 
|  | 98 | +				"account_data": NAO, | 
|  | 99 | +			}, | 
|  | 100 | +		}, | 
|  | 101 | + | 
|  | 102 | +		{ | 
|  | 103 | +			"room": map[string]interface{}{ | 
|  | 104 | +				"timeline": map[string]interface{}{ | 
|  | 105 | +					"rooms": NAL, | 
|  | 106 | +				}, | 
|  | 107 | +			}, | 
|  | 108 | +		}, | 
|  | 109 | +		{ | 
|  | 110 | +			"room": map[string]interface{}{ | 
|  | 111 | +				"timeline": map[string]interface{}{ | 
|  | 112 | +					"not_rooms": NAL, | 
|  | 113 | +				}, | 
|  | 114 | +			}, | 
|  | 115 | +		}, | 
|  | 116 | +		{ | 
|  | 117 | +			"room": map[string]interface{}{ | 
|  | 118 | +				"timeline": map[string]interface{}{ | 
|  | 119 | +					"senders": NAL, | 
|  | 120 | +				}, | 
|  | 121 | +			}, | 
|  | 122 | +		}, | 
|  | 123 | +		{ | 
|  | 124 | +			"room": map[string]interface{}{ | 
|  | 125 | +				"timeline": map[string]interface{}{ | 
|  | 126 | +					"not_senders": NAL, | 
|  | 127 | +				}, | 
|  | 128 | +			}, | 
|  | 129 | +		}, | 
|  | 130 | +		{ | 
|  | 131 | +			"room": map[string]interface{}{ | 
|  | 132 | +				"timeline": map[string]interface{}{ | 
|  | 133 | +					"types": NAL, | 
|  | 134 | +				}, | 
|  | 135 | +			}, | 
|  | 136 | +		}, | 
|  | 137 | +		{ | 
|  | 138 | +			"room": map[string]interface{}{ | 
|  | 139 | +				"timeline": map[string]interface{}{ | 
|  | 140 | +					"not_types": NAL, | 
|  | 141 | +				}, | 
|  | 142 | +			}, | 
|  | 143 | +		}, | 
|  | 144 | + | 
|  | 145 | +		{ | 
|  | 146 | +			"room": map[string]interface{}{ | 
|  | 147 | +				"timeline": map[string]interface{}{ | 
|  | 148 | +					"types": []int{1}, | 
|  | 149 | +				}, | 
|  | 150 | +			}, | 
|  | 151 | +		}, | 
|  | 152 | +		{ | 
|  | 153 | +			"room": map[string]interface{}{ | 
|  | 154 | +				"timeline": map[string]interface{}{ | 
|  | 155 | +					"rooms": []string{"not_a_room_id"}, | 
|  | 156 | +				}, | 
|  | 157 | +			}, | 
|  | 158 | +		}, | 
|  | 159 | +		{ | 
|  | 160 | +			"room": map[string]interface{}{ | 
|  | 161 | +				"timeline": map[string]interface{}{ | 
|  | 162 | +					"senders": []string{"not_a_sender_id"}, | 
|  | 163 | +				}, | 
|  | 164 | +			}, | 
|  | 165 | +		}, | 
|  | 166 | +	} | 
|  | 167 | +} | 
|  | 168 | + | 
|  | 169 | +// sytest: Check creating invalid filters returns 4xx | 
|  | 170 | +func TestFilter(t *testing.T) { | 
|  | 171 | +	runtime.SkipIf(t, runtime.Dendrite) // TODO remove if https://github.com/matrix-org/dendrite/issues/2067 is fixed | 
|  | 172 | + | 
|  | 173 | +	deployment := Deploy(t, b.BlueprintAlice) | 
|  | 174 | +	defer deployment.Destroy(t) | 
|  | 175 | +	alice := deployment.Client(t, "hs1", "@alice:hs1") | 
|  | 176 | + | 
|  | 177 | +	filters := getFilters() | 
|  | 178 | + | 
|  | 179 | +	for _, filter := range filters { | 
|  | 180 | +		res := alice.DoFunc(t, "POST", []string{"_matrix", "client", "r0", "user", alice.UserID, "filter"}, client.WithJSONBody(t, filter)) | 
|  | 181 | + | 
|  | 182 | +		if res.StatusCode >= 500 || res.StatusCode < 400 { | 
|  | 183 | +			t.Errorf("Expected 4XX status code, got %d for testing filter %s", res.StatusCode, filter) | 
|  | 184 | +		} | 
|  | 185 | +	} | 
|  | 186 | +} | 
|  | 187 | + | 
|  | 188 | +// sytest: Event size limits | 
|  | 189 | +func TestEvent(t *testing.T) { | 
|  | 190 | +	deployment := Deploy(t, b.BlueprintAlice) | 
|  | 191 | +	defer deployment.Destroy(t) | 
|  | 192 | +	alice := deployment.Client(t, "hs1", "@alice:hs1") | 
|  | 193 | +	roomID := alice.CreateRoom(t, map[string]interface{}{ | 
|  | 194 | +		"room_opts": map[string]interface{}{ | 
|  | 195 | +			"room_version": "6", | 
|  | 196 | +		}, | 
|  | 197 | +	}) | 
|  | 198 | + | 
|  | 199 | +	t.Run("Parallel", func(t *testing.T) { | 
|  | 200 | +		t.Run("Large Event", func(t *testing.T) { | 
|  | 201 | +			t.Parallel() | 
|  | 202 | + | 
|  | 203 | +			event := map[string]interface{}{ | 
|  | 204 | +				"msgtype": "m.text", | 
|  | 205 | +				"body":    strings.Repeat("and they dont stop coming ", 2700), // 2700 * 26 == 70200 | 
|  | 206 | +			} | 
|  | 207 | + | 
|  | 208 | +			res := alice.DoFunc(t, "PUT", []string{"_matrix", "client", "r0", "rooms", roomID, "send", "m.room.message", "1"}, client.WithJSONBody(t, event)) | 
|  | 209 | + | 
|  | 210 | +			must.MatchResponse(t, res, match.HTTPResponse{ | 
|  | 211 | +				StatusCode: 413, | 
|  | 212 | +			}) | 
|  | 213 | +		}) | 
|  | 214 | + | 
|  | 215 | +		t.Run("Large State Event", func(t *testing.T) { | 
|  | 216 | +			t.Parallel() | 
|  | 217 | + | 
|  | 218 | +			stateEvent := map[string]interface{}{ | 
|  | 219 | +				"body": strings.Repeat("Dormammu, I've Come To Bargain.\n", 2200), // 2200 * 32 == 70400 | 
|  | 220 | +			} | 
|  | 221 | + | 
|  | 222 | +			res := alice.DoFunc(t, "PUT", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "marvel.universe.fate"}, client.WithJSONBody(t, stateEvent)) | 
|  | 223 | + | 
|  | 224 | +			must.MatchResponse(t, res, match.HTTPResponse{ | 
|  | 225 | +				StatusCode: 413, | 
|  | 226 | +			}) | 
|  | 227 | +		}) | 
|  | 228 | +	}) | 
|  | 229 | +} | 
0 commit comments