test_utils.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the 'License');
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an 'AS IS' BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.events import FrozenEvent
  16. from synapse.events.utils import prune_event, serialize_event
  17. from .. import unittest
  18. def MockEvent(**kwargs):
  19. if "event_id" not in kwargs:
  20. kwargs["event_id"] = "fake_event_id"
  21. if "type" not in kwargs:
  22. kwargs["type"] = "fake_type"
  23. return FrozenEvent(kwargs)
  24. class PruneEventTestCase(unittest.TestCase):
  25. """ Asserts that a new event constructed with `evdict` will look like
  26. `matchdict` when it is redacted. """
  27. def run_test(self, evdict, matchdict):
  28. self.assertEquals(prune_event(FrozenEvent(evdict)).get_dict(), matchdict)
  29. def test_minimal(self):
  30. self.run_test(
  31. {'type': 'A', 'event_id': '$test:domain'},
  32. {
  33. 'type': 'A',
  34. 'event_id': '$test:domain',
  35. 'content': {},
  36. 'signatures': {},
  37. 'unsigned': {},
  38. },
  39. )
  40. def test_basic_keys(self):
  41. self.run_test(
  42. {
  43. 'type': 'A',
  44. 'room_id': '!1:domain',
  45. 'sender': '@2:domain',
  46. 'event_id': '$3:domain',
  47. 'origin': 'domain',
  48. },
  49. {
  50. 'type': 'A',
  51. 'room_id': '!1:domain',
  52. 'sender': '@2:domain',
  53. 'event_id': '$3:domain',
  54. 'origin': 'domain',
  55. 'content': {},
  56. 'signatures': {},
  57. 'unsigned': {},
  58. },
  59. )
  60. def test_unsigned_age_ts(self):
  61. self.run_test(
  62. {'type': 'B', 'event_id': '$test:domain', 'unsigned': {'age_ts': 20}},
  63. {
  64. 'type': 'B',
  65. 'event_id': '$test:domain',
  66. 'content': {},
  67. 'signatures': {},
  68. 'unsigned': {'age_ts': 20},
  69. },
  70. )
  71. self.run_test(
  72. {
  73. 'type': 'B',
  74. 'event_id': '$test:domain',
  75. 'unsigned': {'other_key': 'here'},
  76. },
  77. {
  78. 'type': 'B',
  79. 'event_id': '$test:domain',
  80. 'content': {},
  81. 'signatures': {},
  82. 'unsigned': {},
  83. },
  84. )
  85. def test_content(self):
  86. self.run_test(
  87. {'type': 'C', 'event_id': '$test:domain', 'content': {'things': 'here'}},
  88. {
  89. 'type': 'C',
  90. 'event_id': '$test:domain',
  91. 'content': {},
  92. 'signatures': {},
  93. 'unsigned': {},
  94. },
  95. )
  96. self.run_test(
  97. {
  98. 'type': 'm.room.create',
  99. 'event_id': '$test:domain',
  100. 'content': {'creator': '@2:domain', 'other_field': 'here'},
  101. },
  102. {
  103. 'type': 'm.room.create',
  104. 'event_id': '$test:domain',
  105. 'content': {'creator': '@2:domain'},
  106. 'signatures': {},
  107. 'unsigned': {},
  108. },
  109. )
  110. class SerializeEventTestCase(unittest.TestCase):
  111. def serialize(self, ev, fields):
  112. return serialize_event(ev, 1479807801915, only_event_fields=fields)
  113. def test_event_fields_works_with_keys(self):
  114. self.assertEquals(
  115. self.serialize(
  116. MockEvent(sender="@alice:localhost", room_id="!foo:bar"), ["room_id"]
  117. ),
  118. {"room_id": "!foo:bar"},
  119. )
  120. def test_event_fields_works_with_nested_keys(self):
  121. self.assertEquals(
  122. self.serialize(
  123. MockEvent(
  124. sender="@alice:localhost",
  125. room_id="!foo:bar",
  126. content={"body": "A message"},
  127. ),
  128. ["content.body"],
  129. ),
  130. {"content": {"body": "A message"}},
  131. )
  132. def test_event_fields_works_with_dot_keys(self):
  133. self.assertEquals(
  134. self.serialize(
  135. MockEvent(
  136. sender="@alice:localhost",
  137. room_id="!foo:bar",
  138. content={"key.with.dots": {}},
  139. ),
  140. [r"content.key\.with\.dots"],
  141. ),
  142. {"content": {"key.with.dots": {}}},
  143. )
  144. def test_event_fields_works_with_nested_dot_keys(self):
  145. self.assertEquals(
  146. self.serialize(
  147. MockEvent(
  148. sender="@alice:localhost",
  149. room_id="!foo:bar",
  150. content={
  151. "not_me": 1,
  152. "nested.dot.key": {"leaf.key": 42, "not_me_either": 1},
  153. },
  154. ),
  155. [r"content.nested\.dot\.key.leaf\.key"],
  156. ),
  157. {"content": {"nested.dot.key": {"leaf.key": 42}}},
  158. )
  159. def test_event_fields_nops_with_unknown_keys(self):
  160. self.assertEquals(
  161. self.serialize(
  162. MockEvent(
  163. sender="@alice:localhost",
  164. room_id="!foo:bar",
  165. content={"foo": "bar"},
  166. ),
  167. ["content.foo", "content.notexists"],
  168. ),
  169. {"content": {"foo": "bar"}},
  170. )
  171. def test_event_fields_nops_with_non_dict_keys(self):
  172. self.assertEquals(
  173. self.serialize(
  174. MockEvent(
  175. sender="@alice:localhost",
  176. room_id="!foo:bar",
  177. content={"foo": ["I", "am", "an", "array"]},
  178. ),
  179. ["content.foo.am"],
  180. ),
  181. {},
  182. )
  183. def test_event_fields_nops_with_array_keys(self):
  184. self.assertEquals(
  185. self.serialize(
  186. MockEvent(
  187. sender="@alice:localhost",
  188. room_id="!foo:bar",
  189. content={"foo": ["I", "am", "an", "array"]},
  190. ),
  191. ["content.foo.1"],
  192. ),
  193. {},
  194. )
  195. def test_event_fields_all_fields_if_empty(self):
  196. self.assertEquals(
  197. self.serialize(
  198. MockEvent(
  199. type="foo",
  200. event_id="test",
  201. room_id="!foo:bar",
  202. content={"foo": "bar"},
  203. ),
  204. [],
  205. ),
  206. {
  207. "type": "foo",
  208. "event_id": "test",
  209. "room_id": "!foo:bar",
  210. "content": {"foo": "bar"},
  211. "unsigned": {},
  212. },
  213. )
  214. def test_event_fields_fail_if_fields_not_str(self):
  215. with self.assertRaises(TypeError):
  216. self.serialize(
  217. MockEvent(room_id="!foo:bar", content={"foo": "bar"}), ["room_id", 4]
  218. )