test_profile.py 4.7 KB

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