use jsonschema.FormatChecker for RoomID and UserID strings
* use a valid filter in rest/client/v2_alpha test Signed-off-by: pik <alexander.maznev@gmail.com>pull/1783/head
parent
acafcf1c5b
commit
566641a0b5
|
@ -19,6 +19,7 @@ from twisted.internet import defer
|
||||||
|
|
||||||
import ujson as json
|
import ujson as json
|
||||||
import jsonschema
|
import jsonschema
|
||||||
|
from jsonschema import FormatChecker
|
||||||
|
|
||||||
FILTER_SCHEMA = {
|
FILTER_SCHEMA = {
|
||||||
"additionalProperties": False,
|
"additionalProperties": False,
|
||||||
|
@ -55,16 +56,10 @@ ROOM_FILTER_SCHEMA = {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"not_rooms": {
|
"not_rooms": {
|
||||||
"type": "array",
|
"$ref": "#/definitions/room_id_array"
|
||||||
"items": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"rooms": {
|
"rooms": {
|
||||||
"type": "array",
|
"$ref": "#/definitions/room_id_array"
|
||||||
"items": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"ephemeral": {
|
"ephemeral": {
|
||||||
"$ref": "#/definitions/room_event_filter"
|
"$ref": "#/definitions/room_event_filter"
|
||||||
|
@ -110,16 +105,10 @@ ROOM_EVENT_FILTER_SCHEMA = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"rooms": {
|
"rooms": {
|
||||||
"type": "array",
|
"$ref": "#/definitions/room_id_array"
|
||||||
"items": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"not_rooms": {
|
"not_rooms": {
|
||||||
"type": "array",
|
"$ref": "#/definitions/room_id_array"
|
||||||
"items": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"contains_url": {
|
"contains_url": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
|
@ -131,7 +120,15 @@ USER_ID_ARRAY_SCHEMA = {
|
||||||
"type": "array",
|
"type": "array",
|
||||||
"items": {
|
"items": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"pattern": "^@[A-Za-z0-9_]+:[A-Za-z0-9_\-\.]+$"
|
"format": "matrix_user_id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ROOM_ID_ARRAY_SCHEMA = {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "matrix_room_id"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,6 +137,7 @@ USER_FILTER_SCHEMA = {
|
||||||
"description": "schema for a Sync filter",
|
"description": "schema for a Sync filter",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"definitions": {
|
"definitions": {
|
||||||
|
"room_id_array": ROOM_ID_ARRAY_SCHEMA,
|
||||||
"user_id_array": USER_ID_ARRAY_SCHEMA,
|
"user_id_array": USER_ID_ARRAY_SCHEMA,
|
||||||
"filter": FILTER_SCHEMA,
|
"filter": FILTER_SCHEMA,
|
||||||
"room_filter": ROOM_FILTER_SCHEMA,
|
"room_filter": ROOM_FILTER_SCHEMA,
|
||||||
|
@ -175,6 +173,16 @@ USER_FILTER_SCHEMA = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@FormatChecker.cls_checks('matrix_room_id')
|
||||||
|
def matrix_room_id_validator(room_id_str):
|
||||||
|
return RoomID.from_string(room_id_str)
|
||||||
|
|
||||||
|
|
||||||
|
@FormatChecker.cls_checks('matrix_user_id')
|
||||||
|
def matrix_user_id_validator(user_id_str):
|
||||||
|
return UserID.from_string(user_id_str)
|
||||||
|
|
||||||
|
|
||||||
class Filtering(object):
|
class Filtering(object):
|
||||||
|
|
||||||
def __init__(self, hs):
|
def __init__(self, hs):
|
||||||
|
@ -208,7 +216,8 @@ class Filtering(object):
|
||||||
# individual top-level key e.g. public_user_data. Filters are made of
|
# individual top-level key e.g. public_user_data. Filters are made of
|
||||||
# many definitions.
|
# many definitions.
|
||||||
try:
|
try:
|
||||||
jsonschema.validate(user_filter_json, USER_FILTER_SCHEMA)
|
jsonschema.validate(user_filter_json, USER_FILTER_SCHEMA,
|
||||||
|
format_checker=FormatChecker())
|
||||||
except jsonschema.ValidationError as e:
|
except jsonschema.ValidationError as e:
|
||||||
raise SynapseError(400, e.message)
|
raise SynapseError(400, e.message)
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,8 @@ from synapse.api.filtering import Filter
|
||||||
from synapse.events import FrozenEvent
|
from synapse.events import FrozenEvent
|
||||||
from synapse.api.errors import SynapseError
|
from synapse.api.errors import SynapseError
|
||||||
|
|
||||||
|
import jsonschema
|
||||||
|
|
||||||
user_localpart = "test_user"
|
user_localpart = "test_user"
|
||||||
|
|
||||||
|
|
||||||
|
@ -61,7 +63,9 @@ class FilteringTestCase(unittest.TestCase):
|
||||||
{"account_data": "Hello World"},
|
{"account_data": "Hello World"},
|
||||||
{"event_fields": ["\\foo"]},
|
{"event_fields": ["\\foo"]},
|
||||||
{"room": {"timeline": {"limit": 0}, "state": {"not_bars": ["*"]}}},
|
{"room": {"timeline": {"limit": 0}, "state": {"not_bars": ["*"]}}},
|
||||||
{"event_format": "other"}
|
{"event_format": "other"},
|
||||||
|
{"room": {"not_rooms": ["#foo:pik-test"]}},
|
||||||
|
{"presence": {"senders": ["@bar;pik.test.com"]}}
|
||||||
]
|
]
|
||||||
for filter in invalid_filters:
|
for filter in invalid_filters:
|
||||||
with self.assertRaises(SynapseError) as check_filter_error:
|
with self.assertRaises(SynapseError) as check_filter_error:
|
||||||
|
@ -76,8 +80,8 @@ class FilteringTestCase(unittest.TestCase):
|
||||||
"state": {"not_types": ["m.room.member"]},
|
"state": {"not_types": ["m.room.member"]},
|
||||||
"ephemeral": {"limit": 0, "not_types": ["*"]},
|
"ephemeral": {"limit": 0, "not_types": ["*"]},
|
||||||
"include_leave": False,
|
"include_leave": False,
|
||||||
"rooms": ["#dee:pik-test"],
|
"rooms": ["!dee:pik-test"],
|
||||||
"not_rooms": ["#gee:pik-test"],
|
"not_rooms": ["!gee:pik-test"],
|
||||||
"account_data": {"limit": 0, "types": ["*"]}
|
"account_data": {"limit": 0, "types": ["*"]}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -108,7 +112,10 @@ class FilteringTestCase(unittest.TestCase):
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
for filter in valid_filters:
|
for filter in valid_filters:
|
||||||
self.filtering.check_valid_filter(filter)
|
try:
|
||||||
|
self.filtering.check_valid_filter(filter)
|
||||||
|
except jsonschema.ValidationError as e:
|
||||||
|
self.fail(e)
|
||||||
|
|
||||||
def test_limits_are_applied(self):
|
def test_limits_are_applied(self):
|
||||||
# TODO
|
# TODO
|
||||||
|
|
|
@ -33,8 +33,8 @@ PATH_PREFIX = "/_matrix/client/v2_alpha"
|
||||||
class FilterTestCase(unittest.TestCase):
|
class FilterTestCase(unittest.TestCase):
|
||||||
|
|
||||||
USER_ID = "@apple:test"
|
USER_ID = "@apple:test"
|
||||||
EXAMPLE_FILTER = {"type": ["m.*"]}
|
EXAMPLE_FILTER = {"room": {"timeline": {"types": ["m.room.message"]}}}
|
||||||
EXAMPLE_FILTER_JSON = '{"type": ["m.*"]}'
|
EXAMPLE_FILTER_JSON = '{"room": {"timeline": {"types": ["m.room.message"]}}}'
|
||||||
TO_REGISTER = [filter]
|
TO_REGISTER = [filter]
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
|
|
Loading…
Reference in New Issue