You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

78 lines
2.6 KiB

  1. # Copyright 2018 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from http import HTTPStatus
  15. from unittest.mock import Mock
  16. from twisted.test.proto_helpers import MemoryReactor
  17. from synapse.handlers.presence import PresenceHandler
  18. from synapse.rest.client import presence
  19. from synapse.server import HomeServer
  20. from synapse.types import UserID
  21. from synapse.util import Clock
  22. from tests import unittest
  23. from tests.test_utils import make_awaitable
  24. class PresenceTestCase(unittest.HomeserverTestCase):
  25. """Tests presence REST API."""
  26. user_id = "@sid:red"
  27. user = UserID.from_string(user_id)
  28. servlets = [presence.register_servlets]
  29. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  30. self.presence_handler = Mock(spec=PresenceHandler)
  31. self.presence_handler.set_state.return_value = make_awaitable(None)
  32. hs = self.setup_test_homeserver(
  33. "red",
  34. federation_client=Mock(),
  35. presence_handler=self.presence_handler,
  36. )
  37. return hs
  38. def test_put_presence(self) -> None:
  39. """
  40. PUT to the status endpoint with use_presence enabled will call
  41. set_state on the presence handler.
  42. """
  43. self.hs.config.server.use_presence = True
  44. body = {"presence": "here", "status_msg": "beep boop"}
  45. channel = self.make_request(
  46. "PUT", "/presence/%s/status" % (self.user_id,), body
  47. )
  48. self.assertEqual(channel.code, HTTPStatus.OK)
  49. self.assertEqual(self.presence_handler.set_state.call_count, 1)
  50. @unittest.override_config({"use_presence": False})
  51. def test_put_presence_disabled(self) -> None:
  52. """
  53. PUT to the status endpoint with use_presence disabled will NOT call
  54. set_state on the presence handler.
  55. """
  56. body = {"presence": "here", "status_msg": "beep boop"}
  57. channel = self.make_request(
  58. "PUT", "/presence/%s/status" % (self.user_id,), body
  59. )
  60. self.assertEqual(channel.code, HTTPStatus.OK)
  61. self.assertEqual(self.presence_handler.set_state.call_count, 0)