test_typing.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Tests REST events for /rooms paths."""
  17. from mock import Mock, NonCallableMock
  18. from twisted.internet import defer
  19. from synapse.rest.client.v1 import room
  20. from synapse.types import UserID
  21. from tests import unittest
  22. PATH_PREFIX = "/_matrix/client/api/v1"
  23. class RoomTypingTestCase(unittest.HomeserverTestCase):
  24. """ Tests /rooms/$room_id/typing/$user_id REST API. """
  25. user_id = "@sid:red"
  26. user = UserID.from_string(user_id)
  27. servlets = [room.register_servlets]
  28. def make_homeserver(self, reactor, clock):
  29. hs = self.setup_test_homeserver(
  30. "red",
  31. http_client=None,
  32. federation_client=Mock(),
  33. ratelimiter=NonCallableMock(spec_set=["can_do_action"]),
  34. )
  35. self.event_source = hs.get_event_sources().sources["typing"]
  36. self.ratelimiter = hs.get_ratelimiter()
  37. self.ratelimiter.can_do_action.return_value = (True, 0)
  38. hs.get_handlers().federation_handler = Mock()
  39. def get_user_by_access_token(token=None, allow_guest=False):
  40. return {
  41. "user": UserID.from_string(self.auth_user_id),
  42. "token_id": 1,
  43. "is_guest": False,
  44. }
  45. hs.get_auth().get_user_by_access_token = get_user_by_access_token
  46. def _insert_client_ip(*args, **kwargs):
  47. return defer.succeed(None)
  48. hs.get_datastore().insert_client_ip = _insert_client_ip
  49. def get_room_members(room_id):
  50. if room_id == self.room_id:
  51. return defer.succeed([self.user])
  52. else:
  53. return defer.succeed([])
  54. @defer.inlineCallbacks
  55. def fetch_room_distributions_into(
  56. room_id, localusers=None, remotedomains=None, ignore_user=None
  57. ):
  58. members = yield get_room_members(room_id)
  59. for member in members:
  60. if ignore_user is not None and member == ignore_user:
  61. continue
  62. if hs.is_mine(member):
  63. if localusers is not None:
  64. localusers.add(member)
  65. else:
  66. if remotedomains is not None:
  67. remotedomains.add(member.domain)
  68. hs.get_room_member_handler().fetch_room_distributions_into = (
  69. fetch_room_distributions_into
  70. )
  71. return hs
  72. def prepare(self, reactor, clock, hs):
  73. self.room_id = self.helper.create_room_as(self.user_id)
  74. # Need another user to make notifications actually work
  75. self.helper.join(self.room_id, user="@jim:red")
  76. def test_set_typing(self):
  77. request, channel = self.make_request(
  78. "PUT",
  79. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  80. b'{"typing": true, "timeout": 30000}',
  81. )
  82. self.render(request)
  83. self.assertEquals(200, channel.code)
  84. self.assertEquals(self.event_source.get_current_key(), 1)
  85. events = self.get_success(
  86. self.event_source.get_new_events(from_key=0, room_ids=[self.room_id])
  87. )
  88. self.assertEquals(
  89. events[0],
  90. [
  91. {
  92. "type": "m.typing",
  93. "room_id": self.room_id,
  94. "content": {"user_ids": [self.user_id]},
  95. }
  96. ],
  97. )
  98. def test_set_not_typing(self):
  99. request, channel = self.make_request(
  100. "PUT",
  101. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  102. b'{"typing": false}',
  103. )
  104. self.render(request)
  105. self.assertEquals(200, channel.code)
  106. def test_typing_timeout(self):
  107. request, channel = self.make_request(
  108. "PUT",
  109. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  110. b'{"typing": true, "timeout": 30000}',
  111. )
  112. self.render(request)
  113. self.assertEquals(200, channel.code)
  114. self.assertEquals(self.event_source.get_current_key(), 1)
  115. self.reactor.advance(36)
  116. self.assertEquals(self.event_source.get_current_key(), 2)
  117. request, channel = self.make_request(
  118. "PUT",
  119. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  120. b'{"typing": true, "timeout": 30000}',
  121. )
  122. self.render(request)
  123. self.assertEquals(200, channel.code)
  124. self.assertEquals(self.event_source.get_current_key(), 3)