test_profile.py 7.5 KB

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