summary refs log tree commit diff
path: root/tests/storage
diff options
context:
space:
mode:
authorDavid Baker <dave@matrix.org>2014-12-18 15:15:22 +0000
committerDavid Baker <dave@matrix.org>2014-12-18 15:15:22 +0000
commitb56730bb6e549e6b22c95858cae50e091de96844 (patch)
tree0eb011bc6b32a44df8c343ffedbc15e2ddd046e9 /tests/storage
parentschema version is now 10 (diff)
parentUpdate README.rst (diff)
downloadsynapse-b56730bb6e549e6b22c95858cae50e091de96844.tar.xz
Merge branch 'develop' into pushers
Conflicts:
	synapse/api/errors.py
	synapse/server.py
	synapse/storage/__init__.py
Diffstat (limited to 'tests/storage')
-rw-r--r--tests/storage/test_redaction.py141
-rw-r--r--tests/storage/test_room.py8
-rw-r--r--tests/storage/test_roommember.py52
-rw-r--r--tests/storage/test_stream.py85
4 files changed, 135 insertions, 151 deletions
diff --git a/tests/storage/test_redaction.py b/tests/storage/test_redaction.py
index adfe64a980..9806fbc69b 100644
--- a/tests/storage/test_redaction.py
+++ b/tests/storage/test_redaction.py
@@ -18,12 +18,11 @@ from tests import unittest
 from twisted.internet import defer
 
 from synapse.server import HomeServer
-from synapse.api.constants import Membership
-from synapse.api.events.room import (
-    RoomMemberEvent, MessageEvent, RoomRedactionEvent,
-)
+from synapse.api.constants import EventTypes, Membership
 
-from tests.utils import SQLiteMemoryDbPool
+from tests.utils import SQLiteMemoryDbPool, MockKey
+
+from mock import Mock
 
 
 class RedactionTestCase(unittest.TestCase):
@@ -33,13 +32,21 @@ class RedactionTestCase(unittest.TestCase):
         db_pool = SQLiteMemoryDbPool()
         yield db_pool.prepare()
 
+        self.mock_config = Mock()
+        self.mock_config.signing_key = [MockKey()]
+
         hs = HomeServer(
             "test",
             db_pool=db_pool,
+            config=self.mock_config,
+            resource_for_federation=Mock(),
+            http_client=None,
         )
 
         self.store = hs.get_datastore()
-        self.event_factory = hs.get_event_factory()
+        self.event_builder_factory = hs.get_event_builder_factory()
+        self.handlers = hs.get_handlers()
+        self.message_handler = self.handlers.message_handler
 
         self.u_alice = hs.parse_userid("@alice:test")
         self.u_bob = hs.parse_userid("@bob:test")
@@ -49,35 +56,23 @@ class RedactionTestCase(unittest.TestCase):
         self.depth = 1
 
     @defer.inlineCallbacks
