1
0

test_profile.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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, NonCallableMock
  16. from twisted.internet import defer
  17. import synapse.types
  18. from synapse.api.errors import AuthError
  19. from synapse.handlers.profile import MasterProfileHandler
  20. from synapse.types import UserID
  21. from tests import unittest
  22. from tests.utils import setup_test_homeserver
  23. class ProfileHandlers(object):
  24. def __init__(self, hs):
  25. self.profile_handler = MasterProfileHandler(hs)
  26. class ProfileTestCase(unittest.TestCase):
  27. """ Tests profile management. """
  28. @defer.inlineCallbacks
  29. def setUp(self):
  30. self.mock_federation = Mock()
  31. self.mock_registry = Mock()
  32. self.query_handlers = {}
  33. def register_query_handler(query_type, handler):
  34. self.query_handlers[query_type] = handler
  35. self.mock_registry.register_query_handler = register_query_handler
  36. hs = yield setup_test_homeserver(
  37. self.addCleanup,
  38. http_client=None,
  39. handlers=None,
  40. resource_for_federation=Mock(),
  41. federation_client=self.mock_federation,
  42. federation_server=Mock(),
  43. federation_registry=self.mock_registry,
  44. ratelimiter=NonCallableMock(spec_set=["can_do_action"]),
  45. )
  46. self.ratelimiter = hs.get_ratelimiter()
  47. self.ratelimiter.can_do_action.return_value = (True, 0)
  48. self.store = hs.get_datastore()
  49. self.frank = UserID.from_string("@1234ABCD:test")
  50. self.bob = UserID.from_string("@4567:test")
  51. self.alice = UserID.from_string("@alice:remote")
  52. yield self.store.create_profile(self.frank.localpart)
  53. self.handler = hs.get_profile_handler()
  54. @defer.inlineCallbacks
  55. def test_get_my_name(self):
  56. yield self.store.set_profile_displayname(self.frank.localpart, "Frank")
  57. displayname = yield self.handler.get_displayname(self.frank)
  58. self.assertEquals("Frank", displayname)
  59. @defer.inlineCallbacks
  60. def test_set_my_name(self):
  61. yield self.handler.set_displayname(
  62. self.frank, synapse.types.create_requester(self.frank), "Frank Jr."
  63. )
  64. self.assertEquals(
  65. (yield self.store.get_profile_displayname(self.frank.localpart)),
  66. "Frank Jr.",
  67. )
  68. @defer.inlineCallbacks
  69. def test_set_my_name_noauth(self):
  70. d = self.handler.set_displayname(
  71. self.frank, synapse.types.create_requester(self.bob), "Frank Jr."
  72. )
  73. yield self.assertFailure(d, AuthError)
  74. @defer.inlineCallbacks
  75. def test_get_other_name(self):
  76. self.mock_federation.make_query.return_value = defer.succeed(
  77. {"displayname": "Alice"}
  78. )
  79. displayname = yield self.handler.get_displayname(self.alice)
  80. self.assertEquals(displayname, "Alice")
  81. self.mock_federation.make_query.assert_called_with(
  82. destination="remote",
  83. query_type="profile",
  84. args={"user_id": "@alice:remote", "field": "displayname"},
  85. ignore_backoff=True,
  86. )
  87. @defer.inlineCallbacks
  88. def test_incoming_fed_query(self):
  89. yield self.store.create_profile("caroline")
  90. yield self.store.set_profile_displayname("caroline", "Caroline")
  91. response = yield self.query_handlers["profile"](
  92. {"user_id": "@caroline:test", "field": "displayname"}
  93. )
  94. self.assertEquals({"displayname": "Caroline"}, response)
  95. @defer.inlineCallbacks
  96. def test_get_my_avatar(self):
  97. yield self.store.set_profile_avatar_url(
  98. self.frank.localpart, "http://my.server/me.png"
  99. )
  100. avatar_url = yield self.handler.get_avatar_url(self.frank)
  101. self.assertEquals("http://my.server/me.png", avatar_url)
  102. @defer.inlineCallbacks
  103. def test_set_my_avatar(self):
  104. yield self.handler.set_avatar_url(
  105. self.frank,
  106. synapse.types.create_requester(self.frank),
  107. "http://my.server/pic.gif",
  108. )
  109. self.assertEquals(
  110. (yield self.store.get_profile_avatar_url(self.frank.localpart)),
  111. "http://my.server/pic.gif",
  112. )