Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Test fixes for Python 3 #3647

Merged
merged 5 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3647.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tests now correctly execute on Python 3.
4 changes: 3 additions & 1 deletion tests/handlers/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def _expect_edu(destination, edu_type, content, origin="test"):


def _make_edu_json(origin, edu_type, content):
return json.dumps(_expect_edu("test", edu_type, content, origin=origin))
return json.dumps(
_expect_edu("test", edu_type, content, origin=origin)
).encode('utf8')


class TypingNotificationsTestCase(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/rest/client/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def cb():
try:
yield self.cache.fetch_or_execute(self.mock_key, cb)
except Exception as e:
self.assertEqual(e.message, "boo")
self.assertEqual(e.args[0], "boo")
self.assertIs(LoggingContext.current_context(), test_context)

res = yield self.cache.fetch_or_execute(self.mock_key, cb)
Expand All @@ -111,7 +111,7 @@ def cb():
try:
yield self.cache.fetch_or_execute(self.mock_key, cb)
except Exception as e:
self.assertEqual(e.message, "boo")
self.assertEqual(e.args[0], "boo")
self.assertIs(LoggingContext.current_context(), test_context)

res = yield self.cache.fetch_or_execute(self.mock_key, cb)
Expand Down
10 changes: 5 additions & 5 deletions tests/rest/client/v1/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_register_incorrect_nonce(self):
"admin": True,
"mac": want_mac,
}
).encode('utf8')
)
request, channel = make_request("POST", self.url, body.encode('utf8'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

someone really wanted that sweet sweet utf-8 encoding

render(request, self.resource, self.clock)

Expand Down Expand Up @@ -168,7 +168,7 @@ def test_register_correct_nonce(self):
"admin": True,
"mac": want_mac,
}
).encode('utf8')
)
request, channel = make_request("POST", self.url, body.encode('utf8'))
render(request, self.resource, self.clock)

Expand All @@ -195,7 +195,7 @@ def test_nonce_reuse(self):
"admin": True,
"mac": want_mac,
}
).encode('utf8')
)
request, channel = make_request("POST", self.url, body.encode('utf8'))
render(request, self.resource, self.clock)

Expand Down Expand Up @@ -253,7 +253,7 @@ def nonce():
self.assertEqual('Invalid username', channel.json_body["error"])

# Must not have null bytes
body = json.dumps({"nonce": nonce(), "username": b"abcd\x00"})
body = json.dumps({"nonce": nonce(), "username": u"abcd\u0000"})
request, channel = make_request("POST", self.url, body.encode('utf8'))
render(request, self.resource, self.clock)

Expand Down Expand Up @@ -289,7 +289,7 @@ def nonce():
self.assertEqual('Invalid password', channel.json_body["error"])

# Must not have null bytes
body = json.dumps({"nonce": nonce(), "username": "a", "password": b"abcd\x00"})
body = json.dumps({"nonce": nonce(), "username": "a", "password": u"abcd\u0000"})
request, channel = make_request("POST", self.url, body.encode('utf8'))
render(request, self.resource, self.clock)

Expand Down
8 changes: 4 additions & 4 deletions tests/rest/client/v1/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_set_my_name(self):
(code, response) = yield self.mock_resource.trigger(
"PUT",
"/profile/%s/displayname" % (myid),
'{"displayname": "Frank Jr."}'
b'{"displayname": "Frank Jr."}'
)

self.assertEquals(200, code)
Expand All @@ -95,7 +95,7 @@ def test_set_my_name_noauth(self):

(code, response) = yield self.mock_resource.trigger(
"PUT", "/profile/%s/displayname" % ("@4567:test"),
'{"displayname": "Frank Jr."}'
b'{"displayname": "Frank Jr."}'
)

