test_typing.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 twisted.test.proto_helpers import MemoryReactor
  17. from synapse.rest.client import room
  18. from synapse.server import HomeServer
  19. from synapse.types import UserID
  20. from synapse.util import Clock
  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: MemoryReactor, clock: Clock) -> HomeServer:
  29. hs = self.setup_test_homeserver("red")
  30. self.event_source = hs.get_event_sources().sources.typing
  31. return hs
  32. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  33. self.room_id = self.helper.create_room_as(self.user_id)
  34. # Need another user to make notifications actually work
  35. self.helper.join(self.room_id, user="@jim:red")
  36. def test_set_typing(self) -> None:
  37. channel = self.make_request(
  38. "PUT",
  39. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  40. b'{"typing": true, "timeout": 30000}',
  41. )
  42. self.assertEqual(200, channel.code)
  43. self.assertEqual(self.event_source.get_current_key(), 1)
  44. events = self.get_success(
  45. self.event_source.get_new_events(
  46. user=UserID.from_string(self.user_id),
  47. from_key=0,
  48. limit=None,
  49. room_ids=[self.room_id],
  50. is_guest=False,
  51. )
  52. )
  53. self.assertEqual(
  54. events[0],
  55. [
  56. {
  57. "type": "m.typing",
  58. "room_id": self.room_id,
  59. "content": {"user_ids": [self.user_id]},
  60. }
  61. ],
  62. )
  63. def test_set_not_typing(self) -> None:
  64. channel = self.make_request(
  65. "PUT",
  66. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  67. b'{"typing": false}',
  68. )
  69. self.assertEqual(200, channel.code)
  70. def test_typing_timeout(self) -> None:
  71. channel = self.make_request(
  72. "PUT",
  73. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  74. b'{"typing": true, "timeout": 30000}',
  75. )
  76. self.assertEqual(200, channel.code)
  77. self.assertEqual(self.event_source.get_current_key(), 1)
  78. self.reactor.advance(36)
  79. self.assertEqual(self.event_source.get_current_key(), 2)
  80. channel = self.make_request(
  81. "PUT",
  82. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  83. b'{"typing": true, "timeout": 30000}',
  84. )
  85. self.assertEqual(200, channel.code)
  86. self.assertEqual(self.event_source.get_current_key(), 3)