test_roommember.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 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. defer.returnValue(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. )