test_roommember.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.types import RoomID, UserID
  19. from tests import unittest
  20. from tests.utils import setup_test_homeserver
  21. class RoomMemberStoreTestCase(unittest.TestCase):
  22. @defer.inlineCallbacks
  23. def setUp(self):
  24. hs = yield setup_test_homeserver(
  25. self.addCleanup, resource_for_federation=Mock(), http_client=None
  26. )
  27. # We can't test the RoomMemberStore on its own without the other event
  28. # storage logic
  29. self.store = hs.get_datastore()
  30. self.event_builder_factory = hs.get_event_builder_factory()
  31. self.event_creation_handler = hs.get_event_creation_handler()
  32. self.u_alice = UserID.from_string("@alice:test")
  33. self.u_bob = UserID.from_string("@bob:test")
  34. # User elsewhere on another host
  35. self.u_charlie = UserID.from_string("@charlie:elsewhere")
  36. self.room = RoomID.from_string("!abc123:test")
  37. @defer.inlineCallbacks
  38. def inject_room_member(self, room, user, membership, replaces_state=None):
  39. builder = self.event_builder_factory.new(
  40. {
  41. "type": EventTypes.Member,
  42. "sender": user.to_string(),
  43. "state_key": user.to_string(),
  44. "room_id": room.to_string(),
  45. "content": {"membership": membership},
  46. }
  47. )
  48. event, context = yield self.event_creation_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. [
  59. m.room_id
  60. for m in (
  61. yield self.store.get_rooms_for_user_where_membership_is(
  62. self.u_alice.to_string(), [Membership.JOIN]
  63. )
  64. )
  65. ],
  66. )