test_events.py 5.5 KB

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