2014-08-12 18:10:52 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-03 20:29:13 +04:00
|
|
|
# Copyright 2014 OpenMarket Ltd
|
2014-08-13 06:32:18 +04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2014-08-12 18:10:52 +04:00
|
|
|
from synapse.api.events import SynapseEvent
|
2014-11-11 13:31:59 +03:00
|
|
|
from synapse.api.events.validator import EventValidator
|
|
|
|
from synapse.api.errors import SynapseError
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2014-09-12 21:24:53 +04:00
|
|
|
from tests import unittest
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
|
|
|
|
class SynapseTemplateCheckTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2014-11-11 13:31:59 +03:00
|
|
|
self.validator = EventValidator(None)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_top_level_keys(self):
|
|
|
|
template = {
|
|
|
|
"person": {},
|
|
|
|
"friends": ["string"]
|
|
|
|
}
|
|
|
|
|
|
|
|
content = {
|
|
|
|
"person": {"name": "bob"},
|
|
|
|
"friends": ["jill", "mike"]
|
|
|
|
}
|
|
|
|
|
|
|
|
event = MockSynapseEvent(template)
|
2014-11-11 13:31:59 +03:00
|
|
|
event.content = content
|
|
|
|
self.assertTrue(self.validator.validate(event))
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
content = {
|
|
|
|
"person": {"name": "bob"},
|
|
|
|
"friends": ["jill"],
|
|
|
|
"enemies": ["mike"]
|
|
|
|
}
|
2014-11-11 13:31:59 +03:00
|
|
|
event.content = content
|
|
|
|
self.assertTrue(self.validator.validate(event))
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
content = {
|
|
|
|
"person": {"name": "bob"},
|
|
|
|
# missing friends
|
|
|
|
"enemies": ["mike", "jill"]
|
|
|
|
}
|
2014-11-11 13:31:59 +03:00
|
|
|
event.content = content
|
|
|
|
self.assertRaises(
|
|
|
|
SynapseError,
|
|
|
|
self.validator.validate,
|
|
|
|
event
|
|
|
|
)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
def test_lists(self):
|
|
|
|
template = {
|
|
|
|
"person": {},
|
|
|
|
"friends": [{"name":"string"}]
|
|
|
|
}
|
|
|
|
|
|
|
|
content = {
|
|
|
|
"person": {"name": "bob"},
|
|
|
|
"friends": ["jill", "mike"] # should be in objects
|
|
|
|
}
|
|
|
|
|
|
|
|
event = MockSynapseEvent(template)
|
2014-11-11 13:31:59 +03:00
|
|
|
event.content = content
|
|
|
|
self.assertRaises(
|
|
|
|
SynapseError,
|
|
|
|
self.validator.validate,
|
|
|
|
event
|
|
|
|
)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
content = {
|
|
|
|
"person": {"name": "bob"},
|
|
|
|
"friends": [{"name": "jill"}, {"name": "mike"}]
|
|
|
|
}
|
2014-11-11 13:31:59 +03:00
|
|
|
event.content = content
|
|
|
|
self.assertTrue(self.validator.validate(event))
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
def test_nested_lists(self):
|
|
|
|
template = {
|
|
|
|
"results": {
|
|
|
|
"families": [
|
|
|
|
{
|
|
|
|
"name": "string",
|
|
|
|
"members": [
|
|
|
|
{}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
content = {
|
|
|
|
"results": {
|
|
|
|
"families": [
|
|
|
|
{
|
|
|
|
"name": "Smith",
|
|
|
|
"members": [
|
|
|
|
"Alice", "Bob" # wrong types
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event = MockSynapseEvent(template)
|
2014-11-11 13:31:59 +03:00
|
|
|
event.content = content
|
|
|
|
self.assertRaises(
|
|
|
|
SynapseError,
|
|
|
|
self.validator.validate,
|
|
|
|
event
|
|
|
|
)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
content = {
|
|
|
|
"results": {
|
|
|
|
"families": [
|
|
|
|
{
|
|
|
|
"name": "Smith",
|
|
|
|
"members": [
|
|
|
|
{"name": "Alice"}, {"name": "Bob"}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2014-11-11 13:31:59 +03:00
|
|
|
event.content = content
|
|
|
|
self.assertTrue(self.validator.validate(event))
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
def test_nested_keys(self):
|
|
|
|
template = {
|
|
|
|
"person": {
|
|
|
|
"attributes": {
|
|
|
|
"hair": "string",
|
|
|
|
"eye": "string"
|
|
|
|
},
|
|
|
|
"age": 0,
|
|
|
|
"fav_books": ["string"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event = MockSynapseEvent(template)
|
|
|
|
|
|
|
|
content = {
|
|
|
|
"person": {
|
|
|
|
"attributes": {
|
|
|
|
"hair": "brown",
|
|
|
|
"eye": "green",
|
|
|
|
"skin": "purple"
|
|
|
|
},
|
|
|
|
"age": 33,
|
|
|
|
"fav_books": ["lotr", "hobbit"],
|
|
|
|
"fav_music": ["abba", "beatles"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:31:59 +03:00
|
|
|
event.content = content
|
|
|
|
self.assertTrue(self.validator.validate(event))
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
content = {
|
|
|
|
"person": {
|
|
|
|
"attributes": {
|
|
|
|
"hair": "brown"
|
|
|
|
# missing eye
|
|
|
|
},
|
|
|
|
"age": 33,
|
|
|
|
"fav_books": ["lotr", "hobbit"],
|
|
|
|
"fav_music": ["abba", "beatles"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:31:59 +03:00
|
|
|
event.content = content
|
|
|
|
self.assertRaises(
|
|
|
|
SynapseError,
|
|
|
|
self.validator.validate,
|
|
|
|
event
|
|
|
|
)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
content = {
|
|
|
|
"person": {
|
|
|
|
"attributes": {
|
|
|
|
"hair": "brown",
|
|
|
|
"eye": "green",
|
|
|
|
"skin": "purple"
|
|
|
|
},
|
|
|
|
"age": 33,
|
|
|
|
"fav_books": "nothing", # should be a list
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 13:31:59 +03:00
|
|
|
event.content = content
|
|
|
|
self.assertRaises(
|
|
|
|
SynapseError,
|
|
|
|
self.validator.validate,
|
|
|
|
event
|
|
|
|
)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
|
|
|
|
class MockSynapseEvent(SynapseEvent):
|
|
|
|
|
|
|
|
def __init__(self, template):
|
|
|
|
self.template = template
|
|
|
|
|
|
|
|
def get_content_template(self):
|
|
|
|
return self.template
|
|
|
|
|