test_profile.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from unittest.mock import Mock
  15. import synapse.types
  16. from synapse.api.errors import AuthError, SynapseError
  17. from synapse.rest import admin
  18. from synapse.types import UserID
  19. from tests import unittest
  20. from tests.test_utils import make_awaitable
  21. class ProfileTestCase(unittest.HomeserverTestCase):
  22. """Tests profile management."""
  23. servlets = [admin.register_servlets]
  24. def make_homeserver(self, reactor, clock):
  25. self.mock_federation = Mock()
  26. self.mock_registry = Mock()
  27. self.query_handlers = {}
  28. def register_query_handler(query_type, handler):
  29. self.query_handlers[query_type] = handler
  30. self.mock_registry.register_query_handler = register_query_handler
  31. hs = self.setup_test_homeserver(
  32. federation_client=self.mock_federation,
  33. federation_server=Mock(),
  34. federation_registry=self.mock_registry,
  35. )
  36. return hs
  37. def prepare(self, reactor, clock, hs):
  38. self.store = hs.get_datastore()
  39. self.frank = UserID.from_string("@1234abcd:test")
  40. self.bob = UserID.from_string("@4567:test")
  41. self.alice = UserID.from_string("@alice:remote")
  42. self.get_success(self.register_user(self.frank.localpart, "frankpassword"))
  43. self.handler = hs.get_profile_handler()
  44. def test_get_my_name(self):
  45. self.get_success(
  46. self.store.set_profile_displayname(self.frank.localpart, "Frank")
  47. )
  48. displayname = self.get_success(self.handler.get_displayname(self.frank))
  49. self.assertEquals("Frank", displayname)
  50. def test_set_my_name(self):
  51. self.get_success(
  52. self.handler.set_displayname(
  53. self.frank, synapse.types.create_requester(self.frank), "Frank Jr."
  54. )
  55. )
  56. self.assertEquals(
  57. (
  58. self.get_success(
  59. self.store.get_profile_displayname(self.frank.localpart)
  60. )
  61. ),
  62. "Frank Jr.",
  63. )
  64. # Set displayname again
  65. self.get_success(
  66. self.handler.set_displayname(
  67. self.frank, synapse.types.create_requester(self.frank), "Frank"
  68. )
  69. )
  70. self.assertEquals(
  71. (
  72. self.get_success(
  73. self.store.get_profile_displayname(self.frank.localpart)
  74. )
  75. ),
  76. "Frank",
  77. )
  78. # Set displayname to an empty string
  79. self.get_success(
  80. self.handler.set_displayname(
  81. self.frank, synapse.types.create_requester(self.frank), ""
  82. )
  83. )
  84. self.assertIsNone(
  85. self.get_success(self.store.get_profile_displayname(self.frank.localpart))
  86. )
  87. def test_set_my_name_if_disabled(self):
  88. self.hs.config.registration.enable_set_displayname = False
  89. # Setting displayname for the first time is allowed
  90. self.get_success(
  91. self.store.set_profile_displayname(self.frank.localpart, "Frank")
  92. )
  93. self.assertEquals(
  94. (
  95. self.get_success(
  96. self.store.get_profile_displayname(self.frank.localpart)
  97. )
  98. ),
  99. "Frank",
  100. )
  101. # Setting displayname a second time is forbidden
  102. self.get_failure(
  103. self.handler.set_displayname(
  104. self.frank, synapse.types.create_requester(self.frank), "Frank Jr."
  105. ),
  106. SynapseError,
  107. )
  108. def test_set_my_name_noauth(self):
  109. self.get_failure(
  110. self.handler.set_displayname(
  111. self.frank, synapse.types.create_requester(self.bob), "Frank Jr."
  112. ),
  113. AuthError,
  114. )
  115. def test_get_other_name(self):
  116. self.mock_federation.make_query.return_value = make_awaitable(
  117. {"displayname": "Alice"}
  118. )
  119. displayname = self.get_success(self.handler.get_displayname(self.alice))
  120. self.assertEquals(displayname, "Alice")
  121. self.mock_federation.make_query.assert_called_with(
  122. destination="remote",
  123. query_type="profile",
  124. args={"user_id": "@alice:remote", "field": "displayname"},
  125. ignore_backoff=True,
  126. )
  127. def test_incoming_fed_query(self):
  128. self.get_success(self.store.create_profile("caroline"))
  129. self.get_success(self.store.set_profile_displayname("caroline", "Caroline"))
  130. response = self.get_success(
  131. self.query_handlers["profile"](
  132. {
  133. "user_id": "@caroline:test",
  134. "field": "displayname",
  135. "origin": "servername.tld",
  136. }
  137. )
  138. )
  139. self.assertEquals({"displayname": "Caroline"}, response)
  140. def test_get_my_avatar(self):
  141. self.get_success(
  142. self.store.set_profile_avatar_url(
  143. self.frank.localpart, "http://my.server/me.png"
  144. )
  145. )
  146. avatar_url = self.get_success(self.handler.get_avatar_url(self.frank))
  147. self.assertEquals("http://my.server/me.png", avatar_url)
  148. def test_set_my_avatar(self):
  149. self.get_success(
  150. self.handler.set_avatar_url(
  151. self.frank,
  152. synapse.types.create_requester(self.frank),
  153. "http://my.server/pic.gif",
  154. )
  155. )
  156. self.assertEquals(
  157. (self.get_success(self.store.get_profile_avatar_url(self.frank.localpart))),
  158. "http://my.server/pic.gif",
  159. )
  160. # Set avatar again
  161. self.get_success(
  162. self.handler.set_avatar_url(
  163. self.frank,
  164. synapse.types.create_requester(self.frank),
  165. "http://my.server/me.png",
  166. )
  167. )
  168. self.assertEquals(
  169. (self.get_success(self.store.get_profile_avatar_url(self.frank.localpart))),
  170. "http://my.server/me.png",
  171. )
  172. # Set avatar to an empty string
  173. self.get_success(
  174. self.handler.set_avatar_url(
  175. self.frank,
  176. synapse.types.create_requester(self.frank),
  177. "",
  178. )
  179. )
  180. self.assertIsNone(
  181. (self.get_success(self.store.get_profile_avatar_url(self.frank.localpart))),
  182. )
  183. def test_set_my_avatar_if_disabled(self):
  184. self.hs.config.registration.enable_set_avatar_url = False
  185. # Setting displayname for the first time is allowed
  186. self.get_success(
  187. self.store.set_profile_avatar_url(
  188. self.frank.localpart, "http://my.server/me.png"
  189. )
  190. )
  191. self.assertEquals(
  192. (self.get_success(self.store.get_profile_avatar_url(self.frank.localpart))),
  193. "http://my.server/me.png",
  194. )
  195. # Set avatar a second time is forbidden
  196. self.get_failure(
  197. self.handler.set_avatar_url(
  198. self.frank,
  199. synapse.types.create_requester(self.frank),
  200. "http://my.server/pic.gif",
  201. ),
  202. SynapseError,
  203. )