test_stream.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.storage.event_injector import EventInjector
  20. from tests.utils import setup_test_homeserver
  21. from mock import Mock
  22. class StreamStoreTestCase(unittest.TestCase):
  23. @defer.inlineCallbacks
  24. def setUp(self):
  25. hs = yield setup_test_homeserver(
  26. resource_for_federation=Mock(),
  27. http_client=None,
  28. )
  29. self.store = hs.get_datastore()
  30. self.event_builder_factory = hs.get_event_builder_factory()
  31. self.event_injector = EventInjector(hs)
  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. self.room1 = RoomID.from_string("!abc123:test")
  37. self.room2 = RoomID.from_string("!xyx987:test")
  38. @defer.inlineCallbacks
  39. def test_event_stream_get_other(self):
  40. # Both bob and alice joins the room
  41. yield self.event_injector.inject_room_member(
  42. self.room1, self.u_alice, Membership.JOIN
  43. )
  44. yield self.event_injector.inject_room_member(
  45. self.room1, self.u_bob, Membership.JOIN
  46. )
  47. # Initial stream key:
  48. start = yield self.store.get_room_events_max_id()
  49. yield self.event_injector.inject_message(self.room1, self.u_alice, u"test")
  50. end = yield self.store.get_room_events_max_id()
  51. results, _ = yield self.store.get_room_events_stream(
  52. self.u_bob.to_string(),
  53. start,
  54. end,
  55. None, # Is currently ignored
  56. )
  57. self.assertEqual(1, len(results))
  58. event = results[0]
  59. self.assertObjectHasAttributes(
  60. {
  61. "type": EventTypes.Message,
  62. "user_id": self.u_alice.to_string(),
  63. "content": {"body": "test", "msgtype": "message"},
  64. },
  65. event,
  66. )
  67. @defer.inlineCallbacks
  68. def test_event_stream_get_own(self):
  69. # Both bob and alice joins the room
  70. yield self.event_injector.inject_room_member(
  71. self.room1, self.u_alice, Membership.JOIN
  72. )
  73. yield self.event_injector.inject_room_member(
  74. self.room1, self.u_bob, Membership.JOIN
  75. )
  76. # Initial stream key:
  77. start = yield self.store.get_room_events_max_id()
  78. yield self.event_injector.inject_message(self.room1, self.u_alice, u"test")
  79. end = yield self.store.get_room_events_max_id()
  80. results, _ = yield self.store.get_room_events_stream(
  81. self.u_alice.to_string(),
  82. start,
  83. end,
  84. None, # Is currently ignored
  85. )
  86. self.assertEqual(1, len(results))
  87. event = results[0]
  88. self.assertObjectHasAttributes(
  89. {
  90. "type": EventTypes.Message,
  91. "user_id": self.u_alice.to_string(),
  92. "content": {"body": "test", "msgtype": "message"},
  93. },
  94. event,
  95. )
  96. @defer.inlineCallbacks
  97. def test_event_stream_join_leave(self):
  98. # Both bob and alice joins the room
  99. yield self.event_injector.inject_room_member(
  100. self.room1, self.u_alice, Membership.JOIN
  101. )
  102. yield self.event_injector.inject_room_member(
  103. self.room1, self.u_bob, Membership.JOIN
  104. )
  105. # Then bob leaves again.
  106. yield self.event_injector.inject_room_member(
  107. self.room1, self.u_bob, Membership.LEAVE
  108. )
  109. # Initial stream key:
  110. start = yield self.store.get_room_events_max_id()
  111. yield self.event_injector.inject_message(self.room1, self.u_alice, u"test")
  112. end = yield self.store.get_room_events_max_id()
  113. results, _ = yield self.store.get_room_events_stream(
  114. self.u_bob.to_string(),
  115. start,
  116. end,
  117. None, # Is currently ignored
  118. )
  119. # We should not get the message, as it happened *after* bob left.
  120. self.assertEqual(0, len(results))
  121. @defer.inlineCallbacks
  122. def test_event_stream_prev_content(self):
  123. yield self.event_injector.inject_room_member(
  124. self.room1, self.u_bob, Membership.JOIN
  125. )
  126. event1 = yield self.event_injector.inject_room_member(
  127. self.room1, self.u_alice, Membership.JOIN
  128. )
  129. start = yield self.store.get_room_events_max_id()
  130. event2 = yield self.event_injector.inject_room_member(
  131. self.room1, self.u_alice, Membership.JOIN,
  132. )
  133. end = yield self.store.get_room_events_max_id()
  134. results, _ = yield self.store.get_room_events_stream(
  135. self.u_bob.to_string(),
  136. start,
  137. end,
  138. None, # Is currently ignored
  139. )
  140. # We should not get the message, as it happened *after* bob left.
  141. self.assertEqual(1, len(results))
  142. event = results[0]
  143. self.assertTrue(
  144. "prev_content" in event.unsigned,
  145. msg="No prev_content key"
  146. )