test_roommember.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.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. @defer.inlineCallbacks
  39. def inject_room_member(self, room, user, membership, replaces_state=None):
  40. builder = self.event_builder_factory.new({
  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. event, context = yield self.event_creation_handler.create_new_client_event(
  48. builder
  49. )
  50. yield self.store.persist_event(event, context)
  51. defer.returnValue(event)
  52. @defer.inlineCallbacks
  53. def test_one_member(self):
  54. yield self.inject_room_member(self.room, self.u_alice, Membership.JOIN)
  55. self.assertEquals(
  56. [self.room.to_string()],
  57. [m.room_id for m in (
  58. yield self.store.get_rooms_for_user_where_membership_is(
  59. self.u_alice.to_string(), [Membership.JOIN]
  60. )
  61. )]
  62. )