1
0

test_roommember.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 tests import unittest
  16. from twisted.internet import defer
  17. from synapse.api.constants import EventTypes, Membership
  18. from synapse.types import UserID, RoomID
  19. from tests.utils import setup_test_homeserver
  20. from mock import Mock
  21. class RoomMemberStoreTestCase(unittest.TestCase):
  22. @defer.inlineCallbacks
  23. def setUp(self):
  24. hs = yield setup_test_homeserver(
  25. resource_for_federation=Mock(),
  26. 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.handlers = hs.get_handlers()
  33. self.message_handler = self.handlers.message_handler
  34. self.u_alice = UserID.from_string("@alice:test")
  35. self.u_bob = UserID.from_string("@bob:test")
  36. # User elsewhere on another host
  37. self.u_charlie = UserID.from_string("@charlie:elsewhere")
  38. self.room = RoomID.from_string("!abc123:test")
  39. @defer.inlineCallbacks
  40. def inject_room_member(self, room, user, membership, replaces_state=None):
  41. builder = self.event_builder_factory.new({
  42. "type": EventTypes.Member,
  43. "sender": user.to_string(),
  44. "state_key": user.to_string(),
  45. "room_id": room.to_string(),
  46. "content": {"membership": membership},
  47. })
  48. event, context = yield self.message_handler._create_new_client_event(
  49. builder
  50. )
  51. yield self.store.persist_event(event, context)
  52. defer.returnValue(event)
  53. @defer.inlineCallbacks
  54. def test_one_member(self):
  55. yield self.inject_room_member(self.room, self.u_alice, Membership.JOIN)
  56. self.assertEquals(
  57. [self.room.to_string()],
  58. [m.room_id for m in (
  59. yield self.store.get_rooms_for_user_where_membership_is(
  60. self.u_alice.to_string(), [Membership.JOIN]
  61. )
  62. )]
  63. )