test_room.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. import json
  15. from synapse.api.constants import RoomTypes
  16. from synapse.rest import admin
  17. from synapse.rest.client import login, room
  18. from synapse.storage.databases.main.room import _BackgroundUpdates
  19. from tests.unittest import HomeserverTestCase
  20. class RoomBackgroundUpdateStoreTestCase(HomeserverTestCase):
  21. servlets = [
  22. admin.register_servlets,
  23. room.register_servlets,
  24. login.register_servlets,
  25. ]
  26. def prepare(self, reactor, clock, hs):
  27. self.store = hs.get_datastores().main
  28. self.user_id = self.register_user("foo", "pass")
  29. self.token = self.login("foo", "pass")
  30. def _generate_room(self) -> str:
  31. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  32. return room_id
  33. def test_background_populate_rooms_creator_column(self):
  34. """Test that the background update to populate the rooms creator column
  35. works properly.
  36. """
  37. # Insert a room without the creator
  38. room_id = self._generate_room()
  39. self.get_success(
  40. self.store.db_pool.simple_update(
  41. table="rooms",
  42. keyvalues={"room_id": room_id},
  43. updatevalues={"creator": None},
  44. desc="test",
  45. )
  46. )
  47. # Make sure the test is starting out with a room without a creator
  48. room_creator_before = self.get_success(
  49. self.store.db_pool.simple_select_one_onecol(
  50. table="rooms",
  51. keyvalues={"room_id": room_id},
  52. retcol="creator",
  53. allow_none=True,
  54. )
  55. )
  56. self.assertEqual(room_creator_before, None)
  57. # Insert and run the background update.
  58. self.get_success(
  59. self.store.db_pool.simple_insert(
  60. "background_updates",
  61. {
  62. "update_name": _BackgroundUpdates.POPULATE_ROOMS_CREATOR_COLUMN,
  63. "progress_json": "{}",
  64. },
  65. )
  66. )
  67. # ... and tell the DataStore that it hasn't finished all updates yet
  68. self.store.db_pool.updates._all_done = False
  69. # Now let's actually drive the updates to completion
  70. self.wait_for_background_updates()
  71. # Make sure the background update filled in the room creator
  72. room_creator_after = self.get_success(
  73. self.store.db_pool.simple_select_one_onecol(
  74. table="rooms",
  75. keyvalues={"room_id": room_id},
  76. retcol="creator",
  77. allow_none=True,
  78. )
  79. )
  80. self.assertEqual(room_creator_after, self.user_id)
  81. def test_background_add_room_type_column(self):
  82. """Test that the background update to populate the `room_type` column in
  83. `room_stats_state` works properly.
  84. """
  85. # Create a room without a type
  86. room_id = self._generate_room()
  87. # Get event_id of the m.room.create event
  88. event_id = self.get_success(
  89. self.store.db_pool.simple_select_one_onecol(
  90. table="current_state_events",
  91. keyvalues={
  92. "room_id": room_id,
  93. "type": "m.room.create",
  94. },
  95. retcol="event_id",
  96. )
  97. )
  98. # Fake a room creation event with a room type
  99. event = {
  100. "content": {
  101. "creator": "@user:server.org",
  102. "room_version": "9",
  103. "type": RoomTypes.SPACE,
  104. },
  105. "type": "m.room.create",
  106. }
  107. self.get_success(
  108. self.store.db_pool.simple_update(
  109. table="event_json",
  110. keyvalues={"event_id": event_id},
  111. updatevalues={"json": json.dumps(event)},
  112. desc="test",
  113. )
  114. )
  115. # Insert and run the background update
  116. self.get_success(
  117. self.store.db_pool.simple_insert(
  118. "background_updates",
  119. {
  120. "update_name": _BackgroundUpdates.ADD_ROOM_TYPE_COLUMN,
  121. "progress_json": "{}",
  122. },
  123. )
  124. )
  125. # ... and tell the DataStore that it hasn't finished all updates yet
  126. self.store.db_pool.updates._all_done = False
  127. # Now let's actually drive the updates to completion
  128. self.wait_for_background_updates()
  129. # Make sure the background update filled in the room type
  130. room_type_after = self.get_success(
  131. self.store.db_pool.simple_select_one_onecol(
  132. table="room_stats_state",
  133. keyvalues={"room_id": room_id},
  134. retcol="room_type",
  135. allow_none=True,
  136. )
  137. )
  138. self.assertEqual(room_type_after, RoomTypes.SPACE)