test_room.py 5.5 KB

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