test_typing.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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=None,
  50. room_ids=[self.room_id],
  51. is_guest=False,
  52. )
  53. )
  54. self.assertEqual(
  55. events[0],
  56. [
  57. {
  58. "type": EduTypes.TYPING,
  59. "room_id": self.room_id,
  60. "content": {"user_ids": [self.user_id]},
  61. }
  62. ],
  63. )
  64. def test_set_not_typing(self) -> None:
  65. channel = self.make_request(
  66. "PUT",
  67. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  68. b'{"typing": false}',
  69. )
  70. self.assertEqual(200, channel.code)
  71. def test_typing_timeout(self) -> None:
  72. channel = self.make_request(
  73. "PUT",
  74. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  75. b'{"typing": true, "timeout": 30000}',
  76. )
  77. self.assertEqual(200, channel.code)
  78. self.assertEqual(self.event_source.get_current_key(), 1)
  79. self.reactor.advance(36)
  80. self.assertEqual(self.event_source.get_current_key(), 2)
  81. channel = self.make_request(
  82. "PUT",
  83. "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
  84. b'{"typing": true, "timeout": 30000}',
  85. )
  86. self.assertEqual(200, channel.code)
  87. self.assertEqual(self.event_source.get_current_key(), 3)