test_stream.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.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. )
  56. self.assertEqual(1, len(results))
  57. event = results[0]
  58. self.assertObjectHasAttributes(
  59. {
  60. "type": EventTypes.Message,
  61. "user_id": self.u_alice.to_string(),
  62. "content": {"body": "test", "msgtype": "message"},
  63. },
  64. event,
  65. )
  66. @defer.inlineCallbacks
  67. def test_event_stream_get_own(self):
  68. # Both bob and alice joins the room
  69. yield self.event_injector.inject_room_member(
  70. self.room1, self.u_alice, Membership.JOIN
  71. )
  72. yield self.event_injector.inject_room_member(
  73. self.room1, self.u_bob, Membership.JOIN
  74. )
  75. # Initial stream key:
  76. start = yield self.store.get_room_events_max_id()
  77. yield self.event_injector.inject_message(self.room1, self.u_alice, u"test")
  78. end = yield self.store.get_room_events_max_id()
  79. results, _ = yield self.store.get_room_events_stream(
  80. self.u_alice.to_string(),
  81. start,
  82. end,
  83. )
  84. self.assertEqual(1, len(results))
  85. event = results[0]
  86. self.assertObjectHasAttributes(
  87. {
  88. "type": EventTypes.Message,
  89. "user_id": self.u_alice.to_string(),
  90. "content": {"body": "test", "msgtype": "message"},
  91. },
  92. event,
  93. )
  94. @defer.inlineCallbacks
  95. def test_event_stream_join_leave(self):
  96. # Both bob and alice joins the room
  97. yield self.event_injector.inject_room_member(
  98. self.room1, self.u_alice, Membership.JOIN
  99. )
  100. yield self.event_injector.inject_room_member(
  101. self.room1, self.u_bob, Membership.JOIN
  102. )
  103. # Then bob leaves again.
  104. yield self.event_injector.inject_room_member(
  105. self.room1, self.u_bob, Membership.LEAVE
  106. )
  107. # Initial stream key:
  108. start = yield self.store.get_room_events_max_id()
  109. yield self.event_injector.inject_message(self.room1, self.u_alice, u"test")
  110. end = yield self.store.get_room_events_max_id()
  111. results, _ = yield self.store.get_room_events_stream(
  112. self.u_bob.to_string(),
  113. start,
  114. end,
  115. )
  116. # We should not get the message, as it happened *after* bob left.
  117. self.assertEqual(0, len(results))
  118. @defer.inlineCallbacks
  119. def test_event_stream_prev_content(self):
  120. yield self.event_injector.inject_room_member(
  121. self.room1, self.u_bob, Membership.JOIN
  122. )
  123. yield self.event_injector.inject_room_member(
  124. self.room1, self.u_alice, Membership.JOIN
  125. )
  126. start = yield self.store.get_room_events_max_id()
  127. yield self.event_injector.inject_room_member(
  128. self.room1, self.u_alice, Membership.JOIN,
  129. )
  130. end = yield self.store.get_room_events_max_id()
  131. results, _ = yield self.store.get_room_events_stream(
  132. self.u_bob.to_string(),
  133. start,
  134. end,
  135. )
  136. # We should not get the message, as it happened *after* bob left.
  137. self.assertEqual(1, len(results))
  138. event = results[0]
  139. self.assertTrue(
  140. "prev_content" in event.unsigned,
  141. msg="No prev_content key"
  142. )