1
0

test_user_directory.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector
  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
  16. from twisted.internet import defer
  17. from synapse.api.constants import UserTypes
  18. from synapse.handlers.user_directory import UserDirectoryHandler
  19. from synapse.storage.roommember import ProfileInfo
  20. from tests import unittest
  21. from tests.utils import setup_test_homeserver
  22. class UserDirectoryHandlers(object):
  23. def __init__(self, hs):
  24. self.user_directory_handler = UserDirectoryHandler(hs)
  25. class UserDirectoryTestCase(unittest.TestCase):
  26. """ Tests the UserDirectoryHandler. """
  27. @defer.inlineCallbacks
  28. def setUp(self):
  29. hs = yield setup_test_homeserver(self.addCleanup)
  30. self.store = hs.get_datastore()
  31. hs.handlers = UserDirectoryHandlers(hs)
  32. self.handler = hs.get_handlers().user_directory_handler
  33. @defer.inlineCallbacks
  34. def test_handle_local_profile_change_with_support_user(self):
  35. support_user_id = "@support:test"
  36. yield self.store.register(
  37. user_id=support_user_id,
  38. token="123",
  39. password_hash=None,
  40. user_type=UserTypes.SUPPORT
  41. )
  42. yield self.handler.handle_local_profile_change(support_user_id, None)
  43. profile = yield self.store.get_user_in_directory(support_user_id)
  44. self.assertTrue(profile is None)
  45. display_name = 'display_name'
  46. profile_info = ProfileInfo(
  47. avatar_url='avatar_url',
  48. display_name=display_name,
  49. )
  50. regular_user_id = '@regular:test'
  51. yield self.handler.handle_local_profile_change(regular_user_id, profile_info)
  52. profile = yield self.store.get_user_in_directory(regular_user_id)
  53. self.assertTrue(profile['display_name'] == display_name)
  54. @defer.inlineCallbacks
  55. def test_handle_user_deactivated_support_user(self):
  56. s_user_id = "@support:test"
  57. self.store.register(
  58. user_id=s_user_id,
  59. token="123",
  60. password_hash=None,
  61. user_type=UserTypes.SUPPORT
  62. )
  63. self.store.remove_from_user_dir = Mock()
  64. self.store.remove_from_user_in_public_room = Mock()
  65. yield self.handler.handle_user_deactivated(s_user_id)
  66. self.store.remove_from_user_dir.not_called()
  67. self.store.remove_from_user_in_public_room.not_called()
  68. @defer.inlineCallbacks
  69. def test_handle_user_deactivated_regular_user(self):
  70. r_user_id = "@regular:test"
  71. self.store.register(user_id=r_user_id, token="123", password_hash=None)
  72. self.store.remove_from_user_dir = Mock()
  73. self.store.remove_from_user_in_public_room = Mock()
  74. yield self.handler.handle_user_deactivated(r_user_id)
  75. self.store.remove_from_user_dir.called_once_with(r_user_id)
  76. self.store.remove_from_user_in_public_room.assert_called_once_with(r_user_id)