Merge pull request #63 from matrix-org/homeserver_test_setup

Factor out some of the common homeserver setup code
This commit is contained in:
Mark Haines 2015-02-11 11:46:08 +00:00
commit f42e29cf95
25 changed files with 140 additions and 360 deletions

View File

@ -19,11 +19,9 @@ from twisted.internet import defer
from mock import Mock, NonCallableMock from mock import Mock, NonCallableMock
from tests.utils import ( from tests.utils import (
MockHttpResource, MockClock, DeferredMockCallable, SQLiteMemoryDbPool, MockHttpResource, DeferredMockCallable, setup_test_homeserver
MockKey
) )
from synapse.server import HomeServer
from synapse.types import UserID from synapse.types import UserID
from synapse.api.filtering import Filter from synapse.api.filtering import Filter
@ -34,22 +32,14 @@ class FilteringTestCase(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def setUp(self): def setUp(self):
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
self.mock_config = NonCallableMock()
self.mock_config.signing_key = [MockKey()]
self.mock_federation_resource = MockHttpResource() self.mock_federation_resource = MockHttpResource()
self.mock_http_client = Mock(spec=[]) self.mock_http_client = Mock(spec=[])
self.mock_http_client.put_json = DeferredMockCallable() self.mock_http_client.put_json = DeferredMockCallable()
hs = HomeServer("test", hs = yield setup_test_homeserver(
db_pool=db_pool,
handlers=None, handlers=None,
http_client=self.mock_http_client, http_client=self.mock_http_client,
config=self.mock_config,
keyring=Mock(), keyring=Mock(),
) )

View File

@ -19,9 +19,8 @@ from tests import unittest
# python imports # python imports
from mock import Mock, ANY from mock import Mock, ANY
from ..utils import MockHttpResource, MockClock, MockKey from ..utils import MockHttpResource, MockClock, setup_test_homeserver
from synapse.server import HomeServer
from synapse.federation import initialize_http_replication from synapse.federation import initialize_http_replication
from synapse.events import FrozenEvent from synapse.events import FrozenEvent
@ -40,6 +39,7 @@ def make_pdu(prev_pdus=[], **kwargs):
class FederationTestCase(unittest.TestCase): class FederationTestCase(unittest.TestCase):
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource() self.mock_resource = MockHttpResource()
self.mock_http_client = Mock(spec=[ self.mock_http_client = Mock(spec=[
@ -61,17 +61,12 @@ class FederationTestCase(unittest.TestCase):
defer.succeed(DestinationsTable.EntryType("", 0, 0)) defer.succeed(DestinationsTable.EntryType("", 0, 0))
) )
self.mock_persistence.get_auth_chain.return_value = [] self.mock_persistence.get_auth_chain.return_value = []
self.mock_config = Mock()
self.mock_config.signing_key = [MockKey()]
self.clock = MockClock() self.clock = MockClock()
hs = HomeServer( hs = yield setup_test_homeserver(
"test",
resource_for_federation=self.mock_resource, resource_for_federation=self.mock_resource,
http_client=self.mock_http_client, http_client=self.mock_http_client,
db_pool=None,
datastore=self.mock_persistence, datastore=self.mock_persistence,
clock=self.clock, clock=self.clock,
config=self.mock_config,
keyring=Mock(), keyring=Mock(),
) )
self.federation = initialize_http_replication(hs) self.federation = initialize_http_replication(hs)

View File

@ -19,11 +19,10 @@ from twisted.internet import defer
from mock import Mock from mock import Mock
from synapse.server import HomeServer
from synapse.handlers.directory import DirectoryHandler from synapse.handlers.directory import DirectoryHandler
from synapse.types import RoomAlias from synapse.types import RoomAlias
from tests.utils import SQLiteMemoryDbPool, MockKey from tests.utils import setup_test_homeserver
class DirectoryHandlers(object): class DirectoryHandlers(object):
@ -46,19 +45,10 @@ class DirectoryTestCase(unittest.TestCase):
self.query_handlers[query_type] = handler self.query_handlers[query_type] = handler
self.mock_federation.register_query_handler = register_query_handler self.mock_federation.register_query_handler = register_query_handler
db_pool = SQLiteMemoryDbPool() hs = yield setup_test_homeserver(
yield db_pool.prepare()
self.mock_config = Mock()
self.mock_config.signing_key = [MockKey()]
hs = HomeServer(
"test",
db_pool=db_pool,
http_client=None, http_client=None,
resource_for_federation=Mock(), resource_for_federation=Mock(),
replication_layer=self.mock_federation, replication_layer=self.mock_federation,
config=self.mock_config,
) )
hs.handlers = DirectoryHandlers(hs) hs.handlers = DirectoryHandlers(hs)

View File

@ -19,20 +19,17 @@ from tests import unittest
from synapse.api.constants import EventTypes from synapse.api.constants import EventTypes
from synapse.events import FrozenEvent from synapse.events import FrozenEvent
from synapse.handlers.federation import FederationHandler from synapse.handlers.federation import FederationHandler
from synapse.server import HomeServer
from mock import NonCallableMock, ANY, Mock from mock import NonCallableMock, ANY, Mock
from ..utils import MockKey from ..utils import setup_test_homeserver
class FederationTestCase(unittest.TestCase): class FederationTestCase(unittest.TestCase):
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.mock_config = NonCallableMock()
self.mock_config.signing_key = [MockKey()]
self.state_handler = NonCallableMock(spec_set=[ self.state_handler = NonCallableMock(spec_set=[
"compute_event_context", "compute_event_context",
]) ])
@ -43,9 +40,8 @@ class FederationTestCase(unittest.TestCase):
]) ])
self.hostname = "test" self.hostname = "test"
hs = HomeServer( hs = yield setup_test_homeserver(
self.hostname, self.hostname,
db_pool=None,
datastore=NonCallableMock(spec_set=[ datastore=NonCallableMock(spec_set=[
"persist_event", "persist_event",
"store_room", "store_room",
@ -61,7 +57,6 @@ class FederationTestCase(unittest.TestCase):
"room_member_handler", "room_member_handler",
"federation_handler", "federation_handler",
]), ]),
config=self.mock_config,
auth=self.auth, auth=self.auth,
state_handler=self.state_handler, state_handler=self.state_handler,
keyring=Mock(), keyring=Mock(),

View File

@ -21,11 +21,9 @@ from mock import Mock, call, ANY, NonCallableMock
import json import json
from tests.utils import ( from tests.utils import (
MockHttpResource, MockClock, DeferredMockCallable, SQLiteMemoryDbPool, MockHttpResource, MockClock, DeferredMockCallable, setup_test_homeserver
MockKey
) )
from synapse.server import HomeServer
from synapse.api.constants import PresenceState from synapse.api.constants import PresenceState
from synapse.api.errors import SynapseError from synapse.api.errors import SynapseError
from synapse.handlers.presence import PresenceHandler, UserPresenceCache from synapse.handlers.presence import PresenceHandler, UserPresenceCache
@ -66,30 +64,20 @@ class PresenceTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.clock = MockClock() self.clock = MockClock()
self.mock_config = NonCallableMock()
self.mock_config.signing_key = [MockKey()]
self.mock_federation_resource = MockHttpResource() self.mock_federation_resource = MockHttpResource()
self.mock_http_client = Mock(spec=[]) self.mock_http_client = Mock(spec=[])
self.mock_http_client.put_json = DeferredMockCallable() self.mock_http_client.put_json = DeferredMockCallable()
db_pool = None
hs_kwargs = {} hs_kwargs = {}
if hasattr(self, "make_datastore_mock"): if hasattr(self, "make_datastore_mock"):
hs_kwargs["datastore"] = self.make_datastore_mock() hs_kwargs["datastore"] = self.make_datastore_mock()
else:
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer("test", hs = yield setup_test_homeserver(
clock=self.clock, clock=self.clock,
db_pool=db_pool,
handlers=None, handlers=None,
resource_for_federation=self.mock_federation_resource, resource_for_federation=self.mock_federation_resource,
http_client=self.mock_http_client, http_client=self.mock_http_client,
config=self.mock_config,
keyring=Mock(), keyring=Mock(),
**hs_kwargs **hs_kwargs
) )

View File

@ -21,9 +21,8 @@ from twisted.internet import defer
from mock import Mock, call, ANY, NonCallableMock from mock import Mock, call, ANY, NonCallableMock
from ..utils import MockClock, MockKey from ..utils import MockClock, setup_test_homeserver
from synapse.server import HomeServer
from synapse.api.constants import PresenceState from synapse.api.constants import PresenceState
from synapse.handlers.presence import PresenceHandler from synapse.handlers.presence import PresenceHandler
from synapse.handlers.profile import ProfileHandler from synapse.handlers.profile import ProfileHandler
@ -57,19 +56,14 @@ class PresenceAndProfileHandlers(object):
class PresenceProfilelikeDataTestCase(unittest.TestCase): class PresenceProfilelikeDataTestCase(unittest.TestCase):
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.mock_config = Mock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
hs = HomeServer("test",
clock=MockClock(), clock=MockClock(),
db_pool=None,
datastore=Mock(spec=[ datastore=Mock(spec=[
"set_presence_state", "set_presence_state",
"is_presence_visible", "is_presence_visible",
"set_profile_displayname", "set_profile_displayname",
"get_rooms_for_user_where_membership_is", "get_rooms_for_user_where_membership_is",
]), ]),
handlers=None, handlers=None,
@ -79,7 +73,6 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
config=self.mock_config
) )
self.ratelimiter = hs.get_ratelimiter() self.ratelimiter = hs.get_ratelimiter()
self.ratelimiter.send_message.return_value = (True, 0) self.ratelimiter.send_message.return_value = (True, 0)

View File

@ -20,11 +20,10 @@ from twisted.internet import defer
from mock import Mock, NonCallableMock from mock import Mock, NonCallableMock
from synapse.api.errors import AuthError from synapse.api.errors import AuthError
from synapse.server import HomeServer
from synapse.handlers.profile import ProfileHandler from synapse.handlers.profile import ProfileHandler
from synapse.types import UserID from synapse.types import UserID
from tests.utils import SQLiteMemoryDbPool, MockKey from tests.utils import setup_test_homeserver
class ProfileHandlers(object): class ProfileHandlers(object):
@ -46,19 +45,11 @@ class ProfileTestCase(unittest.TestCase):
self.query_handlers[query_type] = handler self.query_handlers[query_type] = handler
self.mock_federation.register_query_handler = register_query_handler self.mock_federation.register_query_handler = register_query_handler
db_pool = SQLiteMemoryDbPool() hs = yield setup_test_homeserver(
yield db_pool.prepare()
self.mock_config = Mock()
self.mock_config.signing_key = [MockKey()]
hs = HomeServer("test",
db_pool=db_pool,
http_client=None, http_client=None,
handlers=None, handlers=None,
resource_for_federation=Mock(), resource_for_federation=Mock(),
replication_layer=self.mock_federation, replication_layer=self.mock_federation,
config=self.mock_config,
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]) ])

