Fix rest.test_rooms

This commit is contained in:
Erik Johnston 2014-11-11 08:09:42 +00:00
parent 0292d991af
commit 339c11dd86

View File

@ -23,11 +23,14 @@ from synapse.api.constants import Membership
from synapse.server import HomeServer from synapse.server import HomeServer
from tests import unittest
# python imports # python imports
import json import json
import urllib import urllib
import types
from ..utils import MockHttpResource, MemoryDataStore, MockKey from ..utils import MockHttpResource, SQLiteMemoryDbPool, MockKey
from .utils import RestTestCase from .utils import RestTestCase
from mock import Mock, NonCallableMock from mock import Mock, NonCallableMock
@ -44,23 +47,17 @@ class RoomPermissionsTestCase(RestTestCase):
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
state_handler = Mock(spec=["handle_new_event"])
state_handler.handle_new_event.return_value = True
persistence_service = Mock(spec=["get_latest_pdus_in_context"])
persistence_service.get_latest_pdus_in_context.return_value = []
self.mock_config = NonCallableMock() self.mock_config = NonCallableMock()
self.mock_config.signing_key = [MockKey()] self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer( hs = HomeServer(
"red", "red",
db_pool=None, db_pool=db_pool,
http_client=None, http_client=None,
datastore=MemoryDataStore(),
replication_layer=Mock(), replication_layer=Mock(),
state_handler=state_handler,
persistence_service=persistence_service,
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
@ -79,6 +76,10 @@ class RoomPermissionsTestCase(RestTestCase):
} }
hs.get_auth().get_user_by_token = _get_user_by_token hs.get_auth().get_user_by_token = _get_user_by_token
def _insert_client_ip(*args, **kwargs):
return defer.succeed(None)
hs.get_datastore().insert_client_ip = _insert_client_ip
self.auth_user_id = self.rmcreator_id self.auth_user_id = self.rmcreator_id
synapse.rest.room.register_servlets(hs, self.mock_resource) synapse.rest.room.register_servlets(hs, self.mock_resource)
@ -150,38 +151,55 @@ class RoomPermissionsTestCase(RestTestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def test_send_message(self): def test_send_message(self):
msg_content = '{"msgtype":"m.text","body":"hello"}' msg_content = '{"msgtype":"m.text","body":"hello"}'
send_msg_path = ("/rooms/%s/send/m.room.message/mid1" % send_msg_path = (
(self.created_rmid)) "/rooms/%s/send/m.room.message/mid1" % (self.created_rmid,)
)
# send message in uncreated room, expect 403 # send message in uncreated room, expect 403
(code, response) = yield self.mock_resource.trigger( (code, response) = yield self.mock_resource.trigger(
"PUT", "PUT",
"/rooms/%s/send/m.room.message/mid2" % "/rooms/%s/send/m.room.message/mid2" % (self.uncreated_rmid,),
(self.uncreated_rmid), msg_content) msg_content
)
self.assertEquals(403, code, msg=str(response)) self.assertEquals(403, code, msg=str(response))
# send message in created room not joined (no state), expect 403 # send message in created room not joined (no state), expect 403
(code, response) = yield self.mock_resource.trigger( (code, response) = yield self.mock_resource.trigger(
"PUT", send_msg_path, msg_content) "PUT",
send_msg_path,
msg_content
)
self.assertEquals(403, code, msg=str(response)) self.assertEquals(403, code, msg=str(response))
# send message in created room and invited, expect 403 # send message in created room and invited, expect 403
yield self.invite(room=self.created_rmid, src=self.rmcreator_id, yield self.invite(
targ=self.user_id) room=self.created_rmid,
src=self.rmcreator_id,
targ=self.user_id
)
(code, response) = yield self.mock_resource.trigger( (code, response) = yield self.mock_resource.trigger(
"PUT", send_msg_path, msg_content) "PUT",
send_msg_path,
msg_content
)
self.assertEquals(403, code, msg=str(response)) self.assertEquals(403, code, msg=str(response))
# send message in created room and joined, expect 200 # send message in created room and joined, expect 200
yield self.join(room=self.created_rmid, user=self.user_id) yield self.join(room=self.created_rmid, user=self.user_id)
(code, response) = yield self.mock_resource.trigger( (code, response) = yield self.mock_resource.trigger(
"PUT", send_msg_path, msg_content) "PUT",
send_msg_path,
msg_content
)
self.assertEquals(200, code, msg=str(response)) self.assertEquals(200, code, msg=str(response))
# send message in created room and left, expect 403 # send message in created room and left, expect 403
yield self.leave(room=self.created_rmid, user=self.user_id) yield self.leave(room=self.created_rmid, user=self.user_id)
(code, response) = yield self.mock_resource.trigger( (code, response) = yield self.mock_resource.trigger(
"PUT", send_msg_path, msg_content) "PUT",
send_msg_path,
msg_content
)
self.assertEquals(403, code, msg=str(response)) self.assertEquals(403, code, msg=str(response))
@defer.inlineCallbacks @defer.inlineCallbacks
@ -218,9 +236,14 @@ class RoomPermissionsTestCase(RestTestCase):
# set/get topic in created PRIVATE room and joined, expect 200 # set/get topic in created PRIVATE room and joined, expect 200
yield self.join(room=self.created_rmid, user=self.user_id) yield self.join(room=self.created_rmid, user=self.user_id)
# Only room ops can set topic by default
self.auth_user_id = self.rmcreator_id
(code, response) = yield self.mock_resource.trigger( (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.assertEquals(200, code, msg=str(response))
self.auth_user_id = self.user_id
(code, response) = yield self.mock_resource.trigger_get(topic_path) (code, response) = yield self.mock_resource.trigger_get(topic_path)
self.assertEquals(200, code, msg=str(response)) self.assertEquals(200, code, msg=str(response))
self.assert_dict(json.loads(topic_content), response) self.assert_dict(json.loads(topic_content), response)
@ -384,44 +407,51 @@ class RoomPermissionsTestCase(RestTestCase):
# set [invite/join/left] of self, set [invite/join/left] of other, # set [invite/join/left] of self, set [invite/join/left] of other,
# expect all 403s # expect all 403s
for usr in [self.user_id, self.rmcreator_id]: for usr in [self.user_id, self.rmcreator_id]:
yield self.change_membership(room=room, src=self.user_id, yield self.change_membership(
room=room,
src=self.user_id,
targ=usr, targ=usr,
membership=Membership.INVITE, membership=Membership.INVITE,
expect_code=403) expect_code=403
yield self.change_membership(room=room, src=self.user_id, )
yield self.change_membership(
room=room,
src=self.user_id,
targ=usr, targ=usr,
membership=Membership.JOIN, membership=Membership.JOIN,
expect_code=403) expect_code=403
yield self.change_membership(room=room, src=self.user_id, )
targ=usr,
# It is always valid to LEAVE if you've already left (currently.)
yield self.change_membership(
room=room,
src=self.user_id,
targ=self.rmcreator_id,
membership=Membership.LEAVE, membership=Membership.LEAVE,
expect_code=403) expect_code=403
)
class RoomsMemberListTestCase(RestTestCase): class RoomsMemberListTestCase(RestTestCase):
""" Tests /rooms/$room_id/members/list REST events.""" """ Tests /rooms/$room_id/members/list REST events."""
user_id = "@sid1:red" user_id = "@sid1:red"
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
state_handler = Mock(spec=["handle_new_event"])
state_handler.handle_new_event.return_value = True
persistence_service = Mock(spec=["get_latest_pdus_in_context"])
persistence_service.get_latest_pdus_in_context.return_value = []
self.mock_config = NonCallableMock() self.mock_config = NonCallableMock()
self.mock_config.signing_key = [MockKey()] self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer( hs = HomeServer(
"red", "red",
db_pool=None, db_pool=db_pool,
http_client=None, http_client=None,
datastore=MemoryDataStore(),
replication_layer=Mock(), replication_layer=Mock(),
state_handler=state_handler,
persistence_service=persistence_service,
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
@ -442,6 +472,10 @@ class RoomsMemberListTestCase(RestTestCase):
} }
hs.get_auth().get_user_by_token = _get_user_by_token hs.get_auth().get_user_by_token = _get_user_by_token
def _insert_client_ip(*args, **kwargs):
return defer.succeed(None)
hs.get_datastore().insert_client_ip = _insert_client_ip
synapse.rest.room.register_servlets(hs, self.mock_resource) synapse.rest.room.register_servlets(hs, self.mock_resource)
def tearDown(self): def tearDown(self):
@ -493,27 +527,22 @@ class RoomsCreateTestCase(RestTestCase):
""" Tests /rooms and /rooms/$room_id REST events. """ """ Tests /rooms and /rooms/$room_id REST events. """
user_id = "@sid1:red" user_id = "@sid1:red"
@defer.inlineCallbacks
def setUp(self): def setUp(self):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.auth_user_id = self.user_id self.auth_user_id = self.user_id
state_handler = Mock(spec=["handle_new_event"])
state_handler.handle_new_event.return_value = True
persistence_service = Mock(spec=["get_latest_pdus_in_context"])
persistence_service.get_latest_pdus_in_context.return_value = []
self.mock_config = NonCallableMock() self.mock_config = NonCallableMock()
self.mock_config.signing_key = [MockKey()] self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer( hs = HomeServer(
"red", "red",
db_pool=None, db_pool=db_pool,
http_client=None, http_client=None,
datastore=MemoryDataStore(),
replication_layer=Mock(), replication_layer=Mock(),
state_handler=state_handler,
persistence_service=persistence_service,
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
@ -532,6 +561,10 @@ class RoomsCreateTestCase(RestTestCase):
} }
hs.get_auth().get_user_by_token = _get_user_by_token hs.get_auth().get_user_by_token = _get_user_by_token
def _insert_client_ip(*args, **kwargs):
return defer.succeed(None)
hs.get_datastore().insert_client_ip = _insert_client_ip
synapse.rest.room.register_servlets(hs, self.mock_resource) synapse.rest.room.register_servlets(hs, self.mock_resource)
def tearDown(self): def tearDown(self):
@ -601,23 +634,17 @@ class RoomTopicTestCase(RestTestCase):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.auth_user_id = self.user_id self.auth_user_id = self.user_id
state_handler = Mock(spec=["handle_new_event"])
state_handler.handle_new_event.return_value = True
persistence_service = Mock(spec=["get_latest_pdus_in_context"])
persistence_service.get_latest_pdus_in_context.return_value = []
self.mock_config = NonCallableMock() self.mock_config = NonCallableMock()
self.mock_config.signing_key = [MockKey()] self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer( hs = HomeServer(
"red", "red",
db_pool=None, db_pool=db_pool,
http_client=None, http_client=None,
datastore=MemoryDataStore(),
replication_layer=Mock(), replication_layer=Mock(),
state_handler=state_handler,
persistence_service=persistence_service,
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
@ -634,13 +661,18 @@ class RoomTopicTestCase(RestTestCase):
"admin": False, "admin": False,
"device_id": None, "device_id": None,
} }
hs.get_auth().get_user_by_token = _get_user_by_token hs.get_auth().get_user_by_token = _get_user_by_token
def _insert_client_ip(*args, **kwargs):
return defer.succeed(None)
hs.get_datastore().insert_client_ip = _insert_client_ip
synapse.rest.room.register_servlets(hs, self.mock_resource) synapse.rest.room.register_servlets(hs, self.mock_resource)
# create the room # create the room
self.room_id = yield self.create_room_as(self.user_id) self.room_id = yield self.create_room_as(self.user_id)
self.path = "/rooms/%s/state/m.room.topic" % self.room_id self.path = "/rooms/%s/state/m.room.topic" % (self.room_id,)
def tearDown(self): def tearDown(self):
pass pass
@ -718,23 +750,17 @@ class RoomMemberStateTestCase(RestTestCase):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.auth_user_id = self.user_id self.auth_user_id = self.user_id
state_handler = Mock(spec=["handle_new_event"])
state_handler.handle_new_event.return_value = True
persistence_service = Mock(spec=["get_latest_pdus_in_context"])
persistence_service.get_latest_pdus_in_context.return_value = []
self.mock_config = NonCallableMock() self.mock_config = NonCallableMock()
self.mock_config.signing_key = [MockKey()] self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer( hs = HomeServer(
"red", "red",
db_pool=None, db_pool=db_pool,
http_client=None, http_client=None,
datastore=MemoryDataStore(),
replication_layer=Mock(), replication_layer=Mock(),
state_handler=state_handler,
persistence_service=persistence_service,
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
@ -751,13 +777,12 @@ class RoomMemberStateTestCase(RestTestCase):
"admin": False, "admin": False,
"device_id": None, "device_id": None,
} }
return {
"user": hs.parse_userid(self.auth_user_id),
"admin": False,
"device_id": None,
}
hs.get_auth().get_user_by_token = _get_user_by_token hs.get_auth().get_user_by_token = _get_user_by_token
def _insert_client_ip(*args, **kwargs):
return defer.succeed(None)
hs.get_datastore().insert_client_ip = _insert_client_ip
synapse.rest.room.register_servlets(hs, self.mock_resource) synapse.rest.room.register_servlets(hs, self.mock_resource)
self.room_id = yield self.create_room_as(self.user_id) self.room_id = yield self.create_room_as(self.user_id)
@ -862,23 +887,17 @@ class RoomMessagesTestCase(RestTestCase):
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX) self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
self.auth_user_id = self.user_id self.auth_user_id = self.user_id
state_handler = Mock(spec=["handle_new_event"])
state_handler.handle_new_event.return_value = True
persistence_service = Mock(spec=["get_latest_pdus_in_context"])
persistence_service.get_latest_pdus_in_context.return_value = []
self.mock_config = NonCallableMock() self.mock_config = NonCallableMock()
self.mock_config.signing_key = [MockKey()] self.mock_config.signing_key = [MockKey()]
db_pool = SQLiteMemoryDbPool()
yield db_pool.prepare()
hs = HomeServer( hs = HomeServer(
"red", "red",
db_pool=None, db_pool=db_pool,
http_client=None, http_client=None,
datastore=MemoryDataStore(),
replication_layer=Mock(), replication_layer=Mock(),
state_handler=state_handler,
persistence_service=persistence_service,
ratelimiter=NonCallableMock(spec_set=[ ratelimiter=NonCallableMock(spec_set=[
"send_message", "send_message",
]), ]),
@ -897,6 +916,10 @@ class RoomMessagesTestCase(RestTestCase):
} }
hs.get_auth().get_user_by_token = _get_user_by_token hs.get_auth().get_user_by_token = _get_user_by_token
def _insert_client_ip(*args, **kwargs):
return defer.succeed(None)
hs.get_datastore().insert_client_ip = _insert_client_ip
synapse.rest.room.register_servlets(hs, self.mock_resource) synapse.rest.room.register_servlets(hs, self.mock_resource)
self.room_id = yield self.create_room_as(self.user_id) self.room_id = yield self.create_room_as(self.user_id)