|
|
@ -14,6 +14,8 @@ |
|
|
|
# limitations under the License. |
|
|
|
# limitations under the License. |
|
|
|
|
|
|
|
|
|
|
|
from synapse.api.events import SynapseEvent |
|
|
|
from synapse.api.events import SynapseEvent |
|
|
|
|
|
|
|
from synapse.api.events.validator import EventValidator |
|
|
|
|
|
|
|
from synapse.api.errors import SynapseError |
|
|
|
|
|
|
|
|
|
|
|
from tests import unittest |
|
|
|
from tests import unittest |
|
|
|
|
|
|
|
|
|
|
@ -21,7 +23,7 @@ from tests import unittest |
|
|
|
class SynapseTemplateCheckTestCase(unittest.TestCase): |
|
|
|
class SynapseTemplateCheckTestCase(unittest.TestCase): |
|
|
|
|
|
|
|
|
|
|
|
def setUp(self): |
|
|
|
def setUp(self): |
|
|
|
pass |
|
|
|
self.validator = EventValidator(None) |
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self): |
|
|
|
def tearDown(self): |
|
|
|
pass |
|
|
|
pass |
|
|
@ -38,22 +40,28 @@ class SynapseTemplateCheckTestCase(unittest.TestCase): |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
event = MockSynapseEvent(template) |
|
|
|
event = MockSynapseEvent(template) |
|
|
|
self.assertTrue(event.check_json(content, raises=False)) |
|
|
|
event.content = content |
|
|
|
|
|
|
|
self.assertTrue(self.validator.validate(event)) |
|
|
|
|
|
|
|
|
|
|
|
content = { |
|
|
|
content = { |
|
|
|
"person": {"name": "bob"}, |
|
|
|
"person": {"name": "bob"}, |
|
|
|
"friends": ["jill"], |
|
|
|
"friends": ["jill"], |
|
|
|
"enemies": ["mike"] |
|
|
|
"enemies": ["mike"] |
|
|
|
} |
|
|
|
} |
|
|
|
event = MockSynapseEvent(template) |
|
|
|
event.content = content |
|
|
|
self.assertTrue(event.check_json(content, raises=False)) |
|
|
|
self.assertTrue(self.validator.validate(event)) |
|
|
|
|
|
|
|
|
|
|
|
content = { |
|
|
|
content = { |
|
|
|
"person": {"name": "bob"}, |
|
|
|
"person": {"name": "bob"}, |
|
|
|
# missing friends |
|
|
|
# missing friends |
|
|
|
"enemies": ["mike", "jill"] |
|
|
|
"enemies": ["mike", "jill"] |
|
|
|
} |
|
|
|
} |
|
|
|
self.assertFalse(event.check_json(content, raises=False)) |
|
|
|
event.content = content |
|
|
|
|
|
|
|
self.assertRaises( |
|
|
|
|
|
|
|
SynapseError, |
|
|
|
|
|
|
|
self.validator.validate, |
|
|
|
|
|
|
|
event |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
def test_lists(self): |
|
|
|
def test_lists(self): |
|
|
|
template = { |
|
|
|
template = { |
|
|
@ -67,13 +75,19 @@ class SynapseTemplateCheckTestCase(unittest.TestCase): |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
event = MockSynapseEvent(template) |
|
|
|
event = MockSynapseEvent(template) |
|
|
|
self.assertFalse(event.check_json(content, raises=False)) |
|
|
|
event.content = content |
|
|
|
|
|
|
|
self.assertRaises( |
|
|
|
|
|
|
|
SynapseError, |
|
|
|
|
|
|
|
self.validator.validate, |
|
|
|
|
|
|
|
event |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
content = { |
|
|
|
content = { |
|
|
|
"person": {"name": "bob"}, |
|
|
|
"person": {"name": "bob"}, |
|
|
|
"friends": [{"name": "jill"}, {"name": "mike"}] |
|
|
|
"friends": [{"name": "jill"}, {"name": "mike"}] |
|
|
|
} |
|
|
|
} |
|
|
|
self.assertTrue(event.check_json(content, raises=False)) |
|
|
|
event.content = content |
|
|
|
|
|
|
|
self.assertTrue(self.validator.validate(event)) |
|
|
|
|
|
|
|
|
|
|
|
def test_nested_lists(self): |
|
|
|
def test_nested_lists(self): |
|
|
|
template = { |
|
|
|
template = { |
|
|
@ -103,7 +117,12 @@ class SynapseTemplateCheckTestCase(unittest.TestCase): |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
event = MockSynapseEvent(template) |
|
|
|
event = MockSynapseEvent(template) |
|
|
|
self.assertFalse(event.check_json(content, raises=False)) |
|
|
|
event.content = content |
|
|
|
|
|
|
|
self.assertRaises( |
|
|
|
|
|
|
|
SynapseError, |
|
|
|
|
|
|
|
self.validator.validate, |
|
|
|
|
|
|
|
event |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
content = { |
|
|
|
content = { |
|
|
|
"results": { |
|
|
|
"results": { |
|
|
@ -117,7 +136,8 @@ class SynapseTemplateCheckTestCase(unittest.TestCase): |
|
|
|
] |
|
|
|
] |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
self.assertTrue(event.check_json(content, raises=False)) |
|
|
|
event.content = content |
|
|
|
|
|
|
|
self.assertTrue(self.validator.validate(event)) |
|
|
|
|
|
|
|
|
|
|
|
def test_nested_keys(self): |
|
|
|
def test_nested_keys(self): |
|
|
|
template = { |
|
|
|
template = { |
|
|
@ -145,7 +165,8 @@ class SynapseTemplateCheckTestCase(unittest.TestCase): |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
self.assertTrue(event.check_json(content, raises=False)) |
|
|
|
event.content = content |
|
|
|
|
|
|
|
self.assertTrue(self.validator.validate(event)) |
|
|
|
|
|
|
|
|
|
|
|
content = { |
|
|
|
content = { |
|
|
|
"person": { |
|
|
|
"person": { |
|
|
@ -159,7 +180,12 @@ class SynapseTemplateCheckTestCase(unittest.TestCase): |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
self.assertFalse(event.check_json(content, raises=False)) |
|
|
|
event.content = content |
|
|
|
|
|
|
|
self.assertRaises( |
|
|
|
|
|
|
|
SynapseError, |
|
|
|
|
|
|
|
self.validator.validate, |
|
|
|
|
|
|
|
event |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
content = { |
|
|
|
content = { |
|
|
|
"person": { |
|
|
|
"person": { |
|
|
@ -173,7 +199,12 @@ class SynapseTemplateCheckTestCase(unittest.TestCase): |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
self.assertFalse(event.check_json(content, raises=False)) |
|
|
|
event.content = content |
|
|
|
|
|
|
|
self.assertRaises( |
|
|
|
|
|
|
|
SynapseError, |
|
|
|
|
|
|
|
self.validator.validate, |
|
|
|
|
|
|
|
event |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MockSynapseEvent(SynapseEvent): |
|
|
|
class MockSynapseEvent(SynapseEvent): |
|
|
|