1
0

test_utils.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. def test_join_rules(self):
  282. """Join rules events have changed behavior starting with MSC3083."""
  283. self.run_test(
  284. {
  285. "type": "m.room.join_rules",
  286. "event_id": "$test:domain",
  287. "content": {
  288. "join_rule": "invite",
  289. "allow": [],
  290. "other_key": "stripped",
  291. },
  292. },
  293. {
  294. "type": "m.room.join_rules",
  295. "event_id": "$test:domain",
  296. "content": {"join_rule": "invite"},
  297. "signatures": {},
  298. "unsigned": {},
  299. },
  300. )
  301. # After MSC3083, the allow key is protected from redaction.
  302. self.run_test(
  303. {
  304. "type": "m.room.join_rules",
  305. "content": {
  306. "join_rule": "invite",
  307. "allow": [],
  308. "other_key": "stripped",
  309. },
  310. },
  311. {
  312. "type": "m.room.join_rules",
  313. "content": {
  314. "join_rule": "invite",
  315. "allow": [],
  316. },
  317. "signatures": {},
  318. "unsigned": {},
  319. },
  320. room_version=RoomVersions.V8,
  321. )
  322. def test_member(self):
  323. """Member events have changed behavior starting with MSC3375."""
  324. self.run_test(
  325. {
  326. "type": "m.room.member",
  327. "event_id": "$test:domain",
  328. "content": {
  329. "membership": "join",
  330. "join_authorised_via_users_server": "@user:domain",
  331. "other_key": "stripped",
  332. },
  333. },
  334. {
  335. "type": "m.room.member",
  336. "event_id": "$test:domain",
  337. "content": {"membership": "join"},
  338. "signatures": {},
  339. "unsigned": {},
  340. },
  341. )
  342. # After MSC3375, the join_authorised_via_users_server key is protected
  343. # from redaction.
  344. self.run_test(
  345. {
  346. "type": "m.room.member",
  347. "content": {
  348. "membership": "join",
  349. "join_authorised_via_users_server": "@user:domain",
  350. "other_key": "stripped",
  351. },
  352. },
  353. {
  354. "type": "m.room.member",
  355. "content": {
  356. "membership": "join",
  357. "join_authorised_via_users_server": "@user:domain",
  358. },
  359. "signatures": {},
  360. "unsigned": {},
  361. },
  362. room_version=RoomVersions.V9,
  363. )
  364. class SerializeEventTestCase(unittest.TestCase):
  365. def serialize(self, ev, fields):
  366. return serialize_event(ev, 1479807801915, only_event_fields=fields)
  367. def test_event_fields_works_with_keys(self):
  368. self.assertEquals(
  369. self.serialize(
  370. MockEvent(sender="@alice:localhost", room_id="!foo:bar"), ["room_id"]
  371. ),
  372. {"room_id": "!foo:bar"},
  373. )
  374. def test_event_fields_works_with_nested_keys(self):
  375. self.assertEquals(
  376. self.serialize(
  377. MockEvent(
  378. sender="@alice:localhost",
  379. room_id="!foo:bar",
  380. content={"body": "A message"},
  381. ),
  382. ["content.body"],
  383. ),
  384. {"content": {"body": "A message"}},
  385. )
  386. def test_event_fields_works_with_dot_keys(self):
  387. self.assertEquals(
  388. self.serialize(
  389. MockEvent(
  390. sender="@alice:localhost",
  391. room_id="!foo:bar",
  392. content={"key.with.dots": {}},
  393. ),
  394. [r"content.key\.with\.dots"],
  395. ),
  396. {"content": {"key.with.dots": {}}},
  397. )
  398. def test_event_fields_works_with_nested_dot_keys(self):
  399. self.assertEquals(
  400. self.serialize(
  401. MockEvent(
  402. sender="@alice:localhost",
  403. room_id="!foo:bar",
  404. content={
  405. "not_me": 1,
  406. "nested.dot.key": {"leaf.key": 42, "not_me_either": 1},
  407. },
  408. ),
  409. [r"content.nested\.dot\.key.leaf\.key"],
  410. ),
  411. {"content": {"nested.dot.key": {"leaf.key": 42}}},
  412. )
  413. def test_event_fields_nops_with_unknown_keys(self):
  414. self.assertEquals(
  415. self.serialize(
  416. MockEvent(
  417. sender="@alice:localhost",
  418. room_id="!foo:bar",
  419. content={"foo": "bar"},
  420. ),
  421. ["content.foo", "content.notexists"],
  422. ),
  423. {"content": {"foo": "bar"}},
  424. )
  425. def test_event_fields_nops_with_non_dict_keys(self):
  426. self.assertEquals(
  427. self.serialize(
  428. MockEvent(
  429. sender="@alice:localhost",
  430. room_id="!foo:bar",
  431. content={"foo": ["I", "am", "an", "array"]},
  432. ),
  433. ["content.foo.am"],
  434. ),
  435. {},
  436. )
  437. def test_event_fields_nops_with_array_keys(self):
  438. self.assertEquals(
  439. self.serialize(
  440. MockEvent(
  441. sender="@alice:localhost",
  442. room_id="!foo:bar",
  443. content={"foo": ["I", "am", "an", "array"]},
  444. ),
  445. ["content.foo.1"],
  446. ),
  447. {},
  448. )
  449. def test_event_fields_all_fields_if_empty(self):
  450. self.assertEquals(
  451. self.serialize(
  452. MockEvent(
  453. type="foo",
  454. event_id="test",
  455. room_id="!foo:bar",
  456. content={"foo": "bar"},
  457. ),
  458. [],
  459. ),
  460. {
  461. "type": "foo",
  462. "event_id": "test",
  463. "room_id": "!foo:bar",
  464. "content": {"foo": "bar"},
  465. "unsigned": {},
  466. },
  467. )
  468. def test_event_fields_fail_if_fields_not_str(self):
  469. with self.assertRaises(TypeError):
  470. self.serialize(
  471. MockEvent(room_id="!foo:bar", content={"foo": "bar"}), ["room_id", 4]
  472. )
  473. class CopyPowerLevelsContentTestCase(unittest.TestCase):
  474. def setUp(self) -> None:
  475. self.test_content = {
  476. "ban": 50,
  477. "events": {"m.room.name": 100, "m.room.power_levels": 100},
  478. "events_default": 0,
  479. "invite": 50,
  480. "kick": 50,
  481. "notifications": {"room": 20},
  482. "redact": 50,
  483. "state_default": 50,
  484. "users": {"@example:localhost": 100},
  485. "users_default": 0,
  486. }
  487. def _test(self, input):
  488. a = copy_power_levels_contents(input)
  489. self.assertEqual(a["ban"], 50)
  490. self.assertEqual(a["events"]["m.room.name"], 100)
  491. # make sure that changing the copy changes the copy and not the orig
  492. a["ban"] = 10
  493. a["events"]["m.room.power_levels"] = 20
  494. self.assertEqual(input["ban"], 50)
  495. self.assertEqual(input["events"]["m.room.power_levels"], 100)
  496. def test_unfrozen(self):
  497. self._test(self.test_content)
  498. def test_frozen(self):
  499. input = freeze(self.test_content)
  500. self._test(input)