summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/api/test_filtering.py7
-rw-r--r--tests/federation/__init__.py0
-rw-r--r--tests/federation/test_federation.py303
-rw-r--r--tests/rest/client/v1/test_presence.py18
-rw-r--r--tests/rest/client/v1/test_rooms.py13
-rw-r--r--tests/storage/test_appservice.py6
-rw-r--r--tests/storage/test_registration.py3
-rw-r--r--tests/test_types.py5
-rw-r--r--tests/util/test_lrucache.py7
-rw-r--r--tests/util/test_treecache.py12
-rw-r--r--tests/utils.py26
11 files changed, 74 insertions, 326 deletions
diff --git a/tests/api/test_filtering.py b/tests/api/test_filtering.py
index 1a4e439d30..ceb0089268 100644
--- a/tests/api/test_filtering.py
+++ b/tests/api/test_filtering.py
@@ -382,19 +382,20 @@ class FilteringTestCase(unittest.TestCase):
                 "types": ["m.*"]
             }
         }
-        user = UserID.from_string("@" + user_localpart + ":test")
+
         filter_id = yield self.datastore.add_user_filter(
-            user_localpart=user_localpart,
+            user_localpart=user_localpart + "2",
             user_filter=user_filter_json,
         )
         event = MockEvent(
+            event_id="$asdasd:localhost",
             sender="@foo:bar",
             type="custom.avatar.3d.crazy",
         )
         events = [event]
 
         user_filter = yield self.filtering.get_user_filter(
-            user_localpart=user_localpart,
+            user_localpart=user_localpart + "2",
             filter_id=filter_id,
         )
 
