test_account_data.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 AsyncMock
  15. from synapse.rest import admin
  16. from synapse.rest.client import account_data, login, room
  17. from tests import unittest
  18. class AccountDataTestCase(unittest.HomeserverTestCase):
  19. servlets = [
  20. admin.register_servlets,
  21. login.register_servlets,
  22. room.register_servlets,
  23. account_data.register_servlets,
  24. ]
  25. def test_on_account_data_updated_callback(self) -> None:
  26. """Tests that the on_account_data_updated module callback is called correctly when
  27. a user's account data changes.
  28. """
  29. mocked_callback = AsyncMock(return_value=None)
  30. self.hs.get_account_data_handler()._on_account_data_updated_callbacks.append(
  31. mocked_callback
  32. )
  33. user_id = self.register_user("user", "password")
  34. tok = self.login("user", "password")
  35. account_data_type = "org.matrix.foo"
  36. account_data_content = {"bar": "baz"}
  37. # Change the user's global account data.
  38. channel = self.make_request(
  39. "PUT",
  40. f"/user/{user_id}/account_data/{account_data_type}",
  41. account_data_content,
  42. access_token=tok,
  43. )
  44. # Test that the callback is called with the user ID, the new account data, and
  45. # None as the room ID.
  46. self.assertEqual(channel.code, 200, channel.result)
  47. mocked_callback.assert_called_once_with(
  48. user_id, None, account_data_type, account_data_content
  49. )
  50. # Change the user's room-specific account data.
  51. room_id = self.helper.create_room_as(user_id, tok=tok)
  52. channel = self.make_request(
  53. "PUT",
  54. f"/user/{user_id}/rooms/{room_id}/account_data/{account_data_type}",
  55. account_data_content,
  56. access_token=tok,
  57. )
  58. # Test that the callback is called with the user ID, the room ID and the new
  59. # account data.
  60. self.assertEqual(channel.code, 200, channel.result)
  61. self.assertEqual(mocked_callback.call_count, 2)
  62. mocked_callback.assert_called_with(
  63. user_id, room_id, account_data_type, account_data_content
  64. )