test_presence.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  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. from mock import Mock
  16. from twisted.internet import defer
  17. from synapse.rest.client.v1 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()
  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.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. def test_put_presence_disabled(self):
  48. """
  49. PUT to the status endpoint with use_presence disabled will NOT call
  50. set_state on the presence handler.
  51. """
  52. self.hs.config.use_presence = False
  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)