1
0

test_event_auth.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector 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. import unittest
  16. from synapse import event_auth
  17. from synapse.api.errors import AuthError
  18. from synapse.events import FrozenEvent
  19. class EventAuthTestCase(unittest.TestCase):
  20. def test_random_users_cannot_send_state_before_first_pl(self):
  21. """
  22. Check that, before the first PL lands, the creator is the only user
  23. that can send a state event.
  24. """
  25. creator = "@creator:example.com"
  26. joiner = "@joiner:example.com"
  27. auth_events = {
  28. ("m.room.create", ""): _create_event(creator),
  29. ("m.room.member", creator): _join_event(creator),
  30. ("m.room.member", joiner): _join_event(joiner),
  31. }
  32. # creator should be able to send state
  33. event_auth.check(_random_state_event(creator), auth_events, do_sig_check=False)
  34. # joiner should not be able to send state
  35. self.assertRaises(
  36. AuthError,
  37. event_auth.check,
  38. _random_state_event(joiner),
  39. auth_events,
  40. do_sig_check=False,
  41. ),
  42. def test_state_default_level(self):
  43. """
  44. Check that users above the state_default level can send state and
  45. those below cannot
  46. """
  47. creator = "@creator:example.com"
  48. pleb = "@joiner:example.com"
  49. king = "@joiner2:example.com"
  50. auth_events = {
  51. ("m.room.create", ""): _create_event(creator),
  52. ("m.room.member", creator): _join_event(creator),
  53. ("m.room.power_levels", ""): _power_levels_event(
  54. creator, {"state_default": "30", "users": {pleb: "29", king: "30"}}
  55. ),
  56. ("m.room.member", pleb): _join_event(pleb),
  57. ("m.room.member", king): _join_event(king),
  58. }
  59. # pleb should not be able to send state
  60. self.assertRaises(
  61. AuthError,
  62. event_auth.check,
  63. _random_state_event(pleb),
  64. auth_events,
  65. do_sig_check=False,
  66. ),
  67. # king should be able to send state
  68. event_auth.check(_random_state_event(king), auth_events, do_sig_check=False)
  69. # helpers for making events
  70. TEST_ROOM_ID = "!test:room"
  71. def _create_event(user_id):
  72. return FrozenEvent(
  73. {
  74. "room_id": TEST_ROOM_ID,
  75. "event_id": _get_event_id(),
  76. "type": "m.room.create",
  77. "sender": user_id,
  78. "content": {"creator": user_id},
  79. }
  80. )
  81. def _join_event(user_id):
  82. return FrozenEvent(
  83. {
  84. "room_id": TEST_ROOM_ID,
  85. "event_id": _get_event_id(),
  86. "type": "m.room.member",
  87. "sender": user_id,
  88. "state_key": user_id,
  89. "content": {"membership": "join"},
  90. }
  91. )
  92. def _power_levels_event(sender, content):
  93. return FrozenEvent(
  94. {
  95. "room_id": TEST_ROOM_ID,
  96. "event_id": _get_event_id(),
  97. "type": "m.room.power_levels",
  98. "sender": sender,
  99. "state_key": "",
  100. "content": content,
  101. }
  102. )
  103. def _random_state_event(sender):
  104. return FrozenEvent(
  105. {
  106. "room_id": TEST_ROOM_ID,
  107. "event_id": _get_event_id(),
  108. "type": "test.state",
  109. "sender": sender,
  110. "state_key": "",
  111. "content": {"membership": "join"},
  112. }
  113. )
  114. event_count = 0
  115. def _get_event_id():
  116. global event_count
  117. c = event_count
  118. event_count += 1
  119. return "!%i:example.com" % (c,)