1
0

test_profile.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 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
  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. # TODO(paul): Icky signal declarings.. booo
  55. hs.get_distributor().declare("changed_presencelike_data")
  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(self.frank, self.frank, "Frank Jr.")
  66. self.assertEquals(
  67. (yield self.store.get_profile_displayname(self.frank.localpart)),
  68. "Frank Jr."
  69. )
  70. @defer.inlineCallbacks
  71. def test_set_my_name_noauth(self):
  72. d = self.handler.set_displayname(self.frank, self.bob, "Frank Jr.")
  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. )
  86. @defer.inlineCallbacks
  87. def test_incoming_fed_query(self):
  88. yield self.store.create_profile("caroline")
  89. yield self.store.set_profile_displayname("caroline", "Caroline")
  90. response = yield self.query_handlers["profile"](
  91. {"user_id": "@caroline:test", "field": "displayname"}
  92. )
  93. self.assertEquals({"displayname": "Caroline"}, response)
  94. @defer.inlineCallbacks
  95. def test_get_my_avatar(self):
  96. yield self.store.set_profile_avatar_url(
  97. self.frank.localpart, "http://my.server/me.png"
  98. )
  99. avatar_url = yield self.handler.get_avatar_url(self.frank)
  100. self.assertEquals("http://my.server/me.png", avatar_url)
  101. @defer.inlineCallbacks
  102. def test_set_my_avatar(self):
  103. yield self.handler.set_avatar_url(self.frank, self.frank,
  104. "http://my.server/pic.gif")
  105. self.assertEquals(
  106. (yield self.store.get_profile_avatar_url(self.frank.localpart)),
  107. "http://my.server/pic.gif"
  108. )