test_presence.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 AsyncMock, 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. class PresenceTestCase(unittest.HomeserverTestCase):
  24. """Tests presence REST API."""
  25. user_id = "@sid:red"
  26. user = UserID.from_string(user_id)
  27. servlets = [presence.register_servlets]
  28. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  29. self.presence_handler = Mock(spec=PresenceHandler)
  30. self.presence_handler.set_state = AsyncMock(return_value=None)
  31. hs = self.setup_test_homeserver(
  32. "red",
  33. federation_client=Mock(),
  34. presence_handler=self.presence_handler,
  35. )
  36. return hs
  37. def test_put_presence(self) -> None:
  38. """
  39. PUT to the status endpoint with use_presence enabled will call
  40. set_state on the presence handler.
  41. """
  42. self.hs.config.server.use_presence = True
  43. body = {"presence": "here", "status_msg": "beep boop"}
  44. channel = self.make_request(
  45. "PUT", "/presence/%s/status" % (self.user_id,), body
  46. )
  47. self.assertEqual(channel.code, HTTPStatus.OK)
  48. self.assertEqual(self.presence_handler.set_state.call_count, 1)
  49. @unittest.override_config({"use_presence": False})
  50. def test_put_presence_disabled(self) -> None:
  51. """
  52. PUT to the status endpoint with use_presence disabled will NOT call
  53. set_state on the presence handler.
  54. """
  55. body = {"presence": "here", "status_msg": "beep boop"}
  56. channel = self.make_request(
  57. "PUT", "/presence/%s/status" % (self.user_id,), body
  58. )
  59. self.assertEqual(channel.code, HTTPStatus.OK)
  60. self.assertEqual(self.presence_handler.set_state.call_count, 0)