self.assertTrue(
Expand All @@ -122,7 +122,7 @@ def test_set_other_name(self):

(code, response) = yield self.mock_resource.trigger(
"PUT", "/profile/%s/displayname" % ("@opaque:elsewhere"),
'{"displayname":"bob"}'
b'{"displayname":"bob"}'
)

self.assertTrue(
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_set_my_avatar(self):
(code, response) = yield self.mock_resource.trigger(
"PUT",
"/profile/%s/avatar_url" % (myid),
'{"avatar_url": "http://my.server/pic.gif"}'
b'{"avatar_url": "http://my.server/pic.gif"}'
)

self.assertEquals(200, code)
Expand Down
10 changes: 5 additions & 5 deletions tests/rest/client/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def register(self, user_id):
"password": "test",
"type": "m.login.password"
}))
self.assertEquals(200, code)
self.assertEquals(200, code, msg=response)
defer.returnValue(response)

@defer.inlineCallbacks
Expand Down Expand Up @@ -149,14 +149,14 @@ class RestHelper(object):
def create_room_as(self, room_creator, is_public=True, tok=None):
temp_id = self.auth_user_id
self.auth_user_id = room_creator
path = b"/_matrix/client/r0/createRoom"
path = "/_matrix/client/r0/createRoom"
content = {}
if not is_public:
content["visibility"] = "private"
if tok:
path = path + b"?access_token=%s" % tok.encode('ascii')
path = path + "?access_token=%s" % tok

request, channel = make_request(b"POST", path, json.dumps(content).encode('utf8'))
request, channel = make_request("POST", path, json.dumps(content).encode('utf8'))
request.render(self.resource)
wait_until_result(self.hs.get_reactor(), channel)

Expand Down Expand Up @@ -205,7 +205,7 @@ def change_membership(self, room, src, targ, membership, tok=None, expect_code=2
data = {"membership": membership}

request, channel = make_request(
b"PUT", path.encode('ascii'), json.dumps(data).encode('utf8')
"PUT", path, json.dumps(data).encode('utf8')
)

request.render(self.resource)
Expand Down
22 changes: 11 additions & 11 deletions tests/rest/client/v2_alpha/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

class FilterTestCase(unittest.TestCase):

USER_ID = b"@apple:test"
USER_ID = "@apple:test"
EXAMPLE_FILTER = {"room": {"timeline": {"types": ["m.room.message"]}}}
EXAMPLE_FILTER_JSON = b'{"room": {"timeline": {"types": ["m.room.message"]}}}'
TO_REGISTER = [filter]
Expand Down Expand Up @@ -72,8 +72,8 @@ def get_user_by_req(request, allow_guest=False, rights="access"):

def test_add_filter(self):
request, channel = make_request(
b"POST",
b"/_matrix/client/r0/user/%s/filter" % (self.USER_ID),
"POST",
"/_matrix/client/r0/user/%s/filter" % (self.USER_ID),
self.EXAMPLE_FILTER_JSON,
)
request.render(self.resource)
Expand All @@ -87,8 +87,8 @@ def test_add_filter(self):

def test_add_filter_for_other_user(self):
request, channel = make_request(
b"POST",
b"/_matrix/client/r0/user/%s/filter" % (b"@watermelon:test"),
"POST",
"/_matrix/client/r0/user/%s/filter" % ("@watermelon:test"),
self.EXAMPLE_FILTER_JSON,
)
request.render(self.resource)
Expand All @@ -101,8 +101,8 @@ def test_add_filter_non_local_user(self):
_is_mine = self.hs.is_mine
self.hs.is_mine = lambda target_user: False
request, channel = make_request(
b"POST",
b"/_matrix/client/r0/user/%s/filter" % (self.USER_ID),
"POST",
"/_matrix/client/r0/user/%s/filter" % (self.USER_ID),
self.EXAMPLE_FILTER_JSON,
)
request.render(self.resource)
Expand All @@ -119,7 +119,7 @@ def test_get_filter(self):
self.clock.advance(1)
filter_id = filter_id.result
request, channel = make_request(
b"GET", b"/_matrix/client/r0/user/%s/filter/%s" % (self.USER_ID, filter_id)
"GET", "/_matrix/client/r0/user/%s/filter/%s" % (self.USER_ID, filter_id)
)
request.render(self.resource)
wait_until_result(self.clock, channel)
Expand All @@ -129,7 +129,7 @@ def test_get_filter(self):

def test_get_filter_non_existant(self):
request, channel = make_request(
b"GET", "/_matrix/client/r0/user/%s/filter/12382148321" % (self.USER_ID)
"GET", "/_matrix/client/r0/user/%s/filter/12382148321" % (self.USER_ID)
)
request.render(self.resource)
wait_until_result(self.clock, channel)
Expand All @@ -141,7 +141,7 @@ def test_get_filter_non_existant(self):
# in errors.py
def test_get_filter_invalid_id(self):
request, channel = make_request(
b"GET", "/_matrix/client/r0/user/%s/filter/foobar" % (self.USER_ID)
"GET", "/_matrix/client/r0/user/%s/filter/foobar" % (self.USER_ID)
)
request.render(self.resource)
wait_until_result(self.clock, channel)
Expand All @@ -151,7 +151,7 @@ def test_get_filter_invalid_id(self):
# No ID also returns an invalid_id error
def test_get_filter_no_id(self):
request, channel = make_request(
b"GET", "/_matrix/client/r0/user/%s/filter/" % (self.USER_ID)
"GET", "/_matrix/client/r0/user/%s/filter/" % (self.USER_ID)
)
request.render(self.resource)
wait_until_result(self.clock, channel)
Expand Down
14 changes: 7 additions & 7 deletions tests/rest/client/v2_alpha/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_POST_appservice_registration_valid(self):
"access_token": token,
"home_server": self.hs.hostname,
}
self.assertDictContainsSubset(det_data, json.loads(channel.result["body"]))
self.assertDictContainsSubset(det_data, channel.json_body)

