Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Nov 22, 2016
1 parent 53b27bb commit 0a8b0ee
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
7 changes: 4 additions & 3 deletions synapse/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def format_event_for_client_v2_without_room_id(d):

def serialize_event(e, time_now_ms, as_client_event=True,
event_format=format_event_for_client_v1,
token_id=None, event_fields=None):
token_id=None, only_event_fields=None):
# FIXME(erikj): To handle the case of presence events and the like
if not isinstance(e, EventBase):
return e
Expand Down Expand Up @@ -258,7 +258,8 @@ def serialize_event(e, time_now_ms, as_client_event=True,
if as_client_event:
d = event_format(d)

if isinstance(event_fields, list):
d = only_fields(d, event_fields)
if (isinstance(only_event_fields, list) and
all(isinstance(f, basestring) for f in only_event_fields)):
d = only_fields(d, only_event_fields)

return d
57 changes: 56 additions & 1 deletion tests/events/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_content(self):
class SerializeEventTestCase(unittest.TestCase):

def serialize(self, ev, fields):
return serialize_event(ev, 1924354, event_fields=fields)
return serialize_event(ev, 1479807801915, only_event_fields=fields)

def test_event_fields_works_with_keys(self):
self.assertEquals(
Expand Down Expand Up @@ -235,3 +235,58 @@ def test_event_fields_nops_with_non_dict_keys(self):
),
{}
)

def test_event_fields_nops_with_array_keys(self):
self.assertEquals(
self.serialize(
MockEvent(
sender="@alice:localhost",
room_id="!foo:bar",
content={
"foo": ["I", "am", "an", "array"],
},
),
["content.foo.1"]
),
{}
)

def test_event_fields_all_fields_if_empty(self):
self.assertEquals(
self.serialize(
MockEvent(
room_id="!foo:bar",
content={
"foo": "bar",
},
),
[]
),
{
"room_id": "!foo:bar",
"content": {
"foo": "bar",
},
"unsigned": {}
}
)

def test_event_fields_fail_if_fields_not_str(self):
self.assertEquals(
self.serialize(
MockEvent(
room_id="!foo:bar",
content={
"foo": "bar",
},
),
["room_id", 4]
),
{
"room_id": "!foo:bar",
"content": {
"foo": "bar",
},
"unsigned": {}
}
)

0 comments on commit 0a8b0ee

Please sign in to comment.