View File

@ -20,23 +20,19 @@ from .. import unittest
from synapse.api.constants import EventTypes, Membership from synapse.api.constants import EventTypes, Membership
from synapse.handlers.room import RoomMemberHandler, RoomCreationHandler from synapse.handlers.room import RoomMemberHandler, RoomCreationHandler
from synapse.handlers.profile import ProfileHandler from synapse.handlers.profile import ProfileHandler
from synapse.server import HomeServer
from synapse.types import UserID from synapse.types import UserID
from ..utils import MockKey from ..utils import setup_test_homeserver
from mock import Mock, NonCallableMock from mock import Mock, NonCallableMock
class RoomMemberHandlerTestCase(unittest.TestCase): class RoomMemberHandlerTestCase(unittest.TestCase):
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.mock_config = NonCallableMock()
self.mock_config.signing_key = [MockKey()]
self.hostname = "red" self.hostname = "red"
hs = HomeServer( hs = yield setup_test_homeserver(
self.hostname, self.hostname,
db_pool=None,
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
@ -64,7 +60,6 @@ class RoomMemberHandlerTestCase(unittest.TestCase):
"compute_event_context", "compute_event_context",
"get_current_state", "get_current_state",
]), ]),
config=self.mock_config,
) )
self.federation = NonCallableMock(spec_set=[ self.federation = NonCallableMock(spec_set=[
@ -319,15 +314,12 @@ class RoomMemberHandlerTestCase(unittest.TestCase):
class RoomCreationTest(unittest.TestCase): class RoomCreationTest(unittest.TestCase):
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.hostname = "red" self.hostname = "red"
self.mock_config = NonCallableMock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
hs = HomeServer(
self.hostname, self.hostname,
db_pool=None,
datastore=NonCallableMock(spec_set=[ datastore=NonCallableMock(spec_set=[
"store_room", "store_room",
"snapshot_room", "snapshot_room",
@ -344,7 +336,6 @@ class RoomCreationTest(unittest.TestCase):
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
config=self.mock_config,
) )
self.federation = NonCallableMock(spec_set=[ self.federation = NonCallableMock(spec_set=[

View File

@ -20,10 +20,11 @@ from twisted.internet import defer
from mock import Mock, call, ANY from mock import Mock, call, ANY
import json import json
from ..utils import MockHttpResource, MockClock, DeferredMockCallable, MockKey from ..utils import (
MockHttpResource, MockClock, DeferredMockCallable, setup_test_homeserver
)
from synapse.api.errors import AuthError from synapse.api.errors import AuthError
from synapse.server import HomeServer
from synapse.handlers.typing import TypingNotificationHandler from synapse.handlers.typing import TypingNotificationHandler
from synapse.storage.transactions import DestinationsTable from synapse.storage.transactions import DestinationsTable
@ -56,6 +57,7 @@ class JustTypingNotificationHandlers(object):
class TypingNotificationsTestCase(unittest.TestCase): class TypingNotificationsTestCase(unittest.TestCase):
"""Tests typing notifications to rooms.""" """Tests typing notifications to rooms."""
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.clock = MockClock() self.clock = MockClock()
@ -64,18 +66,14 @@ class TypingNotificationsTestCase(unittest.TestCase):
self.mock_federation_resource = MockHttpResource() self.mock_federation_resource = MockHttpResource()
self.mock_config = Mock()
self.mock_config.signing_key = [MockKey()]
mock_notifier = Mock(spec=["on_new_user_event"]) mock_notifier = Mock(spec=["on_new_user_event"])
self.on_new_user_event = mock_notifier.on_new_user_event self.on_new_user_event = mock_notifier.on_new_user_event
self.auth = Mock(spec=[]) self.auth = Mock(spec=[])
hs = HomeServer("test", hs = yield setup_test_homeserver(
auth=self.auth, auth=self.auth,
clock=self.clock, clock=self.clock,
db_pool=None,
datastore=Mock(spec=[ datastore=Mock(spec=[
# Bits that Federation needs # Bits that Federation needs
"prep_send_transaction", "prep_send_transaction",
@ -89,7 +87,6 @@ class TypingNotificationsTestCase(unittest.TestCase):
resource_for_client=Mock(), resource_for_client=Mock(),
resource_for_federation=self.mock_federation_resource, resource_for_federation=self.mock_federation_resource,
http_client=self.mock_http_client, http_client=self.mock_http_client,
config=self.mock_config,
keyring=Mock(), keyring=Mock(),
) )
hs.handlers = JustTypingNotificationHandlers(hs) hs.handlers = JustTypingNotificationHandlers(hs)

View File

@ -23,9 +23,8 @@ import synapse.rest.client.v1.events
import synapse.rest.client.v1.register import synapse.rest.client.v1.register
import synapse.rest.client.v1.room import synapse.rest.client.v1.room
from synapse.server import HomeServer
from ....utils import MockHttpResource, SQLiteMemoryDbPool, MockKey from ....utils import MockHttpResource, setup_test_homeserver
from .utils import RestTestCase from .utils import RestTestCase
from mock import Mock, NonCallableMock from mock import Mock, NonCallableMock
@ -113,15 +112,7 @@ class EventStreamPermissionsTestCase(RestTestCase):
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.mock_config = NonCallableMock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer(
"test",
db_pool=db_pool,
http_client=None, http_client=None,
replication_layer=Mock(), replication_layer=Mock(),
clock=Mock(spec=[ clock=Mock(spec=[
@ -133,7 +124,6 @@ class EventStreamPermissionsTestCase(RestTestCase):
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
config=self.mock_config,
) )
self.ratelimiter = hs.get_ratelimiter() self.ratelimiter = hs.get_ratelimiter()
self.ratelimiter.send_message.return_value = (True, 0) self.ratelimiter.send_message.return_value = (True, 0)

View File

@ -20,11 +20,10 @@ from twisted.internet import defer
from mock import Mock from mock import Mock
from ....utils import MockHttpResource, MockKey from ....utils import MockHttpResource, setup_test_homeserver
from synapse.api.constants import PresenceState from synapse.api.constants import PresenceState
from synapse.handlers.presence import PresenceHandler from synapse.handlers.presence import PresenceHandler
from synapse.server import HomeServer
from synapse.rest.client.v1 import presence from synapse.rest.client.v1 import presence
from synapse.rest.client.v1 import events from synapse.rest.client.v1 import events
from synapse.types import UserID from synapse.types import UserID
@ -46,12 +45,10 @@ class JustPresenceHandlers(object):
class PresenceStateTestCase(unittest.TestCase): class PresenceStateTestCase(unittest.TestCase):
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.mock_config = Mock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
hs = HomeServer("test",
db_pool=None,
datastore=Mock(spec=[ datastore=Mock(spec=[
"get_presence_state", "get_presence_state",
"set_presence_state", "set_presence_state",
@ -60,7 +57,6 @@ class PresenceStateTestCase(unittest.TestCase):
http_client=None, http_client=None,
resource_for_client=self.mock_resource, resource_for_client=self.mock_resource,
resource_for_federation=self.mock_resource, resource_for_federation=self.mock_resource,
config=self.mock_config,
) )
hs.handlers = JustPresenceHandlers(hs) hs.handlers = JustPresenceHandlers(hs)
@ -128,13 +124,11 @@ class PresenceStateTestCase(unittest.TestCase):
class PresenceListTestCase(unittest.TestCase): class PresenceListTestCase(unittest.TestCase):
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.mock_config = Mock()
self.mock_config.signing_key = [MockKey()]
hs = HomeServer("test", hs = yield setup_test_homeserver(
db_pool=None,
datastore=Mock(spec=[ datastore=Mock(spec=[
"has_presence_state", "has_presence_state",
"get_presence_state", "get_presence_state",
@ -149,7 +143,6 @@ class PresenceListTestCase(unittest.TestCase):
http_client=None, http_client=None,
resource_for_client=self.mock_resource, resource_for_client=self.mock_resource,
resource_for_federation=self.mock_resource, resource_for_federation=self.mock_resource,
config=self.mock_config,
) )
hs.handlers = JustPresenceHandlers(hs) hs.handlers = JustPresenceHandlers(hs)
@ -242,12 +235,10 @@ class PresenceListTestCase(unittest.TestCase):
class PresenceEventStreamTestCase(unittest.TestCase): class PresenceEventStreamTestCase(unittest.TestCase):
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.mock_config = Mock()
self.mock_config.signing_key = [MockKey()]
# HIDEOUS HACKERY # HIDEOUS HACKERY
# TODO(paul): This should be injected in via the HomeServer DI system # TODO(paul): This should be injected in via the HomeServer DI system
from synapse.streams.events import ( from synapse.streams.events import (
@ -264,8 +255,7 @@ class PresenceEventStreamTestCase(unittest.TestCase):
} }
EventSources.SOURCE_TYPES["presence"] = PresenceEventSource EventSources.SOURCE_TYPES["presence"] = PresenceEventSource
hs = HomeServer("test", hs = yield setup_test_homeserver(
db_pool=None,
http_client=None, http_client=None,
resource_for_client=self.mock_resource, resource_for_client=self.mock_resource,
resource_for_federation=self.mock_resource, resource_for_federation=self.mock_resource,
@ -278,7 +268,6 @@ class PresenceEventStreamTestCase(unittest.TestCase):
"cancel_call_later", "cancel_call_later",
"time_msec", "time_msec",
]), ]),
config=self.mock_config,
) )
hs.get_clock().time_msec.return_value = 1000000 hs.get_clock().time_msec.return_value = 1000000

View File

@ -20,10 +20,9 @@ from twisted.internet import defer
from mock import Mock, NonCallableMock from mock import Mock, NonCallableMock
from ....utils import MockHttpResource, MockKey from ....utils import MockHttpResource, setup_test_homeserver
from synapse.api.errors import SynapseError, AuthError from synapse.api.errors import SynapseError, AuthError
from synapse.server import HomeServer
from synapse.types import UserID from synapse.types import UserID
from synapse.rest.client.v1 import profile from synapse.rest.client.v1 import profile
@ -35,6 +34,7 @@ PATH_PREFIX = "/_matrix/client/api/v1"
class ProfileTestCase(unittest.TestCase): class ProfileTestCase(unittest.TestCase):
""" Tests profile management. """ """ Tests profile management. """
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.mock_handler = Mock(spec=[ self.mock_handler = Mock(spec=[
@ -44,17 +44,12 @@ class ProfileTestCase(unittest.TestCase):
"set_avatar_url", "set_avatar_url",
]) ])
self.mock_config = NonCallableMock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()] "test",
hs = HomeServer("test",
db_pool=None,
http_client=None, http_client=None,
resource_for_client=self.mock_resource, resource_for_client=self.mock_resource,
federation=Mock(), federation=Mock(),
replication_layer=Mock(), replication_layer=Mock(),
datastore=None,
config=self.mock_config,
) )
def _get_user_by_req(request=None): def _get_user_by_req(request=None):

View File

@ -21,13 +21,12 @@ from twisted.internet import defer
import synapse.rest.client.v1.room import synapse.rest.client.v1.room
from synapse.api.constants import Membership from synapse.api.constants import Membership
from synapse.server import HomeServer
from synapse.types import UserID from synapse.types import UserID
import json import json
import urllib import urllib
from ....utils import MockHttpResource, SQLiteMemoryDbPool, MockKey from ....utils import MockHttpResource, setup_test_homeserver
from .utils import RestTestCase from .utils import RestTestCase
from mock import Mock, NonCallableMock from mock import Mock, NonCallableMock
@ -44,21 +43,11 @@ class RoomPermissionsTestCase(RestTestCase):
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.mock_config = NonCallableMock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer(
"red", "red",
db_pool=db_pool,
http_client=None, http_client=None,
replication_layer=Mock(), replication_layer=Mock(),
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=["send_message"]),
"send_message",
]),
config=self.mock_config,
) )
self.ratelimiter = hs.get_ratelimiter() self.ratelimiter = hs.get_ratelimiter()
self.ratelimiter.send_message.return_value = (True, 0) self.ratelimiter.send_message.return_value = (True, 0)
@ -439,21 +428,11 @@ class RoomsMemberListTestCase(RestTestCase):
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.mock_config = NonCallableMock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer(
"red", "red",
db_pool=db_pool,
http_client=None, http_client=None,
replication_layer=Mock(), replication_layer=Mock(),
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=["send_message"]),
"send_message",
]),
config=self.mock_config,
) )
self.ratelimiter = hs.get_ratelimiter() self.ratelimiter = hs.get_ratelimiter()
self.ratelimiter.send_message.return_value = (True, 0) self.ratelimiter.send_message.return_value = (True, 0)
@ -531,21 +510,11 @@ class RoomsCreateTestCase(RestTestCase):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.auth_user_id = self.user_id self.auth_user_id = self.user_id
self.mock_config = NonCallableMock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer(
"red", "red",
db_pool=db_pool,
http_client=None, http_client=None,
replication_layer=Mock(), replication_layer=Mock(),
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=["send_message"]),
"send_message",
]),
config=self.mock_config,
) )
self.ratelimiter = hs.get_ratelimiter() self.ratelimiter = hs.get_ratelimiter()
self.ratelimiter.send_message.return_value = (True, 0) self.ratelimiter.send_message.return_value = (True, 0)
@ -634,21 +603,11 @@ class RoomTopicTestCase(RestTestCase):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.auth_user_id = self.user_id self.auth_user_id = self.user_id
self.mock_config = NonCallableMock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer(
"red", "red",
db_pool=db_pool,
http_client=None, http_client=None,
replication_layer=Mock(), replication_layer=Mock(),
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=["send_message"]),
"send_message",
]),
config=self.mock_config,
) )
self.ratelimiter = hs.get_ratelimiter() self.ratelimiter = hs.get_ratelimiter()
self.ratelimiter.send_message.return_value = (True, 0) self.ratelimiter.send_message.return_value = (True, 0)
@ -751,21 +710,11 @@ class RoomMemberStateTestCase(RestTestCase):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.auth_user_id = self.user_id self.auth_user_id = self.user_id
self.mock_config = NonCallableMock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer(
"red", "red",
db_pool=db_pool,
http_client=None, http_client=None,
replication_layer=Mock(), replication_layer=Mock(),
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=["send_message"]),
"send_message",
]),
config=self.mock_config,
) )
self.ratelimiter = hs.get_ratelimiter() self.ratelimiter = hs.get_ratelimiter()
self.ratelimiter.send_message.return_value = (True, 0) self.ratelimiter.send_message.return_value = (True, 0)
@ -888,21 +837,11 @@ class RoomMessagesTestCase(RestTestCase):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.auth_user_id = self.user_id self.auth_user_id = self.user_id
self.mock_config = NonCallableMock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer(
"red", "red",
db_pool=db_pool,
http_client=None, http_client=None,
replication_layer=Mock(), replication_layer=Mock(),
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=["send_message"]),
"send_message",
]),
config=self.mock_config,
) )
self.ratelimiter = hs.get_ratelimiter() self.ratelimiter = hs.get_ratelimiter()
self.ratelimiter.send_message.return_value = (True, 0) self.ratelimiter.send_message.return_value = (True, 0)
@ -993,21 +932,13 @@ class RoomInitialSyncTestCase(RestTestCase):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.auth_user_id = self.user_id self.auth_user_id = self.user_id
self.mock_config = NonCallableMock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer(
"red", "red",
db_pool=db_pool,
http_client=None, http_client=None,
replication_layer=Mock(), replication_layer=Mock(),
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
config=self.mock_config,
) )
self.ratelimiter = hs.get_ratelimiter() self.ratelimiter = hs.get_ratelimiter()
self.ratelimiter.send_message.return_value = (True, 0) self.ratelimiter.send_message.return_value = (True, 0)

