test_events.py 4.6 KB

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