test_events.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Copyright 2014-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. """ Tests REST events for /events paths."""
  15. from unittest.mock import Mock
  16. from twisted.test.proto_helpers import MemoryReactor
  17. import synapse.rest.admin
  18. from synapse.rest.client import events, login, room
  19. from synapse.server import HomeServer
  20. from synapse.util import Clock
  21. from tests import unittest
  22. class EventStreamPermissionsTestCase(unittest.HomeserverTestCase):
  23. """Tests event streaming (GET /events)."""
  24. servlets = [
  25. events.register_servlets,
  26. room.register_servlets,
  27. synapse.rest.admin.register_servlets_for_client_rest_resource,
  28. login.register_servlets,
  29. ]
  30. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  31. config = self.default_config()
  32. config["enable_registration_captcha"] = False
  33. config["enable_registration"] = True
  34. config["auto_join_rooms"] = []
  35. hs = self.setup_test_homeserver(config=config)
  36. hs.get_federation_handler = Mock() # type: ignore[assignment]
  37. return hs
  38. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  39. # register an account
  40. self.user_id = self.register_user("sid1", "pass")
  41. self.token = self.login(self.user_id, "pass")
  42. # register a 2nd account
  43. self.other_user = self.register_user("other2", "pass")
  44. self.other_token = self.login(self.other_user, "pass")
  45. def test_stream_basic_permissions(self) -> None:
  46. # invalid token, expect 401
  47. # note: this is in violation of the original v1 spec, which expected
  48. # 403. However, since the v1 spec no longer exists and the v1
  49. # implementation is now part of the r0 implementation, the newer
  50. # behaviour is used instead to be consistent with the r0 spec.
  51. # see issue #2602
  52. channel = self.make_request(
  53. "GET", "/events?access_token=%s" % ("invalid" + self.token,)
  54. )
  55. self.assertEqual(channel.code, 401, msg=channel.result)
  56. # valid token, expect content
  57. channel = self.make_request(
  58. "GET", "/events?access_token=%s&timeout=0" % (self.token,)
  59. )
  60. self.assertEqual(channel.code, 200, msg=channel.result)
  61. self.assertTrue("chunk" in channel.json_body)
  62. self.assertTrue("start" in channel.json_body)
  63. self.assertTrue("end" in channel.json_body)
  64. def test_stream_room_permissions(self) -> None:
  65. room_id = self.helper.create_room_as(self.other_user, tok=self.other_token)
  66. self.helper.send(room_id, tok=self.other_token)
  67. # invited to room (expect no content for room)
  68. self.helper.invite(
  69. room_id, src=self.other_user, targ=self.user_id, tok=self.other_token
  70. )
  71. # valid token, expect content
  72. channel = self.make_request(
  73. "GET", "/events?access_token=%s&timeout=0" % (self.token,)
  74. )
  75. self.assertEqual(channel.code, 200, msg=channel.result)
  76. # We may get a presence event for ourselves down
  77. self.assertEqual(
  78. 0,
  79. len(
  80. [
  81. c
  82. for c in channel.json_body["chunk"]
  83. if not (
  84. c.get("type") == "m.presence"
  85. and c["content"].get("user_id") == self.user_id
  86. )
  87. ]
  88. ),
  89. )
  90. # joined room (expect all content for room)
  91. self.helper.join(room=room_id, user=self.user_id, tok=self.token)
  92. # left to room (expect no content for room)
  93. def TODO_test_stream_items(self) -> None:
  94. # new user, no content
  95. # join room, expect 1 item (join)
  96. # send message, expect 2 items (join,send)
  97. # set topic, expect 3 items (join,send,topic)
  98. # someone else join room, expect 4 (join,send,topic,join)
  99. # someone else send message, expect 5 (join,send.topic,join,send)
  100. # someone else set topic, expect 6 (join,send,topic,join,send,topic)
  101. pass
  102. class GetEventsTestCase(unittest.HomeserverTestCase):
  103. servlets = [
  104. events.register_servlets,
  105. room.register_servlets,
  106. synapse.rest.admin.register_servlets_for_client_rest_resource,
  107. login.register_servlets,
  108. ]
  109. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  110. # register an account
  111. self.user_id = self.register_user("sid1", "pass")
  112. self.token = self.login(self.user_id, "pass")
  113. self.room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  114. def test_get_event_via_events(self) -> None:
  115. resp = self.helper.send(self.room_id, tok=self.token)
  116. event_id = resp["event_id"]
  117. channel = self.make_request(
  118. "GET",
  119. "/events/" + event_id,
  120. access_token=self.token,
  121. )
  122. self.assertEqual(channel.code, 200, msg=channel.result)