test_profile.py 4.8 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 twisted.trial import unittest
  16. from twisted.internet import defer
  17. from mock import Mock
  18. import logging
  19. from synapse.api.errors import AuthError
  20. from synapse.server import HomeServer
  21. from synapse.handlers.profile import ProfileHandler
  22. logging.getLogger().addHandler(logging.NullHandler())
  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. 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 = HomeServer("test",
  37. db_pool=None,
  38. http_client=None,
  39. datastore=Mock(spec=[
  40. "get_profile_displayname",
  41. "set_profile_displayname",
  42. "get_profile_avatar_url",
  43. "set_profile_avatar_url",
  44. ]),
  45. handlers=None,
  46. resource_for_federation=Mock(),
  47. replication_layer=self.mock_federation,
  48. )
  49. hs.handlers = ProfileHandlers(hs)
  50. self.datastore = hs.get_datastore()
  51. self.frank = hs.parse_userid("@1234ABCD:test")
  52. self.bob = hs.parse_userid("@4567:test")
  53. self.alice = hs.parse_userid("@alice:remote")
  54. self.handler = hs.get_handlers().profile_handler
  55. # TODO(paul): Icky signal declarings.. booo
  56. hs.get_distributor().declare("changed_presencelike_data")
  57. @defer.inlineCallbacks
  58. def test_get_my_name(self):
  59. mocked_get = self.datastore.get_profile_displayname
  60. mocked_get.return_value = defer.succeed("Frank")
  61. displayname = yield self.handler.get_displayname(self.frank)
  62. self.assertEquals("Frank", displayname)
  63. mocked_get.assert_called_with("1234ABCD")
  64. @defer.inlineCallbacks
  65. def test_set_my_name(self):
  66. mocked_set = self.datastore.set_profile_displayname
  67. mocked_set.return_value = defer.succeed(())
  68. yield self.handler.set_displayname(self.frank, self.frank, "Frank Jr.")
  69. mocked_set.assert_called_with("1234ABCD", "Frank Jr.")
  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. mocked_get = self.datastore.get_profile_displayname
  89. mocked_get.return_value = defer.succeed("Caroline")
  90. response = yield self.query_handlers["profile"](
  91. {"user_id": "@caroline:test", "field": "displayname"}
  92. )
  93. self.assertEquals({"displayname": "Caroline"}, response)
  94. mocked_get.assert_called_with("caroline")
  95. @defer.inlineCallbacks
  96. def test_get_my_avatar(self):
  97. mocked_get = self.datastore.get_profile_avatar_url
  98. mocked_get.return_value = defer.succeed("http://my.server/me.png")
  99. avatar_url = yield self.handler.get_avatar_url(self.frank)
  100. self.assertEquals("http://my.server/me.png", avatar_url)
  101. mocked_get.assert_called_with("1234ABCD")
  102. @defer.inlineCallbacks
  103. def test_set_my_avatar(self):
  104. mocked_set = self.datastore.set_profile_avatar_url
  105. mocked_set.return_value = defer.succeed(())
  106. yield self.handler.set_avatar_url(self.frank, self.frank,
  107. "http://my.server/pic.gif")
  108. mocked_set.assert_called_with("1234ABCD", "http://my.server/pic.gif")