test_typing.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. federation_client=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_auth().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_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. @defer.inlineCallbacks
  84. def test_set_typing(self):
  85. (code, _) = yield self.mock_resource.trigger(
  86. "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  87. '{"typing": true, "timeout": 30000}'
  88. )
  89. self.assertEquals(200, code)
  90. self.assertEquals(self.event_source.get_current_key(), 1)
  91. events = yield self.event_source.get_new_events(
  92. from_key=0,
  93. room_ids=[self.room_id],
  94. )
  95. self.assertEquals(events[0], [{
  96. "type": "m.typing",
  97. "room_id": self.room_id,
  98. "content": {
  99. "user_ids": [self.user_id],
  100. }
  101. }])
  102. @defer.inlineCallbacks
  103. def test_set_not_typing(self):
  104. (code, _) = yield self.mock_resource.trigger(
  105. "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  106. '{"typing": false}'
  107. )
  108. self.assertEquals(200, code)
  109. @defer.inlineCallbacks
  110. def test_typing_timeout(self):
  111. (code, _) = yield self.mock_resource.trigger(
  112. "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  113. '{"typing": true, "timeout": 30000}'
  114. )
  115. self.assertEquals(200, code)
  116. self.assertEquals(self.event_source.get_current_key(), 1)
  117. self.clock.advance_time(36)
  118. self.assertEquals(self.event_source.get_current_key(), 2)
  119. (code, _) = yield self.mock_resource.trigger(
  120. "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  121. '{"typing": true, "timeout": 30000}'
  122. )
  123. self.assertEquals(200, code)
  124. self.assertEquals(self.event_source.get_current_key(), 3)