test_utils.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the 'License');
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an 'AS IS' BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.api.room_versions import RoomVersions
  15. from synapse.events import make_event_from_dict
  16. from synapse.events.utils import (
  17. copy_power_levels_contents,
  18. prune_event,
  19. serialize_event,
  20. )
  21. from synapse.util.frozenutils import freeze
  22. from tests import unittest
  23. def MockEvent(**kwargs):
  24. if "event_id" not in kwargs:
  25. kwargs["event_id"] = "fake_event_id"
  26. if "type" not in kwargs:
  27. kwargs["type"] = "fake_type"
  28. return make_event_from_dict(kwargs)
  29. class PruneEventTestCase(unittest.TestCase):
  30. def run_test(self, evdict, matchdict, **kwargs):
  31. """
  32. Asserts that a new event constructed with `evdict` will look like
  33. `matchdict` when it is redacted.
  34. Args:
  35. evdict: The dictionary to build the event from.
  36. matchdict: The expected resulting dictionary.
  37. kwargs: Additional keyword arguments used to create the event.
  38. """
  39. self.assertEqual(
  40. prune_event(make_event_from_dict(evdict, **kwargs)).get_dict(), matchdict
  41. )
  42. def test_minimal(self):
  43. self.run_test(
  44. {"type": "A", "event_id": "$test:domain"},
  45. {
  46. "type": "A",
  47. "event_id": "$test:domain",
  48. "content": {},
  49. "signatures": {},
  50. "unsigned": {},
  51. },
  52. )
  53. def test_basic_keys(self):
  54. """Ensure that the keys that should be untouched are kept."""
  55. # Note that some of the values below don't really make sense, but the
  56. # pruning of events doesn't worry about the values of any fields (with
  57. # the exception of the content field).
  58. self.run_test(
  59. {
  60. "event_id": "$3:domain",
  61. "type": "A",
  62. "room_id": "!1:domain",
  63. "sender": "@2:domain",
  64. "state_key": "B",
  65. "content": {"other_key": "foo"},
  66. "hashes": "hashes",
  67. "signatures": {"domain": {"algo:1": "sigs"}},
  68. "depth": 4,
  69. "prev_events": "prev_events",
  70. "prev_state": "prev_state",
  71. "auth_events": "auth_events",
  72. "origin": "domain",
  73. "origin_server_ts": 1234,
  74. "membership": "join",
  75. # Also include a key that should be removed.
  76. "other_key": "foo",
  77. },
  78. {
  79. "event_id": "$3:domain",
  80. "type": "A",
  81. "room_id": "!1:domain",
  82. "sender": "@2:domain",
  83. "state_key": "B",
  84. "hashes": "hashes",
  85. "depth": 4,
  86. "prev_events": "prev_events",
  87. "prev_state": "prev_state",
  88. "auth_events": "auth_events",
  89. "origin": "domain",
  90. "origin_server_ts": 1234,
  91. "membership": "join",
  92. "content": {},
  93. "signatures": {"domain": {"algo:1": "sigs"}},
  94. "unsigned": {},
  95. },
  96. )
  97. # As of MSC2176 we now redact the membership and prev_states keys.
  98. self.run_test(
  99. {"type": "A", "prev_state": "prev_state", "membership": "join"},
  100. {"type": "A", "content": {}, "signatures": {}, "unsigned": {}},
  101. room_version=RoomVersions.MSC2176,
  102. )
  103. def test_unsigned(self):
  104. """Ensure that unsigned properties get stripped (except age_ts and replaces_state)."""
  105. self.run_test(
  106. {
  107. "type": "B",
  108. "event_id": "$test:domain",
  109. "unsigned": {
  110. "age_ts": 20,
  111. "replaces_state": "$test2:domain",
  112. "other_key": "foo",
  113. },
  114. },
  115. {
  116. "type": "B",
  117. "event_id": "$test:domain",
  118. "content": {},
  119. "signatures": {},
  120. "unsigned": {"age_ts": 20, "replaces_state": "$test2:domain"},
  121. },
  122. )
  123. def test_content(self):
  124. """The content dictionary should be stripped in most cases."""
  125. self.run_test(
  126. {"type": "C", "event_id": "$test:domain", "content": {"things": "here"}},
  127. {
  128. "type": "C",
  129. "event_id": "$test:domain",
  130. "content": {},
  131. "signatures": {},
  132. "unsigned": {},
  133. },
  134. )
  135. # Some events keep a single content key/value.
  136. EVENT_KEEP_CONTENT_KEYS = [
  137. ("member", "membership", "join"),
  138. ("join_rules", "join_rule", "invite"),
  139. ("history_visibility", "history_visibility", "shared"),
  140. ]
  141. for event_type, key, value in EVENT_KEEP_CONTENT_KEYS:
  142. self.run_test(
  143. {
  144. "type": "m.room." + event_type,
  145. "event_id": "$test:domain",
  146. "content": {key: value, "other_key": "foo"},
  147. },
  148. {
  149. "type": "m.room." + event_type,
  150. "event_id": "$test:domain",
  151. "content": {key: value},
  152. "signatures": {},
  153. "unsigned": {},
  154. },
  155. )
  156. def test_create(self):
  157. """Create events are partially redacted until MSC2176."""
  158. self.run_test(
  159. {
  160. "type": "m.room.create",
  161. "event_id": "$test:domain",
  162. "content": {"creator": "@2:domain", "other_key": "foo"},
  163. },
  164. {
  165. "type": "m.room.create",
  166. "event_id": "$test:domain",
  167. "content": {"creator": "@2:domain"},
  168. "signatures": {},
  169. "unsigned": {},
  170. },
  171. )
  172. # After MSC2176, create events get nothing redacted.
  173. self.run_test(
  174. {"type": "m.room.create", "content": {"not_a_real_key": True}},
  175. {
  176. "type": "m.room.create",
  177. "content": {"not_a_real_key": True},
  178. "signatures": {},
  179. "unsigned": {},
  180. },
  181. room_version=RoomVersions.MSC2176,
  182. )
  183. def test_power_levels(self):
  184. """Power level events keep a variety of content keys."""
  185. self.run_test(
  186. {
  187. "type": "m.room.power_levels",
  188. "event_id": "$test:domain",
  189. "content": {
  190. "ban": 1,
  191. "events": {"m.room.name": 100},
  192. "events_default": 2,
  193. "invite": 3,
  194. "kick": 4,
  195. "redact": 5,
  196. "state_default": 6,
  197. "users": {"@admin:domain": 100},
  198. "users_default": 7,
  199. "other_key": 8,
  200. },
  201. },
  202. {
  203. "type": "m.room.power_levels",
  204. "event_id": "$test:domain",
  205. "content": {
  206. "ban": 1,
  207. "events": {"m.room.name": 100},
  208. "events_default": 2,
  209. # Note that invite is not here.
  210. "kick": 4,
  211. "redact": 5,
  212. "state_default": 6,
  213. "users": {"@admin:domain": 100},
  214. "users_default": 7,
  215. },
  216. "signatures": {},
  217. "unsigned": {},
  218. },
  219. )
  220. # After MSC2176, power levels events keep the invite key.
  221. self.run_test(
  222. {"type": "m.room.power_levels", "content": {"invite": 75}},
  223. {
  224. "type": "m.room.power_levels",
  225. "content": {"invite": 75},
  226. "signatures": {},
  227. "unsigned": {},
  228. },
  229. room_version=RoomVersions.MSC2176,
  230. )
  231. def test_alias_event(self):
  232. """Alias events have special behavior up through room version 6."""
  233. self.run_test(
  234. {
  235. "type": "m.room.aliases",
  236. "event_id": "$test:domain",
  237. "content": {"aliases": ["test"]},
  238. },
  239. {
  240. "type": "m.room.aliases",
  241. "event_id": "$test:domain",
  242. "content": {"aliases": ["test"]},
  243. "signatures": {},
  244. "unsigned": {},
  245. },
  246. )
  247. # After MSC2432, alias events have no special behavior.
  248. self.run_test(
  249. {"type": "m.room.aliases", "content": {"aliases": ["test"]}},
  250. {
  251. "type": "m.room.aliases",
  252. "content": {},
  253. "signatures": {},
  254. "unsigned": {},
  255. },
  256. room_version=RoomVersions.V6,
  257. )
  258. def test_redacts(self):
  259. """Redaction events have no special behaviour until MSC2174/MSC2176."""
  260. self.run_test(
  261. {"type": "m.room.redaction", "content": {"redacts": "$test2:domain"}},
  262. {
  263. "type": "m.room.redaction",
  264. "content": {},
  265. "signatures": {},
  266. "unsigned": {},
  267. },
  268. room_version=RoomVersions.V6,
  269. )
  270. # After MSC2174, redaction events keep the redacts content key.
  271. self.run_test(
  272. {"type": "m.room.redaction", "content": {"redacts": "$test2:domain"}},
  273. {
  274. "type": "m.room.redaction",
  275. "content": {"redacts": "$test2:domain"},
  276. "signatures": {},
  277. "unsigned": {},
  278. },
  279. room_version=RoomVersions.MSC2176,
  280. )
  281. class SerializeEventTestCase(unittest.TestCase):
  282. def serialize(self, ev, fields):
  283. return serialize_event(ev, 1479807801915, only_event_fields=fields)
  284. def test_event_fields_works_with_keys(self):
  285. self.assertEquals(
  286. self.serialize(
  287. MockEvent(sender="@alice:localhost", room_id="!foo:bar"), ["room_id"]
  288. ),
  289. {"room_id": "!foo:bar"},
  290. )
  291. def test_event_fields_works_with_nested_keys(self):
  292. self.assertEquals(
  293. self.serialize(
  294. MockEvent(
  295. sender="@alice:localhost",
  296. room_id="!foo:bar",
  297. content={"body": "A message"},
  298. ),
  299. ["content.body"],
  300. ),
  301. {"content": {"body": "A message"}},
  302. )
  303. def test_event_fields_works_with_dot_keys(self):
  304. self.assertEquals(
  305. self.serialize(
  306. MockEvent(
  307. sender="@alice:localhost",
  308. room_id="!foo:bar",
  309. content={"key.with.dots": {}},
  310. ),
  311. [r"content.key\.with\.dots"],
  312. ),
  313. {"content": {"key.with.dots": {}}},
  314. )
  315. def test_event_fields_works_with_nested_dot_keys(self):
  316. self.assertEquals(
  317. self.serialize(
  318. MockEvent(
  319. sender="@alice:localhost",
  320. room_id="!foo:bar",
  321. content={
  322. "not_me": 1,
  323. "nested.dot.key": {"leaf.key": 42, "not_me_either": 1},
  324. },
  325. ),
  326. [r"content.nested\.dot\.key.leaf\.key"],
  327. ),
  328. {"content": {"nested.dot.key": {"leaf.key": 42}}},
  329. )
  330. def test_event_fields_nops_with_unknown_keys(self):
  331. self.assertEquals(
  332. self.serialize(
  333. MockEvent(
  334. sender="@alice:localhost",
  335. room_id="!foo:bar",
  336. content={"foo": "bar"},
  337. ),
  338. ["content.foo", "content.notexists"],
  339. ),
  340. {"content": {"foo": "bar"}},
  341. )
  342. def test_event_fields_nops_with_non_dict_keys(self):
  343. self.assertEquals(
  344. self.serialize(
  345. MockEvent(
  346. sender="@alice:localhost",
  347. room_id="!foo:bar",
  348. content={"foo": ["I", "am", "an", "array"]},
  349. ),
  350. ["content.foo.am"],
  351. ),
  352. {},
  353. )
  354. def test_event_fields_nops_with_array_keys(self):
  355. self.assertEquals(
  356. self.serialize(
  357. MockEvent(
  358. sender="@alice:localhost",
  359. room_id="!foo:bar",
  360. content={"foo": ["I", "am", "an", "array"]},
  361. ),
  362. ["content.foo.1"],
  363. ),
  364. {},
  365. )
  366. def test_event_fields_all_fields_if_empty(self):
  367. self.assertEquals(
  368. self.serialize(
  369. MockEvent(
  370. type="foo",
  371. event_id="test",
  372. room_id="!foo:bar",
  373. content={"foo": "bar"},
  374. ),
  375. [],
  376. ),
  377. {
  378. "type": "foo",
  379. "event_id": "test",
  380. "room_id": "!foo:bar",
  381. "content": {"foo": "bar"},
  382. "unsigned": {},
  383. },
  384. )
  385. def test_event_fields_fail_if_fields_not_str(self):
  386. with self.assertRaises(TypeError):
  387. self.serialize(
  388. MockEvent(room_id="!foo:bar", content={"foo": "bar"}), ["room_id", 4]
  389. )
  390. class CopyPowerLevelsContentTestCase(unittest.TestCase):
  391. def setUp(self) -> None:
  392. self.test_content = {
  393. "ban": 50,
  394. "events": {"m.room.name": 100, "m.room.power_levels": 100},
  395. "events_default": 0,
  396. "invite": 50,
  397. "kick": 50,
  398. "notifications": {"room": 20},
  399. "redact": 50,
  400. "state_default": 50,
  401. "users": {"@example:localhost": 100},
  402. "users_default": 0,
  403. }
  404. def _test(self, input):
  405. a = copy_power_levels_contents(input)
  406. self.assertEqual(a["ban"], 50)
  407. self.assertEqual(a["events"]["m.room.name"], 100)
  408. # make sure that changing the copy changes the copy and not the orig
  409. a["ban"] = 10
  410. a["events"]["m.room.power_levels"] = 20
  411. self.assertEqual(input["ban"], 50)
  412. self.assertEqual(input["events"]["m.room.power_levels"], 100)
  413. def test_unfrozen(self):
  414. self._test(self.test_content)
  415. def test_frozen(self):
  416. input = freeze(self.test_content)
  417. self._test(input)