View File

@ -19,10 +19,9 @@
from twisted.internet import defer from twisted.internet import defer
import synapse.rest.client.v1.room import synapse.rest.client.v1.room
from synapse.server import HomeServer
from synapse.types import UserID from synapse.types import UserID
from ....utils import MockHttpResource, MockClock, SQLiteMemoryDbPool, MockKey from ....utils import MockHttpResource, MockClock, setup_test_homeserver
from .utils import RestTestCase from .utils import RestTestCase
from mock import Mock, NonCallableMock from mock import Mock, NonCallableMock
@ -42,22 +41,14 @@ class RoomTypingTestCase(RestTestCase):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.auth_user_id = self.user_id self.auth_user_id = self.user_id
self.mock_config = NonCallableMock() hs = yield setup_test_homeserver(
self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer(
"red", "red",
clock=self.clock, clock=self.clock,
db_pool=db_pool,
http_client=None, http_client=None,
replication_layer=Mock(), replication_layer=Mock(),
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
config=self.mock_config,
) )
self.hs = hs self.hs = hs

View File

@ -17,11 +17,12 @@ from tests import unittest
from mock import Mock from mock import Mock
from ....utils import MockHttpResource, MockKey from ....utils import MockHttpResource, setup_test_homeserver
from synapse.server import HomeServer
from synapse.types import UserID from synapse.types import UserID
from twisted.internet import defer
PATH_PREFIX = "/_matrix/client/v2_alpha" PATH_PREFIX = "/_matrix/client/v2_alpha"
@ -31,19 +32,15 @@ class V2AlphaRestTestCase(unittest.TestCase):
# USER_ID = <some string> # USER_ID = <some string>
# TO_REGISTER = [<list of REST servlets to register>] # TO_REGISTER = [<list of REST servlets to register>]
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
mock_config = Mock() hs = yield setup_test_homeserver(
mock_config.signing_key = [MockKey()]
hs = HomeServer("test",
db_pool=None,
datastore=self.make_datastore_mock(), datastore=self.make_datastore_mock(),
http_client=None, http_client=None,
resource_for_client=self.mock_resource, resource_for_client=self.mock_resource,
resource_for_federation=self.mock_resource, resource_for_federation=self.mock_resource,
config=mock_config,
) )
def _get_user_by_token(token=None): def _get_user_by_token(token=None):

