test_profile.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 ProfileHandler
  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 = ProfileHandler(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. http_client=None,
  38. handlers=None,
  39. resource_for_federation=Mock(),
  40. federation_client=self.mock_federation,
  41. federation_server=Mock(),
  42. federation_registry=self.mock_registry,
  43. ratelimiter=NonCallableMock(spec_set=[
  44. "send_message",
  45. ])
  46. )
  47. self.ratelimiter = hs.get_ratelimiter()
  48. self.ratelimiter.send_message.return_value = (True, 0)
  49. self.store = hs.get_datastore()
  50. self.frank = UserID.from_string("@1234ABCD:test")
  51. self.bob = UserID.from_string("@4567:test")
  52. self.alice = UserID.from_string("@alice:remote")
  53. yield self.store.create_profile(self.frank.localpart)
  54. self.handler = hs.get_profile_handler()
  55. @defer.inlineCallbacks
  56. def test_get_my_name(self):
  57. yield self.store.set_profile_displayname(
  58. self.frank.localpart, "Frank"
  59. )
  60. displayname = yield self.handler.get_displayname(self.frank)
  61. self.assertEquals("Frank", displayname)
  62. @defer.inlineCallbacks
  63. def test_set_my_name(self):
  64. yield self.handler.set_displayname(
  65. self.frank,
  66. synapse.types.create_requester(self.frank),
  67. "Frank Jr."
  68. )
  69. self.assertEquals(
  70. (yield self.store.get_profile_displayname(self.frank.localpart)),
  71. "Frank Jr."
  72. )
  73. @defer.inlineCallbacks
  74. def test_set_my_name_noauth(self):
  75. d = self.handler.set_displayname(
  76. self.frank,
  77. synapse.types.create_requester(self.bob),
  78. "Frank Jr."
  79. )
  80. yield self.assertFailure(d, AuthError)
  81. @defer.inlineCallbacks
  82. def test_get_other_name(self):
  83. self.mock_federation.make_query.return_value = defer.succeed(
  84. {"displayname": "Alice"}
  85. )
  86. displayname = yield self.handler.get_displayname(self.alice)
  87. self.assertEquals(displayname, "Alice")
  88. self.mock_federation.make_query.assert_called_with(
  89. destination="remote",
  90. query_type="profile",
  91. args={"user_id": "@alice:remote", "field": "displayname"},
  92. ignore_backoff=True,
  93. )
  94. @defer.inlineCallbacks
  95. def test_incoming_fed_query(self):
  96. yield self.store.create_profile("caroline")
  97. yield self.store.set_profile_displayname("caroline", "Caroline")
  98. response = yield self.query_handlers["profile"](
  99. {"user_id": "@caroline:test", "field": "displayname"}
  100. )
  101. self.assertEquals({"displayname": "Caroline"}, response)
  102. @defer.inlineCallbacks
  103. def test_get_my_avatar(self):
  104. yield self.store.set_profile_avatar_url(
  105. self.frank.localpart, "http://my.server/me.png"
  106. )
  107. avatar_url = yield self.handler.get_avatar_url(self.frank)
  108. self.assertEquals("http://my.server/me.png", avatar_url)
  109. @defer.inlineCallbacks
  110. def test_set_my_avatar(self):
  111. yield self.handler.set_avatar_url(
  112. self.frank, synapse.types.create_requester(self.frank),
  113. "http://my.server/pic.gif"
  114. )
  115. self.assertEquals(
  116. (yield self.store.get_profile_avatar_url(self.frank.localpart)),
  117. "http://my.server/pic.gif"
  118. )