test_utils.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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(
  29. prune_event(FrozenEvent(evdict)).get_dict(),
  30. matchdict
  31. )
  32. def test_minimal(self):
  33. self.run_test(
  34. {
  35. 'type': 'A',
  36. 'event_id': '$test:domain',
  37. },
  38. {
  39. 'type': 'A',
  40. 'event_id': '$test:domain',
  41. 'content': {},
  42. 'signatures': {},
  43. 'unsigned': {},
  44. }
  45. )
  46. def test_basic_keys(self):
  47. self.run_test(
  48. {
  49. 'type': 'A',
  50. 'room_id': '!1:domain',
  51. 'sender': '@2:domain',
  52. 'event_id': '$3:domain',
  53. 'origin': 'domain',
  54. },
  55. {
  56. 'type': 'A',
  57. 'room_id': '!1:domain',
  58. 'sender': '@2:domain',
  59. 'event_id': '$3:domain',
  60. 'origin': 'domain',
  61. 'content': {},
  62. 'signatures': {},
  63. 'unsigned': {},
  64. }
  65. )
  66. def test_unsigned_age_ts(self):
  67. self.run_test(
  68. {
  69. 'type': 'B',
  70. 'event_id': '$test:domain',
  71. 'unsigned': {'age_ts': 20},
  72. },
  73. {
  74. 'type': 'B',
  75. 'event_id': '$test:domain',
  76. 'content': {},
  77. 'signatures': {},
  78. 'unsigned': {'age_ts': 20},
  79. }
  80. )
  81. self.run_test(
  82. {
  83. 'type': 'B',
  84. 'event_id': '$test:domain',
  85. 'unsigned': {'other_key': 'here'},
  86. },
  87. {
  88. 'type': 'B',
  89. 'event_id': '$test:domain',
  90. 'content': {},
  91. 'signatures': {},
  92. 'unsigned': {},
  93. }
  94. )
  95. def test_content(self):
  96. self.run_test(
  97. {
  98. 'type': 'C',
  99. 'event_id': '$test:domain',
  100. 'content': {'things': 'here'},
  101. },
  102. {
  103. 'type': 'C',
  104. 'event_id': '$test:domain',
  105. 'content': {},
  106. 'signatures': {},
  107. 'unsigned': {},
  108. }
  109. )
  110. self.run_test(
  111. {
  112. 'type': 'm.room.create',
  113. 'event_id': '$test:domain',
  114. 'content': {'creator': '@2:domain', 'other_field': 'here'},
  115. },
  116. {
  117. 'type': 'm.room.create',
  118. 'event_id': '$test:domain',
  119. 'content': {'creator': '@2:domain'},
  120. 'signatures': {},
  121. 'unsigned': {},
  122. }
  123. )
  124. class SerializeEventTestCase(unittest.TestCase):
  125. def serialize(self, ev, fields):
  126. return serialize_event(ev, 1479807801915, only_event_fields=fields)
  127. def test_event_fields_works_with_keys(self):
  128. self.assertEquals(
  129. self.serialize(
  130. MockEvent(
  131. sender="@alice:localhost",
  132. room_id="!foo:bar"
  133. ),
  134. ["room_id"]
  135. ),
  136. {
  137. "room_id": "!foo:bar",
  138. }
  139. )
  140. def test_event_fields_works_with_nested_keys(self):
  141. self.assertEquals(
  142. self.serialize(
  143. MockEvent(
  144. sender="@alice:localhost",
  145. room_id="!foo:bar",
  146. content={
  147. "body": "A message",
  148. },
  149. ),
  150. ["content.body"]
  151. ),
  152. {
  153. "content": {
  154. "body": "A message",
  155. }
  156. }
  157. )
  158. def test_event_fields_works_with_dot_keys(self):
  159. self.assertEquals(
  160. self.serialize(
  161. MockEvent(
  162. sender="@alice:localhost",
  163. room_id="!foo:bar",
  164. content={
  165. "key.with.dots": {},
  166. },
  167. ),
  168. ["content.key\.with\.dots"]
  169. ),
  170. {
  171. "content": {
  172. "key.with.dots": {},
  173. }
  174. }
  175. )
  176. def test_event_fields_works_with_nested_dot_keys(self):
  177. self.assertEquals(
  178. self.serialize(
  179. MockEvent(
  180. sender="@alice:localhost",
  181. room_id="!foo:bar",
  182. content={
  183. "not_me": 1,
  184. "nested.dot.key": {
  185. "leaf.key": 42,
  186. "not_me_either": 1,
  187. },
  188. },
  189. ),
  190. ["content.nested\.dot\.key.leaf\.key"]
  191. ),
  192. {
  193. "content": {
  194. "nested.dot.key": {
  195. "leaf.key": 42,
  196. },
  197. }
  198. }
  199. )
  200. def test_event_fields_nops_with_unknown_keys(self):
  201. self.assertEquals(
  202. self.serialize(
  203. MockEvent(
  204. sender="@alice:localhost",
  205. room_id="!foo:bar",
  206. content={
  207. "foo": "bar",
  208. },
  209. ),
  210. ["content.foo", "content.notexists"]
  211. ),
  212. {
  213. "content": {
  214. "foo": "bar",
  215. }
  216. }
  217. )
  218. def test_event_fields_nops_with_non_dict_keys(self):
  219. self.assertEquals(
  220. self.serialize(
  221. MockEvent(
  222. sender="@alice:localhost",
  223. room_id="!foo:bar",
  224. content={
  225. "foo": ["I", "am", "an", "array"],
  226. },
  227. ),
  228. ["content.foo.am"]
  229. ),
  230. {}
  231. )
  232. def test_event_fields_nops_with_array_keys(self):
  233. self.assertEquals(
  234. self.serialize(
  235. MockEvent(
  236. sender="@alice:localhost",
  237. room_id="!foo:bar",
  238. content={
  239. "foo": ["I", "am", "an", "array"],
  240. },
  241. ),
  242. ["content.foo.1"]
  243. ),
  244. {}
  245. )
  246. def test_event_fields_all_fields_if_empty(self):
  247. self.assertEquals(
  248. self.serialize(
  249. MockEvent(
  250. type="foo",
  251. event_id="test",
  252. room_id="!foo:bar",
  253. content={
  254. "foo": "bar",
  255. },
  256. ),
  257. []
  258. ),
  259. {
  260. "type": "foo",
  261. "event_id": "test",
  262. "room_id": "!foo:bar",
  263. "content": {
  264. "foo": "bar",
  265. },
  266. "unsigned": {}
  267. }
  268. )
  269. def test_event_fields_fail_if_fields_not_str(self):
  270. with self.assertRaises(TypeError):
  271. self.serialize(
  272. MockEvent(
  273. room_id="!foo:bar",
  274. content={
  275. "foo": "bar",
  276. },
  277. ),
  278. ["room_id", 4]
  279. )