summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--synapse/handlers/presence.py131
-rw-r--r--synapse/handlers/sync.py16
-rw-r--r--tests/__init__.py1
-rw-r--r--tests/api/test_auth.py4
-rw-r--r--tests/api/test_filtering.py30
-rw-r--r--tests/api/test_ratelimiting.py1
-rw-r--r--tests/appservice/test_appservice.py2
-rw-r--r--tests/appservice/test_scheduler.py4
-rw-r--r--tests/crypto/__init__.py1
-rw-r--r--tests/events/test_utils.py1
-rw-r--r--tests/handlers/test_appservice.py2
-rw-r--r--tests/handlers/test_auth.py1
-rw-r--r--tests/handlers/test_profile.py9
-rw-r--r--tests/handlers/test_typing.py115
-rw-r--r--tests/metrics/test_metric.py3
-rw-r--r--tests/rest/client/v1/__init__.py1
-rw-r--r--tests/rest/client/v1/test_profile.py56
-rw-r--r--tests/rest/client/v1/test_rooms.py220
-rw-r--r--tests/rest/client/v1/test_typing.py44
-rw-r--r--tests/rest/client/v1/utils.py5
-rw-r--r--tests/rest/client/v2_alpha/__init__.py2
-rw-r--r--tests/rest/client/v2_alpha/test_filter.py19
-rw-r--r--tests/rest/client/v2_alpha/test_register.py4
-rw-r--r--tests/storage/event_injector.py8
-rw-r--r--tests/storage/test__base.py8
-rw-r--r--tests/storage/test_background_update.py8
-rw-r--r--tests/storage/test_base.py89
-rw-r--r--tests/storage/test_events.py1
-rw-r--r--tests/storage/test_profile.py2
-rw-r--r--tests/storage/test_registration.py6
-rw-r--r--tests/storage/test_room.py14
-rw-r--r--tests/storage/test_roommember.py22
-rw-r--r--tests/storage/test_stream.py4
-rw-r--r--tests/test_distributor.py11
-rw-r--r--tests/test_test_utils.py3
-rw-r--r--tests/unittest.py7
-rw-r--r--tests/util/__init__.py1
-rw-r--r--tests/util/test_dict_cache.py1
-rw-r--r--tests/util/test_snapshot_cache.py1
-rw-r--r--tests/util/test_treecache.py7
-rw-r--r--tests/utils.py30
-rw-r--r--tox.ini2
42 files changed, 470 insertions, 427 deletions
diff --git a/synapse/handlers/presence.py b/synapse/handlers/presence.py
index 4197311a97..aed640450f 100644
--- a/synapse/handlers/presence.py
+++ b/synapse/handlers/presence.py
@@ -31,6 +31,7 @@ from synapse.storage.presence import UserPresenceState
 
 from synapse.util.logcontext import preserve_fn
 from synapse.util.logutils import log_function
+from synapse.util.metrics import Measure
 from synapse.util.wheel_timer import WheelTimer
 from synapse.types import UserID
 import synapse.metrics
@@ -209,57 +210,60 @@ class PresenceHandler(BaseHandler):
         """
         now = self.clock.time_msec()
 
-        # NOTE: We purposefully don't yield between now and when we've
-        # calculated what we want to do with the new states, to avoid races.
+        with Measure(self.clock, "presence_update_states"):
 
-        to_notify = {}  # Changes we want to notify everyone about
-        to_federation_ping = {}  # These need sending keep-alives
-        for new_state in new_states:
-            user_id = new_state.user_id
+            # NOTE: We purposefully don't yield between now and when we've
+            # calculated what we want to do with the new states, to avoid races.
 
-            # Its fine to not hit the database here, as the only thing not in
-            # the current state cache are OFFLINE states, where the only field
-            # of interest is last_active which is safe enough to assume is 0
-            # here.
-            prev_state = self.user_to_current_state.get(
-                user_id, UserPresenceState.default(user_id)
-            )
+            to_notify = {}  # Changes we want to notify everyone about
+            to_federation_ping = {}  # These need sending keep-alives
 
-            new_state, should_notify, should_ping = handle_update(
-                prev_state, new_state,
-                is_mine=self.hs.is_mine_id(user_id),
-                wheel_timer=self.wheel_timer,
-                now=now
-            )
+            for new_state in new_states:
+                user_id = new_state.user_id
 
-            self.user_to_current_state[user_id] = new_state
+                # Its fine to not hit the database here, as the only thing not in
+                # the current state cache are OFFLINE states, where the only field
+                # of interest is last_active which is safe enough to assume is 0
+                # here.
+                prev_state = self.user_to_current_state.get(
+                    user_id, UserPresenceState.default(user_id)
+                )
 
-            if should_notify:
-                to_notify[user_id] = new_state
-            elif should_ping:
-                to_federation_ping[user_id] = new_state
+                new_state, should_notify, should_ping = handle_update(
+                    prev_state, new_state,
+                    is_mine=self.hs.is_mine_id(user_id),
+                    wheel_timer=self.wheel_timer,
+                    now=now
+                )
 
-        # TODO: We should probably ensure there are no races hereafter
+                self.user_to_current_state[user_id] = new_state
 
-        presence_updates_counter.inc_by(len(new_states))
+                if should_notify:
+                    to_notify[user_id] = new_state
+                elif should_ping:
+                    to_federation_ping[user_id] = new_state
 
-        if to_notify:
-            notified_presence_counter.inc_by(len(to_notify))
-            yield self._persist_and_notify(to_notify.values())
+            # TODO: We should probably ensure there are no races hereafter
 
-        self.unpersisted_users_changes |= set(s.user_id for s in new_states)
-        self.unpersisted_users_changes -= set(to_notify.keys())
+            presence_updates_counter.inc_by(len(new_states))
 
-        to_federation_ping = {
-            user_id: state for user_id, state in to_federation_ping.items()
-            if user_id not in to_notify
-        }
-        if to_federation_ping:
-            _, _, hosts_to_states = yield self._get_interested_parties(
-                to_federation_ping.values()
-            )
+            if to_notify:
+                notified_presence_counter.inc_by(len(to_notify))
+                yield self._persist_and_notify(to_notify.values())
+
+            self.unpersisted_users_changes |= set(s.user_id for s in new_states)
+            self.unpersisted_users_changes -= set(to_notify.keys())
+
+            to_federation_ping = {
+                user_id: state for user_id, state in to_federation_ping.items()
+                if user_id not in to_notify
+            }
+            if to_federation_ping:
+                _, _, hosts_to_states = yield self._get_interested_parties(
+                    to_federation_ping.values()
+                )
 
-            self._push_to_remotes(hosts_to_states)
+                self._push_to_remotes(hosts_to_states)
 
     def _handle_timeouts(self):
         """Checks the presence of users that have timed out and updates as
