test_account_data.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright 2022 The Matrix.org Foundation C.I.C
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from unittest.mock import Mock
  15. from synapse.rest import admin
  16. from synapse.rest.client import account_data, login, room
  17. from tests import unittest
  18. from tests.test_utils import make_awaitable
  19. class AccountDataTestCase(unittest.HomeserverTestCase):
  20. servlets = [
  21. admin.register_servlets,
  22. login.register_servlets,
  23. room.register_servlets,
  24. account_data.register_servlets,
  25. ]
  26. def test_on_account_data_updated_callback(self) -> None:
  27. """Tests that the on_account_data_updated module callback is called correctly when
  28. a user's account data changes.
  29. """
  30. mocked_callback = Mock(return_value=make_awaitable(None))
  31. self.hs.get_account_data_handler()._on_account_data_updated_callbacks.append(
  32. mocked_callback
  33. )
  34. user_id = self.register_user("user", "password")
  35. tok = self.login("user", "password")
  36. account_data_type = "org.matrix.foo"
  37. account_data_content = {"bar": "baz"}
  38. # Change the user's global account data.
  39. channel = self.make_request(
  40. "PUT",
  41. f"/user/{user_id}/account_data/{account_data_type}",
  42. account_data_content,
  43. access_token=tok,
  44. )
  45. # Test that the callback is called with the user ID, the new account data, and
  46. # None as the room ID.
  47. self.assertEqual(channel.code, 200, channel.result)
  48. mocked_callback.assert_called_once_with(
  49. user_id, None, account_data_type, account_data_content
  50. )
  51. # Change the user's room-specific account data.
  52. room_id = self.helper.create_room_as(user_id, tok=tok)
  53. channel = self.make_request(
  54. "PUT",
  55. f"/user/{user_id}/rooms/{room_id}/account_data/{account_data_type}",
  56. account_data_content,
  57. access_token=tok,
  58. )
  59. # Test that the callback is called with the user ID, the room ID and the new
  60. # account data.
  61. self.assertEqual(channel.code, 200, channel.result)
  62. self.assertEqual(mocked_callback.call_count, 2)
  63. mocked_callback.assert_called_with(
  64. user_id, room_id, account_data_type, account_data_content
  65. )