diff --git a/tests/federation/__init__.py b/tests/federation/__init__.py
deleted file mode 100644
index e69de29bb2..0000000000
--- a/tests/federation/__init__.py
+++ /dev/null
diff --git a/tests/federation/test_federation.py b/tests/federation/test_federation.py
deleted file mode 100644
index f2c2ee4127..0000000000
--- a/tests/federation/test_federation.py
+++ /dev/null
@@ -1,303 +0,0 @@
-# Copyright 2014-2016 OpenMarket Ltd
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-# trial imports
-from twisted.internet import defer
-from tests import unittest
-
-# python imports
-from mock import Mock, ANY
-
-from ..utils import MockHttpResource, MockClock, setup_test_homeserver
-
-from synapse.federation import initialize_http_replication
-from synapse.events import FrozenEvent
-
-
-def make_pdu(prev_pdus=[], **kwargs):
-    """Provide some default fields for making a PduTuple."""
-    pdu_fields = {
-        "state_key": None,
-        "prev_events": prev_pdus,
-    }
-    pdu_fields.update(kwargs)
-
-    return FrozenEvent(pdu_fields)
-
-
-class FederationTestCase(unittest.TestCase):
-    @defer.inlineCallbacks
-    def setUp(self):
-        self.mock_resource = MockHttpResource()
-        self.mock_http_client = Mock(spec=[
-            "get_json",
-            "put_json",
-        ])
-        self.mock_persistence = Mock(spec=[
-            "prep_send_transaction",
-            "delivered_txn",
-            "get_received_txn_response",
-            "set_received_txn_response",
-            "get_destination_retry_timings",
-            "get_auth_chain",
-        ])
-        self.mock_persistence.get_received_txn_response.return_value = (
-            defer.succeed(None)
-        )
-
-        retry_timings_res = {
-            "destination": "",
-            "retry_last_ts": 0,
-            "retry_interval": 0,
-        }
-        self.mock_persistence.get_destination_retry_timings.return_value = (
-            defer.succeed(retry_timings_res)
-        )
-        self.mock_persistence.get_auth_chain.return_value = []
-        self.clock = MockClock()
-        hs = yield setup_test_homeserver(
-            resource_for_federation=self.mock_resource,
-            http_client=self.mock_http_client,
-            datastore=self.mock_persistence,
-            clock=self.clock,
-            keyring=Mock(),
-        )
-        self.federation = initialize_http_replication(hs)
-        self.distributor = hs.get_distributor()
-
-    @defer.inlineCallbacks
-    def test_get_state(self):
-        mock_handler = Mock(spec=[
-            "get_state_for_pdu",
-        ])
-
-        self.federation.set_handler(mock_handler)
-
-        mock_handler.get_state_for_pdu.return_value = defer.succeed([])
-
-        # Empty context initially
-        (code, response) = yield self.mock_resource.trigger(
-            "GET",
-            "/_matrix/federation/v1/state/my-context/",
-            None
-        )
-        self.assertEquals(200, code)
-        self.assertFalse(response["pdus"])
-
-        # Now lets give the context some state
-        mock_handler.get_state_for_pdu.return_value = (
-            defer.succeed([
-                make_pdu(
-                    event_id="the-pdu-id",
-                    origin="red",
-                    user_id="@a:red",
-                    room_id="my-context",
-                    type="m.topic",
-                    origin_server_ts=123456789000,
-                    depth=1,
-                    content={"topic": "The topic"},
-                    state_key="",
-                    power_level=1000,
-                    prev_state="last-pdu-id",
-                ),
-            ])
-        )
-
-        (code, response) = yield self.mock_resource.trigger(
-            "GET",
-            "/_matrix/federation/v1/state/my-context/",
-            None
-        )
-        self.assertEquals(200, code)
-        self.assertEquals(1, len(response["pdus"]))
-
-    @defer.inlineCallbacks
-    def test_get_pdu(self):
-        mock_handler = Mock(spec=[
-            "get_persisted_pdu",
-        ])
-
-        self.federation.set_handler(mock_handler)
-
-        mock_handler.get_persisted_pdu.return_value = (
-            defer.succeed(None)
-        )
-
-        (code, response) = yield self.mock_resource.trigger(
-            "GET",
-            "/_matrix/federation/v1/event/abc123def456/",
-            None
-        )
-        self.assertEquals(404, code)
-
-        # Now insert such a PDU
-        mock_handler.get_persisted_pdu.return_value = (
-            defer.succeed(
-                make_pdu(
-                    event_id="abc123def456",
-                    origin="red",
-                    user_id="@a:red",
-                    room_id="my-context",
-                    type="m.text",
-                    origin_server_ts=123456789001,
-                    depth=1,
-                    content={"text": "Here is the message"},
-                )
-            )
-        )
-
-        (code, response) = yield self.mock_resource.trigger(
-            "GET",
-            "/_matrix/federation/v1/event/abc123def456/",
-            None
-        )
-        self.assertEquals(200, code)
-        self.assertEquals(1, len(response["pdus"]))
-        self.assertEquals("m.text", response["pdus"][0]["type"])
-
-    @defer.inlineCallbacks
-    def test_send_pdu(self):
-        self.mock_http_client.put_json.return_value = defer.succeed(
-            (200, "OK")
-        )
-
-        pdu = make_pdu(
-            event_id="abc123def456",
-            origin="red",
-            user_id="@a:red",
-            room_id="my-context",
-            type="m.text",
-            origin_server_ts=123456789001,
-            depth=1,
-            content={"text": "Here is the message"},
-        )
-
-        yield self.federation.send_pdu(pdu, ["remote"])
-
-        self.mock_http_client.put_json.assert_called_with(
-            "remote",
-            path="/_matrix/federation/v1/send/1000000/",
-            data={
-                "origin_server_ts": 1000000,
-                "origin": "test",
-                "pdus": [
-                    pdu.get_pdu_json(),
-                ],
-                'pdu_failures': [],
-            },
-            json_data_callback=ANY,
-            long_retries=True,
-        )
-
-    @defer.inlineCallbacks
-    def test_send_edu(self):
-        self.mock_http_client.put_json.return_value = defer.succeed(
-            (200, "OK")
-        )
-
-        yield self.federation.send_edu(
-            destination="remote",
-            edu_type="m.test",
-            content={"testing": "content here"},
-        )
-
-        # MockClock ensures we can guess these timestamps
-        self.mock_http_client.put_json.assert_called_with(
-            "remote",
-            path="/_matrix/federation/v1/send/1000000/",
-            data={
-                "origin": "test",
-                "origin_server_ts": 1000000,
-                "pdus": [],
-                "edus": [
-                    {
-                        "edu_type": "m.test",
-                        "content": {"testing": "content here"},
-                    }
-                ],
-                'pdu_failures': [],
-            },
-            json_data_callback=ANY,
-            long_retries=True,
-        )
-
-    @defer.inlineCallbacks
-    def test_recv_edu(self):
-        recv_observer = Mock()
-        recv_observer.return_value = defer.succeed(())
-
-        self.federation.register_edu_handler("m.test", recv_observer)
-
-        yield self.mock_resource.trigger(
-            "PUT",
-            "/_matrix/federation/v1/send/1001000/",
-            """{
-                "origin": "remote",
-                "origin_server_ts": 1001000,
-                "pdus": [],
-                "edus": [
-                    {
-                        "origin": "remote",
-                        "destination": "test",
-                        "edu_type": "m.test",
-                        "content": {"testing": "reply here"}
-                    }
-                ]
-            }"""
-        )
-
-        recv_observer.assert_called_with(
-            "remote", {"testing": "reply here"}
-        )
-
-    @defer.inlineCallbacks
-    def test_send_query(self):
-        self.mock_http_client.get_json.return_value = defer.succeed(
-            {"your": "response"}
-        )
-
-        response = yield self.federation.make_query(
-            destination="remote",
-            query_type="a-question",
-            args={"one": "1", "two": "2"},
-        )
-
-        self.assertEquals({"your": "response"}, response)
-
-        self.mock_http_client.get_json.assert_called_with(
-            destination="remote",
-            path="/_matrix/federation/v1/query/a-question",
-            args={"one": "1", "two": "2"},
-            retry_on_dns_fail=True,
-        )
-
-    @defer.inlineCallbacks
-    def test_recv_query(self):
-        recv_handler = Mock()
-        recv_handler.return_value = defer.succeed({"another": "response"})
-
-        self.federation.register_query_handler("a-question", recv_handler)
-
-        code, response = yield self.mock_resource.trigger(
-            "GET",
-            "/_matrix/federation/v1/query/a-question?three=3&four=4",
-            None
-        )
-
-        self.assertEquals(200, code)
-        self.assertEquals({"another": "response"}, response)
-
-        recv_handler.assert_called_with(
-            {"three": "3", "four": "4"}
-        )
diff --git a/tests/rest/client/v1/test_presence.py b/tests/rest/client/v1/test_presence.py
index 90b911f879..8d7cfd79ab 100644
--- a/tests/rest/client/v1/test_presence.py
+++ b/tests/rest/client/v1/test_presence.py
@@ -280,6 +280,15 @@ class PresenceEventStreamTestCase(unittest.TestCase):
         }
         EventSources.SOURCE_TYPES["presence"] = PresenceEventSource
 
