test_profile.py 7.5 KB

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