test_profile.py 4.6 KB

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