test_presence.py 2.4 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 unittest.mock import Mock
  15. from twisted.internet import defer
  16. from synapse.handlers.presence import PresenceHandler
  17. from synapse.rest.client import presence
  18. from synapse.types import UserID
  19. from tests import unittest
  20. class PresenceTestCase(unittest.HomeserverTestCase):
  21. """Tests presence REST API."""
  22. user_id = "@sid:red"
  23. user = UserID.from_string(user_id)
  24. servlets = [presence.register_servlets]
  25. def make_homeserver(self, reactor, clock):
  26. presence_handler = Mock(spec=PresenceHandler)
  27. presence_handler.set_state.return_value = defer.succeed(None)
  28. hs = self.setup_test_homeserver(
  29. "red",
  30. federation_http_client=None,
  31. federation_client=Mock(),
  32. presence_handler=presence_handler,
  33. )
  34. return hs
  35. def test_put_presence(self):
  36. """
  37. PUT to the status endpoint with use_presence enabled will call
  38. set_state on the presence handler.
  39. """
  40. self.hs.config.server.use_presence = True
  41. body = {"presence": "here", "status_msg": "beep boop"}
  42. channel = self.make_request(
  43. "PUT", "/presence/%s/status" % (self.user_id,), body
  44. )
  45. self.assertEqual(channel.code, 200)
  46. self.assertEqual(self.hs.get_presence_handler().set_state.call_count, 1)
  47. @unittest.override_config({"use_presence": False})
  48. def test_put_presence_disabled(self):
  49. """
  50. PUT to the status endpoint with use_presence disabled will NOT call
  51. set_state on the presence handler.
  52. """
  53. body = {"presence": "here", "status_msg": "beep boop"}
  54. channel = self.make_request(
  55. "PUT", "/presence/%s/status" % (self.user_id,), body
  56. )
  57. self.assertEqual(channel.code, 200)
  58. self.assertEqual(self.hs.get_presence_handler().set_state.call_count, 0)