test_typing.py 3.5 KB

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