test_roommember.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 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. Membership.JOIN,
  58. (yield self.store.get_room_member(
  59. user_id=self.u_alice.to_string(),
  60. room_id=self.room.to_string(),
  61. )).membership
  62. )
  63. self.assertEquals(
  64. [self.u_alice.to_string()],
  65. [m.user_id for m in (
  66. yield self.store.get_room_members(self.room.to_string())
  67. )]
  68. )
  69. self.assertEquals(
  70. [self.room.to_string()],
  71. [m.room_id for m in (
  72. yield self.store.get_rooms_for_user_where_membership_is(
  73. self.u_alice.to_string(), [Membership.JOIN]
  74. ))
  75. ]
  76. )
  77. self.assertFalse(
  78. (yield self.store.user_rooms_intersect(
  79. [self.u_alice.to_string(), self.u_bob.to_string()]
  80. ))
  81. )
  82. @defer.inlineCallbacks
  83. def test_two_members(self):
  84. yield self.inject_room_member(self.room, self.u_alice, Membership.JOIN)
  85. yield self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  86. self.assertEquals(
  87. {self.u_alice.to_string(), self.u_bob.to_string()},
  88. {m.user_id for m in (
  89. yield self.store.get_room_members(self.room.to_string())
  90. )}
  91. )
  92. self.assertTrue(
  93. (yield self.store.user_rooms_intersect(
  94. [self.u_alice.to_string(), self.u_bob.to_string()]
  95. ))
  96. )
  97. @defer.inlineCallbacks
  98. def test_room_hosts(self):
  99. yield self.inject_room_member(self.room, self.u_alice, Membership.JOIN)
  100. self.assertEquals(
  101. {"test"},
  102. (yield self.store.get_joined_hosts_for_room(self.room.to_string()))
  103. )
  104. # Should still have just one host after second join from it
  105. yield self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  106. self.assertEquals(
  107. {"test"},
  108. (yield self.store.get_joined_hosts_for_room(self.room.to_string()))
  109. )
  110. # Should now have two hosts after join from other host
  111. yield self.inject_room_member(self.room, self.u_charlie, Membership.JOIN)
  112. self.assertEquals(
  113. {"test", "elsewhere"},
  114. (yield
  115. self.store.get_joined_hosts_for_room(self.room.to_string())
  116. )
  117. )
  118. # Should still have both hosts
  119. yield self.inject_room_member(self.room, self.u_alice, Membership.LEAVE)
  120. self.assertEquals(
  121. {"test", "elsewhere"},
  122. (yield
  123. self.store.get_joined_hosts_for_room(self.room.to_string())
  124. )
  125. )
  126. # Should have only one host after other leaves
  127. yield self.inject_room_member(self.room, self.u_charlie, Membership.LEAVE)
  128. self.assertEquals(
  129. {"test"},
  130. (yield self.store.get_joined_hosts_for_room(self.room.to_string()))
  131. )