test_events.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 tests import unittest
  17. # twisted imports
  18. from twisted.internet import defer
  19. import synapse.rest.client.v1.events
  20. import synapse.rest.client.v1.register
  21. import synapse.rest.client.v1.room
  22. from ....utils import MockHttpResource, setup_test_homeserver
  23. from .utils import RestTestCase
  24. from mock import Mock, NonCallableMock
  25. PATH_PREFIX = "/_matrix/client/api/v1"
  26. class EventStreamPaginationApiTestCase(unittest.TestCase):
  27. """ Tests event streaming query parameters and start/end keys used in the
  28. Pagination stream API. """
  29. user_id = "sid1"
  30. def setUp(self):
  31. # configure stream and inject items
  32. pass
  33. def tearDown(self):
  34. pass
  35. def TODO_test_long_poll(self):
  36. # stream from 'end' key, send (self+other) message, expect message.
  37. # stream from 'END', send (self+other) message, expect message.
  38. # stream from 'end' key, send (self+other) topic, expect topic.
  39. # stream from 'END', send (self+other) topic, expect topic.
  40. # stream from 'end' key, send (self+other) invite, expect invite.
  41. # stream from 'END', send (self+other) invite, expect invite.
  42. pass
  43. def TODO_test_stream_forward(self):
  44. # stream from START, expect injected items
  45. # stream from 'start' key, expect same content
  46. # stream from 'end' key, expect nothing
  47. # stream from 'END', expect nothing
  48. # The following is needed for cases where content is removed e.g. you
  49. # left a room, so the token you're streaming from is > the one that
  50. # would be returned naturally from START>END.
  51. # stream from very new token (higher than end key), expect same token
  52. # returned as end key
  53. pass
  54. def TODO_test_limits(self):
  55. # stream from a key, expect limit_num items
  56. # stream from START, expect limit_num items
  57. pass
  58. def TODO_test_range(self):
  59. # stream from key to key, expect X items
  60. # stream from key to END, expect X items
  61. # stream from START to key, expect X items
  62. # stream from START to END, expect all items
  63. pass
  64. def TODO_test_direction(self):
  65. # stream from END to START and fwds, expect newest first
  66. # stream from END to START and bwds, expect oldest first
  67. # stream from START to END and fwds, expect oldest first
  68. # stream from START to END and bwds, expect newest first
  69. pass
  70. class EventStreamPermissionsTestCase(RestTestCase):
  71. """ Tests event streaming (GET /events). """
  72. @defer.inlineCallbacks
  73. def setUp(self):
  74. self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
  75. hs = yield setup_test_homeserver(
  76. http_client=None,
  77. replication_layer=Mock(),
  78. ratelimiter=NonCallableMock(spec_set=[
  79. "send_message",
  80. ]),
  81. )
  82. self.ratelimiter = hs.get_ratelimiter()
  83. self.ratelimiter.send_message.return_value = (True, 0)
  84. hs.config.enable_registration_captcha = False
  85. hs.config.enable_registration = True
  86. hs.get_handlers().federation_handler = Mock()
  87. synapse.rest.client.v1.register.register_servlets(hs, self.mock_resource)
  88. synapse.rest.client.v1.events.register_servlets(hs, self.mock_resource)
  89. synapse.rest.client.v1.room.register_servlets(hs, self.mock_resource)
  90. # register an account
  91. self.user_id = "sid1"
  92. response = yield self.register(self.user_id)
  93. self.token = response["access_token"]
  94. self.user_id = response["user_id"]
  95. # register a 2nd account
  96. self.other_user = "other1"
  97. response = yield self.register(self.other_user)
  98. self.other_token = response["access_token"]
  99. self.other_user = response["user_id"]
  100. def tearDown(self):
  101. pass
  102. @defer.inlineCallbacks
  103. def test_stream_basic_permissions(self):
  104. # invalid token, expect 403
  105. (code, response) = yield self.mock_resource.trigger_get(
  106. "/events?access_token=%s" % ("invalid" + self.token, )
  107. )
  108. self.assertEquals(403, code, msg=str(response))
  109. # valid token, expect content
  110. (code, response) = yield self.mock_resource.trigger_get(
  111. "/events?access_token=%s&timeout=0" % (self.token,)
  112. )
  113. self.assertEquals(200, code, msg=str(response))
  114. self.assertTrue("chunk" in response)
  115. self.assertTrue("start" in response)
  116. self.assertTrue("end" in response)
  117. @defer.inlineCallbacks
  118. def test_stream_room_permissions(self):
  119. room_id = yield self.create_room_as(
  120. self.other_user,
  121. tok=self.other_token
  122. )
  123. yield self.send(room_id, tok=self.other_token)
  124. # invited to room (expect no content for room)
  125. yield self.invite(
  126. room_id,
  127. src=self.other_user,
  128. targ=self.user_id,
  129. tok=self.other_token
  130. )
  131. (code, response) = yield self.mock_resource.trigger_get(
  132. "/events?access_token=%s&timeout=0" % (self.token,)
  133. )
  134. self.assertEquals(200, code, msg=str(response))
  135. # We may get a presence event for ourselves down
  136. self.assertEquals(
  137. 0,
  138. len([
  139. c for c in response["chunk"]
  140. if not (
  141. c.get("type") == "m.presence"
  142. and c["content"].get("user_id") == self.user_id
  143. )
  144. ])
  145. )
  146. # joined room (expect all content for room)
  147. yield self.join(room=room_id, user=self.user_id, tok=self.token)
  148. # left to room (expect no content for room)
  149. def TODO_test_stream_items(self):
  150. # new user, no content
  151. # join room, expect 1 item (join)
  152. # send message, expect 2 items (join,send)
  153. # set topic, expect 3 items (join,send,topic)
  154. # someone else join room, expect 4 (join,send,topic,join)
  155. # someone else send message, expect 5 (join,send.topic,join,send)
  156. # someone else set topic, expect 6 (join,send,topic,join,send,topic)
  157. pass