test_room.py 6.3 KB

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