test_typing.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 /rooms paths."""
  16. # twisted imports
  17. from twisted.internet import defer
  18. import synapse.rest.client.v1.room
  19. from synapse.types import UserID
  20. from ....utils import MockHttpResource, MockClock, setup_test_homeserver
  21. from .utils import RestTestCase
  22. from mock import Mock, NonCallableMock
  23. PATH_PREFIX = "/_matrix/client/api/v1"
  24. class RoomTypingTestCase(RestTestCase):
  25. """ Tests /rooms/$room_id/typing/$user_id REST API. """
  26. user_id = "@sid:red"
  27. user = UserID.from_string(user_id)
  28. @defer.inlineCallbacks
  29. def setUp(self):
  30. self.clock = MockClock()
  31. self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
  32. self.auth_user_id = self.user_id
  33. hs = yield setup_test_homeserver(
  34. "red",
  35. clock=self.clock,
  36. http_client=None,
  37. replication_layer=Mock(),
  38. ratelimiter=NonCallableMock(spec_set=[
  39. "send_message",
  40. ]),
  41. )
  42. self.hs = hs
  43. self.event_source = hs.get_event_sources().sources["typing"]
  44. self.ratelimiter = hs.get_ratelimiter()
  45. self.ratelimiter.send_message.return_value = (True, 0)
  46. hs.get_handlers().federation_handler = Mock()
  47. def get_user_by_access_token(token=None, allow_guest=False):
  48. return {
  49. "user": UserID.from_string(self.auth_user_id),
  50. "token_id": 1,
  51. "is_guest": False,
  52. }
  53. hs.get_v1auth().get_user_by_access_token = get_user_by_access_token
  54. def _insert_client_ip(*args, **kwargs):
  55. return defer.succeed(None)
  56. hs.get_datastore().insert_client_ip = _insert_client_ip
  57. def get_room_members(room_id):
  58. if room_id == self.room_id:
  59. return defer.succeed([self.user])
  60. else:
  61. return defer.succeed([])
  62. @defer.inlineCallbacks
  63. def fetch_room_distributions_into(
  64. room_id, localusers=None, remotedomains=None, ignore_user=None
  65. ):
  66. members = yield get_room_members(room_id)
  67. for member in members:
  68. if ignore_user is not None and member == ignore_user:
  69. continue
  70. if hs.is_mine(member):
  71. if localusers is not None:
  72. localusers.add(member)
  73. else:
  74. if remotedomains is not None:
  75. remotedomains.add(member.domain)
  76. hs.get_handlers().room_member_handler.fetch_room_distributions_into = (
  77. fetch_room_distributions_into
  78. )
  79. synapse.rest.client.v1.room.register_servlets(hs, self.mock_resource)
  80. self.room_id = yield self.create_room_as(self.user_id)
  81. # Need another user to make notifications actually work
  82. yield self.join(self.room_id, user="@jim:red")
  83. def tearDown(self):
  84. self.hs.get_handlers().typing_notification_handler.tearDown()
  85. @defer.inlineCallbacks
  86. def test_set_typing(self):
  87. (code, _) = yield self.mock_resource.trigger(
  88. "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  89. '{"typing": true, "timeout": 30000}'
  90. )
  91. self.assertEquals(200, code)
  92. self.assertEquals(self.event_source.get_current_key(), 1)
  93. events = yield self.event_source.get_new_events(
  94. from_key=0,
  95. room_ids=[self.room_id],
  96. )
  97. self.assertEquals(events[0], [{
  98. "type": "m.typing",
  99. "room_id": self.room_id,
  100. "content": {
  101. "user_ids": [self.user_id],
  102. }
  103. }])
  104. @defer.inlineCallbacks
  105. def test_set_not_typing(self):
  106. (code, _) = yield self.mock_resource.trigger(
  107. "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  108. '{"typing": false}'
  109. )
  110. self.assertEquals(200, code)
  111. @defer.inlineCallbacks
  112. def test_typing_timeout(self):
  113. (code, _) = yield self.mock_resource.trigger(
  114. "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  115. '{"typing": true, "timeout": 30000}'
  116. )
  117. self.assertEquals(200, code)
  118. self.assertEquals(self.event_source.get_current_key(), 1)
  119. self.clock.advance_time(31)
  120. self.assertEquals(self.event_source.get_current_key(), 2)
  121. (code, _) = yield self.mock_resource.trigger(
  122. "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  123. '{"typing": true, "timeout": 30000}'
  124. )
  125. self.assertEquals(200, code)
  126. self.assertEquals(self.event_source.get_current_key(), 3)