View File

@ -22,6 +22,7 @@ from mock import Mock, call
from collections import OrderedDict from collections import OrderedDict
from synapse.server import HomeServer from synapse.server import HomeServer
from synapse.storage._base import SQLBaseStore from synapse.storage._base import SQLBaseStore

View File

@ -17,24 +17,17 @@
from tests import unittest from tests import unittest
from twisted.internet import defer from twisted.internet import defer
from synapse.server import HomeServer
from synapse.storage.directory import DirectoryStore from synapse.storage.directory import DirectoryStore
from synapse.types import RoomID, RoomAlias from synapse.types import RoomID, RoomAlias
from tests.utils import SQLiteMemoryDbPool from tests.utils import setup_test_homeserver
class DirectoryStoreTestCase(unittest.TestCase): class DirectoryStoreTestCase(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def setUp(self): def setUp(self):
db_pool = SQLiteMemoryDbPool() hs = yield setup_test_homeserver()
yield db_pool.prepare()
hs = HomeServer(
"test",
db_pool=db_pool,
)
self.store = DirectoryStore(hs) self.store = DirectoryStore(hs)

View File

@ -17,24 +17,17 @@
from tests import unittest from tests import unittest
from twisted.internet import defer from twisted.internet import defer
from synapse.server import HomeServer
from synapse.storage.presence import PresenceStore from synapse.storage.presence import PresenceStore
from synapse.types import UserID from synapse.types import UserID
from tests.utils import SQLiteMemoryDbPool, MockClock from tests.utils import setup_test_homeserver, MockClock
class PresenceStoreTestCase(unittest.TestCase): class PresenceStoreTestCase(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def setUp(self): def setUp(self):
db_pool = SQLiteMemoryDbPool() hs = yield setup_test_homeserver(clock=MockClock())
yield db_pool.prepare()
hs = HomeServer("test",
clock=MockClock(),
db_pool=db_pool,
)
self.store = PresenceStore(hs) self.store = PresenceStore(hs)

View File

@ -17,23 +17,17 @@
from tests import unittest from tests import unittest
from twisted.internet import defer from twisted.internet import defer
from synapse.server import HomeServer
from synapse.storage.profile import ProfileStore from synapse.storage.profile import ProfileStore
from synapse.types import UserID from synapse.types import UserID
from tests.utils import SQLiteMemoryDbPool from tests.utils import setup_test_homeserver
class ProfileStoreTestCase(unittest.TestCase): class ProfileStoreTestCase(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def setUp(self): def setUp(self):
db_pool = SQLiteMemoryDbPool() hs = yield setup_test_homeserver()
yield db_pool.prepare()
hs = HomeServer("test",
db_pool=db_pool,
)
self.store = ProfileStore(hs) self.store = ProfileStore(hs)

View File

@ -17,11 +17,10 @@
from tests import unittest from tests import unittest
from twisted.internet import defer from twisted.internet import defer
from synapse.server import HomeServer
from synapse.api.constants import EventTypes, Membership from synapse.api.constants import EventTypes, Membership
from synapse.types import UserID, RoomID from synapse.types import UserID, RoomID
from tests.utils import SQLiteMemoryDbPool, MockKey from tests.utils import setup_test_homeserver
from mock import Mock from mock import Mock
@ -30,16 +29,7 @@ class RedactionTestCase(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def setUp(self): def setUp(self):
db_pool = SQLiteMemoryDbPool() hs = yield setup_test_homeserver(
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(), resource_for_federation=Mock(),
http_client=None, http_client=None,
) )

View File

@ -17,22 +17,16 @@
from tests import unittest from tests import unittest
from twisted.internet import defer from twisted.internet import defer
from synapse.server import HomeServer
from synapse.storage.registration import RegistrationStore from synapse.storage.registration import RegistrationStore
from tests.utils import SQLiteMemoryDbPool from tests.utils import setup_test_homeserver
class RegistrationStoreTestCase(unittest.TestCase): class RegistrationStoreTestCase(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def setUp(self): def setUp(self):
db_pool = SQLiteMemoryDbPool() hs = yield setup_test_homeserver()
yield db_pool.prepare()
hs = HomeServer("test",
db_pool=db_pool,
)
self.store = RegistrationStore(hs) self.store = RegistrationStore(hs)

View File

@ -17,23 +17,17 @@
from tests import unittest from tests import unittest
from twisted.internet import defer from twisted.internet import defer
from synapse.server import HomeServer
from synapse.api.constants import EventTypes from synapse.api.constants import EventTypes
from synapse.types import UserID, RoomID, RoomAlias from synapse.types import UserID, RoomID, RoomAlias
from tests.utils import SQLiteMemoryDbPool from tests.utils import setup_test_homeserver
class RoomStoreTestCase(unittest.TestCase): class RoomStoreTestCase(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def setUp(self): def setUp(self):
db_pool = SQLiteMemoryDbPool() hs = yield setup_test_homeserver()
yield db_pool.prepare()
hs = HomeServer("test",
db_pool=db_pool,
)
# We can't test RoomStore on its own without the DirectoryStore, for # We can't test RoomStore on its own without the DirectoryStore, for
# management of the 'room_aliases' table # management of the 'room_aliases' table
@ -86,12 +80,7 @@ class RoomEventsStoreTestCase(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def setUp(self): def setUp(self):
db_pool = SQLiteMemoryDbPool() hs = setup_test_homeserver()
yield db_pool.prepare()
hs = HomeServer("test",
db_pool=db_pool,
)
# Room events need the full datastore, for persist_event() and # Room events need the full datastore, for persist_event() and
# get_room_state() # get_room_state()

View File

@ -17,11 +17,10 @@
from tests import unittest from tests import unittest
from twisted.internet import defer from twisted.internet import defer
from synapse.server import HomeServer
from synapse.api.constants import EventTypes, Membership from synapse.api.constants import EventTypes, Membership
from synapse.types import UserID, RoomID from synapse.types import UserID, RoomID
from tests.utils import SQLiteMemoryDbPool, MockKey from tests.utils import setup_test_homeserver
from mock import Mock from mock import Mock
@ -30,16 +29,7 @@ class RoomMemberStoreTestCase(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def setUp(self): def setUp(self):
db_pool = SQLiteMemoryDbPool() hs = yield setup_test_homeserver(
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(), resource_for_federation=Mock(),
http_client=None, http_client=None,
) )

View File

@ -17,11 +17,10 @@
from tests import unittest from tests import unittest
from twisted.internet import defer from twisted.internet import defer
from synapse.server import HomeServer
from synapse.api.constants import EventTypes, Membership from synapse.api.constants import EventTypes, Membership
from synapse.types import UserID, RoomID from synapse.types import UserID, RoomID
from tests.utils import SQLiteMemoryDbPool, MockKey from tests.utils import setup_test_homeserver
from mock import Mock from mock import Mock
@ -30,16 +29,7 @@ class StreamStoreTestCase(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def setUp(self): def setUp(self):
db_pool = SQLiteMemoryDbPool() hs = yield setup_test_homeserver(
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(), resource_for_federation=Mock(),
http_client=None, http_client=None,
) )

View File

@ -17,6 +17,7 @@ from synapse.http.server import HttpServer
from synapse.api.errors import cs_error, CodeMessageException, StoreError from synapse.api.errors import cs_error, CodeMessageException, StoreError
from synapse.api.constants import EventTypes from synapse.api.constants import EventTypes
from synapse.storage import prepare_database from synapse.storage import prepare_database
from synapse.server import HomeServer
from synapse.util.logcontext import LoggingContext from synapse.util.logcontext import LoggingContext
@ -31,6 +32,28 @@ import urlparse
from inspect import getcallargs from inspect import getcallargs
@defer.inlineCallbacks
def setup_test_homeserver(name="test", datastore=None, config=None, **kargs):
"""Setup a homeserver suitable for running tests against. Keyword arguments
are passed to the Homeserver constructor. If no datastore is supplied a
datastore backed by an in-memory sqlite db will be given to the HS.
"""
if config is None:
config = Mock()
config.signing_key = [MockKey()]
if datastore is None:
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer(name, db_pool=db_pool, config=config, **kargs)
else:
hs = HomeServer(
name, db_pool=None, datastore=datastore, config=config, **kargs
)
defer.returnValue(hs)
def get_mock_call_args(pattern_func, mock_func): def get_mock_call_args(pattern_func, mock_func):
""" Return the arguments the mock function was called with interpreted """ Return the arguments the mock function was called with interpreted
by the pattern functions argument list. by the pattern functions argument list.