test_presence.py 2.2 KB

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