1
0

test_events.py 5.1 KB

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