def test_POST_appservice_registration_invalid(self):
self.appservice = None # no application service exists
Expand All @@ -102,7 +102,7 @@ def test_POST_bad_password(self):

self.assertEquals(channel.result["code"], b"400", channel.result)
self.assertEquals(
json.loads(channel.result["body"])["error"], "Invalid password"
channel.json_body["error"], "Invalid password"
)

def test_POST_bad_username(self):
Expand All @@ -113,7 +113,7 @@ def test_POST_bad_username(self):

self.assertEquals(channel.result["code"], b"400", channel.result)
self.assertEquals(
json.loads(channel.result["body"])["error"], "Invalid username"
channel.json_body["error"], "Invalid username"
)

def test_POST_user_valid(self):
Expand All @@ -140,7 +140,7 @@ def test_POST_user_valid(self):
"device_id": device_id,
}
self.assertEquals(channel.result["code"], b"200", channel.result)
self.assertDictContainsSubset(det_data, json.loads(channel.result["body"]))
self.assertDictContainsSubset(det_data, channel.json_body)
self.auth_handler.get_login_tuple_for_user_id(
user_id, device_id=device_id, initial_device_display_name=None
)
Expand All @@ -158,7 +158,7 @@ def test_POST_disabled_registration(self):

self.assertEquals(channel.result["code"], b"403", channel.result)
self.assertEquals(
json.loads(channel.result["body"])["error"],
channel.json_body["error"],
"Registration has been disabled",
)

Expand All @@ -178,7 +178,7 @@ def test_POST_guest_registration(self):
"device_id": "guest_device",
}
self.assertEquals(channel.result["code"], b"200", channel.result)
self.assertDictContainsSubset(det_data, json.loads(channel.result["body"]))
self.assertDictContainsSubset(det_data, channel.json_body)

def test_POST_disabled_guest_registration(self):
self.hs.config.allow_guest_access = False
Expand All @@ -189,5 +189,5 @@ def test_POST_disabled_guest_registration(self):

self.assertEquals(channel.result["code"], b"403", channel.result)
self.assertEquals(
json.loads(channel.result["body"])["error"], "Guest access is disabled"
channel.json_body["error"], "Guest access is disabled"
)
4 changes: 2 additions & 2 deletions tests/rest/client/v2_alpha/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

