test_presence.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. hs = self.setup_test_homeserver(
  27. "red", http_client=None, federation_client=Mock()
  28. )
  29. hs.presence_handler = Mock()
  30. hs.presence_handler.set_state.return_value = defer.succeed(None)
  31. return hs
  32. def test_put_presence(self):
  33. """
  34. PUT to the status endpoint with use_presence enabled will call
  35. set_state on the presence handler.
  36. """
  37. self.hs.config.use_presence = True
  38. body = {"presence": "here", "status_msg": "beep boop"}
  39. request, channel = self.make_request(
  40. "PUT", "/presence/%s/status" % (self.user_id,), body
  41. )
  42. self.render(request)
  43. self.assertEqual(channel.code, 200)
  44. self.assertEqual(self.hs.presence_handler.set_state.call_count, 1)
  45. def test_put_presence_disabled(self):
  46. """
  47. PUT to the status endpoint with use_presence disbled will NOT call
  48. set_state on the presence handler.
  49. """
  50. self.hs.config.use_presence = False
  51. body = {"presence": "here", "status_msg": "beep boop"}
  52. request, channel = self.make_request(
  53. "PUT", "/presence/%s/status" % (self.user_id,), body
  54. )
  55. self.render(request)
  56. self.assertEqual(channel.code, 200)
  57. self.assertEqual(self.hs.presence_handler.set_state.call_count, 0)