1
0

test_profile.py 8.4 KB

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