test_profile.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket 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. """Tests REST events for /profile paths."""
  16. from tests import unittest
  17. from twisted.internet import defer
  18. from mock import Mock
  19. from ....utils import MockHttpResource, setup_test_homeserver
  20. from synapse.api.errors import SynapseError, AuthError
  21. from synapse.types import Requester, UserID
  22. from synapse.rest.client.v1 import profile
  23. myid = "@1234ABCD:test"
  24. PATH_PREFIX = "/_matrix/client/api/v1"
  25. class ProfileTestCase(unittest.TestCase):
  26. """ Tests profile management. """
  27. @defer.inlineCallbacks
  28. def setUp(self):
  29. self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
  30. self.mock_handler = Mock(spec=[
  31. "get_displayname",
  32. "set_displayname",
  33. "get_avatar_url",
  34. "set_avatar_url",
  35. ])
  36. hs = yield setup_test_homeserver(
  37. "test",
  38. http_client=None,
  39. resource_for_client=self.mock_resource,
  40. federation=Mock(),
  41. replication_layer=Mock(),
  42. )
  43. def _get_user_by_req(request=None, allow_guest=False):
  44. return Requester(UserID.from_string(myid), "", False)
  45. hs.get_v1auth().get_user_by_req = _get_user_by_req
  46. hs.get_handlers().profile_handler = self.mock_handler
  47. profile.register_servlets(hs, self.mock_resource)
  48. @defer.inlineCallbacks
  49. def test_get_my_name(self):
  50. mocked_get = self.mock_handler.get_displayname
  51. mocked_get.return_value = defer.succeed("Frank")
  52. (code, response) = yield self.mock_resource.trigger(
  53. "GET", "/profile/%s/displayname" % (myid), None
  54. )
  55. self.assertEquals(200, code)
  56. self.assertEquals({"displayname": "Frank"}, response)
  57. self.assertEquals(mocked_get.call_args[0][0].localpart, "1234ABCD")
  58. @defer.inlineCallbacks
  59. def test_set_my_name(self):
  60. mocked_set = self.mock_handler.set_displayname
  61. mocked_set.return_value = defer.succeed(())
  62. (code, response) = yield self.mock_resource.trigger(
  63. "PUT",
  64. "/profile/%s/displayname" % (myid),
  65. '{"displayname": "Frank Jr."}'
  66. )
  67. self.assertEquals(200, code)
  68. self.assertEquals(mocked_set.call_args[0][0].localpart, "1234ABCD")
  69. self.assertEquals(mocked_set.call_args[0][1].user.localpart, "1234ABCD")
  70. self.assertEquals(mocked_set.call_args[0][2], "Frank Jr.")
  71. @defer.inlineCallbacks
  72. def test_set_my_name_noauth(self):
  73. mocked_set = self.mock_handler.set_displayname
  74. mocked_set.side_effect = AuthError(400, "message")
  75. (code, response) = yield self.mock_resource.trigger(
  76. "PUT", "/profile/%s/displayname" % ("@4567:test"),
  77. '{"displayname": "Frank Jr."}'
  78. )
  79. self.assertTrue(
  80. 400 <= code < 499,
  81. msg="code %d is in the 4xx range" % (code)
  82. )
  83. @defer.inlineCallbacks
  84. def test_get_other_name(self):
  85. mocked_get = self.mock_handler.get_displayname
  86. mocked_get.return_value = defer.succeed("Bob")
  87. (code, response) = yield self.mock_resource.trigger(
  88. "GET", "/profile/%s/displayname" % ("@opaque:elsewhere"), None
  89. )
  90. self.assertEquals(200, code)
  91. self.assertEquals({"displayname": "Bob"}, response)
  92. @defer.inlineCallbacks
  93. def test_set_other_name(self):
  94. mocked_set = self.mock_handler.set_displayname
  95. mocked_set.side_effect = SynapseError(400, "message")
  96. (code, response) = yield self.mock_resource.trigger(
  97. "PUT", "/profile/%s/displayname" % ("@opaque:elsewhere"),
  98. '{"displayname":"bob"}'
  99. )
  100. self.assertTrue(
  101. 400 <= code <= 499,
  102. msg="code %d is in the 4xx range" % (code)
  103. )
  104. @defer.inlineCallbacks
  105. def test_get_my_avatar(self):
  106. mocked_get = self.mock_handler.get_avatar_url
  107. mocked_get.return_value = defer.succeed("http://my.server/me.png")
  108. (code, response) = yield self.mock_resource.trigger(
  109. "GET", "/profile/%s/avatar_url" % (myid), None
  110. )
  111. self.assertEquals(200, code)
  112. self.assertEquals({"avatar_url": "http://my.server/me.png"}, response)
  113. self.assertEquals(mocked_get.call_args[0][0].localpart, "1234ABCD")
  114. @defer.inlineCallbacks
  115. def test_set_my_avatar(self):
  116. mocked_set = self.mock_handler.set_avatar_url
  117. mocked_set.return_value = defer.succeed(())
  118. (code, response) = yield self.mock_resource.trigger(
  119. "PUT",
  120. "/profile/%s/avatar_url" % (myid),
  121. '{"avatar_url": "http://my.server/pic.gif"}'
  122. )
  123. self.assertEquals(200, code)
  124. self.assertEquals(mocked_set.call_args[0][0].localpart, "1234ABCD")
  125. self.assertEquals(mocked_set.call_args[0][1].user.localpart, "1234ABCD")
  126. self.assertEquals(mocked_set.call_args[0][2], "http://my.server/pic.gif")