test_profile.py 7.7 KB

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