Port tests/ to Python 3 (#3808)

This commit is contained in:
Amber Brown 2018-09-07 02:58:18 +10:00 committed by GitHub
parent c5440b2ca0
commit 52ec6e9dfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 349 additions and 430 deletions

1
changelog.d/3808.misc Normal file
View file

@ -0,0 +1 @@
tests/ is now ported to Python 3.

View file

@ -471,6 +471,7 @@ class AuthTestCase(unittest.TestCase):
def test_reserved_threepid(self): def test_reserved_threepid(self):
self.hs.config.limit_usage_by_mau = True self.hs.config.limit_usage_by_mau = True
self.hs.config.max_mau_value = 1 self.hs.config.max_mau_value = 1
self.store.get_monthly_active_count = lambda: defer.succeed(2)
threepid = {'medium': 'email', 'address': 'reserved@server.com'} threepid = {'medium': 'email', 'address': 'reserved@server.com'}
unknown_threepid = {'medium': 'email', 'address': 'unreserved@server.com'} unknown_threepid = {'medium': 'email', 'address': 'unreserved@server.com'}
self.hs.config.mau_limits_reserved_threepids = [threepid] self.hs.config.mau_limits_reserved_threepids = [threepid]

View file

@ -47,7 +47,7 @@ class FrontendProxyTests(HomeserverTestCase):
self.assertEqual(len(self.reactor.tcpServers), 1) self.assertEqual(len(self.reactor.tcpServers), 1)
site = self.reactor.tcpServers[0][1] site = self.reactor.tcpServers[0][1]
self.resource = ( self.resource = (
site.resource.children["_matrix"].children["client"].children["r0"] site.resource.children[b"_matrix"].children[b"client"].children[b"r0"]
) )
request, channel = self.make_request("PUT", "presence/a/status") request, channel = self.make_request("PUT", "presence/a/status")
@ -77,7 +77,7 @@ class FrontendProxyTests(HomeserverTestCase):
self.assertEqual(len(self.reactor.tcpServers), 1) self.assertEqual(len(self.reactor.tcpServers), 1)
site = self.reactor.tcpServers[0][1] site = self.reactor.tcpServers[0][1]
self.resource = ( self.resource = (
site.resource.children["_matrix"].children["client"].children["r0"] site.resource.children[b"_matrix"].children[b"client"].children[b"r0"]
) )
request, channel = self.make_request("PUT", "presence/a/status") request, channel = self.make_request("PUT", "presence/a/status")

View file

@ -43,9 +43,7 @@ def _expect_edu_transaction(edu_type, content, origin="test"):
def _make_edu_transaction_json(edu_type, content): def _make_edu_transaction_json(edu_type, content):
return json.dumps(_expect_edu_transaction(edu_type, content)).encode( return json.dumps(_expect_edu_transaction(edu_type, content)).encode('utf8')
'utf8'
)
class TypingNotificationsTestCase(unittest.TestCase): class TypingNotificationsTestCase(unittest.TestCase):

View file

@ -22,39 +22,24 @@ from six.moves.urllib import parse as urlparse
from twisted.internet import defer from twisted.internet import defer
import synapse.rest.client.v1.room
from synapse.api.constants import Membership from synapse.api.constants import Membership
from synapse.http.server import JsonResource from synapse.rest.client.v1 import room
from synapse.types import UserID
from synapse.util import Clock
from tests import unittest from tests import unittest
from tests.server import (
ThreadedMemoryReactorClock,
make_request,
render,
setup_test_homeserver,
)
from .utils import RestHelper
PATH_PREFIX = b"/_matrix/client/api/v1" PATH_PREFIX = b"/_matrix/client/api/v1"
class RoomBase(unittest.TestCase): class RoomBase(unittest.HomeserverTestCase):
rmcreator_id = None rmcreator_id = None
def setUp(self): servlets = [room.register_servlets, room.register_deprecated_servlets]
self.clock = ThreadedMemoryReactorClock() def make_homeserver(self, reactor, clock):
self.hs_clock = Clock(self.clock)
self.hs = setup_test_homeserver( self.hs = self.setup_test_homeserver(
self.addCleanup,
"red", "red",
http_client=None, http_client=None,
clock=self.hs_clock,
reactor=self.clock,
federation_client=Mock(), federation_client=Mock(),
ratelimiter=NonCallableMock(spec_set=["send_message"]), ratelimiter=NonCallableMock(spec_set=["send_message"]),
) )
@ -63,42 +48,21 @@ class RoomBase(unittest.TestCase):
self.hs.get_federation_handler = Mock(return_value=Mock()) self.hs.get_federation_handler = Mock(return_value=Mock())
def get_user_by_access_token(token=None, allow_guest=False):
return {
"user": UserID.from_string(self.helper.auth_user_id),
"token_id": 1,
"is_guest": False,
}
def get_user_by_req(request, allow_guest=False, rights="access"):
return synapse.types.create_requester(
UserID.from_string(self.helper.auth_user_id), 1, False, None
)
self.hs.get_auth().get_user_by_req = get_user_by_req
self.hs.get_auth().get_user_by_access_token = get_user_by_access_token
self.hs.get_auth().get_access_token_from_request = Mock(return_value=b"1234")
def _insert_client_ip(*args, **kwargs): def _insert_client_ip(*args, **kwargs):
return defer.succeed(None) return defer.succeed(None)
self.hs.get_datastore().insert_client_ip = _insert_client_ip self.hs.get_datastore().insert_client_ip = _insert_client_ip
self.resource = JsonResource(self.hs) return self.hs
synapse.rest.client.v1.room.register_servlets(self.hs, self.resource)
synapse.rest.client.v1.room.register_deprecated_servlets(self.hs, self.resource)
self.helper = RestHelper(self.hs, self.resource, self.user_id)
class RoomPermissionsTestCase(RoomBase): class RoomPermissionsTestCase(RoomBase):
""" Tests room permissions. """ """ Tests room permissions. """
user_id = b"@sid1:red" user_id = "@sid1:red"
rmcreator_id = b"@notme:red" rmcreator_id = "@notme:red"
def setUp(self): def prepare(self, reactor, clock, hs):
super(RoomPermissionsTestCase, self).setUp()
self.helper.auth_user_id = self.rmcreator_id self.helper.auth_user_id = self.rmcreator_id
# create some rooms under the name rmcreator_id # create some rooms under the name rmcreator_id
@ -114,22 +78,20 @@ class RoomPermissionsTestCase(RoomBase):
self.created_rmid_msg_path = ( self.created_rmid_msg_path = (
"rooms/%s/send/m.room.message/a1" % (self.created_rmid) "rooms/%s/send/m.room.message/a1" % (self.created_rmid)
).encode('ascii') ).encode('ascii')
request, channel = make_request( request, channel = self.make_request(
b"PUT", "PUT", self.created_rmid_msg_path, b'{"msgtype":"m.text","body":"test msg"}'
self.created_rmid_msg_path,
b'{"msgtype":"m.text","body":"test msg"}',
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(channel.result["code"], b"200", channel.result) self.assertEquals(200, channel.code, channel.result)
# set topic for public room # set topic for public room
request, channel = make_request( request, channel = self.make_request(
b"PUT", "PUT",
("rooms/%s/state/m.room.topic" % self.created_public_rmid).encode('ascii'), ("rooms/%s/state/m.room.topic" % self.created_public_rmid).encode('ascii'),
b'{"topic":"Public Room Topic"}', b'{"topic":"Public Room Topic"}',
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(channel.result["code"], b"200", channel.result) self.assertEquals(200, channel.code, channel.result)
# auth as user_id now # auth as user_id now
self.helper.auth_user_id = self.user_id self.helper.auth_user_id = self.user_id
@ -140,128 +102,128 @@ class RoomPermissionsTestCase(RoomBase):
seq = iter(range(100)) seq = iter(range(100))
def send_msg_path(): def send_msg_path():
return b"/rooms/%s/send/m.room.message/mid%s" % ( return "/rooms/%s/send/m.room.message/mid%s" % (
self.created_rmid, self.created_rmid,
str(next(seq)).encode('ascii'), str(next(seq)),
) )
# send message in uncreated room, expect 403 # send message in uncreated room, expect 403
request, channel = make_request( request, channel = self.make_request(
b"PUT", "PUT",
b"/rooms/%s/send/m.room.message/mid2" % (self.uncreated_rmid,), "/rooms/%s/send/m.room.message/mid2" % (self.uncreated_rmid,),
msg_content, msg_content,
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
# send message in created room not joined (no state), expect 403 # send message in created room not joined (no state), expect 403
request, channel = make_request(b"PUT", send_msg_path(), msg_content) request, channel = self.make_request("PUT", send_msg_path(), msg_content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
# send message in created room and invited, expect 403 # send message in created room and invited, expect 403
self.helper.invite( self.helper.invite(
room=self.created_rmid, src=self.rmcreator_id, targ=self.user_id room=self.created_rmid, src=self.rmcreator_id, targ=self.user_id
) )
request, channel = make_request(b"PUT", send_msg_path(), msg_content) request, channel = self.make_request("PUT", send_msg_path(), msg_content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
# send message in created room and joined, expect 200 # send message in created room and joined, expect 200
self.helper.join(room=self.created_rmid, user=self.user_id) self.helper.join(room=self.created_rmid, user=self.user_id)
request, channel = make_request(b"PUT", send_msg_path(), msg_content) request, channel = self.make_request("PUT", send_msg_path(), msg_content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
# send message in created room and left, expect 403 # send message in created room and left, expect 403
self.helper.leave(room=self.created_rmid, user=self.user_id) self.helper.leave(room=self.created_rmid, user=self.user_id)
request, channel = make_request(b"PUT", send_msg_path(), msg_content) request, channel = self.make_request("PUT", send_msg_path(), msg_content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
def test_topic_perms(self): def test_topic_perms(self):
topic_content = b'{"topic":"My Topic Name"}' topic_content = b'{"topic":"My Topic Name"}'
topic_path = b"/rooms/%s/state/m.room.topic" % self.created_rmid topic_path = "/rooms/%s/state/m.room.topic" % self.created_rmid
# set/get topic in uncreated room, expect 403 # set/get topic in uncreated room, expect 403
request, channel = make_request( request, channel = self.make_request(
b"PUT", b"/rooms/%s/state/m.room.topic" % self.uncreated_rmid, topic_content "PUT", "/rooms/%s/state/m.room.topic" % self.uncreated_rmid, topic_content
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
request, channel = make_request( request, channel = self.make_request(
b"GET", "/rooms/%s/state/m.room.topic" % self.uncreated_rmid "GET", "/rooms/%s/state/m.room.topic" % self.uncreated_rmid
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
# set/get topic in created PRIVATE room not joined, expect 403 # set/get topic in created PRIVATE room not joined, expect 403
request, channel = make_request(b"PUT", topic_path, topic_content) request, channel = self.make_request("PUT", topic_path, topic_content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
request, channel = make_request(b"GET", topic_path) request, channel = self.make_request("GET", topic_path)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
# set topic in created PRIVATE room and invited, expect 403 # set topic in created PRIVATE room and invited, expect 403
self.helper.invite( self.helper.invite(
room=self.created_rmid, src=self.rmcreator_id, targ=self.user_id room=self.created_rmid, src=self.rmcreator_id, targ=self.user_id
) )
request, channel = make_request(b"PUT", topic_path, topic_content) request, channel = self.make_request("PUT", topic_path, topic_content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
# get topic in created PRIVATE room and invited, expect 403 # get topic in created PRIVATE room and invited, expect 403
request, channel = make_request(b"GET", topic_path) request, channel = self.make_request("GET", topic_path)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
# set/get topic in created PRIVATE room and joined, expect 200 # set/get topic in created PRIVATE room and joined, expect 200
self.helper.join(room=self.created_rmid, user=self.user_id) self.helper.join(room=self.created_rmid, user=self.user_id)
# Only room ops can set topic by default # Only room ops can set topic by default
self.helper.auth_user_id = self.rmcreator_id self.helper.auth_user_id = self.rmcreator_id
request, channel = make_request(b"PUT", topic_path, topic_content) request, channel = self.make_request("PUT", topic_path, topic_content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
self.helper.auth_user_id = self.user_id self.helper.auth_user_id = self.user_id
request, channel = make_request(b"GET", topic_path) request, channel = self.make_request("GET", topic_path)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
self.assert_dict(json.loads(topic_content), channel.json_body) self.assert_dict(json.loads(topic_content.decode('utf8')), channel.json_body)
# set/get topic in created PRIVATE room and left, expect 403 # set/get topic in created PRIVATE room and left, expect 403
self.helper.leave(room=self.created_rmid, user=self.user_id) self.helper.leave(room=self.created_rmid, user=self.user_id)
request, channel = make_request(b"PUT", topic_path, topic_content) request, channel = self.make_request("PUT", topic_path, topic_content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
request, channel = make_request(b"GET", topic_path) request, channel = self.make_request("GET", topic_path)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
# get topic in PUBLIC room, not joined, expect 403 # get topic in PUBLIC room, not joined, expect 403
request, channel = make_request( request, channel = self.make_request(
b"GET", b"/rooms/%s/state/m.room.topic" % self.created_public_rmid "GET", "/rooms/%s/state/m.room.topic" % self.created_public_rmid
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
# set topic in PUBLIC room, not joined, expect 403 # set topic in PUBLIC room, not joined, expect 403
request, channel = make_request( request, channel = self.make_request(
b"PUT", "PUT",
b"/rooms/%s/state/m.room.topic" % self.created_public_rmid, "/rooms/%s/state/m.room.topic" % self.created_public_rmid,
topic_content, topic_content,
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
def _test_get_membership(self, room=None, members=[], expect_code=None): def _test_get_membership(self, room=None, members=[], expect_code=None):
for member in members: for member in members:
path = b"/rooms/%s/state/m.room.member/%s" % (room, member) path = "/rooms/%s/state/m.room.member/%s" % (room, member)
request, channel = make_request(b"GET", path) request, channel = self.make_request("GET", path)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(expect_code, int(channel.result["code"])) self.assertEquals(expect_code, channel.code)
def test_membership_basic_room_perms(self): def test_membership_basic_room_perms(self):
# === room does not exist === # === room does not exist ===
@ -428,217 +390,211 @@ class RoomPermissionsTestCase(RoomBase):
class RoomsMemberListTestCase(RoomBase): class RoomsMemberListTestCase(RoomBase):
""" Tests /rooms/$room_id/members/list REST events.""" """ Tests /rooms/$room_id/members/list REST events."""
user_id = b"@sid1:red" user_id = "@sid1:red"
def test_get_member_list(self): def test_get_member_list(self):
room_id = self.helper.create_room_as(self.user_id) room_id = self.helper.create_room_as(self.user_id)
request, channel = make_request(b"GET", b"/rooms/%s/members" % room_id) request, channel = self.make_request("GET", "/rooms/%s/members" % room_id)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
def test_get_member_list_no_room(self): def test_get_member_list_no_room(self):
request, channel = make_request(b"GET", b"/rooms/roomdoesnotexist/members") request, channel = self.make_request("GET", "/rooms/roomdoesnotexist/members")
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
def test_get_member_list_no_permission(self): def test_get_member_list_no_permission(self):
room_id = self.helper.create_room_as(b"@some_other_guy:red") room_id = self.helper.create_room_as("@some_other_guy:red")
request, channel = make_request(b"GET", b"/rooms/%s/members" % room_id) request, channel = self.make_request("GET", "/rooms/%s/members" % room_id)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
def test_get_member_list_mixed_memberships(self): def test_get_member_list_mixed_memberships(self):
room_creator = b"@some_other_guy:red" room_creator = "@some_other_guy:red"
room_id = self.helper.create_room_as(room_creator) room_id = self.helper.create_room_as(room_creator)
room_path = b"/rooms/%s/members" % room_id room_path = "/rooms/%s/members" % room_id
self.helper.invite(room=room_id, src=room_creator, targ=self.user_id) self.helper.invite(room=room_id, src=room_creator, targ=self.user_id)
# can't see list if you're just invited. # can't see list if you're just invited.
request, channel = make_request(b"GET", room_path) request, channel = self.make_request("GET", room_path)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(403, channel.code, msg=channel.result["body"])
self.helper.join(room=room_id, user=self.user_id) self.helper.join(room=room_id, user=self.user_id)
# can see list now joined # can see list now joined
request, channel = make_request(b"GET", room_path) request, channel = self.make_request("GET", room_path)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
self.helper.leave(room=room_id, user=self.user_id) self.helper.leave(room=room_id, user=self.user_id)
# can see old list once left # can see old list once left
request, channel = make_request(b"GET", room_path) request, channel = self.make_request("GET", room_path)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
class RoomsCreateTestCase(RoomBase): class RoomsCreateTestCase(RoomBase):
""" Tests /rooms and /rooms/$room_id REST events. """ """ Tests /rooms and /rooms/$room_id REST events. """
user_id = b"@sid1:red" user_id = "@sid1:red"
def test_post_room_no_keys(self): def test_post_room_no_keys(self):
# POST with no config keys, expect new room id # POST with no config keys, expect new room id
request, channel = make_request(b"POST", b"/createRoom", b"{}") request, channel = self.make_request("POST", "/createRoom", "{}")
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), channel.result) self.assertEquals(200, channel.code, channel.result)
self.assertTrue("room_id" in channel.json_body) self.assertTrue("room_id" in channel.json_body)
def test_post_room_visibility_key(self): def test_post_room_visibility_key(self):
# POST with visibility config key, expect new room id # POST with visibility config key, expect new room id
request, channel = make_request( request, channel = self.make_request(
b"POST", b"/createRoom", b'{"visibility":"private"}' "POST", "/createRoom", b'{"visibility":"private"}'
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"])) self.assertEquals(200, channel.code)
self.assertTrue("room_id" in channel.json_body) self.assertTrue("room_id" in channel.json_body)
def test_post_room_custom_key(self): def test_post_room_custom_key(self):
# POST with custom config keys, expect new room id # POST with custom config keys, expect new room id
request, channel = make_request(b"POST", b"/createRoom", b'{"custom":"stuff"}') request, channel = self.make_request(
render(request, self.resource, self.clock) "POST", "/createRoom", b'{"custom":"stuff"}'
self.assertEquals(200, int(channel.result["code"])) )
self.render(request)
self.assertEquals(200, channel.code)
self.assertTrue("room_id" in channel.json_body) self.assertTrue("room_id" in channel.json_body)
def test_post_room_known_and_unknown_keys(self): def test_post_room_known_and_unknown_keys(self):
# POST with custom + known config keys, expect new room id # POST with custom + known config keys, expect new room id
request, channel = make_request( request, channel = self.make_request(
b"POST", b"/createRoom", b'{"visibility":"private","custom":"things"}' "POST", "/createRoom", b'{"visibility":"private","custom":"things"}'
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"])) self.assertEquals(200, channel.code)
self.assertTrue("room_id" in channel.json_body) self.assertTrue("room_id" in channel.json_body)
def test_post_room_invalid_content(self): def test_post_room_invalid_content(self):
# POST with invalid content / paths, expect 400 # POST with invalid content / paths, expect 400
request, channel = make_request(b"POST", b"/createRoom", b'{"visibili') request, channel = self.make_request("POST", "/createRoom", b'{"visibili')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"])) self.assertEquals(400, channel.code)
request, channel = make_request(b"POST", b"/createRoom", b'["hello"]') request, channel = self.make_request("POST", "/createRoom", b'["hello"]')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"])) self.assertEquals(400, channel.code)
class RoomTopicTestCase(RoomBase): class RoomTopicTestCase(RoomBase):
""" Tests /rooms/$room_id/topic REST events. """ """ Tests /rooms/$room_id/topic REST events. """
user_id = b"@sid1:red" user_id = "@sid1:red"
def setUp(self):
super(RoomTopicTestCase, self).setUp()
def prepare(self, reactor, clock, hs):
# create the room # create the room
self.room_id = self.helper.create_room_as(self.user_id) self.room_id = self.helper.create_room_as(self.user_id)
self.path = b"/rooms/%s/state/m.room.topic" % (self.room_id,) self.path = "/rooms/%s/state/m.room.topic" % (self.room_id,)
def test_invalid_puts(self): def test_invalid_puts(self):
# missing keys or invalid json # missing keys or invalid json
request, channel = make_request(b"PUT", self.path, '{}') request, channel = self.make_request("PUT", self.path, '{}')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", self.path, '{"_name":"bob"}') request, channel = self.make_request("PUT", self.path, '{"_name":"bo"}')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", self.path, '{"nao') request, channel = self.make_request("PUT", self.path, '{"nao')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request( request, channel = self.make_request(
b"PUT", self.path, '[{"_name":"bob"},{"_name":"jill"}]' "PUT", self.path, '[{"_name":"bo"},{"_name":"jill"}]'
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", self.path, 'text only') request, channel = self.make_request("PUT", self.path, 'text only')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", self.path, '') request, channel = self.make_request("PUT", self.path, '')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
# valid key, wrong type # valid key, wrong type
content = '{"topic":["Topic name"]}' content = '{"topic":["Topic name"]}'
request, channel = make_request(b"PUT", self.path, content) request, channel = self.make_request("PUT", self.path, content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
def test_rooms_topic(self): def test_rooms_topic(self):
# nothing should be there # nothing should be there
request, channel = make_request(b"GET", self.path) request, channel = self.make_request("GET", self.path)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(404, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(404, channel.code, msg=channel.result["body"])
# valid put # valid put
content = '{"topic":"Topic name"}' content = '{"topic":"Topic name"}'
request, channel = make_request(b"PUT", self.path, content) request, channel = self.make_request("PUT", self.path, content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
# valid get # valid get
request, channel = make_request(b"GET", self.path) request, channel = self.make_request("GET", self.path)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
self.assert_dict(json.loads(content), channel.json_body) self.assert_dict(json.loads(content), channel.json_body)
def test_rooms_topic_with_extra_keys(self): def test_rooms_topic_with_extra_keys(self):
# valid put with extra keys # valid put with extra keys
content = '{"topic":"Seasons","subtopic":"Summer"}' content = '{"topic":"Seasons","subtopic":"Summer"}'
request, channel = make_request(b"PUT", self.path, content) request, channel = self.make_request("PUT", self.path, content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
# valid get # valid get
request, channel = make_request(b"GET", self.path) request, channel = self.make_request("GET", self.path)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
self.assert_dict(json.loads(content), channel.json_body) self.assert_dict(json.loads(content), channel.json_body)
class RoomMemberStateTestCase(RoomBase): class RoomMemberStateTestCase(RoomBase):
""" Tests /rooms/$room_id/members/$user_id/state REST events. """ """ Tests /rooms/$room_id/members/$user_id/state REST events. """
user_id = b"@sid1:red" user_id = "@sid1:red"
def setUp(self): def prepare(self, reactor, clock, hs):
super(RoomMemberStateTestCase, self).setUp()
self.room_id = self.helper.create_room_as(self.user_id) self.room_id = self.helper.create_room_as(self.user_id)
def tearDown(self):
pass
def test_invalid_puts(self): def test_invalid_puts(self):
path = "/rooms/%s/state/m.room.member/%s" % (self.room_id, self.user_id) path = "/rooms/%s/state/m.room.member/%s" % (self.room_id, self.user_id)
# missing keys or invalid json # missing keys or invalid json
request, channel = make_request(b"PUT", path, '{}') request, channel = self.make_request("PUT", path, '{}')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", path, '{"_name":"bob"}') request, channel = self.make_request("PUT", path, '{"_name":"bo"}')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", path, '{"nao') request, channel = self.make_request("PUT", path, '{"nao')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request( request, channel = self.make_request(
b"PUT", path, b'[{"_name":"bob"},{"_name":"jill"}]' "PUT", path, b'[{"_name":"bo"},{"_name":"jill"}]'
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", path, 'text only') request, channel = self.make_request("PUT", path, 'text only')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", path, '') request, channel = self.make_request("PUT", path, '')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
# valid keys, wrong types # valid keys, wrong types
content = '{"membership":["%s","%s","%s"]}' % ( content = '{"membership":["%s","%s","%s"]}' % (
@ -646,9 +602,9 @@ class RoomMemberStateTestCase(RoomBase):
Membership.JOIN, Membership.JOIN,
Membership.LEAVE, Membership.LEAVE,
) )
request, channel = make_request(b"PUT", path, content.encode('ascii')) request, channel = self.make_request("PUT", path, content.encode('ascii'))
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
def test_rooms_members_self(self): def test_rooms_members_self(self):
path = "/rooms/%s/state/m.room.member/%s" % ( path = "/rooms/%s/state/m.room.member/%s" % (
@ -658,13 +614,13 @@ class RoomMemberStateTestCase(RoomBase):
# valid join message (NOOP since we made the room) # valid join message (NOOP since we made the room)
content = '{"membership":"%s"}' % Membership.JOIN content = '{"membership":"%s"}' % Membership.JOIN
request, channel = make_request(b"PUT", path, content.encode('ascii')) request, channel = self.make_request("PUT", path, content.encode('ascii'))
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
request, channel = make_request(b"GET", path, None) request, channel = self.make_request("GET", path, None)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
expected_response = {"membership": Membership.JOIN} expected_response = {"membership": Membership.JOIN}
self.assertEquals(expected_response, channel.json_body) self.assertEquals(expected_response, channel.json_body)
@ -678,13 +634,13 @@ class RoomMemberStateTestCase(RoomBase):
# valid invite message # valid invite message
content = '{"membership":"%s"}' % Membership.INVITE content = '{"membership":"%s"}' % Membership.INVITE
request, channel = make_request(b"PUT", path, content) request, channel = self.make_request("PUT", path, content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
request, channel = make_request(b"GET", path, None) request, channel = self.make_request("GET", path, None)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
self.assertEquals(json.loads(content), channel.json_body) self.assertEquals(json.loads(content), channel.json_body)
def test_rooms_members_other_custom_keys(self): def test_rooms_members_other_custom_keys(self):
@ -699,13 +655,13 @@ class RoomMemberStateTestCase(RoomBase):
Membership.INVITE, Membership.INVITE,
"Join us!", "Join us!",
) )
request, channel = make_request(b"PUT", path, content) request, channel = self.make_request("PUT", path, content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
request, channel = make_request(b"GET", path, None) request, channel = self.make_request("GET", path, None)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
self.assertEquals(json.loads(content), channel.json_body) self.assertEquals(json.loads(content), channel.json_body)
@ -714,60 +670,58 @@ class RoomMessagesTestCase(RoomBase):
user_id = "@sid1:red" user_id = "@sid1:red"
def setUp(self): def prepare(self, reactor, clock, hs):
super(RoomMessagesTestCase, self).setUp()
self.room_id = self.helper.create_room_as(self.user_id) self.room_id = self.helper.create_room_as(self.user_id)
def test_invalid_puts(self): def test_invalid_puts(self):
path = "/rooms/%s/send/m.room.message/mid1" % (urlparse.quote(self.room_id)) path = "/rooms/%s/send/m.room.message/mid1" % (urlparse.quote(self.room_id))
# missing keys or invalid json # missing keys or invalid json
request, channel = make_request(b"PUT", path, '{}') request, channel = self.make_request("PUT", path, b'{}')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", path, '{"_name":"bob"}') request, channel = self.make_request("PUT", path, b'{"_name":"bo"}')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", path, '{"nao') request, channel = self.make_request("PUT", path, b'{"nao')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request( request, channel = self.make_request(
b"PUT", path, '[{"_name":"bob"},{"_name":"jill"}]' "PUT", path, b'[{"_name":"bo"},{"_name":"jill"}]'
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", path, 'text only') request, channel = self.make_request("PUT", path, b'text only')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
request, channel = make_request(b"PUT", path, '') request, channel = self.make_request("PUT", path, b'')
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
def test_rooms_messages_sent(self): def test_rooms_messages_sent(self):
path = "/rooms/%s/send/m.room.message/mid1" % (urlparse.quote(self.room_id)) path = "/rooms/%s/send/m.room.message/mid1" % (urlparse.quote(self.room_id))
content = '{"body":"test","msgtype":{"type":"a"}}' content = b'{"body":"test","msgtype":{"type":"a"}}'
request, channel = make_request(b"PUT", path, content) request, channel = self.make_request("PUT", path, content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(400, channel.code, msg=channel.result["body"])
# custom message types # custom message types
content = '{"body":"test","msgtype":"test.custom.text"}' content = b'{"body":"test","msgtype":"test.custom.text"}'
request, channel = make_request(b"PUT", path, content) request, channel = self.make_request("PUT", path, content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
# m.text message type # m.text message type
path = "/rooms/%s/send/m.room.message/mid2" % (urlparse.quote(self.room_id)) path = "/rooms/%s/send/m.room.message/mid2" % (urlparse.quote(self.room_id))
content = '{"body":"test2","msgtype":"m.text"}' content = b'{"body":"test2","msgtype":"m.text"}'
request, channel = make_request(b"PUT", path, content) request, channel = self.make_request("PUT", path, content)
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEquals(200, channel.code, msg=channel.result["body"])
class RoomInitialSyncTestCase(RoomBase): class RoomInitialSyncTestCase(RoomBase):
@ -775,16 +729,16 @@ class RoomInitialSyncTestCase(RoomBase):
user_id = "@sid1:red" user_id = "@sid1:red"
def setUp(self): def prepare(self, reactor, clock, hs):
super(RoomInitialSyncTestCase, self).setUp()
# create the room # create the room
self.room_id = self.helper.create_room_as(self.user_id) self.room_id = self.helper.create_room_as(self.user_id)
def test_initial_sync(self): def test_initial_sync(self):
request, channel = make_request(b"GET", "/rooms/%s/initialSync" % self.room_id) request, channel = self.make_request(
render(request, self.resource, self.clock) "GET", "/rooms/%s/initialSync" % self.room_id
self.assertEquals(200, int(channel.result["code"])) )
self.render(request)
self.assertEquals(200, channel.code)
self.assertEquals(self.room_id, channel.json_body["room_id"]) self.assertEquals(self.room_id, channel.json_body["room_id"])
self.assertEquals("join", channel.json_body["membership"]) self.assertEquals("join", channel.json_body["membership"])
@ -819,17 +773,16 @@ class RoomMessageListTestCase(RoomBase):
user_id = "@sid1:red" user_id = "@sid1:red"
def setUp(self): def prepare(self, reactor, clock, hs):
super(RoomMessageListTestCase, self).setUp()
self.room_id = self.helper.create_room_as(self.user_id) self.room_id = self.helper.create_room_as(self.user_id)
def test_topo_token_is_accepted(self): def test_topo_token_is_accepted(self):
token = "t1-0_0_0_0_0_0_0_0_0" token = "t1-0_0_0_0_0_0_0_0_0"
request, channel = make_request( request, channel = self.make_request(
b"GET", "/rooms/%s/messages?access_token=x&from=%s" % (self.room_id, token) "GET", "/rooms/%s/messages?access_token=x&from=%s" % (self.room_id, token)
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"])) self.assertEquals(200, channel.code)
self.assertTrue("start" in channel.json_body) self.assertTrue("start" in channel.json_body)
self.assertEquals(token, channel.json_body['start']) self.assertEquals(token, channel.json_body['start'])
self.assertTrue("chunk" in channel.json_body) self.assertTrue("chunk" in channel.json_body)
@ -837,11 +790,11 @@ class RoomMessageListTestCase(RoomBase):
def test_stream_token_is_accepted_for_fwd_pagianation(self): def test_stream_token_is_accepted_for_fwd_pagianation(self):
token = "s0_0_0_0_0_0_0_0_0" token = "s0_0_0_0_0_0_0_0_0"
request, channel = make_request( request, channel = self.make_request(
b"GET", "/rooms/%s/messages?access_token=x&from=%s" % (self.room_id, token) "GET", "/rooms/%s/messages?access_token=x&from=%s" % (self.room_id, token)
) )
render(request, self.resource, self.clock) self.render(request)
self.assertEquals(200, int(channel.result["code"])) self.assertEquals(200, channel.code)
self.assertTrue("start" in channel.json_body) self.assertTrue("start" in channel.json_body)
self.assertEquals(token, channel.json_body['start']) self.assertEquals(token, channel.json_body['start'])
self.assertTrue("chunk" in channel.json_body) self.assertTrue("chunk" in channel.json_body)

View file

@ -62,12 +62,6 @@ class FilterTestCase(unittest.HomeserverTestCase):
self.assertEqual(channel.code, 200) self.assertEqual(channel.code, 200)
self.assertTrue( self.assertTrue(
set( set(
[ ["next_batch", "rooms", "account_data", "to_device", "device_lists"]
"next_batch",
"rooms",
"account_data",
"to_device",
"device_lists",
]
).issubset(set(channel.json_body.keys())) ).issubset(set(channel.json_body.keys()))
) )

View file

@ -65,7 +65,7 @@ class FakeChannel(object):
def getPeer(self): def getPeer(self):
# We give an address so that getClientIP returns a non null entry, # We give an address so that getClientIP returns a non null entry,
# causing us to record the MAU # causing us to record the MAU
return address.IPv4Address(b"TCP", "127.0.0.1", 3423) return address.IPv4Address("TCP", "127.0.0.1", 3423)
def getHost(self): def getHost(self):
return None return None

View file

@ -80,12 +80,11 @@ class TestResourceLimitsServerNotices(unittest.TestCase):
self._rlsn._auth.check_auth_blocking = Mock() self._rlsn._auth.check_auth_blocking = Mock()
mock_event = Mock( mock_event = Mock(
type=EventTypes.Message, type=EventTypes.Message, content={"msgtype": ServerNoticeMsgType}
content={"msgtype": ServerNoticeMsgType}, )
self._rlsn._store.get_events = Mock(
return_value=defer.succeed({"123": mock_event})
) )
self._rlsn._store.get_events = Mock(return_value=defer.succeed(
{"123": mock_event}
))
yield self._rlsn.maybe_send_server_notice_to_user(self.user_id) yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
# Would be better to check the content, but once == remove blocking event # Would be better to check the content, but once == remove blocking event
@ -99,12 +98,11 @@ class TestResourceLimitsServerNotices(unittest.TestCase):
) )
mock_event = Mock( mock_event = Mock(
type=EventTypes.Message, type=EventTypes.Message, content={"msgtype": ServerNoticeMsgType}
content={"msgtype": ServerNoticeMsgType}, )
self._rlsn._store.get_events = Mock(
return_value=defer.succeed({"123": mock_event})
) )
self._rlsn._store.get_events = Mock(return_value=defer.succeed(
{"123": mock_event}
))
yield self._rlsn.maybe_send_server_notice_to_user(self.user_id) yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
self._send_notice.assert_not_called() self._send_notice.assert_not_called()
@ -177,13 +175,9 @@ class TestResourceLimitsServerNoticesWithRealRooms(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def test_server_notice_only_sent_once(self): def test_server_notice_only_sent_once(self):
self.store.get_monthly_active_count = Mock( self.store.get_monthly_active_count = Mock(return_value=1000)
return_value=1000,
)
self.store.user_last_seen_monthly_active = Mock( self.store.user_last_seen_monthly_active = Mock(return_value=1000)
return_value=1000,
)
# Call the function multiple times to ensure we only send the notice once # Call the function multiple times to ensure we only send the notice once
yield self._rlsn.maybe_send_server_notice_to_user(self.user_id) yield self._rlsn.maybe_send_server_notice_to_user(self.user_id)
@ -193,12 +187,12 @@ class TestResourceLimitsServerNoticesWithRealRooms(unittest.TestCase):
# Now lets get the last load of messages in the service notice room and # Now lets get the last load of messages in the service notice room and
# check that there is only one server notice # check that there is only one server notice
room_id = yield self.server_notices_manager.get_notice_room_for_user( room_id = yield self.server_notices_manager.get_notice_room_for_user(
self.user_id, self.user_id
) )
token = yield self.event_source.get_current_token() token = yield self.event_source.get_current_token()
events, _ = yield self.store.get_recent_events_for_room( events, _ = yield self.store.get_recent_events_for_room(
room_id, limit=100, end_token=token.room_key, room_id, limit=100, end_token=token.room_key
) )
count = 0 count = 0

View file

@ -185,8 +185,7 @@ class StateStoreTestCase(tests.unittest.TestCase):
# test _get_some_state_from_cache correctly filters out members with types=[] # test _get_some_state_from_cache correctly filters out members with types=[]
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_cache, self.store._state_group_cache, group, [], filtered_types=[EventTypes.Member]
group, [], filtered_types=[EventTypes.Member]
) )
self.assertEqual(is_all, True) self.assertEqual(is_all, True)
@ -200,19 +199,20 @@ class StateStoreTestCase(tests.unittest.TestCase):
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache, self.store._state_group_members_cache,
group, [], filtered_types=[EventTypes.Member] group,
[],
filtered_types=[EventTypes.Member],
) )
self.assertEqual(is_all, True) self.assertEqual(is_all, True)
self.assertDictEqual( self.assertDictEqual({}, state_dict)
{},
state_dict,
)
# test _get_some_state_from_cache correctly filters in members with wildcard types # test _get_some_state_from_cache correctly filters in members with wildcard types
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_cache, self.store._state_group_cache,
group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member] group,
[(EventTypes.Member, None)],
filtered_types=[EventTypes.Member],
) )
self.assertEqual(is_all, True) self.assertEqual(is_all, True)
@ -226,7 +226,9 @@ class StateStoreTestCase(tests.unittest.TestCase):
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache, self.store._state_group_members_cache,
group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member] group,
[(EventTypes.Member, None)],
filtered_types=[EventTypes.Member],
) )
self.assertEqual(is_all, True) self.assertEqual(is_all, True)
@ -264,18 +266,15 @@ class StateStoreTestCase(tests.unittest.TestCase):
) )
self.assertEqual(is_all, True) self.assertEqual(is_all, True)
self.assertDictEqual( self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)
{
(e5.type, e5.state_key): e5.event_id,
},
state_dict,
)
# test _get_some_state_from_cache correctly filters in members with specific types # test _get_some_state_from_cache correctly filters in members with specific types
# and no filtered_types # and no filtered_types
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache, self.store._state_group_members_cache,
group, [(EventTypes.Member, e5.state_key)], filtered_types=None group,
[(EventTypes.Member, e5.state_key)],
filtered_types=None,
) )
self.assertEqual(is_all, True) self.assertEqual(is_all, True)
@ -305,9 +304,7 @@ class StateStoreTestCase(tests.unittest.TestCase):
key=group, key=group,
value=state_dict_ids, value=state_dict_ids,
# list fetched keys so it knows it's partial # list fetched keys so it knows it's partial
fetched_keys=( fetched_keys=((e1.type, e1.state_key),),
(e1.type, e1.state_key),
),
) )
(is_all, known_absent, state_dict_ids) = self.store._state_group_cache.get( (is_all, known_absent, state_dict_ids) = self.store._state_group_cache.get(
@ -315,20 +312,8 @@ class StateStoreTestCase(tests.unittest.TestCase):
) )
self.assertEqual(is_all, False) self.assertEqual(is_all, False)
self.assertEqual( self.assertEqual(known_absent, set([(e1.type, e1.state_key)]))
known_absent, self.assertDictEqual(state_dict_ids, {(e1.type, e1.state_key): e1.event_id})
set(
[
(e1.type, e1.state_key),
]
),
)
self.assertDictEqual(
state_dict_ids,
{
(e1.type, e1.state_key): e1.event_id,
},
)
############################################ ############################################
# test that things work with a partial cache # test that things work with a partial cache
@ -336,8 +321,7 @@ class StateStoreTestCase(tests.unittest.TestCase):
# test _get_some_state_from_cache correctly filters out members with types=[] # test _get_some_state_from_cache correctly filters out members with types=[]
room_id = self.room.to_string() room_id = self.room.to_string()
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_cache, self.store._state_group_cache, group, [], filtered_types=[EventTypes.Member]
group, [], filtered_types=[EventTypes.Member]
) )
self.assertEqual(is_all, False) self.assertEqual(is_all, False)
@ -346,7 +330,9 @@ class StateStoreTestCase(tests.unittest.TestCase):
room_id = self.room.to_string() room_id = self.room.to_string()
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache, self.store._state_group_members_cache,
group, [], filtered_types=[EventTypes.Member] group,
[],
filtered_types=[EventTypes.Member],
) )
self.assertEqual(is_all, True) self.assertEqual(is_all, True)
@ -355,20 +341,19 @@ class StateStoreTestCase(tests.unittest.TestCase):
# test _get_some_state_from_cache correctly filters in members wildcard types # test _get_some_state_from_cache correctly filters in members wildcard types
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_cache, self.store._state_group_cache,
group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member] group,
[(EventTypes.Member, None)],
filtered_types=[EventTypes.Member],
) )
self.assertEqual(is_all, False) self.assertEqual(is_all, False)
self.assertDictEqual( self.assertDictEqual({(e1.type, e1.state_key): e1.event_id}, state_dict)
{
(e1.type, e1.state_key): e1.event_id,
},
state_dict,
)
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache, self.store._state_group_members_cache,
group, [(EventTypes.Member, None)], filtered_types=[EventTypes.Member] group,
[(EventTypes.Member, None)],
filtered_types=[EventTypes.Member],
) )
self.assertEqual(is_all, True) self.assertEqual(is_all, True)
@ -389,12 +374,7 @@ class StateStoreTestCase(tests.unittest.TestCase):
) )
self.assertEqual(is_all, False) self.assertEqual(is_all, False)
self.assertDictEqual( self.assertDictEqual({(e1.type, e1.state_key): e1.event_id}, state_dict)
{
(e1.type, e1.state_key): e1.event_id,
},
state_dict,
)
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache, self.store._state_group_members_cache,
@ -404,18 +384,15 @@ class StateStoreTestCase(tests.unittest.TestCase):
) )
self.assertEqual(is_all, True) self.assertEqual(is_all, True)
self.assertDictEqual( self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)
{
(e5.type, e5.state_key): e5.event_id,
},
state_dict,
)
# test _get_some_state_from_cache correctly filters in members with specific types # test _get_some_state_from_cache correctly filters in members with specific types
# and no filtered_types # and no filtered_types
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_cache, self.store._state_group_cache,
group, [(EventTypes.Member, e5.state_key)], filtered_types=None group,
[(EventTypes.Member, e5.state_key)],
filtered_types=None,
) )
self.assertEqual(is_all, False) self.assertEqual(is_all, False)
@ -423,13 +400,10 @@ class StateStoreTestCase(tests.unittest.TestCase):
(state_dict, is_all) = yield self.store._get_some_state_from_cache( (state_dict, is_all) = yield self.store._get_some_state_from_cache(
self.store._state_group_members_cache, self.store._state_group_members_cache,
group, [(EventTypes.Member, e5.state_key)], filtered_types=None group,
[(EventTypes.Member, e5.state_key)],
filtered_types=None,
) )
self.assertEqual(is_all, True) self.assertEqual(is_all, True)
self.assertDictEqual( self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)
{
(e5.type, e5.state_key): e5.event_id,
},
state_dict,
)

View file

@ -185,20 +185,20 @@ class TestMauLimit(unittest.TestCase):
self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED) self.assertEqual(e.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
def create_user(self, localpart): def create_user(self, localpart):
request_data = json.dumps({ request_data = json.dumps(
{
"username": localpart, "username": localpart,
"password": "monkey", "password": "monkey",
"auth": {"type": LoginType.DUMMY}, "auth": {"type": LoginType.DUMMY},
}) }
)
request, channel = make_request(b"POST", b"/register", request_data) request, channel = make_request("POST", "/register", request_data)
render(request, self.resource, self.reactor) render(request, self.resource, self.reactor)
if channel.result["code"] != b"200": if channel.code != 200:
raise HttpResponseException( raise HttpResponseException(
int(channel.result["code"]), channel.code, channel.result["reason"], channel.result["body"]
channel.result["reason"],
channel.result["body"],
).to_synapse_error() ).to_synapse_error()
access_token = channel.json_body["access_token"] access_token = channel.json_body["access_token"]
@ -206,12 +206,12 @@ class TestMauLimit(unittest.TestCase):
return access_token return access_token
def do_sync_for_user(self, token): def do_sync_for_user(self, token):
request, channel = make_request(b"GET", b"/sync", access_token=token) request, channel = make_request(
"GET", "/sync", access_token=token.encode('ascii')
)
render(request, self.resource, self.reactor) render(request, self.resource, self.reactor)
if channel.result["code"] != b"200": if channel.code != 200:
raise HttpResponseException( raise HttpResponseException(
int(channel.result["code"]), channel.code, channel.result["reason"], channel.result["body"]
channel.result["reason"],
channel.result["body"],
).to_synapse_error() ).to_synapse_error()

View file

@ -180,7 +180,7 @@ class StateTestCase(unittest.TestCase):
graph = Graph( graph = Graph(
nodes={ nodes={
"START": DictObj( "START": DictObj(
type=EventTypes.Create, state_key="", content={}, depth=1, type=EventTypes.Create, state_key="", content={}, depth=1
), ),
"A": DictObj(type=EventTypes.Message, depth=2), "A": DictObj(type=EventTypes.Message, depth=2),
"B": DictObj(type=EventTypes.Message, depth=3), "B": DictObj(type=EventTypes.Message, depth=3),

View file

@ -100,8 +100,13 @@ class TestHomeServer(HomeServer):
@defer.inlineCallbacks @defer.inlineCallbacks
def setup_test_homeserver( def setup_test_homeserver(
cleanup_func, name="test", datastore=None, config=None, reactor=None, cleanup_func,
homeserverToUse=TestHomeServer, **kargs name="test",
datastore=None,
config=None,
reactor=None,
homeserverToUse=TestHomeServer,
**kargs
): ):
""" """
Setup a homeserver suitable for running tests against. Keyword arguments Setup a homeserver suitable for running tests against. Keyword arguments
@ -323,8 +328,7 @@ class MockHttpResource(HttpServer):
@patch('twisted.web.http.Request') @patch('twisted.web.http.Request')
@defer.inlineCallbacks @defer.inlineCallbacks
def trigger( def trigger(
self, http_method, path, content, mock_request, self, http_method, path, content, mock_request, federation_auth_origin=None
federation_auth_origin=None,
): ):
""" Fire an HTTP event. """ Fire an HTTP event.
@ -577,16 +581,16 @@ def create_room(hs, room_id, creator_id):
event_builder_factory = hs.get_event_builder_factory() event_builder_factory = hs.get_event_builder_factory()
event_creation_handler = hs.get_event_creation_handler() event_creation_handler = hs.get_event_creation_handler()
builder = event_builder_factory.new({ builder = event_builder_factory.new(
{
"type": EventTypes.Create, "type": EventTypes.Create,
"state_key": "", "state_key": "",
"sender": creator_id, "sender": creator_id,
"room_id": room_id, "room_id": room_id,
"content": {}, "content": {},
}) }
event, context = yield event_creation_handler.create_new_client_event(
builder
) )
event, context = yield event_creation_handler.create_new_client_event(builder)
yield store.persist_event(event, context) yield store.persist_event(event, context)