test_typing.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 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(
  59. user=UserID.from_string(self.user_id),
  60. from_key=0,
  61. limit=None,
  62. room_ids=[self.room_id],
  63. is_guest=False,
  64. )
  65. )
  66. self.assertEquals(
  67. events[0],
  68. [
  69. {
  70. "type": "m.typing",
  71. "room_id": self.room_id,
  72. "content": {"user_ids": [self.user_id]},
  73. }
  74. ],
  75. )
  76. def test_set_not_typing(self):
  77. channel = self.make_request(
  78. "PUT",
  79. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  80. b'{"typing": false}',
  81. )
  82. self.assertEquals(200, channel.code)
  83. def test_typing_timeout(self):
  84. channel = self.make_request(
  85. "PUT",
  86. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  87. b'{"typing": true, "timeout": 30000}',
  88. )
  89. self.assertEquals(200, channel.code)
  90. self.assertEquals(self.event_source.get_current_key(), 1)
  91. self.reactor.advance(36)
  92. self.assertEquals(self.event_source.get_current_key(), 2)
  93. channel = self.make_request(
  94. "PUT",
  95. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  96. b'{"typing": true, "timeout": 30000}',
  97. )
  98. self.assertEquals(200, channel.code)
  99. self.assertEquals(self.event_source.get_current_key(), 3)