test_typing.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2018 New Vector
  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. from unittest.mock import Mock
  17. from synapse.rest.client.v1 import room
  18. from synapse.types import UserID
  19. from tests import unittest
  20. PATH_PREFIX = "/_matrix/client/api/v1"
  21. class RoomTypingTestCase(unittest.HomeserverTestCase):
  22. """ Tests /rooms/$room_id/typing/$user_id REST API. """
  23. user_id = "@sid:red"
  24. user = UserID.from_string(user_id)
  25. servlets = [room.register_servlets]
  26. def make_homeserver(self, reactor, clock):
  27. hs = self.setup_test_homeserver(
  28. "red",
  29. federation_http_client=None,
  30. federation_client=Mock(),
  31. )
  32. self.event_source = hs.get_event_sources().sources["typing"]
  33. hs.get_federation_handler = Mock()
  34. async def get_user_by_access_token(token=None, allow_guest=False):
  35. return {
  36. "user": UserID.from_string(self.auth_user_id),
  37. "token_id": 1,
  38. "is_guest": False,
  39. }
  40. hs.get_auth().get_user_by_access_token = get_user_by_access_token
  41. async def _insert_client_ip(*args, **kwargs):
  42. return None
  43. hs.get_datastore().insert_client_ip = _insert_client_ip
  44. return hs
  45. def prepare(self, reactor, clock, hs):
  46. self.room_id = self.helper.create_room_as(self.user_id)
  47. # Need another user to make notifications actually work
  48. self.helper.join(self.room_id, user="@jim:red")
  49. def test_set_typing(self):
  50. channel = self.make_request(
  51. "PUT",
  52. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  53. b'{"typing": true, "timeout": 30000}',
  54. )
  55. self.assertEquals(200, channel.code)
  56. self.assertEquals(self.event_source.get_current_key(), 1)
  57. events = self.get_success(
  58. self.event_source.get_new_events(from_key=0, room_ids=[self.room_id])
  59. )
  60. self.assertEquals(
  61. events[0],
  62. [
  63. {
  64. "type": "m.typing",
  65. "room_id": self.room_id,
  66. "content": {"user_ids": [self.user_id]},
  67. }
  68. ],
  69. )
  70. def test_set_not_typing(self):
  71. channel = self.make_request(
  72. "PUT",
  73. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  74. b'{"typing": false}',
  75. )
  76. self.assertEquals(200, channel.code)
  77. def test_typing_timeout(self):
  78. channel = self.make_request(
  79. "PUT",
  80. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  81. b'{"typing": true, "timeout": 30000}',
  82. )
  83. self.assertEquals(200, channel.code)
  84. self.assertEquals(self.event_source.get_current_key(), 1)
  85. self.reactor.advance(36)
  86. self.assertEquals(self.event_source.get_current_key(), 2)
  87. channel = self.make_request(
  88. "PUT",
  89. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  90. b'{"typing": true, "timeout": 30000}',
  91. )
  92. self.assertEquals(200, channel.code)
  93. self.assertEquals(self.event_source.get_current_key(), 3)