test_presence.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. presence_handler = Mock(spec=PresenceHandler)
  31. presence_handler.set_state.return_value = make_awaitable(None)
  32. hs = self.setup_test_homeserver(
  33. "red",
  34. federation_http_client=None,
  35. federation_client=Mock(),
  36. presence_handler=presence_handler,
  37. )
  38. return hs
  39. def test_put_presence(self) -> None:
  40. """
  41. PUT to the status endpoint with use_presence enabled will call
  42. set_state on the presence handler.
  43. """
  44. self.hs.config.server.use_presence = True
  45. body = {"presence": "here", "status_msg": "beep boop"}
  46. channel = self.make_request(
  47. "PUT", "/presence/%s/status" % (self.user_id,), body
  48. )
  49. self.assertEqual(channel.code, HTTPStatus.OK)
  50. self.assertEqual(self.hs.get_presence_handler().set_state.call_count, 1)
  51. @unittest.override_config({"use_presence": False})
  52. def test_put_presence_disabled(self) -> None:
  53. """
  54. PUT to the status endpoint with use_presence disabled will NOT call
  55. set_state on the presence handler.
  56. """
  57. body = {"presence": "here", "status_msg": "beep boop"}
  58. channel = self.make_request(
  59. "PUT", "/presence/%s/status" % (self.user_id,), body
  60. )
  61. self.assertEqual(channel.code, HTTPStatus.OK)
  62. self.assertEqual(self.hs.get_presence_handler().set_state.call_count, 0)