+        clock = Mock(spec=[
+            "call_later",
+            "cancel_call_later",
+            "time_msec",
+            "looping_call",
+        ])
+
+        clock.time_msec.return_value = 1000000
+
         hs = yield setup_test_homeserver(
             http_client=None,
             resource_for_client=self.mock_resource,
@@ -289,16 +298,9 @@ class PresenceEventStreamTestCase(unittest.TestCase):
                 "get_presence_list",
                 "get_rooms_for_user",
             ]),
-            clock=Mock(spec=[
-                "call_later",
-                "cancel_call_later",
-                "time_msec",
-                "looping_call",
-            ]),
+            clock=clock,
         )
 
-        hs.get_clock().time_msec.return_value = 1000000
-
         def _get_user_by_req(req=None, allow_guest=False):
             return Requester(UserID.from_string(myid), "", False)
 
diff --git a/tests/rest/client/v1/test_rooms.py b/tests/rest/client/v1/test_rooms.py
index cd03106e88..ad5dd3bd6e 100644
--- a/tests/rest/client/v1/test_rooms.py
+++ b/tests/rest/client/v1/test_rooms.py
@@ -1045,8 +1045,13 @@ class RoomMessageListTestCase(RestTestCase):
         self.assertTrue("end" in response)
 
     @defer.inlineCallbacks
-    def test_stream_token_is_rejected(self):
+    def test_stream_token_is_accepted_for_fwd_pagianation(self):
+        token = "s0_0_0_0_0"
         (code, response) = yield self.mock_resource.trigger_get(
-            "/rooms/%s/messages?access_token=x&from=s0_0_0_0" %
-            self.room_id)
-        self.assertEquals(400, code)
+            "/rooms/%s/messages?access_token=x&from=%s" %
+            (self.room_id, token))
+        self.assertEquals(200, code)
+        self.assertTrue("start" in response)
+        self.assertEquals(token, response['start'])
+        self.assertTrue("chunk" in response)
+        self.assertTrue("end" in response)
diff --git a/tests/storage/test_appservice.py b/tests/storage/test_appservice.py
index 5abecdf6e0..ed8af10d87 100644
--- a/tests/storage/test_appservice.py
+++ b/tests/storage/test_appservice.py
@@ -439,7 +439,7 @@ class ApplicationServiceStoreConfigTestCase(unittest.TestCase):
         f2 = self._write_config(suffix="2")
 
         config = Mock(app_service_config_files=[f1, f2])
