test_profile.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. from mock import Mock, NonCallableMock
  16. from twisted.internet import defer
  17. import synapse.types
  18. from synapse.api.errors import AuthError, SynapseError
  19. from synapse.handlers.profile import MasterProfileHandler
  20. from synapse.types import UserID
  21. from tests import unittest
  22. from tests.utils import setup_test_homeserver
  23. class ProfileHandlers(object):
  24. def __init__(self, hs):
  25. self.profile_handler = MasterProfileHandler(hs)
  26. class ProfileTestCase(unittest.TestCase):
  27. """ Tests profile management. """
  28. @defer.inlineCallbacks
  29. def setUp(self):
  30. self.mock_federation = Mock()
  31. self.mock_registry = Mock()
  32. self.query_handlers = {}
  33. def register_query_handler(query_type, handler):
  34. self.query_handlers[query_type] = handler
  35. self.mock_registry.register_query_handler = register_query_handler
  36. hs = yield setup_test_homeserver(
  37. self.addCleanup,
  38. http_client=None,
  39. handlers=None,
  40. resource_for_federation=Mock(),
  41. federation_client=self.mock_federation,
  42. federation_server=Mock(),
  43. federation_registry=self.mock_registry,
  44. ratelimiter=NonCallableMock(spec_set=["can_do_action"]),
  45. )
  46. self.ratelimiter = hs.get_ratelimiter()
  47. self.ratelimiter.can_do_action.return_value = (True, 0)
  48. self.store = hs.get_datastore()
  49. self.frank = UserID.from_string("@1234ABCD:test")
  50. self.bob = UserID.from_string("@4567:test")
  51. self.alice = UserID.from_string("@alice:remote")
  52. yield self.store.create_profile(self.frank.localpart)
  53. self.handler = hs.get_profile_handler()
  54. self.hs = hs
  55. @defer.inlineCallbacks
  56. def test_get_my_name(self):
  57. yield self.store.set_profile_displayname(self.frank.localpart, "Frank")
  58. displayname = yield self.handler.get_displayname(self.frank)
  59. self.assertEquals("Frank", displayname)
  60. @defer.inlineCallbacks
  61. def test_set_my_name(self):
  62. yield defer.ensureDeferred(
  63. self.handler.set_displayname(
  64. self.frank, synapse.types.create_requester(self.frank), "Frank Jr."
  65. )
  66. )
  67. self.assertEquals(
  68. (
  69. yield defer.ensureDeferred(
  70. self.store.get_profile_displayname(self.frank.localpart)
  71. )
  72. ),
  73. "Frank Jr.",
  74. )
  75. # Set displayname again
  76. yield defer.ensureDeferred(
  77. self.handler.set_displayname(
  78. self.frank, synapse.types.create_requester(self.frank), "Frank"
  79. )
  80. )
  81. self.assertEquals(
  82. (yield self.store.get_profile_displayname(self.frank.localpart)), "Frank",
  83. )
  84. @defer.inlineCallbacks
  85. def test_set_my_name_if_disabled(self):
  86. self.hs.config.enable_set_displayname = False
  87. # Setting displayname for the first time is allowed
  88. yield self.store.set_profile_displayname(self.frank.localpart, "Frank")
  89. self.assertEquals(
  90. (yield self.store.get_profile_displayname(self.frank.localpart)), "Frank",
  91. )
  92. # Setting displayname a second time is forbidden
  93. d = defer.ensureDeferred(
  94. self.handler.set_displayname(
  95. self.frank, synapse.types.create_requester(self.frank), "Frank Jr."
  96. )
  97. )
  98. yield self.assertFailure(d, SynapseError)
  99. @defer.inlineCallbacks
  100. def test_set_my_name_noauth(self):
  101. d = defer.ensureDeferred(
  102. self.handler.set_displayname(
  103. self.frank, synapse.types.create_requester(self.bob), "Frank Jr."
  104. )
  105. )
  106. yield self.assertFailure(d, AuthError)
  107. @defer.inlineCallbacks
  108. def test_get_other_name(self):
  109. self.mock_federation.make_query.return_value = defer.succeed(
  110. {"displayname": "Alice"}
  111. )
  112. displayname = yield self.handler.get_displayname(self.alice)
  113. self.assertEquals(displayname, "Alice")
  114. self.mock_federation.make_query.assert_called_with(
  115. destination="remote",
  116. query_type="profile",
  117. args={"user_id": "@alice:remote", "field": "displayname"},
  118. ignore_backoff=True,
  119. )
  120. @defer.inlineCallbacks
  121. def test_incoming_fed_query(self):
  122. yield self.store.create_profile("caroline")
  123. yield self.store.set_profile_displayname("caroline", "Caroline")
  124. response = yield self.query_handlers["profile"](
  125. {"user_id": "@caroline:test", "field": "displayname"}
  126. )
  127. self.assertEquals({"displayname": "Caroline"}, response)
  128. @defer.inlineCallbacks
  129. def test_get_my_avatar(self):
  130. yield self.store.set_profile_avatar_url(
  131. self.frank.localpart, "http://my.server/me.png"
  132. )
  133. avatar_url = yield self.handler.get_avatar_url(self.frank)
  134. self.assertEquals("http://my.server/me.png", avatar_url)
  135. @defer.inlineCallbacks
  136. def test_set_my_avatar(self):
  137. yield defer.ensureDeferred(
  138. self.handler.set_avatar_url(
  139. self.frank,
  140. synapse.types.create_requester(self.frank),
  141. "http://my.server/pic.gif",
  142. )
  143. )
  144. self.assertEquals(
  145. (yield self.store.get_profile_avatar_url(self.frank.localpart)),
  146. "http://my.server/pic.gif",
  147. )
  148. # Set avatar again
  149. yield defer.ensureDeferred(
  150. self.handler.set_avatar_url(
  151. self.frank,
  152. synapse.types.create_requester(self.frank),
  153. "http://my.server/me.png",
  154. )
  155. )
  156. self.assertEquals(
  157. (yield self.store.get_profile_avatar_url(self.frank.localpart)),
  158. "http://my.server/me.png",
  159. )
  160. @defer.inlineCallbacks
  161. def test_set_my_avatar_if_disabled(self):
  162. self.hs.config.enable_set_avatar_url = False
  163. # Setting displayname for the first time is allowed
  164. yield self.store.set_profile_avatar_url(
  165. self.frank.localpart, "http://my.server/me.png"
  166. )
  167. self.assertEquals(
  168. (yield self.store.get_profile_avatar_url(self.frank.localpart)),
  169. "http://my.server/me.png",
  170. )
  171. # Set avatar a second time is forbidden
  172. d = defer.ensureDeferred(
  173. self.handler.set_avatar_url(
  174. self.frank,
  175. synapse.types.create_requester(self.frank),
  176. "http://my.server/pic.gif",
  177. )
  178. )
  179. yield self.assertFailure(d, SynapseError)