test_room.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright 2021 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 synapse.rest import admin
  15. from synapse.rest.client import login, room
  16. from synapse.storage.databases.main.room import _BackgroundUpdates
  17. from tests.unittest import HomeserverTestCase
  18. class RoomBackgroundUpdateStoreTestCase(HomeserverTestCase):
  19. servlets = [
  20. admin.register_servlets,
  21. room.register_servlets,
  22. login.register_servlets,
  23. ]
  24. def prepare(self, reactor, clock, hs):
  25. self.store = hs.get_datastores().main
  26. self.user_id = self.register_user("foo", "pass")
  27. self.token = self.login("foo", "pass")
  28. def _generate_room(self) -> str:
  29. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  30. return room_id
  31. def test_background_populate_rooms_creator_column(self):
  32. """Test that the background update to populate the rooms creator column
  33. works properly.
  34. """
  35. # Insert a room without the creator
  36. room_id = self._generate_room()
  37. self.get_success(
  38. self.store.db_pool.simple_update(
  39. table="rooms",
  40. keyvalues={"room_id": room_id},
  41. updatevalues={"creator": None},
  42. desc="test",
  43. )
  44. )
  45. # Make sure the test is starting out with a room without a creator
  46. room_creator_before = self.get_success(
  47. self.store.db_pool.simple_select_one_onecol(
  48. table="rooms",
  49. keyvalues={"room_id": room_id},
  50. retcol="creator",
  51. allow_none=True,
  52. )
  53. )
  54. self.assertEqual(room_creator_before, None)
  55. # Insert and run the background update.
  56. self.get_success(
  57. self.store.db_pool.simple_insert(
  58. "background_updates",
  59. {
  60. "update_name": _BackgroundUpdates.POPULATE_ROOMS_CREATOR_COLUMN,
  61. "progress_json": "{}",
  62. },
  63. )
  64. )
  65. # ... and tell the DataStore that it hasn't finished all updates yet
  66. self.store.db_pool.updates._all_done = False
  67. # Now let's actually drive the updates to completion
  68. self.wait_for_background_updates()
  69. # Make sure the background update filled in the room creator
  70. room_creator_after = self.get_success(
  71. self.store.db_pool.simple_select_one_onecol(
  72. table="rooms",
  73. keyvalues={"room_id": room_id},
  74. retcol="creator",
  75. allow_none=True,
  76. )
  77. )
  78. self.assertEqual(room_creator_after, self.user_id)