-        hs = yield setup_test_homeserver(config=config)
+        hs = yield setup_test_homeserver(config=config, datastore=Mock())
 
         ApplicationServiceStore(hs)
 
@@ -449,7 +449,7 @@ class ApplicationServiceStoreConfigTestCase(unittest.TestCase):
         f2 = self._write_config(id="id", suffix="2")
 
         config = Mock(app_service_config_files=[f1, f2])
-        hs = yield setup_test_homeserver(config=config)
+        hs = yield setup_test_homeserver(config=config, datastore=Mock())
 
         with self.assertRaises(ConfigError) as cm:
             ApplicationServiceStore(hs)
@@ -465,7 +465,7 @@ class ApplicationServiceStoreConfigTestCase(unittest.TestCase):
         f2 = self._write_config(as_token="as_token", suffix="2")
 
         config = Mock(app_service_config_files=[f1, f2])
-        hs = yield setup_test_homeserver(config=config)
+        hs = yield setup_test_homeserver(config=config, datastore=Mock())
 
         with self.assertRaises(ConfigError) as cm:
             ApplicationServiceStore(hs)
diff --git a/tests/storage/test_registration.py b/tests/storage/test_registration.py
index a35efcc71e..7b3b4c13bc 100644
--- a/tests/storage/test_registration.py
+++ b/tests/storage/test_registration.py
@@ -18,7 +18,6 @@ from tests import unittest
 from twisted.internet import defer
 
 from synapse.api.errors import StoreError
-from synapse.storage.registration import RegistrationStore
 from synapse.util import stringutils
 
 from tests.utils import setup_test_homeserver
@@ -31,7 +30,7 @@ class RegistrationStoreTestCase(unittest.TestCase):
         hs = yield setup_test_homeserver()
         self.db_pool = hs.get_db_pool()
 