-    def inject_room_member(self, room, user, membership, prev_state=None,
+    def inject_room_member(self, room, user, membership, replaces_state=None,
                            extra_content={}):
-        self.depth += 1
-
-        event = self.event_factory.create_event(
-            etype=RoomMemberEvent.TYPE,
-            user_id=user.to_string(),
-            state_key=user.to_string(),
-            room_id=room.to_string(),
-            membership=membership,
-            content={"membership": membership},
-            depth=self.depth,
-            prev_events=[],
+        content = {"membership": membership}
+        content.update(extra_content)
+        builder = self.event_builder_factory.new({
+            "type": EventTypes.Member,
+            "sender": user.to_string(),
+            "state_key": user.to_string(),
+            "room_id": room.to_string(),
+            "content": content,
+        })
+
+        event, context = yield self.message_handler._create_new_client_event(
+            builder
         )
 
-        event.content.update(extra_content)
-
-        if prev_state:
-            event.prev_state = prev_state
-
-        event.state_events = None
-        event.hashes = {}
-        event.prev_state = []
-        event.auth_events = []
-
-        # Have to create a join event using the eventfactory
-        yield self.store.persist_event(
-            event
-        )
+        yield self.store.persist_event(event, context)
 
         defer.returnValue(event)
 
@@ -85,46 +80,38 @@ class RedactionTestCase(unittest.TestCase):
     def inject_message(self, room, user, body):
         self.depth += 1
 
-        event = self.event_factory.create_event(
-            etype=MessageEvent.TYPE,
-            user_id=user.to_string(),
-            room_id=room.to_string(),
-            content={"body": body, "msgtype": u"message"},
-            depth=self.depth,
-            prev_events=[],
-        )
+        builder = self.event_builder_factory.new({
+            "type": EventTypes.Message,
+            "sender": user.to_string(),
+            "state_key": user.to_string(),
+            "room_id": room.to_string(),
+            "content": {"body": body, "msgtype": u"message"},
+        })
 
-        event.state_events = None
-        event.hashes = {}
-        event.auth_events = []
-
-        yield self.store.persist_event(
-            event
+        event, context = yield self.message_handler._create_new_client_event(
+            builder
         )
 
+        yield self.store.persist_event(event, context)
+
         defer.returnValue(event)
 
     @defer.inlineCallbacks
     def inject_redaction(self, room, event_id, user, reason):
-        event = self.event_factory.create_event(
-            etype=RoomRedactionEvent.TYPE,
-            user_id=user.to_string(),
-            room_id=room.to_string(),
-            content={"reason": reason},
-            depth=self.depth,
-            redacts=event_id,
-            prev_events=[],
+        builder = self.event_builder_factory.new({
+            "type": EventTypes.Redaction,
+            "sender": user.to_string(),
+            "state_key": user.to_string(),
+            "room_id": room.to_string(),
+            "content": {"reason": reason},
+            "redacts": event_id,
+        })
+
+        event, context = yield self.message_handler._create_new_client_event(
+            builder
         )
 
-        event.state_events = None
-        event.hashes = {}
-        event.auth_events = []
-
-        yield self.store.persist_event(
-            event
-        )
-
-        defer.returnValue(event)
+        yield self.store.persist_event(event, context)
 
     @defer.inlineCallbacks
     def test_redact(self):
@@ -152,14 +139,14 @@ class RedactionTestCase(unittest.TestCase):
 
         self.assertObjectHasAttributes(
             {
-                "type": MessageEvent.TYPE,
+                "type": EventTypes.Message,
                 "user_id": self.u_alice.to_string(),
                 "content": {"body": "t", "msgtype": "message"},
             },
             event,
         )
 
-        self.assertFalse(hasattr(event, "redacted_because"))
+        self.assertFalse("redacted_because" in event.unsigned)
 
         # Redact event
         reason = "Because I said so"
@@ -180,24 +167,26 @@ class RedactionTestCase(unittest.TestCase):
 
         event = results[0]
 
+        self.assertEqual(msg_event.event_id, event.event_id)
+
+        self.assertTrue("redacted_because" in event.unsigned)
+
         self.assertObjectHasAttributes(
             {
-                "type": MessageEvent.TYPE,
+                "type": EventTypes.Message,
                 "user_id": self.u_alice.to_string(),
                 "content": {},
             },
             event,
         )
 
-        self.assertTrue(hasattr(event, "redacted_because"))
-
         self.assertObjectHasAttributes(
             {
-                "type": RoomRedactionEvent.TYPE,
+                "type": EventTypes.Redaction,
                 "user_id": self.u_alice.to_string(),
                 "content": {"reason": reason},
             },
-            event.redacted_because,
+            event.unsigned["redacted_because"],
         )
 
     @defer.inlineCallbacks
@@ -229,7 +218,7 @@ class RedactionTestCase(unittest.TestCase):
 
         self.assertObjectHasAttributes(
             {
-                "type": RoomMemberEvent.TYPE,
+                "type": EventTypes.Member,
                 "user_id": self.u_bob.to_string(),
                 "content": {"membership": Membership.JOIN, "blue": "red"},
             },
@@ -257,22 +246,22 @@ class RedactionTestCase(unittest.TestCase):
 
         event = results[0]
 
+        self.assertTrue("redacted_because" in event.unsigned)
+
         self.assertObjectHasAttributes(
             {
-                "type": RoomMemberEvent.TYPE,
+                "type": EventTypes.Member,
                 "user_id": self.u_bob.to_string(),
                 "content": {"membership": Membership.JOIN},
             },
             event,
         )
 
-        self.assertTrue(hasattr(event, "redacted_because"))
-
         self.assertObjectHasAttributes(
             {
-                "type": RoomRedactionEvent.TYPE,
+                "type": EventTypes.Redaction,
                 "user_id": self.u_alice.to_string(),
                 "content": {"reason": reason},
             },
-            event.redacted_because,
+            event.unsigned["redacted_because"],
         )
diff --git a/tests/storage/test_room.py b/tests/storage/test_room.py
index 4ff02c306b..11761fe29a 100644
--- a/tests/storage/test_room.py
+++ b/tests/storage/test_room.py
@@ -18,9 +18,7 @@ from tests import unittest
 from twisted.internet import defer
 
 from synapse.server import HomeServer
-from synapse.api.events.room import (
-    RoomNameEvent, RoomTopicEvent
-)
+from synapse.api.constants import EventTypes
 
 from tests.utils import SQLiteMemoryDbPool
 
@@ -131,7 +129,7 @@ class RoomEventsStoreTestCase(unittest.TestCase):
         name = u"A-Room-Name"
 
         yield self.inject_room_event(
-            etype=RoomNameEvent.TYPE,
+            etype=EventTypes.Name,
             name=name,
             content={"name": name},
             depth=1,
@@ -154,7 +152,7 @@ class RoomEventsStoreTestCase(unittest.TestCase):
         topic = u"A place for things"
 
         yield self.inject_room_event(
-            etype=RoomTopicEvent.TYPE,
+            etype=EventTypes.Topic,
             topic=topic,
             content={"topic": topic},
             depth=1,
diff --git a/tests/storage/test_roommember.py b/tests/storage/test_roommember.py
index 8614e5ca9d..a23a8189df 100644
--- a/tests/storage/test_roommember.py
+++ b/tests/storage/test_roommember.py
@@ -18,10 +18,11 @@ from tests import unittest
 from twisted.internet import defer
 
 from synapse.server import HomeServer
-from synapse.api.constants import Membership
-from synapse.api.events.room import RoomMemberEvent
+from synapse.api.constants import EventTypes, Membership
 
-from tests.utils import SQLiteMemoryDbPool
+from tests.utils import SQLiteMemoryDbPool, MockKey
+
+from mock import Mock
 
 
 class RoomMemberStoreTestCase(unittest.TestCase):
@@ -31,14 +32,22 @@ class RoomMemberStoreTestCase(unittest.TestCase):
         db_pool = SQLiteMemoryDbPool()
         yield db_pool.prepare()
 
-        hs = HomeServer("test",
+        self.mock_config = Mock()
+        self.mock_config.signing_key = [MockKey()]
+
+        hs = HomeServer(
+            "test",
             db_pool=db_pool,
+            config=self.mock_config,
+            resource_for_federation=Mock(),
+            http_client=None,
         )
-
         # We can't test the RoomMemberStore on its own without the other event
         # storage logic
         self.store = hs.get_datastore()
-        self.event_factory = hs.get_event_factory()
+        self.event_builder_factory = hs.get_event_builder_factory()
+        self.handlers = hs.get_handlers()
+        self.message_handler = self.handlers.message_handler
 
         self.u_alice = hs.parse_userid("@alice:test")
         self.u_bob = hs.parse_userid("@bob:test")
@@ -49,27 +58,22 @@ class RoomMemberStoreTestCase(unittest.TestCase):
         self.room = hs.parse_roomid("!abc123:test")
 
     @defer.inlineCallbacks
-    def inject_room_member(self, room, user, membership):
-        # Have to create a join event using the eventfactory
-        event = self.event_factory.create_event(
-            etype=RoomMemberEvent.TYPE,
-            user_id=user.to_string(),
-            state_key=user.to_string(),
-            room_id=room.to_string(),
-            membership=membership,
-            content={"membership": membership},
-            depth=1,
-            prev_events=[],
+    def inject_room_member(self, room, user, membership, replaces_state=None):
+        builder = self.event_builder_factory.new({
+            "type": EventTypes.Member,
+            "sender": user.to_string(),
+            "state_key": user.to_string(),
+            "room_id": room.to_string(),
+            "content": {"membership": membership},
+        })
+
+        event, context = yield self.message_handler._create_new_client_event(
+            builder
         )
 
-        event.state_events = None
-        event.hashes = {}
-        event.prev_state = {}
-        event.auth_events = {}
+        yield self.store.persist_event(event, context)
 
-        yield self.store.persist_event(
-            event
-        )
+        defer.returnValue(event)
 
     @defer.inlineCallbacks
     def test_one_member(self):
diff --git a/tests/storage/test_stream.py b/tests/storage/test_stream.py
index 5038546aee..9247fc579e 100644
--- a/tests/storage/test_stream.py
+++ b/tests/storage/test_stream.py
@@ -18,10 +18,11 @@ from tests import unittest
 from twisted.internet import defer
 
 from synapse.server import HomeServer
-from synapse.api.constants import Membership
-from synapse.api.events.room import RoomMemberEvent, MessageEvent
+from synapse.api.constants import EventTypes, Membership
 
-from tests.utils import SQLiteMemoryDbPool
+from tests.utils import SQLiteMemoryDbPool, MockKey
+
+from mock import Mock
 
 
 class StreamStoreTestCase(unittest.TestCase):
@@ -31,13 +32,21 @@ class StreamStoreTestCase(unittest.TestCase):
         db_pool = SQLiteMemoryDbPool()
         yield db_pool.prepare()
 
+        self.mock_config = Mock()
+        self.mock_config.signing_key = [MockKey()]
+
         hs = HomeServer(
             "test",
             db_pool=db_pool,
+            config=self.mock_config,
+            resource_for_federation=Mock(),
+            http_client=None,
         )
 
         self.store = hs.get_datastore()
-        self.event_factory = hs.get_event_factory()
+        self.event_builder_factory = hs.get_event_builder_factory()
+        self.handlers = hs.get_handlers()
+        self.message_handler = self.handlers.message_handler
 
         self.u_alice = hs.parse_userid("@alice:test")
         self.u_bob = hs.parse_userid("@bob:test")
@@ -48,58 +57,43 @@ class StreamStoreTestCase(unittest.TestCase):
         self.depth = 1
 
     @defer.inlineCallbacks
-    def inject_room_member(self, room, user, membership, replaces_state=None):
+    def inject_room_member(self, room, user, membership):
         self.depth += 1
 
-        event = self.event_factory.create_event(
-            etype=RoomMemberEvent.TYPE,
-            user_id=user.to_string(),
-            state_key=user.to_string(),
-            room_id=room.to_string(),
-            membership=membership,
-            content={"membership": membership},
-            depth=self.depth,
-            prev_events=[],
-        )
-
-        event.state_events = None
-        event.hashes = {}
-        event.prev_state = []
-        event.auth_events = []
-
-        if replaces_state:
-            event.prev_state = [(replaces_state, "hash")]
-            event.replaces_state = replaces_state
+        builder = self.event_builder_factory.new({
+            "type": EventTypes.Member,
+            "sender": user.to_string(),
+            "state_key": user.to_string(),
+            "room_id": room.to_string(),
+            "content": {"membership": membership},
+        })
 
-        # Have to create a join event using the eventfactory
-        yield self.store.persist_event(
-            event
+        event, context = yield self.message_handler._create_new_client_event(
+            builder
         )
 
+        yield self.store.persist_event(event, context)
+
         defer.returnValue(event)
 
     @defer.inlineCallbacks
     def inject_message(self, room, user, body):
         self.depth += 1
 
-        event = self.event_factory.create_event(
-            etype=MessageEvent.TYPE,
-            user_id=user.to_string(),
-            room_id=room.to_string(),
-            content={"body": body, "msgtype": u"message"},
-            depth=self.depth,
-            prev_events=[],
-        )
-
-        event.state_events = None
-        event.hashes = {}
-        event.auth_events = []
+        builder = self.event_builder_factory.new({
+            "type": EventTypes.Message,
+            "sender": user.to_string(),
+            "state_key": user.to_string(),
+            "room_id": room.to_string(),
+            "content": {"body": body, "msgtype": u"message"},
+        })
 
-        # Have to create a join event using the eventfactory
-        yield self.store.persist_event(
-            event
+        event, context = yield self.message_handler._create_new_client_event(
+            builder
         )
 
+        yield self.store.persist_event(event, context)
+
     @defer.inlineCallbacks
     def test_event_stream_get_other(self):
         # Both bob and alice joins the room
@@ -130,7 +124,7 @@ class StreamStoreTestCase(unittest.TestCase):
 
         self.assertObjectHasAttributes(
             {
-                "type": MessageEvent.TYPE,
+                "type": EventTypes.Message,
                 "user_id": self.u_alice.to_string(),
                 "content": {"body": "test", "msgtype": "message"},
             },
@@ -167,7 +161,7 @@ class StreamStoreTestCase(unittest.TestCase):
 
         self.assertObjectHasAttributes(
             {
-                "type": MessageEvent.TYPE,
+                "type": EventTypes.Message,
                 "user_id": self.u_alice.to_string(),
                 "content": {"body": "test", "msgtype": "message"},
             },
@@ -220,7 +214,6 @@ class StreamStoreTestCase(unittest.TestCase):
 
         event2 = yield self.inject_room_member(
             self.room1, self.u_alice, Membership.JOIN,
-            replaces_state=event1.event_id,
         )
 
         end = yield self.store.get_room_events_max_id()
@@ -238,6 +231,6 @@ class StreamStoreTestCase(unittest.TestCase):
         event = results[0]
 
         self.assertTrue(
-            hasattr(event, "prev_content"),
+            "prev_content" in event.unsigned,
             msg="No prev_content key"
         )