class FilterTestCase(unittest.TestCase):

USER_ID = b"@apple:test"
USER_ID = "@apple:test"
TO_REGISTER = [sync]

def setUp(self):
Expand Down Expand Up @@ -68,7 +68,7 @@ def get_user_by_req(request, allow_guest=False, rights="access"):
r.register_servlets(self.hs, self.resource)

def test_sync_argless(self):
request, channel = make_request(b"GET", b"/_matrix/client/r0/sync")
request, channel = make_request("GET", "/_matrix/client/r0/sync")
request.render(self.resource)
wait_until_result(self.clock, channel)

Expand Down
22 changes: 20 additions & 2 deletions tests/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from twisted.test.proto_helpers import MemoryReactorClock

from synapse.http.site import SynapseRequest
from synapse.util import Clock

from tests.utils import setup_test_homeserver as _sth

Expand All @@ -28,7 +29,13 @@ class FakeChannel(object):
def json_body(self):
if not self.result:
raise Exception("No result yet.")
return json.loads(self.result["body"])
return json.loads(self.result["body"].decode('utf8'))

@property
def code(self):
if not self.result:
raise Exception("No result yet.")
return int(self.result["code"])

def writeHeaders(self, version, code, reason, headers):
self.result["version"] = version
Expand Down Expand Up @@ -79,11 +86,16 @@ def make_request(method, path, content=b""):
Make a web request using the given method and path, feed it the
content, and return the Request and the Channel underneath.
"""
if not isinstance(method, bytes):
method = method.encode('ascii')

if not isinstance(path, bytes):
path = path.encode('ascii')

# Decorate it to be the full path
if not path.startswith(b"/_matrix"):
path = b"/_matrix/client/r0/" + path
path = path.replace("//", "/")
path = path.replace(b"//", b"/")

if isinstance(content, text_type):
content = content.encode('utf8')
Expand Down Expand Up @@ -191,3 +203,9 @@ def _(res):
clock.threadpool = ThreadPool()
pool.threadpool = ThreadPool()
return d


def get_clock():
clock = ThreadedMemoryReactorClock()
hs_clock = Clock(clock)
return (clock, hs_clock)
2 changes: 1 addition & 1 deletion tests/storage/test_event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def insert_event(txn, i):
'INSERT INTO event_reference_hashes '
'(event_id, algorithm, hash) '
"VALUES (?, 'sha256', ?)"
), (event_id, 'ffff'))
), (event_id, b'ffff'))

for i in range(0, 11):
yield self.store.runInteraction("insert", insert_event, i)
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_get_state_for_event(self):

room_id = self.room.to_string()
group_ids = yield self.store.get_state_groups_ids(room_id, [e5.event_id])
group = group_ids.keys()[0]
group = list(group_ids.keys())[0]

# test _get_some_state_from_cache correctly filters out members with types=[]
(state_dict, is_all) = yield self.store._get_some_state_from_cache(
Expand Down
11 changes: 4 additions & 7 deletions tests/test_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import re

from twisted.internet.defer import Deferred
Expand Down Expand Up @@ -104,9 +103,8 @@ def _callback(request, **kwargs):
request.render(res)

self.assertEqual(channel.result["code"], b'403')
reply_body = json.loads(channel.result["body"])
self.assertEqual(reply_body["error"], "Forbidden!!one!")
self.assertEqual(reply_body["errcode"], "M_FORBIDDEN")
self.assertEqual(channel.json_body["error"], "Forbidden!!one!")
self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")

def test_no_handler(self):
"""
Expand All @@ -126,6 +124,5 @@ def _callback(request, **kwargs):
request.render(res)

self.assertEqual(channel.result["code"], b'400')
reply_body = json.loads(channel.result["body"])
self.assertEqual(reply_body["error"], "Unrecognized request")
self.assertEqual(reply_body["errcode"], "M_UNRECOGNIZED")
self.assertEqual(channel.json_body["error"], "Unrecognized request")
self.assertEqual(channel.json_body["errcode"], "M_UNRECOGNIZED")
Loading