-        self.store = RegistrationStore(hs)
+        self.store = hs.get_datastore()
 
         self.user_id = "@my-user:test"
         self.tokens = ["AbCdEfGhIjKlMnOpQrStUvWxYz",
diff --git a/tests/test_types.py b/tests/test_types.py
index b9534329e6..24d61dbe54 100644
--- a/tests/test_types.py
+++ b/tests/test_types.py
@@ -16,10 +16,10 @@
 from tests import unittest
 
 from synapse.api.errors import SynapseError
-from synapse.server import BaseHomeServer
+from synapse.server import HomeServer
 from synapse.types import UserID, RoomAlias
 
-mock_homeserver = BaseHomeServer(hostname="my.domain")
+mock_homeserver = HomeServer(hostname="my.domain")
 
 
 class UserIDTestCase(unittest.TestCase):
@@ -34,7 +34,6 @@ class UserIDTestCase(unittest.TestCase):
         with self.assertRaises(SynapseError):
             UserID.from_string("")
 
-
     def test_build(self):
         user = UserID("5678efgh", "my.domain")
 
diff --git a/tests/util/test_lrucache.py b/tests/util/test_lrucache.py
index 2cd3d26454..bab366fb7f 100644
--- a/tests/util/test_lrucache.py
+++ b/tests/util/test_lrucache.py
@@ -19,6 +19,7 @@ from .. import unittest
 from synapse.util.caches.lrucache import LruCache
 from synapse.util.caches.treecache import TreeCache
 
+
 class LruCacheTestCase(unittest.TestCase):
 
     def test_get_set(self):
@@ -72,3 +73,9 @@ class LruCacheTestCase(unittest.TestCase):
         self.assertEquals(cache.get(("vehicles", "car")), "vroom")
         self.assertEquals(cache.get(("vehicles", "train")), "chuff")
         # Man from del_multi say "Yes".
+
+    def test_clear(self):
+        cache = LruCache(1)
+        cache["key"] = 1
+        cache.clear()
+        self.assertEquals(len(cache), 0)
diff --git a/tests/util/test_treecache.py b/tests/util/test_treecache.py
index 9946ceb3f1..1efbeb6b33 100644
--- a/tests/util/test_treecache.py
+++ b/tests/util/test_treecache.py
@@ -25,6 +25,7 @@ class TreeCacheTestCase(unittest.TestCase):
         cache[("b",)] = "B"
         self.assertEquals(cache.get(("a",)), "A")
         self.assertEquals(cache.get(("b",)), "B")
+        self.assertEquals(len(cache), 2)
 
     def test_pop_onelevel(self):
         cache = TreeCache()
@@ -33,6 +34,7 @@ class TreeCacheTestCase(unittest.TestCase):
         self.assertEquals(cache.pop(("a",)), "A")
         self.assertEquals(cache.pop(("a",)), None)
         self.assertEquals(cache.get(("b",)), "B")
+        self.assertEquals(len(cache), 1)
 
     def test_get_set_twolevel(self):
         cache = TreeCache()
@@ -42,6 +44,7 @@ class TreeCacheTestCase(unittest.TestCase):
         self.assertEquals(cache.get(("a", "a")), "AA")
         self.assertEquals(cache.get(("a", "b")), "AB")
         self.assertEquals(cache.get(("b", "a")), "BA")
+        self.assertEquals(len(cache), 3)
 
     def test_pop_twolevel(self):
         cache = TreeCache()
@@ -53,6 +56,7 @@ class TreeCacheTestCase(unittest.TestCase):
         self.assertEquals(cache.get(("a", "b")), "AB")
         self.assertEquals(cache.pop(("b", "a")), "BA")
         self.assertEquals(cache.pop(("b", "a")), None)
+        self.assertEquals(len(cache), 1)
 
     def test_pop_mixedlevel(self):
         cache = TreeCache()
@@ -64,3 +68,11 @@ class TreeCacheTestCase(unittest.TestCase):
         self.assertEquals(cache.get(("a", "a")), None)
         self.assertEquals(cache.get(("a", "b")), None)
         self.assertEquals(cache.get(("b", "a")), "BA")
+        self.assertEquals(len(cache), 1)
+
+    def test_clear(self):
+        cache = TreeCache()
+        cache[("a",)] = "A"
+        cache[("b",)] = "B"
+        cache.clear()
+        self.assertEquals(len(cache), 0)
diff --git a/tests/utils.py b/tests/utils.py
index 358b5b72b7..43cc2b30cd 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -19,6 +19,8 @@ from synapse.api.constants import EventTypes
 from synapse.storage.prepare_database import prepare_database
 from synapse.storage.engines import create_engine
 from synapse.server import HomeServer
+from synapse.federation.transport import server
+from synapse.util.ratelimitutils import FederationRateLimiter
 
 from synapse.util.logcontext import LoggingContext
 
@@ -58,8 +60,10 @@ def setup_test_homeserver(name="test", datastore=None, config=None, **kargs):
             name, db_pool=db_pool, config=config,
             version_string="Synapse/tests",
             database_engine=create_engine("sqlite3"),
+            get_db_conn=db_pool.get_db_conn,
             **kargs
         )
+        hs.setup()
     else:
         hs = HomeServer(
             name, db_pool=None, datastore=datastore, config=config,
@@ -80,6 +84,22 @@ def setup_test_homeserver(name="test", datastore=None, config=None, **kargs):
 
     hs.build_handlers = swap_out_hash_for_testing(hs.build_handlers)
 
+    fed = kargs.get("resource_for_federation", None)
+    if fed:
+        server.register_servlets(
+            hs,
+            resource=fed,
+            authenticator=server.Authenticator(hs),
+            ratelimiter=FederationRateLimiter(
+                hs.get_clock(),
+                window_size=hs.config.federation_rc_window_size,
+                sleep_limit=hs.config.federation_rc_sleep_limit,
+                sleep_msec=hs.config.federation_rc_sleep_delay,
+                reject_limit=hs.config.federation_rc_reject_limit,
+                concurrent_requests=hs.config.federation_rc_concurrent
+            ),
+        )
+
     defer.returnValue(hs)
 
 
@@ -262,6 +282,12 @@ class SQLiteMemoryDbPool(ConnectionPool, object):
             lambda conn: prepare_database(conn, engine)
         )
 
+    def get_db_conn(self):
+        conn = self.connect()
+        engine = create_engine("sqlite3")
+        prepare_database(conn, engine)
+        return conn
+
 
 class MemoryDataStore(object):