test_utils.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 .. import unittest
  16. from synapse.events import FrozenEvent
  17. from synapse.events.utils import prune_event
  18. class PruneEventTestCase(unittest.TestCase):
  19. """ Asserts that a new event constructed with `evdict` will look like
  20. `matchdict` when it is redacted. """
  21. def run_test(self, evdict, matchdict):
  22. self.assertEquals(
  23. prune_event(FrozenEvent(evdict)).get_dict(),
  24. matchdict
  25. )
  26. def test_minimal(self):
  27. self.run_test(
  28. {'type': 'A'},
  29. {
  30. 'type': 'A',
  31. 'content': {},
  32. 'signatures': {},
  33. 'unsigned': {},
  34. }
  35. )
  36. def test_basic_keys(self):
  37. self.run_test(
  38. {
  39. 'type': 'A',
  40. 'room_id': '!1:domain',
  41. 'sender': '@2:domain',
  42. 'event_id': '$3:domain',
  43. 'origin': 'domain',
  44. },
  45. {
  46. 'type': 'A',
  47. 'room_id': '!1:domain',
  48. 'sender': '@2:domain',
  49. 'event_id': '$3:domain',
  50. 'origin': 'domain',
  51. 'content': {},
  52. 'signatures': {},
  53. 'unsigned': {},
  54. }
  55. )
  56. def test_unsigned_age_ts(self):
  57. self.run_test(
  58. {
  59. 'type': 'B',
  60. 'unsigned': {'age_ts': 20},
  61. },
  62. {
  63. 'type': 'B',
  64. 'content': {},
  65. 'signatures': {},
  66. 'unsigned': {'age_ts': 20},
  67. }
  68. )
  69. self.run_test(
  70. {
  71. 'type': 'B',
  72. 'unsigned': {'other_key': 'here'},
  73. },
  74. {
  75. 'type': 'B',
  76. 'content': {},
  77. 'signatures': {},
  78. 'unsigned': {},
  79. }
  80. )
  81. def test_content(self):
  82. self.run_test(
  83. {
  84. 'type': 'C',
  85. 'content': {'things': 'here'},
  86. },
  87. {
  88. 'type': 'C',
  89. 'content': {},
  90. 'signatures': {},
  91. 'unsigned': {},
  92. }
  93. )
  94. self.run_test(
  95. {
  96. 'type': 'm.room.create',
  97. 'content': {'creator': '@2:domain', 'other_field': 'here'},
  98. },
  99. {
  100. 'type': 'm.room.create',
  101. 'content': {'creator': '@2:domain'},
  102. 'signatures': {},
  103. 'unsigned': {},
  104. }
  105. )