@@ -267,26 +271,27 @@ class PresenceHandler(BaseHandler):
         """
         now = self.clock.time_msec()
 
-        # Fetch the list of users that *may* have timed out. Things may have
-        # changed since the timeout was set, so we won't necessarily have to
-        # take any action.
-        users_to_check = self.wheel_timer.fetch(now)
+        with Measure(self.clock, "presence_handle_timeouts"):
+            # Fetch the list of users that *may* have timed out. Things may have
+            # changed since the timeout was set, so we won't necessarily have to
+            # take any action.
+            users_to_check = self.wheel_timer.fetch(now)
 
-        states = [
-            self.user_to_current_state.get(
-                user_id, UserPresenceState.default(user_id)
-            )
-            for user_id in set(users_to_check)
-        ]
+            states = [
+                self.user_to_current_state.get(
+                    user_id, UserPresenceState.default(user_id)
+                )
+                for user_id in set(users_to_check)
+            ]
 
-        timers_fired_counter.inc_by(len(states))
+            timers_fired_counter.inc_by(len(states))
 
-        changes = handle_timeouts(
-            states,
-            is_mine_fn=self.hs.is_mine_id,
-            user_to_num_current_syncs=self.user_to_num_current_syncs,
-            now=now,
-        )
+            changes = handle_timeouts(
+                states,
+                is_mine_fn=self.hs.is_mine_id,
+                user_to_num_current_syncs=self.user_to_num_current_syncs,
+                now=now,
+            )
 
         preserve_fn(self._update_states)(changes)
 
@@ -423,13 +428,21 @@ class PresenceHandler(BaseHandler):
 
         hosts_to_states = {}
         for room_id, states in room_ids_to_states.items():
+            local_states = filter(lambda s: self.hs.is_mine_id(s.user_id), states)
+            if not local_states:
+                continue
+
             hosts = yield self.store.get_joined_hosts_for_room(room_id)
             for host in hosts:
-                hosts_to_states.setdefault(host, []).extend(states)
+                hosts_to_states.setdefault(host, []).extend(local_states)
 
         for user_id, states in users_to_states.items():
+            local_states = filter(lambda s: self.hs.is_mine_id(s.user_id), states)
+            if not local_states:
+                continue
+
             host = UserID.from_string(user_id).domain
-            hosts_to_states.setdefault(host, []).extend(states)
+            hosts_to_states.setdefault(host, []).extend(local_states)
 
         # TODO: de-dup hosts_to_states, as a single host might have multiple
         # of same presence
diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py
index 75e4ffb3ab..c87ff75c05 100644
--- a/synapse/handlers/sync.py
+++ b/synapse/handlers/sync.py
@@ -843,16 +843,20 @@ class SyncHandler(BaseHandler):
         # TODO(mjark) Check for new redactions in the state events.
 
         with Measure(self.clock, "compute_state_delta"):
-            current_state = yield self.get_state_at(
-                room_id, stream_position=now_token
-            )
-
             if full_state:
                 if batch:
+                    current_state = yield self.store.get_state_for_event(
+                        batch.events[-1].event_id
+                    )
+
                     state = yield self.store.get_state_for_event(
                         batch.events[0].event_id
                     )
                 else:
+                    current_state = yield self.get_state_at(
+                        room_id, stream_position=now_token
+                    )
+
                     state = current_state
 
                 timeline_state = {
@@ -871,6 +875,10 @@ class SyncHandler(BaseHandler):
                     room_id, stream_position=since_token
                 )
 
+                current_state = yield self.store.get_state_for_event(
+                    batch.events[-1].event_id
+                )
+
                 state_at_timeline_start = yield self.store.get_state_for_event(
                     batch.events[0].event_id
                 )
diff --git a/tests/__init__.py b/tests/__init__.py
index d0e9399dda..bfebb0f644 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -12,4 +12,3 @@
 # 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.
-
diff --git a/tests/api/test_auth.py b/tests/api/test_auth.py
index 474c5c418f..7e7b0b4b1d 100644
--- a/tests/api/test_auth.py
+++ b/tests/api/test_auth.py
@@ -281,9 +281,9 @@ class AuthTestCase(unittest.TestCase):
         macaroon.add_first_party_caveat("gen = 1")
         macaroon.add_first_party_caveat("type = access")
         macaroon.add_first_party_caveat("user_id = %s" % (user,))
-        macaroon.add_first_party_caveat("time < 1") # ms
+        macaroon.add_first_party_caveat("time < 1")  # ms
 
-        self.hs.clock.now = 5000 # seconds
+        self.hs.clock.now = 5000  # seconds
 
         yield self.auth.get_user_from_macaroon(macaroon.serialize())
         # TODO(daniel): Turn on the check that we validate expiration, when we
diff --git a/tests/api/test_filtering.py b/tests/api/test_filtering.py
index ceb0089268..dcb6c5bc31 100644
--- a/tests/api/test_filtering.py
+++ b/tests/api/test_filtering.py
@@ -21,7 +21,6 @@ from tests.utils import (
     MockHttpResource, DeferredMockCallable, setup_test_homeserver
 )
 
-from synapse.types import UserID
 from synapse.api.filtering import Filter
 from synapse.events import FrozenEvent
 
@@ -356,7 +355,6 @@ 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_filter=user_filter_json,
@@ -411,7 +409,6 @@ class FilteringTestCase(unittest.TestCase):
                 }
             }
         }
-        user = UserID.from_string("@" + user_localpart + ":test")
         filter_id = yield self.datastore.add_user_filter(
             user_localpart=user_localpart,
             user_filter=user_filter_json,
@@ -440,7 +437,6 @@ class FilteringTestCase(unittest.TestCase):
                 }
             }
         }
-        user = UserID.from_string("@" + user_localpart + ":test")
         filter_id = yield self.datastore.add_user_filter(
             user_localpart=user_localpart,
             user_filter=user_filter_json,
@@ -460,6 +456,22 @@ class FilteringTestCase(unittest.TestCase):
         results = user_filter.filter_room_state(events)
         self.assertEquals([], results)
 
+    def test_filter_rooms(self):
+        definition = {
+            "rooms": ["!allowed:example.com", "!excluded:example.com"],
+            "not_rooms": ["!excluded:example.com"],
+        }
+
+        room_ids = [
+            "!allowed:example.com",  # Allowed because in rooms and not in not_rooms.
+            "!excluded:example.com",  # Disallowed because in not_rooms.
+            "!not_included:example.com",  # Disallowed because not in rooms.
+        ]
+
+        filtered_room_ids = list(Filter(definition).filter_rooms(room_ids))
+
+        self.assertEquals(filtered_room_ids, ["!allowed:example.com"])
+
     @defer.inlineCallbacks
     def test_add_filter(self):
         user_filter_json = {
@@ -476,12 +488,12 @@ class FilteringTestCase(unittest.TestCase):
         )
 
         self.assertEquals(filter_id, 0)
-        self.assertEquals(user_filter_json,
-            (yield self.datastore.get_user_filter(
+        self.assertEquals(user_filter_json, (
+            yield self.datastore.get_user_filter(
                 user_localpart=user_localpart,
                 filter_id=0,
-            ))
-        )
+            )
+        ))
 
     @defer.inlineCallbacks
     def test_get_filter(self):
@@ -504,3 +516,5 @@ class FilteringTestCase(unittest.TestCase):
         )
 
         self.assertEquals(filter.get_filter_json(), user_filter_json)
+
+        self.assertRegexpMatches(repr(filter), r"<FilterCollection \{.*\}>")
diff --git a/tests/api/test_ratelimiting.py b/tests/api/test_ratelimiting.py
index dd0bc19ecf..c45b59b36c 100644
--- a/tests/api/test_ratelimiting.py
+++ b/tests/api/test_ratelimiting.py
@@ -2,6 +2,7 @@ from synapse.api.ratelimiting import Ratelimiter
 
 from tests import unittest
 
+
 class TestRatelimiter(unittest.TestCase):
 
     def test_allowed(self):
diff --git a/tests/appservice/test_appservice.py b/tests/appservice/test_appservice.py
index ef48bbc296..d6cc1881e9 100644
--- a/tests/appservice/test_appservice.py
+++ b/tests/appservice/test_appservice.py
@@ -14,7 +14,7 @@
 # limitations under the License.
 from synapse.appservice import ApplicationService
 
-from mock import Mock, PropertyMock
+from mock import Mock
 from tests import unittest
 
 
diff --git a/tests/appservice/test_scheduler.py b/tests/appservice/test_scheduler.py
index c9c2d36210..631a229332 100644
--- a/tests/appservice/test_scheduler.py
+++ b/tests/appservice/test_scheduler.py
@@ -12,7 +12,7 @@
 # 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.
-from synapse.appservice import ApplicationServiceState, AppServiceTransaction
+from synapse.appservice import ApplicationServiceState
 from synapse.appservice.scheduler import (
     _ServiceQueuer, _TransactionController, _Recoverer
 )
@@ -235,7 +235,7 @@ class ApplicationServiceSchedulerQueuerTestCase(unittest.TestCase):
         srv_2_event2 = Mock(event_id="srv2b")
 
         send_return_list = [srv_1_defer, srv_2_defer]
-        self.txn_ctrl.send = Mock(side_effect=lambda x,y: send_return_list.pop(0))
+        self.txn_ctrl.send = Mock(side_effect=lambda x, y: send_return_list.pop(0))
 
         # send events for different ASes and make sure they are sent
         self.queuer.enqueue(srv1, srv_1_event)
diff --git a/tests/crypto/__init__.py b/tests/crypto/__init__.py
index d0e9399dda..bfebb0f644 100644
--- a/tests/crypto/__init__.py
+++ b/tests/crypto/__init__.py
@@ -12,4 +12,3 @@
 # 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.
-
diff --git a/tests/events/test_utils.py b/tests/events/test_utils.py
index 894d0c3845..fb0953c4ec 100644
--- a/tests/events/test_utils.py
+++ b/tests/events/test_utils.py
@@ -19,6 +19,7 @@ from .. import unittest
 from synapse.events import FrozenEvent
 from synapse.events.utils import prune_event
 
+
 class PruneEventTestCase(unittest.TestCase):
     """ Asserts that a new event constructed with `evdict` will look like
     `matchdict` when it is redacted. """
diff --git a/tests/handlers/test_appservice.py b/tests/handlers/test_appservice.py
index ba6e2c640e..7ddbbb9b4a 100644
--- a/tests/handlers/test_appservice.py
+++ b/tests/handlers/test_appservice.py
@@ -129,8 +129,6 @@ class AppServiceHandlerTestCase(unittest.TestCase):
         self.assertEquals(result.room_id, room_id)
         self.assertEquals(result.servers, servers)
 
-
-
     def _mkservice(self, is_interested):
         service = Mock()
         service.is_interested = Mock(return_value=is_interested)
diff --git a/tests/handlers/test_auth.py b/tests/handlers/test_auth.py
index 2f21bf91e5..21077cbe9a 100644
--- a/tests/handlers/test_auth.py
+++ b/tests/handlers/test_auth.py
@@ -15,7 +15,6 @@
 
 import pymacaroons
 
-from mock import Mock, NonCallableMock
 from synapse.handlers.auth import AuthHandler
 from tests import unittest
 from tests.utils import setup_test_homeserver
diff --git a/tests/handlers/test_profile.py b/tests/handlers/test_profile.py
index 95c87f0ebd..a87703bbfd 100644
--- a/tests/handlers/test_profile.py
+++ b/tests/handlers/test_profile.py
@@ -41,8 +41,10 @@ class ProfileTestCase(unittest.TestCase):
         ])
 
         self.query_handlers = {}
+
         def register_query_handler(query_type, handler):
             self.query_handlers[query_type] = handler
+
         self.mock_federation.register_query_handler = register_query_handler
 
         hs = yield setup_test_homeserver(
@@ -63,7 +65,7 @@ class ProfileTestCase(unittest.TestCase):
         self.store = hs.get_datastore()
 
         self.frank = UserID.from_string("@1234ABCD:test")
-        self.bob   = UserID.from_string("@4567:test")
+        self.bob = UserID.from_string("@4567:test")
         self.alice = UserID.from_string("@alice:remote")
 
         yield self.store.create_profile(self.frank.localpart)
@@ -133,8 +135,9 @@ class ProfileTestCase(unittest.TestCase):
 
     @defer.inlineCallbacks
     def test_set_my_avatar(self):
-        yield self.handler.set_avatar_url(self.frank, self.frank,
-                "http://my.server/pic.gif")
+        yield self.handler.set_avatar_url(
+            self.frank, self.frank, "http://my.server/pic.gif"
+        )
 
         self.assertEquals(
             (yield self.store.get_profile_avatar_url(self.frank.localpart)),
diff --git a/tests/handlers/test_typing.py b/tests/handlers/test_typing.py
index 763c04d667..3955e7f5b1 100644
--- a/tests/handlers/test_typing.py
+++ b/tests/handlers/test_typing.py
@@ -138,9 +138,9 @@ class TypingNotificationsTestCase(unittest.TestCase):
         self.room_member_handler.get_joined_rooms_for_user = get_joined_rooms_for_user
 
         @defer.inlineCallbacks
-        def fetch_room_distributions_into(room_id, localusers=None,
-                remotedomains=None, ignore_user=None):
-
+        def fetch_room_distributions_into(
+            room_id, localusers=None, remotedomains=None, ignore_user=None
+        ):
             members = yield get_room_members(room_id)
             for member in members:
                 if ignore_user is not None and member == ignore_user:
@@ -153,7 +153,8 @@ class TypingNotificationsTestCase(unittest.TestCase):
                     if remotedomains is not None:
                         remotedomains.add(member.domain)
         self.room_member_handler.fetch_room_distributions_into = (
-                fetch_room_distributions_into)
+            fetch_room_distributions_into
+        )
 
         def check_joined_room(room_id, user_id):
             if user_id not in [u.to_string() for u in self.room_members]:
@@ -207,9 +208,12 @@ class TypingNotificationsTestCase(unittest.TestCase):
 
         put_json = self.mock_http_client.put_json
         put_json.expect_call_and_return(
-            call("farm",
+            call(
+                "farm",
                 path="/_matrix/federation/v1/send/1000000/",
-                data=_expect_edu("farm", "m.typing",
+                data=_expect_edu(
+                    "farm",
+                    "m.typing",
                     content={
                         "room_id": self.room_id,
                         "user_id": self.u_apple.to_string(),
@@ -237,9 +241,12 @@ class TypingNotificationsTestCase(unittest.TestCase):
 
         self.assertEquals(self.event_source.get_current_key(), 0)
 
-        yield self.mock_federation_resource.trigger("PUT",
+        yield self.mock_federation_resource.trigger(
+            "PUT",
             "/_matrix/federation/v1/send/1000000/",
-            _make_edu_json("farm", "m.typing",
+            _make_edu_json(
+                "farm",
+                "m.typing",
                 content={
                     "room_id": self.room_id,
                     "user_id": self.u_onion.to_string(),
@@ -257,16 +264,13 @@ class TypingNotificationsTestCase(unittest.TestCase):
             room_ids=[self.room_id],
             from_key=0
         )
-        self.assertEquals(
-            events[0],
-            [
-                {"type": "m.typing",
-                 "room_id": self.room_id,
-                 "content": {
-                     "user_ids": [self.u_onion.to_string()],
-                }},
-            ]
-        )
+        self.assertEquals(events[0], [{
+            "type": "m.typing",
+            "room_id": self.room_id,
+            "content": {
+                "user_ids": [self.u_onion.to_string()],
+            },
+        }])
 
     @defer.inlineCallbacks
     def test_stopped_typing(self):
@@ -274,9 +278,12 @@ class TypingNotificationsTestCase(unittest.TestCase):
 
         put_json = self.mock_http_client.put_json
         put_json.expect_call_and_return(
-            call("farm",
+            call(
+                "farm",
                 path="/_matrix/federation/v1/send/1000000/",
-                data=_expect_edu("farm", "m.typing",
+                data=_expect_edu(
+                    "farm",
+                    "m.typing",
                     content={
                         "room_id": self.room_id,
                         "user_id": self.u_apple.to_string(),
@@ -317,16 +324,13 @@ class TypingNotificationsTestCase(unittest.TestCase):
             room_ids=[self.room_id],
             from_key=0,
         )
-        self.assertEquals(
-            events[0],
-            [
-                {"type": "m.typing",
-                 "room_id": self.room_id,
-                 "content": {
-                     "user_ids": [],
-                }},
-            ]
-        )
+        self.assertEquals(events[0], [{
+            "type": "m.typing",
+            "room_id": self.room_id,
+            "content": {
+                "user_ids": [],
+            },
+        }])
 
     @defer.inlineCallbacks
     def test_typing_timeout(self):
@@ -351,16 +355,13 @@ class TypingNotificationsTestCase(unittest.TestCase):
             room_ids=[self.room_id],
             from_key=0,
         )
-        self.assertEquals(
-            events[0],
-            [
-                {"type": "m.typing",
-                 "room_id": self.room_id,
-                 "content": {
-                     "user_ids": [self.u_apple.to_string()],
-                }},
-            ]
-        )
+        self.assertEquals(events[0], [{
+            "type": "m.typing",
+            "room_id": self.room_id,
+            "content": {
+                "user_ids": [self.u_apple.to_string()],
+            },
+        }])
 
         self.clock.advance_time(11)
 
@@ -373,16 +374,13 @@ class TypingNotificationsTestCase(unittest.TestCase):
             room_ids=[self.room_id],
             from_key=1,
         )
-        self.assertEquals(
-            events[0],
-            [
-                {"type": "m.typing",
-                 "room_id": self.room_id,
-                 "content": {
-                     "user_ids": [],
-                }},
-            ]
-        )
+        self.assertEquals(events[0], [{
+            "type": "m.typing",
+            "room_id": self.room_id,
+            "content": {
+                "user_ids": [],
+            },
+        }])
 
         # SYN-230 - see if we can still set after timeout
 
@@ -403,13 +401,10 @@ class TypingNotificationsTestCase(unittest.TestCase):
             room_ids=[self.room_id],
             from_key=0,
         )
-        self.assertEquals(
-            events[0],
-            [
-                {"type": "m.typing",
-                 "room_id": self.room_id,
-                 "content": {
-                     "user_ids": [self.u_apple.to_string()],
-                }},
-            ]
-        )
+        self.assertEquals(events[0], [{
+            "type": "m.typing",
+            "room_id": self.room_id,
+            "content": {
+                "user_ids": [self.u_apple.to_string()],
+            },
+        }])
diff --git a/tests/metrics/test_metric.py b/tests/metrics/test_metric.py
index f9e5e5af01..f3c1927ce1 100644
--- a/tests/metrics/test_metric.py
+++ b/tests/metrics/test_metric.py
@@ -61,6 +61,9 @@ class CounterMetricTestCase(unittest.TestCase):
             'vector{method="PUT"} 1',
         ])
 
+        # Check that passing too few values errors
+        self.assertRaises(ValueError, counter.inc)
+
 
 class CallbackMetricTestCase(unittest.TestCase):
 
diff --git a/tests/rest/client/v1/__init__.py b/tests/rest/client/v1/__init__.py
index d0e9399dda..bfebb0f644 100644
--- a/tests/rest/client/v1/__init__.py
+++ b/tests/rest/client/v1/__init__.py
@@ -12,4 +12,3 @@
 # 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.
-
diff --git a/tests/rest/client/v1/test_profile.py b/tests/rest/client/v1/test_profile.py
index c1a3f52043..0785965de2 100644
--- a/tests/rest/client/v1/test_profile.py
+++ b/tests/rest/client/v1/test_profile.py
@@ -65,8 +65,9 @@ class ProfileTestCase(unittest.TestCase):
         mocked_get = self.mock_handler.get_displayname
         mocked_get.return_value = defer.succeed("Frank")
 
-        (code, response) = yield self.mock_resource.trigger("GET",
-                "/profile/%s/displayname" % (myid), None)
+        (code, response) = yield self.mock_resource.trigger(
+            "GET", "/profile/%s/displayname" % (myid), None
+        )
 
         self.assertEquals(200, code)
         self.assertEquals({"displayname": "Frank"}, response)
@@ -77,9 +78,11 @@ class ProfileTestCase(unittest.TestCase):
         mocked_set = self.mock_handler.set_displayname
         mocked_set.return_value = defer.succeed(())
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                "/profile/%s/displayname" % (myid),
-                '{"displayname": "Frank Jr."}')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT",
+            "/profile/%s/displayname" % (myid),
+            '{"displayname": "Frank Jr."}'
+        )
 
         self.assertEquals(200, code)
         self.assertEquals(mocked_set.call_args[0][0].localpart, "1234ABCD")
@@ -91,19 +94,23 @@ class ProfileTestCase(unittest.TestCase):
         mocked_set = self.mock_handler.set_displayname
         mocked_set.side_effect = AuthError(400, "message")
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                "/profile/%s/displayname" % ("@4567:test"), '"Frank Jr."')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", "/profile/%s/displayname" % ("@4567:test"), '"Frank Jr."'
+        )
 
-        self.assertTrue(400 <= code < 499,
-                msg="code %d is in the 4xx range" % (code))
+        self.assertTrue(
+            400 <= code < 499,
+            msg="code %d is in the 4xx range" % (code)
+        )
 
     @defer.inlineCallbacks
     def test_get_other_name(self):
         mocked_get = self.mock_handler.get_displayname
         mocked_get.return_value = defer.succeed("Bob")
 
-        (code, response) = yield self.mock_resource.trigger("GET",
-                "/profile/%s/displayname" % ("@opaque:elsewhere"), None)
+        (code, response) = yield self.mock_resource.trigger(
+            "GET", "/profile/%s/displayname" % ("@opaque:elsewhere"), None
+        )
 
         self.assertEquals(200, code)
         self.assertEquals({"displayname": "Bob"}, response)
@@ -113,19 +120,23 @@ class ProfileTestCase(unittest.TestCase):
         mocked_set = self.mock_handler.set_displayname
         mocked_set.side_effect = SynapseError(400, "message")
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                "/profile/%s/displayname" % ("@opaque:elsewhere"), None)
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", "/profile/%s/displayname" % ("@opaque:elsewhere"), None
+        )
 
-        self.assertTrue(400 <= code <= 499,
-                msg="code %d is in the 4xx range" % (code))
+        self.assertTrue(
+            400 <= code <= 499,
+            msg="code %d is in the 4xx range" % (code)
+        )
 
     @defer.inlineCallbacks
     def test_get_my_avatar(self):
         mocked_get = self.mock_handler.get_avatar_url
         mocked_get.return_value = defer.succeed("http://my.server/me.png")
 
-        (code, response) = yield self.mock_resource.trigger("GET",
-                "/profile/%s/avatar_url" % (myid), None)
+        (code, response) = yield self.mock_resource.trigger(
+            "GET", "/profile/%s/avatar_url" % (myid), None
+        )
 
         self.assertEquals(200, code)
         self.assertEquals({"avatar_url": "http://my.server/me.png"}, response)
@@ -136,12 +147,13 @@ class ProfileTestCase(unittest.TestCase):
         mocked_set = self.mock_handler.set_avatar_url
         mocked_set.return_value = defer.succeed(())
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                "/profile/%s/avatar_url" % (myid),
-                '{"avatar_url": "http://my.server/pic.gif"}')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT",
+            "/profile/%s/avatar_url" % (myid),
+            '{"avatar_url": "http://my.server/pic.gif"}'
+        )
 
         self.assertEquals(200, code)
         self.assertEquals(mocked_set.call_args[0][0].localpart, "1234ABCD")
         self.assertEquals(mocked_set.call_args[0][1].localpart, "1234ABCD")
-        self.assertEquals(mocked_set.call_args[0][2],
-                "http://my.server/pic.gif")
+        self.assertEquals(mocked_set.call_args[0][2], "http://my.server/pic.gif")
diff --git a/tests/rest/client/v1/test_rooms.py b/tests/rest/client/v1/test_rooms.py
index a90b9dc3d8..afca5303ba 100644
--- a/tests/rest/client/v1/test_rooms.py
+++ b/tests/rest/client/v1/test_rooms.py
@@ -82,19 +82,22 @@ class RoomPermissionsTestCase(RestTestCase):
                                                              is_public=True)
 
         # send a message in one of the rooms
-        self.created_rmid_msg_path = ("/rooms/%s/send/m.room.message/a1" %
-                                (self.created_rmid))
+        self.created_rmid_msg_path = (
+            "/rooms/%s/send/m.room.message/a1" % (self.created_rmid)
+        )
         (code, response) = yield self.mock_resource.trigger(
-                           "PUT",
-                           self.created_rmid_msg_path,
-                           '{"msgtype":"m.text","body":"test msg"}')
+            "PUT",
+            self.created_rmid_msg_path,
+            '{"msgtype":"m.text","body":"test msg"}'
+        )
         self.assertEquals(200, code, msg=str(response))
 
         # set topic for public room
         (code, response) = yield self.mock_resource.trigger(
-                           "PUT",
-                           "/rooms/%s/state/m.room.topic" % self.created_public_rmid,
-                           '{"topic":"Public Room Topic"}')
+            "PUT",
+            "/rooms/%s/state/m.room.topic" % self.created_public_rmid,
+            '{"topic":"Public Room Topic"}'
+        )
         self.assertEquals(200, code, msg=str(response))
 
         # auth as user_id now
@@ -103,37 +106,6 @@ class RoomPermissionsTestCase(RestTestCase):
     def tearDown(self):
         pass
 
-#    @defer.inlineCallbacks
-#    def test_get_message(self):
-#        # get message in uncreated room, expect 403
-#        (code, response) = yield self.mock_resource.trigger_get(
-#                           "/rooms/noroom/messages/someid/m1")
-#        self.assertEquals(403, code, msg=str(response))
-#
-#        # get message in created room not joined (no state), expect 403
-#        (code, response) = yield self.mock_resource.trigger_get(
-#                           self.created_rmid_msg_path)
-#        self.assertEquals(403, code, msg=str(response))
-#
-#        # get message in created room and invited, expect 403
-#        yield self.invite(room=self.created_rmid, src=self.rmcreator_id,
-#                          targ=self.user_id)
-#        (code, response) = yield self.mock_resource.trigger_get(
-#                           self.created_rmid_msg_path)
-#        self.assertEquals(403, code, msg=str(response))
-#
-#        # get message in created room and joined, expect 200
-#        yield self.join(room=self.created_rmid, user=self.user_id)
-#        (code, response) = yield self.mock_resource.trigger_get(
-#                           self.created_rmid_msg_path)
-#        self.assertEquals(200, code, msg=str(response))
-#
-#        # get message in created room and left, expect 403
-#        yield self.leave(room=self.created_rmid, user=self.user_id)
-#        (code, response) = yield self.mock_resource.trigger_get(
-#                           self.created_rmid_msg_path)
-#        self.assertEquals(403, code, msg=str(response))
-
     @defer.inlineCallbacks
     def test_send_message(self):
         msg_content = '{"msgtype":"m.text","body":"hello"}'
@@ -195,25 +167,30 @@ class RoomPermissionsTestCase(RestTestCase):
 
         # set/get topic in uncreated room, expect 403
         (code, response) = yield self.mock_resource.trigger(
-                           "PUT", "/rooms/%s/state/m.room.topic" % self.uncreated_rmid,
-                           topic_content)
+            "PUT", "/rooms/%s/state/m.room.topic" % self.uncreated_rmid,
+            topic_content
+        )
         self.assertEquals(403, code, msg=str(response))
         (code, response) = yield self.mock_resource.trigger_get(
-                           "/rooms/%s/state/m.room.topic" % self.uncreated_rmid)
+            "/rooms/%s/state/m.room.topic" % self.uncreated_rmid
+        )
         self.assertEquals(403, code, msg=str(response))
 
         # set/get topic in created PRIVATE room not joined, expect 403
         (code, response) = yield self.mock_resource.trigger(
-                           "PUT", topic_path, topic_content)
+            "PUT", topic_path, topic_content
+        )
         self.assertEquals(403, code, msg=str(response))
         (code, response) = yield self.mock_resource.trigger_get(topic_path)
         self.assertEquals(403, code, msg=str(response))
 
         # set topic in created PRIVATE room and invited, expect 403
-        yield self.invite(room=self.created_rmid, src=self.rmcreator_id,
-                          targ=self.user_id)
+        yield self.invite(
+            room=self.created_rmid, src=self.rmcreator_id, targ=self.user_id
+        )
         (code, response) = yield self.mock_resource.trigger(
-                           "PUT", topic_path, topic_content)
+            "PUT", topic_path, topic_content
+        )
         self.assertEquals(403, code, msg=str(response))
 
         # get topic in created PRIVATE room and invited, expect 403
@@ -226,7 +203,8 @@ class RoomPermissionsTestCase(RestTestCase):
         # Only room ops can set topic by default
         self.auth_user_id = self.rmcreator_id
         (code, response) = yield self.mock_resource.trigger(
-                           "PUT", topic_path, topic_content)
+            "PUT", topic_path, topic_content
+        )
         self.assertEquals(200, code, msg=str(response))
         self.auth_user_id = self.user_id
 
@@ -237,30 +215,31 @@ class RoomPermissionsTestCase(RestTestCase):
         # set/get topic in created PRIVATE room and left, expect 403
         yield self.leave(room=self.created_rmid, user=self.user_id)
         (code, response) = yield self.mock_resource.trigger(
-                           "PUT", topic_path, topic_content)
+            "PUT", topic_path, topic_content
+        )
         self.assertEquals(403, code, msg=str(response))
         (code, response) = yield self.mock_resource.trigger_get(topic_path)
         self.assertEquals(200, code, msg=str(response))
 
         # get topic in PUBLIC room, not joined, expect 403
         (code, response) = yield self.mock_resource.trigger_get(
-                           "/rooms/%s/state/m.room.topic" % self.created_public_rmid)
+            "/rooms/%s/state/m.room.topic" % self.created_public_rmid
+        )
         self.assertEquals(403, code, msg=str(response))
 
         # set topic in PUBLIC room, not joined, expect 403
         (code, response) = yield self.mock_resource.trigger(
-                           "PUT",
-                           "/rooms/%s/state/m.room.topic" % self.created_public_rmid,
-                           topic_content)
+            "PUT",
+            "/rooms/%s/state/m.room.topic" % self.created_public_rmid,
+            topic_content
+        )
         self.assertEquals(403, code, msg=str(response))
 
     @defer.inlineCallbacks
     def _test_get_membership(self, room=None, members=[], expect_code=None):
-        path = "/rooms/%s/state/m.room.member/%s"
         for member in members:
-            (code, response) = yield self.mock_resource.trigger_get(
-                               path %
-                               (room, member))
+            path = "/rooms/%s/state/m.room.member/%s" % (room, member)
+            (code, response) = yield self.mock_resource.trigger_get(path)
             self.assertEquals(expect_code, code)
 
     @defer.inlineCallbacks
@@ -461,20 +440,23 @@ class RoomsMemberListTestCase(RestTestCase):
     def test_get_member_list(self):
         room_id = yield self.create_room_as(self.user_id)
         (code, response) = yield self.mock_resource.trigger_get(
-                           "/rooms/%s/members" % room_id)
+            "/rooms/%s/members" % room_id
+        )
         self.assertEquals(200, code, msg=str(response))
 
     @defer.inlineCallbacks
     def test_get_member_list_no_room(self):
         (code, response) = yield self.mock_resource.trigger_get(
-                           "/rooms/roomdoesnotexist/members")
+            "/rooms/roomdoesnotexist/members"
+        )
         self.assertEquals(403, code, msg=str(response))
 
     @defer.inlineCallbacks
     def test_get_member_list_no_permission(self):
         room_id = yield self.create_room_as("@some_other_guy:red")
         (code, response) = yield self.mock_resource.trigger_get(
-                           "/rooms/%s/members" % room_id)
+            "/rooms/%s/members" % room_id
+        )
         self.assertEquals(403, code, msg=str(response))
 
     @defer.inlineCallbacks
@@ -636,34 +618,41 @@ class RoomTopicTestCase(RestTestCase):
     @defer.inlineCallbacks
     def test_invalid_puts(self):
         # missing keys or invalid json
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           self.path, '{}')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", self.path, '{}'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           self.path, '{"_name":"bob"}')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", self.path, '{"_name":"bob"}'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           self.path, '{"nao')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", self.path, '{"nao'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           self.path, '[{"_name":"bob"},{"_name":"jill"}]')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", self.path, '[{"_name":"bob"},{"_name":"jill"}]'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           self.path, 'text only')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", self.path, 'text only'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           self.path, '')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", self.path, ''
+        )
         self.assertEquals(400, code, msg=str(response))
 
         # valid key, wrong type
         content = '{"topic":["Topic name"]}'
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           self.path, content)
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", self.path, content
+        )
         self.assertEquals(400, code, msg=str(response))
 
     @defer.inlineCallbacks
@@ -674,8 +663,9 @@ class RoomTopicTestCase(RestTestCase):
 
         # valid put
         content = '{"topic":"Topic name"}'
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           self.path, content)
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", self.path, content
+        )
         self.assertEquals(200, code, msg=str(response))
 
         # valid get
@@ -687,8 +677,9 @@ class RoomTopicTestCase(RestTestCase):
     def test_rooms_topic_with_extra_keys(self):
         # valid put with extra keys
         content = '{"topic":"Seasons","subtopic":"Summer"}'
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           self.path, content)
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", self.path, content
+        )
         self.assertEquals(200, code, msg=str(response))
 
         # valid get
@@ -740,33 +731,38 @@ class RoomMemberStateTestCase(RestTestCase):
     def test_invalid_puts(self):
         path = "/rooms/%s/state/m.room.member/%s" % (self.room_id, self.user_id)
         # missing keys or invalid json
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, '{}')
+        (code, response) = yield self.mock_resource.trigger("PUT", path, '{}')
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, '{"_name":"bob"}')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, '{"_name":"bob"}'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, '{"nao')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, '{"nao'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, '[{"_name":"bob"},{"_name":"jill"}]')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, '[{"_name":"bob"},{"_name":"jill"}]'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, 'text only')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, 'text only'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, '')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, ''
+        )
         self.assertEquals(400, code, msg=str(response))
 
         # valid keys, wrong types
-        content = ('{"membership":["%s","%s","%s"]}' %
-                  (Membership.INVITE, Membership.JOIN, Membership.LEAVE))
+        content = ('{"membership":["%s","%s","%s"]}' % (
+            Membership.INVITE, Membership.JOIN, Membership.LEAVE
+        ))
         (code, response) = yield self.mock_resource.trigger("PUT", path, content)
         self.assertEquals(400, code, msg=str(response))
 
@@ -813,8 +809,9 @@ class RoomMemberStateTestCase(RestTestCase):
         )
 
         # valid invite message with custom key
-        content = ('{"membership":"%s","invite_text":"%s"}' %
-                    (Membership.INVITE, "Join us!"))
+        content = ('{"membership":"%s","invite_text":"%s"}' % (
+            Membership.INVITE, "Join us!"
+        ))
         (code, response) = yield self.mock_resource.trigger("PUT", path, content)
         self.assertEquals(200, code, msg=str(response))
 
@@ -867,28 +864,34 @@ class RoomMessagesTestCase(RestTestCase):
         path = "/rooms/%s/send/m.room.message/mid1" % (
             urllib.quote(self.room_id))
         # missing keys or invalid json
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, '{}')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, '{}'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, '{"_name":"bob"}')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, '{"_name":"bob"}'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, '{"nao')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, '{"nao'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, '[{"_name":"bob"},{"_name":"jill"}]')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, '[{"_name":"bob"},{"_name":"jill"}]'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, 'text only')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, 'text only'
+        )
         self.assertEquals(400, code, msg=str(response))
 
-        (code, response) = yield self.mock_resource.trigger("PUT",
-                           path, '')
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, ''
+        )
         self.assertEquals(400, code, msg=str(response))
 
     @defer.inlineCallbacks
@@ -959,7 +962,8 @@ class RoomInitialSyncTestCase(RestTestCase):
     @defer.inlineCallbacks
     def test_initial_sync(self):
         (code, response) = yield self.mock_resource.trigger_get(
-                "/rooms/%s/initialSync" % self.room_id)
+            "/rooms/%s/initialSync" % self.room_id
+        )
         self.assertEquals(200, code)
 
         self.assertEquals(self.room_id, response["room_id"])
@@ -983,8 +987,8 @@ class RoomInitialSyncTestCase(RestTestCase):
 
         self.assertTrue("presence" in response)
 
-        presence_by_user = {e["content"]["user_id"]: e
-            for e in response["presence"]
+        presence_by_user = {
+            e["content"]["user_id"]: e for e in response["presence"]
         }
         self.assertTrue(self.user_id in presence_by_user)
         self.assertEquals("m.presence", presence_by_user[self.user_id]["type"])
diff --git a/tests/rest/client/v1/test_typing.py b/tests/rest/client/v1/test_typing.py
index c4ac181a33..16d788ff61 100644
--- a/tests/rest/client/v1/test_typing.py
+++ b/tests/rest/client/v1/test_typing.py
@@ -81,9 +81,9 @@ class RoomTypingTestCase(RestTestCase):
                 return defer.succeed([])
 
         @defer.inlineCallbacks
-        def fetch_room_distributions_into(room_id, localusers=None,
-                remotedomains=None, ignore_user=None):
-
+        def fetch_room_distributions_into(
+            room_id, localusers=None, remotedomains=None, ignore_user=None
+        ):
             members = yield get_room_members(room_id)
             for member in members:
                 if ignore_user is not None and member == ignore_user:
@@ -96,7 +96,8 @@ class RoomTypingTestCase(RestTestCase):
                     if remotedomains is not None:
                         remotedomains.add(member.domain)
         hs.get_handlers().room_member_handler.fetch_room_distributions_into = (
-                fetch_room_distributions_into)
+            fetch_room_distributions_into
+        )
 
         synapse.rest.client.v1.room.register_servlets(hs, self.mock_resource)
 
@@ -109,8 +110,8 @@ class RoomTypingTestCase(RestTestCase):
 
     @defer.inlineCallbacks
     def test_set_typing(self):
-        (code, _) = yield self.mock_resource.trigger("PUT",
-            "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
+        (code, _) = yield self.mock_resource.trigger(
+            "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
             '{"typing": true, "timeout": 30000}'
         )
         self.assertEquals(200, code)
@@ -120,41 +121,38 @@ class RoomTypingTestCase(RestTestCase):
             from_key=0,
             room_ids=[self.room_id],
         )
-        self.assertEquals(
-            events[0],
-            [
-                {"type": "m.typing",
-                 "room_id": self.room_id,
-                 "content": {
-                     "user_ids": [self.user_id],
-                }},
-            ]
-        )
+        self.assertEquals(events[0], [{
+            "type": "m.typing",
+            "room_id": self.room_id,
+            "content": {
+                "user_ids": [self.user_id],
+            }
+        }])
 
     @defer.inlineCallbacks
     def test_set_not_typing(self):
-        (code, _) = yield self.mock_resource.trigger("PUT",
-            "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
+        (code, _) = yield self.mock_resource.trigger(
+            "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
             '{"typing": false}'
         )
         self.assertEquals(200, code)
 
     @defer.inlineCallbacks
     def test_typing_timeout(self):
-        (code, _) = yield self.mock_resource.trigger("PUT",
-            "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
+        (code, _) = yield self.mock_resource.trigger(
+            "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
             '{"typing": true, "timeout": 30000}'
         )
         self.assertEquals(200, code)
 
         self.assertEquals(self.event_source.get_current_key(), 1)
 
-        self.clock.advance_time(31);
+        self.clock.advance_time(31)
 
         self.assertEquals(self.event_source.get_current_key(), 2)
 
-        (code, _) = yield self.mock_resource.trigger("PUT",
-            "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
+        (code, _) = yield self.mock_resource.trigger(
+            "PUT", "/rooms/%s/typing/%s" % (self.room_id, self.user_id),
             '{"typing": true, "timeout": 30000}'
         )
         self.assertEquals(200, code)
diff --git a/tests/rest/client/v1/utils.py b/tests/rest/client/v1/utils.py
index af376804f6..17524b2e23 100644
--- a/tests/rest/client/v1/utils.py
+++ b/tests/rest/client/v1/utils.py
@@ -84,8 +84,9 @@ class RestTestCase(unittest.TestCase):
             "membership": membership
         }
 
-        (code, response) = yield self.mock_resource.trigger("PUT", path,
-            json.dumps(data))
+        (code, response) = yield self.mock_resource.trigger(
+            "PUT", path, json.dumps(data)
+        )
         self.assertEquals(expect_code, code, msg=str(response))
 
         self.auth_user_id = temp_id
diff --git a/tests/rest/client/v2_alpha/__init__.py b/tests/rest/client/v2_alpha/__init__.py
index 16dce6c723..84334dce34 100644
--- a/tests/rest/client/v2_alpha/__init__.py
+++ b/tests/rest/client/v2_alpha/__init__.py
@@ -55,7 +55,7 @@ class V2AlphaRestTestCase(unittest.TestCase):
             r.register_servlets(hs, self.mock_resource)
 
     def make_datastore_mock(self):
-        store =  Mock(spec=[
+        store = Mock(spec=[
             "insert_client_ip",
         ])
         store.get_app_service_by_token = Mock(return_value=None)
diff --git a/tests/rest/client/v2_alpha/test_filter.py b/tests/rest/client/v2_alpha/test_filter.py
index c86e904c8e..d1442aafac 100644
--- a/tests/rest/client/v2_alpha/test_filter.py
+++ b/tests/rest/client/v2_alpha/test_filter.py
@@ -15,8 +15,6 @@
 
 from twisted.internet import defer
 
-from mock import Mock
-
 from . import V2AlphaRestTestCase
 
 from synapse.rest.client.v2_alpha import filter
@@ -53,9 +51,8 @@ class FilterTestCase(V2AlphaRestTestCase):
 
     @defer.inlineCallbacks
     def test_add_filter(self):
-        (code, response) = yield self.mock_resource.trigger("POST",
-            "/user/%s/filter" % (self.USER_ID),
-            '{"type": ["m.*"]}'
+        (code, response) = yield self.mock_resource.trigger(
+            "POST", "/user/%s/filter" % (self.USER_ID), '{"type": ["m.*"]}'
         )
         self.assertEquals(200, code)
         self.assertEquals({"filter_id": "0"}, response)
@@ -70,8 +67,8 @@ class FilterTestCase(V2AlphaRestTestCase):
             {"type": ["m.*"]}
         ]
 
-        (code, response) = yield self.mock_resource.trigger("GET",
-            "/user/%s/filter/0" % (self.USER_ID), None
+        (code, response) = yield self.mock_resource.trigger_get(
+            "/user/%s/filter/0" % (self.USER_ID)
         )
         self.assertEquals(200, code)
         self.assertEquals({"type": ["m.*"]}, response)
@@ -82,14 +79,14 @@ class FilterTestCase(V2AlphaRestTestCase):
             {"type": ["m.*"]}
         ]
 
-        (code, response) = yield self.mock_resource.trigger("GET",
-            "/user/%s/filter/2" % (self.USER_ID), None
+        (code, response) = yield self.mock_resource.trigger_get(
+            "/user/%s/filter/2" % (self.USER_ID)
         )
         self.assertEquals(404, code)
 
     @defer.inlineCallbacks
     def test_get_filter_no_user(self):
-        (code, response) = yield self.mock_resource.trigger("GET",
-            "/user/%s/filter/0" % (self.USER_ID), None
+        (code, response) = yield self.mock_resource.trigger_get(
+            "/user/%s/filter/0" % (self.USER_ID)
         )
         self.assertEquals(404, code)
diff --git a/tests/rest/client/v2_alpha/test_register.py b/tests/rest/client/v2_alpha/test_register.py
index df0841b0b1..b867599079 100644
--- a/tests/rest/client/v2_alpha/test_register.py
+++ b/tests/rest/client/v2_alpha/test_register.py
@@ -1,7 +1,7 @@
 from synapse.rest.client.v2_alpha.register import RegisterRestServlet
 from synapse.api.errors import SynapseError
 from twisted.internet import defer
-from mock import Mock, MagicMock
+from mock import Mock
 from tests import unittest
 import json
 
@@ -24,7 +24,7 @@ class RegisterRestServletTestCase(unittest.TestCase):
 
         self.auth_result = (False, None, None)
         self.auth_handler = Mock(
-            check_auth=Mock(side_effect=lambda x,y,z: self.auth_result)
+            check_auth=Mock(side_effect=lambda x, y, z: self.auth_result)
         )
         self.registration_handler = Mock()
         self.identity_handler = Mock()
diff --git a/tests/storage/event_injector.py b/tests/storage/event_injector.py
index dca785eb27..f22ba8db89 100644
--- a/tests/storage/event_injector.py
+++ b/tests/storage/event_injector.py
@@ -14,15 +14,9 @@
 # limitations under the License.
 
 
-from tests import unittest
 from twisted.internet import defer
 
-from synapse.api.constants import EventTypes, Membership
-from synapse.types import UserID, RoomID
-
-from tests.utils import setup_test_homeserver
-
-from mock import Mock
+from synapse.api.constants import EventTypes
 
 
 class EventInjector:
diff --git a/tests/storage/test__base.py b/tests/storage/test__base.py
index 219288621d..96b7dba5fe 100644
--- a/tests/storage/test__base.py
+++ b/tests/storage/test__base.py
@@ -174,11 +174,13 @@ class CacheDecoratorTestCase(unittest.TestCase):
 
         # There must have been at least 2 evictions, meaning if we calculate
         # all 12 values again, we must get called at least 2 more times
-        for k in range(0,12):
+        for k in range(0, 12):
             yield a.func(k)
 
-        self.assertTrue(callcount[0] >= 14,
-            msg="Expected callcount >= 14, got %d" % (callcount[0]))
+        self.assertTrue(
+            callcount[0] >= 14,
+            msg="Expected callcount >= 14, got %d" % (callcount[0])
+        )
 
     def test_prefill(self):
         callcount = [0]
diff --git a/tests/storage/test_background_update.py b/tests/storage/test_background_update.py
index 29289fa9b4..6e4d9b1373 100644
--- a/tests/storage/test_background_update.py
+++ b/tests/storage/test_background_update.py
@@ -1,13 +1,11 @@
 from tests import unittest
 from twisted.internet import defer
 
-from synapse.api.constants import EventTypes
-from synapse.types import UserID, RoomID, RoomAlias
-
 from tests.utils import setup_test_homeserver
 
 from mock import Mock
 
+
 class BackgroundUpdateTestCase(unittest.TestCase):
 
     @defer.inlineCallbacks
@@ -24,8 +22,8 @@ class BackgroundUpdateTestCase(unittest.TestCase):
 
     @defer.inlineCallbacks
     def test_do_background_update(self):
-        desired_count = 1000;
-        duration_ms = 42;
+        desired_count = 1000
+        duration_ms = 42
 
         @defer.inlineCallbacks
         def update(progress, count):
diff --git a/tests/storage/test_base.py b/tests/storage/test_base.py
index 152d027663..c76545be65 100644
--- a/tests/storage/test_base.py
+++ b/tests/storage/test_base.py
@@ -17,7 +17,7 @@
 from tests import unittest
 from twisted.internet import defer
 
-from mock import Mock, call
+from mock import Mock
 
 from collections import OrderedDict
 
@@ -62,13 +62,12 @@ class SQLBaseStoreTestCase(unittest.TestCase):
         self.mock_txn.rowcount = 1
 
         yield self.datastore._simple_insert(
-                table="tablename",
-                values={"columname": "Value"}
+            table="tablename",
+            values={"columname": "Value"}
         )
 
         self.mock_txn.execute.assert_called_with(
-                "INSERT INTO tablename (columname) VALUES(?)",
-                ("Value",)
+            "INSERT INTO tablename (columname) VALUES(?)", ("Value",)
         )
 
     @defer.inlineCallbacks
@@ -76,14 +75,14 @@ class SQLBaseStoreTestCase(unittest.TestCase):
         self.mock_txn.rowcount = 1
 
         yield self.datastore._simple_insert(
-                table="tablename",
-                # Use OrderedDict() so we can assert on the SQL generated
-                values=OrderedDict([("colA", 1), ("colB", 2), ("colC", 3)])
+            table="tablename",
+            # Use OrderedDict() so we can assert on the SQL generated
+            values=OrderedDict([("colA", 1), ("colB", 2), ("colC", 3)])
         )
 
         self.mock_txn.execute.assert_called_with(
-                "INSERT INTO tablename (colA, colB, colC) VALUES(?, ?, ?)",
-                (1, 2, 3,)
+            "INSERT INTO tablename (colA, colB, colC) VALUES(?, ?, ?)",
+            (1, 2, 3,)
         )
 
     @defer.inlineCallbacks
@@ -92,15 +91,14 @@ class SQLBaseStoreTestCase(unittest.TestCase):
         self.mock_txn.fetchall.return_value = [("Value",)]
 
         value = yield self.datastore._simple_select_one_onecol(
-                table="tablename",
-                keyvalues={"keycol": "TheKey"},
-                retcol="retcol"
+            table="tablename",
+            keyvalues={"keycol": "TheKey"},
+            retcol="retcol"
         )
 
         self.assertEquals("Value", value)
         self.mock_txn.execute.assert_called_with(
-                "SELECT retcol FROM tablename WHERE keycol = ?",
-                ["TheKey"]
+            "SELECT retcol FROM tablename WHERE keycol = ?", ["TheKey"]
         )
 
     @defer.inlineCallbacks
@@ -109,15 +107,15 @@ class SQLBaseStoreTestCase(unittest.TestCase):
         self.mock_txn.fetchone.return_value = (1, 2, 3)
 
         ret = yield self.datastore._simple_select_one(
-                table="tablename",
-                keyvalues={"keycol": "TheKey"},
-                retcols=["colA", "colB", "colC"]
+            table="tablename",
+            keyvalues={"keycol": "TheKey"},
+            retcols=["colA", "colB", "colC"]
         )
 
         self.assertEquals({"colA": 1, "colB": 2, "colC": 3}, ret)
         self.mock_txn.execute.assert_called_with(
-                "SELECT colA, colB, colC FROM tablename WHERE keycol = ?",
-                ["TheKey"]
+            "SELECT colA, colB, colC FROM tablename WHERE keycol = ?",
+            ["TheKey"]
         )
 
     @defer.inlineCallbacks
@@ -126,32 +124,32 @@ class SQLBaseStoreTestCase(unittest.TestCase):
         self.mock_txn.fetchone.return_value = None
 
         ret = yield self.datastore._simple_select_one(
-                table="tablename",
-                keyvalues={"keycol": "Not here"},
-                retcols=["colA"],
-                allow_none=True
+            table="tablename",
+            keyvalues={"keycol": "Not here"},
+            retcols=["colA"],
+            allow_none=True
         )
 
         self.assertFalse(ret)
 
     @defer.inlineCallbacks
     def test_select_list(self):
-        self.mock_txn.rowcount = 3;
+        self.mock_txn.rowcount = 3
         self.mock_txn.fetchall.return_value = ((1,), (2,), (3,))
         self.mock_txn.description = (
-                ("colA", None, None, None, None, None, None),
+            ("colA", None, None, None, None, None, None),
         )
 
         ret = yield self.datastore._simple_select_list(
-                table="tablename",
-                keyvalues={"keycol": "A set"},
-                retcols=["colA"],
+            table="tablename",
+            keyvalues={"keycol": "A set"},
+            retcols=["colA"],
         )
 
         self.assertEquals([{"colA": 1}, {"colA": 2}, {"colA": 3}], ret)
         self.mock_txn.execute.assert_called_with(
-                "SELECT colA FROM tablename WHERE keycol = ?",
-                ["A set"]
+            "SELECT colA FROM tablename WHERE keycol = ?",
+            ["A set"]
         )
 
     @defer.inlineCallbacks
@@ -159,14 +157,14 @@ class SQLBaseStoreTestCase(unittest.TestCase):
         self.mock_txn.rowcount = 1
 
         yield self.datastore._simple_update_one(
-                table="tablename",
-                keyvalues={"keycol": "TheKey"},
-                updatevalues={"columnname": "New Value"}
+            table="tablename",
+            keyvalues={"keycol": "TheKey"},
+            updatevalues={"columnname": "New Value"}
         )
 
         self.mock_txn.execute.assert_called_with(
-                "UPDATE tablename SET columnname = ? WHERE keycol = ?",
-                ["New Value", "TheKey"]
+            "UPDATE tablename SET columnname = ? WHERE keycol = ?",
+            ["New Value", "TheKey"]
         )
 
     @defer.inlineCallbacks
@@ -174,15 +172,15 @@ class SQLBaseStoreTestCase(unittest.TestCase):
         self.mock_txn.rowcount = 1
 
         yield self.datastore._simple_update_one(
-                table="tablename",
-                keyvalues=OrderedDict([("colA", 1), ("colB", 2)]),
-                updatevalues=OrderedDict([("colC", 3), ("colD", 4)])
+            table="tablename",
+            keyvalues=OrderedDict([("colA", 1), ("colB", 2)]),
+            updatevalues=OrderedDict([("colC", 3), ("colD", 4)])
         )
 
         self.mock_txn.execute.assert_called_with(
-                "UPDATE tablename SET colC = ?, colD = ? WHERE " +
-                    "colA = ? AND colB = ?",
-                [3, 4, 1, 2]
+            "UPDATE tablename SET colC = ?, colD = ? WHERE"
+            " colA = ? AND colB = ?",
+            [3, 4, 1, 2]
         )
 
     @defer.inlineCallbacks
@@ -190,11 +188,10 @@ class SQLBaseStoreTestCase(unittest.TestCase):
         self.mock_txn.rowcount = 1
 
         yield self.datastore._simple_delete_one(
-                table="tablename",
-                keyvalues={"keycol": "Go away"},
+            table="tablename",
+            keyvalues={"keycol": "Go away"},
         )
 
         self.mock_txn.execute.assert_called_with(
-                "DELETE FROM tablename WHERE keycol = ?",
-                ["Go away"]
+            "DELETE FROM tablename WHERE keycol = ?", ["Go away"]
         )
diff --git a/tests/storage/test_events.py b/tests/storage/test_events.py
index 4aa82d4c9d..18a6cff0c7 100644
--- a/tests/storage/test_events.py
+++ b/tests/storage/test_events.py
@@ -12,7 +12,6 @@
 # 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.
-import uuid
 from mock import Mock
 from synapse.types import RoomID, UserID
 
diff --git a/tests/storage/test_profile.py b/tests/storage/test_profile.py
index 47e2768b2c..24118bbc86 100644
--- a/tests/storage/test_profile.py
+++ b/tests/storage/test_profile.py
@@ -55,7 +55,7 @@ class ProfileStoreTestCase(unittest.TestCase):
         )
 
         yield self.store.set_profile_avatar_url(
-                self.u_frank.localpart, "http://my.site/here"
+            self.u_frank.localpart, "http://my.site/here"
         )
 
         self.assertEquals(
diff --git a/tests/storage/test_registration.py b/tests/storage/test_registration.py
index 7b3b4c13bc..b8384c98d8 100644
--- a/tests/storage/test_registration.py
+++ b/tests/storage/test_registration.py
@@ -33,8 +33,10 @@ class RegistrationStoreTestCase(unittest.TestCase):
         self.store = hs.get_datastore()
 
         self.user_id = "@my-user:test"
-        self.tokens = ["AbCdEfGhIjKlMnOpQrStUvWxYz",
-                        "BcDeFgHiJkLmNoPqRsTuVwXyZa"]
+        self.tokens = [
+            "AbCdEfGhIjKlMnOpQrStUvWxYz",
+            "BcDeFgHiJkLmNoPqRsTuVwXyZa"
+        ]
         self.pwhash = "{xx1}123456789"
 
     @defer.inlineCallbacks
diff --git a/tests/storage/test_room.py b/tests/storage/test_room.py
index 0baaf3df21..ef8a4d234f 100644
--- a/tests/storage/test_room.py
+++ b/tests/storage/test_room.py
@@ -37,7 +37,8 @@ class RoomStoreTestCase(unittest.TestCase):
         self.alias = RoomAlias.from_string("#a-room-name:test")
         self.u_creator = UserID.from_string("@creator:test")
 
-        yield self.store.store_room(self.room.to_string(),
+        yield self.store.store_room(
+            self.room.to_string(),
             room_creator_user_id=self.u_creator.to_string(),
             is_public=True
         )
@@ -45,9 +46,11 @@ class RoomStoreTestCase(unittest.TestCase):
     @defer.inlineCallbacks
     def test_get_room(self):
         self.assertDictContainsSubset(
-            {"room_id": self.room.to_string(),
-             "creator": self.u_creator.to_string(),
-             "is_public": True},
+            {
+                "room_id": self.room.to_string(),
+                "creator": self.u_creator.to_string(),
+                "is_public": True
+            },
             (yield self.store.get_room(self.room.to_string()))
         )
 
@@ -65,7 +68,8 @@ class RoomEventsStoreTestCase(unittest.TestCase):
 
         self.room = RoomID.from_string("!abcde:test")
 
-        yield self.store.store_room(self.room.to_string(),
+        yield self.store.store_room(
+            self.room.to_string(),
             room_creator_user_id="@creator:text",
             is_public=True
         )
diff --git a/tests/storage/test_roommember.py b/tests/storage/test_roommember.py
index bab15c4165..677d11f68d 100644
--- a/tests/storage/test_roommember.py
+++ b/tests/storage/test_roommember.py
@@ -88,8 +88,8 @@ class RoomMemberStoreTestCase(unittest.TestCase):
             [m.room_id for m in (
                 yield self.store.get_rooms_for_user_where_membership_is(
                     self.u_alice.to_string(), [Membership.JOIN]
-                ))
-            ]
+                )
+            )]
         )
         self.assertFalse(
             (yield self.store.user_rooms_intersect(
@@ -108,11 +108,11 @@ class RoomMemberStoreTestCase(unittest.TestCase):
                 yield self.store.get_room_members(self.room.to_string())
             )}
         )
-        self.assertTrue(
-            (yield self.store.user_rooms_intersect(
-                [self.u_alice.to_string(), self.u_bob.to_string()]
-            ))
-        )
+        self.assertTrue((
+            yield self.store.user_rooms_intersect([
+                self.u_alice.to_string(), self.u_bob.to_string()
+            ])
+        ))
 
     @defer.inlineCallbacks
     def test_room_hosts(self):
@@ -136,9 +136,7 @@ class RoomMemberStoreTestCase(unittest.TestCase):
 
         self.assertEquals(
             {"test", "elsewhere"},
-            (yield
-                self.store.get_joined_hosts_for_room(self.room.to_string())
-            )
+            (yield self.store.get_joined_hosts_for_room(self.room.to_string()))
         )
 
         # Should still have both hosts
@@ -146,9 +144,7 @@ class RoomMemberStoreTestCase(unittest.TestCase):
 
         self.assertEquals(
             {"test", "elsewhere"},
-            (yield
-                self.store.get_joined_hosts_for_room(self.room.to_string())
-            )
+            (yield self.store.get_joined_hosts_for_room(self.room.to_string()))
         )
 
         # Should have only one host after other leaves
diff --git a/tests/storage/test_stream.py b/tests/storage/test_stream.py
index 708208aff1..da322152c7 100644
--- a/tests/storage/test_stream.py
+++ b/tests/storage/test_stream.py
@@ -156,13 +156,13 @@ class StreamStoreTestCase(unittest.TestCase):
             self.room1, self.u_bob, Membership.JOIN
         )
 
-        event1 = yield self.event_injector.inject_room_member(
+        yield self.event_injector.inject_room_member(
             self.room1, self.u_alice, Membership.JOIN
         )
 
         start = yield self.store.get_room_events_max_id()
 
-        event2 = yield self.event_injector.inject_room_member(
+        yield self.event_injector.inject_room_member(
             self.room1, self.u_alice, Membership.JOIN,
         )
 
diff --git a/tests/test_distributor.py b/tests/test_distributor.py
index a80f580ba6..acebcf4a86 100644
--- a/tests/test_distributor.py
+++ b/tests/test_distributor.py
@@ -44,8 +44,10 @@ class DistributorTestCase(unittest.TestCase):
         self.dist.declare("whine")
 
         d_inner = defer.Deferred()
+
         def observer():
             return d_inner
+
         self.dist.observe("whine", observer)
 
         d_outer = self.dist.fire("whine")
@@ -66,8 +68,8 @@ class DistributorTestCase(unittest.TestCase):
 
         observers[0].side_effect = Exception("Awoogah!")
 
-        with patch("synapse.util.distributor.logger",
-                spec=["warning"]
+        with patch(
+            "synapse.util.distributor.logger", spec=["warning"]
         ) as mock_logger:
             d = self.dist.fire("alarm", "Go")
             yield d
@@ -77,8 +79,9 @@ class DistributorTestCase(unittest.TestCase):
             observers[1].assert_called_once_with("Go")
 
             self.assertEquals(mock_logger.warning.call_count, 1)
-            self.assertIsInstance(mock_logger.warning.call_args[0][0],
-                    str)
+            self.assertIsInstance(
+                mock_logger.warning.call_args[0][0], str
+            )
 
     @defer.inlineCallbacks
     def test_signal_catch_no_suppress(self):
diff --git a/tests/test_test_utils.py b/tests/test_test_utils.py
index 3718f4fc3f..d28bb726bb 100644
--- a/tests/test_test_utils.py
+++ b/tests/test_test_utils.py
@@ -17,6 +17,7 @@ from tests import unittest
 
 from tests.utils import MockClock
 
+
 class MockClockTestCase(unittest.TestCase):
 
     def setUp(self):
@@ -60,7 +61,7 @@ class MockClockTestCase(unittest.TestCase):
 
         def _cb1():
             invoked[1] = 1
-        t1 = self.clock.call_later(20, _cb1)
+        self.clock.call_later(20, _cb1)
 
         self.clock.cancel_call_later(t0)
 
diff --git a/tests/unittest.py b/tests/unittest.py
index 6f02eb4cac..5b22abfe74 100644
--- a/tests/unittest.py
+++ b/tests/unittest.py
@@ -37,9 +37,12 @@ def around(target):
     def _around(code):
         name = code.__name__
         orig = getattr(target, name)
+
         def new(*args, **kwargs):
             return code(orig, *args, **kwargs)
+
         setattr(target, name, new)
+
     return _around
 
 
@@ -53,9 +56,7 @@ class TestCase(unittest.TestCase):
 
         method = getattr(self, methodName)
 
-        level = getattr(method, "loglevel",
-                    getattr(self, "loglevel",
-                        NEVER))
+        level = getattr(method, "loglevel", getattr(self, "loglevel", NEVER))
 
         @around(self)
         def setUp(orig):
diff --git a/tests/util/__init__.py b/tests/util/__init__.py
index d0e9399dda..bfebb0f644 100644
--- a/tests/util/__init__.py
+++ b/tests/util/__init__.py
@@ -12,4 +12,3 @@
 # 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.
-
diff --git a/tests/util/test_dict_cache.py b/tests/util/test_dict_cache.py
index 7bbe795622..272b71034a 100644
--- a/tests/util/test_dict_cache.py
+++ b/tests/util/test_dict_cache.py
@@ -14,7 +14,6 @@
 # limitations under the License.
 
 
-from twisted.internet import defer
 from tests import unittest
 
 from synapse.util.caches.dictionary_cache import DictionaryCache
diff --git a/tests/util/test_snapshot_cache.py b/tests/util/test_snapshot_cache.py
index 4ee0d49673..7e289715ba 100644
--- a/tests/util/test_snapshot_cache.py
+++ b/tests/util/test_snapshot_cache.py
@@ -19,6 +19,7 @@ from .. import unittest
 from synapse.util.caches.snapshot_cache import SnapshotCache
 from twisted.internet.defer import Deferred
 
+
 class SnapshotCacheTestCase(unittest.TestCase):
 
     def setUp(self):
diff --git a/tests/util/test_treecache.py b/tests/util/test_treecache.py
index 1efbeb6b33..7ab578a185 100644
--- a/tests/util/test_treecache.py
+++ b/tests/util/test_treecache.py
@@ -18,6 +18,7 @@ from .. import unittest
 
 from synapse.util.caches.treecache import TreeCache
 
+
 class TreeCacheTestCase(unittest.TestCase):
     def test_get_set_onelevel(self):
         cache = TreeCache()
@@ -76,3 +77,9 @@ class TreeCacheTestCase(unittest.TestCase):
         cache[("b",)] = "B"
         cache.clear()
         self.assertEquals(len(cache), 0)
+
+    def test_contains(self):
+        cache = TreeCache()
+        cache[("a",)] = "A"
+        self.assertTrue(("a",) in cache)
+        self.assertFalse(("b",) in cache)
diff --git a/tests/utils.py b/tests/utils.py
index f71125042b..bf7a31ff9e 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -152,7 +152,7 @@ class MockHttpResource(HttpServer):
 
         mock_request.getClientIP.return_value = "-"
 
-        mock_request.requestHeaders.getRawHeaders.return_value=[
+        mock_request.requestHeaders.getRawHeaders.return_value = [
             "X-Matrix origin=test,key=,sig="
         ]
 
@@ -360,13 +360,12 @@ class MemoryDataStore(object):
 
     def get_rooms_for_user_where_membership_is(self, user_id, membership_list):
         return [
-            self.members[r].get(user_id) for r in self.members
-            if user_id in self.members[r] and
-                self.members[r][user_id].membership in membership_list
+            m[user_id] for m in self.members.values()
+            if user_id in m and m[user_id].membership in membership_list
         ]
 
     def get_room_events_stream(self, user_id=None, from_key=None, to_key=None,
-                            limit=0, with_feedback=False):
+                               limit=0, with_feedback=False):
         return ([], from_key)  # TODO
 
     def get_joined_hosts_for_room(self, room_id):
@@ -376,7 +375,6 @@ class MemoryDataStore(object):
         if event.type == EventTypes.Member:
             room_id = event.room_id
             user = event.state_key
-            membership = event.membership
             self.members.setdefault(room_id, {})[user] = event
 
         if hasattr(event, "state_key"):
@@ -456,9 +454,9 @@ class DeferredMockCallable(object):
                 d.callback(None)
                 return result
 
-        failure = AssertionError("Was not expecting call(%s)" %
+        failure = AssertionError("Was not expecting call(%s)" % (
             _format_call(args, kwargs)
-        )
+        ))
 
         for _, _, d in self.expectations:
             try:
@@ -479,14 +477,12 @@ class DeferredMockCallable(object):
         )
 
         timer = reactor.callLater(
-            timeout/1000,
+            timeout / 1000,
             deferred.errback,
-            AssertionError(
-                "%d pending calls left: %s"% (
-                    len([e for e in self.expectations if not e[2].called]),
-                    [e for e in self.expectations if not e[2].called]
-                )
-            )
+            AssertionError("%d pending calls left: %s" % (
+                len([e for e in self.expectations if not e[2].called]),
+                [e for e in self.expectations if not e[2].called]
+            ))
         )
 
         yield deferred
@@ -500,8 +496,8 @@ class DeferredMockCallable(object):
             calls = self.calls
             self.calls = []
 
-            raise AssertionError("Expected not to received any calls, got:\n" +
-                "\n".join([
+            raise AssertionError(
+                "Expected not to received any calls, got:\n" + "\n".join([
                     "call(%s)" % _format_call(c[0], c[1]) for c in calls
                 ])
             )
diff --git a/tox.ini b/tox.ini
index 6f01599af7..757b7189c3 100644
--- a/tox.ini
+++ b/tox.ini
@@ -26,4 +26,4 @@ skip_install = True
 basepython = python2.7
 deps =
     flake8
-commands = /bin/bash -c "flake8 synapse {env:PEP8SUFFIX:}"
+commands = /bin/bash -c "flake8 synapse tests {env:PEP8SUFFIX:}"