test_roommember.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from mock import Mock
  16. from twisted.internet import defer
  17. from synapse.api.constants import EventTypes, Membership
  18. from synapse.api.room_versions import RoomVersions
  19. from synapse.types import Requester, RoomID, UserID
  20. from tests import unittest
  21. from tests.utils import create_room, setup_test_homeserver
  22. class RoomMemberStoreTestCase(unittest.TestCase):
  23. @defer.inlineCallbacks
  24. def setUp(self):
  25. hs = yield setup_test_homeserver(
  26. self.addCleanup, resource_for_federation=Mock(), http_client=None
  27. )
  28. # We can't test the RoomMemberStore on its own without the other event
  29. # storage logic
  30. self.store = hs.get_datastore()
  31. self.event_builder_factory = hs.get_event_builder_factory()
  32. self.event_creation_handler = hs.get_event_creation_handler()
  33. self.u_alice = UserID.from_string("@alice:test")
  34. self.u_bob = UserID.from_string("@bob:test")
  35. # User elsewhere on another host
  36. self.u_charlie = UserID.from_string("@charlie:elsewhere")
  37. self.room = RoomID.from_string("!abc123:test")
  38. yield create_room(hs, self.room.to_string(), self.u_alice.to_string())
  39. @defer.inlineCallbacks
  40. def inject_room_member(self, room, user, membership, replaces_state=None):
  41. builder = self.event_builder_factory.for_room_version(
  42. RoomVersions.V1,
  43. {
  44. "type": EventTypes.Member,
  45. "sender": user.to_string(),
  46. "state_key": user.to_string(),
  47. "room_id": room.to_string(),
  48. "content": {"membership": membership},
  49. },
  50. )
  51. event, context = yield self.event_creation_handler.create_new_client_event(
  52. builder
  53. )
  54. yield self.store.persist_event(event, context)
  55. return event
  56. @defer.inlineCallbacks
  57. def test_one_member(self):
  58. yield self.inject_room_member(self.room, self.u_alice, Membership.JOIN)
  59. self.assertEquals(
  60. [self.room.to_string()],
  61. [
  62. m.room_id
  63. for m in (
  64. yield self.store.get_rooms_for_user_where_membership_is(
  65. self.u_alice.to_string(), [Membership.JOIN]
  66. )
  67. )
  68. ],
  69. )
  70. class CurrentStateMembershipUpdateTestCase(unittest.HomeserverTestCase):
  71. def prepare(self, reactor, clock, homeserver):
  72. self.store = homeserver.get_datastore()
  73. self.room_creator = homeserver.get_room_creation_handler()
  74. def test_can_rerun_update(self):
  75. # First make sure we have completed all updates.
  76. while not self.get_success(self.store.has_completed_background_updates()):
  77. self.get_success(self.store.do_next_background_update(100), by=0.1)
  78. # Now let's create a room, which will insert a membership
  79. user = UserID("alice", "test")
  80. requester = Requester(user, None, False, None, None)
  81. self.get_success(self.room_creator.create_room(requester, {}))
  82. # Register the background update to run again.
  83. self.get_success(
  84. self.store._simple_insert(
  85. table="background_updates",
  86. values={
  87. "update_name": "current_state_events_membership",
  88. "progress_json": "{}",
  89. "depends_on": None,
  90. },
  91. )
  92. )
  93. # ... and tell the DataStore that it hasn't finished all updates yet
  94. self.store._all_done = False
  95. # Now let's actually drive the updates to completion
  96. while not self.get_success(self.store.has_completed_background_updates()):
  97. self.get_success(self.store.do_next_background_update(100), by=0.1)