mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2025-01-05 16:50:54 -05:00
Perform the 'REST'-level tests of Presence against the real Presence handler as well, mocking out the datastore beneath it
This commit is contained in:
parent
83f031207e
commit
88f7482b92
@ -383,7 +383,7 @@ class PresenceHandler(BaseHandler):
|
|||||||
logger.debug("Start polling for presence from %s", user)
|
logger.debug("Start polling for presence from %s", user)
|
||||||
|
|
||||||
if target_user:
|
if target_user:
|
||||||
target_users = set(target_user)
|
target_users = set([target_user])
|
||||||
else:
|
else:
|
||||||
presence = yield self.store.get_presence_list(
|
presence = yield self.store.get_presence_list(
|
||||||
user.localpart, accepted=True
|
user.localpart, accepted=True
|
||||||
|
@ -24,6 +24,7 @@ import logging
|
|||||||
from ..utils import MockHttpResource
|
from ..utils import MockHttpResource
|
||||||
|
|
||||||
from synapse.api.constants import PresenceState
|
from synapse.api.constants import PresenceState
|
||||||
|
from synapse.handlers.presence import PresenceHandler
|
||||||
from synapse.server import HomeServer
|
from synapse.server import HomeServer
|
||||||
|
|
||||||
|
|
||||||
@ -39,28 +40,48 @@ myid = "@apple:test"
|
|||||||
PATH_PREFIX = "/matrix/client/api/v1"
|
PATH_PREFIX = "/matrix/client/api/v1"
|
||||||
|
|
||||||
|
|
||||||
|
class JustPresenceHandlers(object):
|
||||||
|
def __init__(self, hs):
|
||||||
|
self.presence_handler = PresenceHandler(hs)
|
||||||
|
|
||||||
|
|
||||||
class PresenceStateTestCase(unittest.TestCase):
|
class PresenceStateTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
|
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
|
||||||
self.mock_handler = Mock(spec=[
|
|
||||||
"get_state",
|
|
||||||
"set_state",
|
|
||||||
])
|
|
||||||
|
|
||||||
hs = HomeServer("test",
|
hs = HomeServer("test",
|
||||||
db_pool=None,
|
db_pool=None,
|
||||||
|
datastore=Mock(spec=[
|
||||||
|
"get_presence_state",
|
||||||
|
"set_presence_state",
|
||||||
|
]),
|
||||||
http_client=None,
|
http_client=None,
|
||||||
resource_for_client=self.mock_resource,
|
resource_for_client=self.mock_resource,
|
||||||
resource_for_federation=self.mock_resource,
|
resource_for_federation=self.mock_resource,
|
||||||
)
|
)
|
||||||
|
hs.handlers = JustPresenceHandlers(hs)
|
||||||
|
|
||||||
|
self.datastore = hs.get_datastore()
|
||||||
|
|
||||||
|
def get_presence_list(*a, **kw):
|
||||||
|
return defer.succeed([])
|
||||||
|
self.datastore.get_presence_list = get_presence_list
|
||||||
|
|
||||||
def _get_user_by_token(token=None):
|
def _get_user_by_token(token=None):
|
||||||
return hs.parse_userid(myid)
|
return hs.parse_userid(myid)
|
||||||
|
|
||||||
hs.get_auth().get_user_by_token = _get_user_by_token
|
hs.get_auth().get_user_by_token = _get_user_by_token
|
||||||
|
|
||||||
hs.get_handlers().presence_handler = self.mock_handler
|
room_member_handler = hs.handlers.room_member_handler = Mock(
|
||||||
|
spec=[
|
||||||
|
"get_rooms_for_user",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_rooms_for_user(user):
|
||||||
|
return defer.succeed([])
|
||||||
|
room_member_handler.get_rooms_for_user = get_rooms_for_user
|
||||||
|
|
||||||
hs.register_servlets()
|
hs.register_servlets()
|
||||||
|
|
||||||
@ -68,9 +89,10 @@ class PresenceStateTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_get_my_status(self):
|
def test_get_my_status(self):
|
||||||
mocked_get = self.mock_handler.get_state
|
mocked_get = self.datastore.get_presence_state
|
||||||
mocked_get.return_value = defer.succeed(
|
mocked_get.return_value = defer.succeed(
|
||||||
{"state": ONLINE, "status_msg": "Available"})
|
{"state": ONLINE, "status_msg": "Available"}
|
||||||
|
)
|
||||||
|
|
||||||
(code, response) = yield self.mock_resource.trigger("GET",
|
(code, response) = yield self.mock_resource.trigger("GET",
|
||||||
"/presence/%s/status" % (myid), None)
|
"/presence/%s/status" % (myid), None)
|
||||||
@ -78,47 +100,63 @@ class PresenceStateTestCase(unittest.TestCase):
|
|||||||
self.assertEquals(200, code)
|
self.assertEquals(200, code)
|
||||||
self.assertEquals({"state": ONLINE, "status_msg": "Available"},
|
self.assertEquals({"state": ONLINE, "status_msg": "Available"},
|
||||||
response)
|
response)
|
||||||
mocked_get.assert_called_with(target_user=self.u_apple,
|
mocked_get.assert_called_with("apple")
|
||||||
auth_user=self.u_apple)
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_set_my_status(self):
|
def test_set_my_status(self):
|
||||||
mocked_set = self.mock_handler.set_state
|
mocked_set = self.datastore.set_presence_state
|
||||||
mocked_set.return_value = defer.succeed(())
|
mocked_set.return_value = defer.succeed({"state": OFFLINE})
|
||||||
|
|
||||||
(code, response) = yield self.mock_resource.trigger("PUT",
|
(code, response) = yield self.mock_resource.trigger("PUT",
|
||||||
"/presence/%s/status" % (myid),
|
"/presence/%s/status" % (myid),
|
||||||
'{"state": "unavailable", "status_msg": "Away"}')
|
'{"state": "unavailable", "status_msg": "Away"}')
|
||||||
|
|
||||||
self.assertEquals(200, code)
|
self.assertEquals(200, code)
|
||||||
mocked_set.assert_called_with(target_user=self.u_apple,
|
mocked_set.assert_called_with("apple",
|
||||||
auth_user=self.u_apple,
|
{"state": UNAVAILABLE, "status_msg": "Away"})
|
||||||
state={"state": UNAVAILABLE, "status_msg": "Away"})
|
|
||||||
|
|
||||||
|
|
||||||
class PresenceListTestCase(unittest.TestCase):
|
class PresenceListTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
|
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
|
||||||
self.mock_handler = Mock(spec=[
|
|
||||||
"get_presence_list",
|
|
||||||
"send_invite",
|
|
||||||
"drop",
|
|
||||||
])
|
|
||||||
|
|
||||||
hs = HomeServer("test",
|
hs = HomeServer("test",
|
||||||
db_pool=None,
|
db_pool=None,
|
||||||
|
datastore=Mock(spec=[
|
||||||
|
"has_presence_state",
|
||||||
|
"get_presence_state",
|
||||||
|
"allow_presence_visible",
|
||||||
|
"is_presence_visible",
|
||||||
|
"add_presence_list_pending",
|
||||||
|
"set_presence_list_accepted",
|
||||||
|
"del_presence_list",
|
||||||
|
"get_presence_list",
|
||||||
|
]),
|
||||||
http_client=None,
|
http_client=None,
|
||||||
resource_for_client=self.mock_resource,
|
resource_for_client=self.mock_resource,
|
||||||
resource_for_federation=self.mock_resource
|
resource_for_federation=self.mock_resource
|
||||||
)
|
)
|
||||||
|
hs.handlers = JustPresenceHandlers(hs)
|
||||||
|
|
||||||
|
self.datastore = hs.get_datastore()
|
||||||
|
|
||||||
|
def has_presence_state(user_localpart):
|
||||||
|
return defer.succeed(
|
||||||
|
user_localpart in ("apple", "banana",)
|
||||||
|
)
|
||||||
|
self.datastore.has_presence_state = has_presence_state
|
||||||
|
|
||||||
def _get_user_by_token(token=None):
|
def _get_user_by_token(token=None):
|
||||||
return hs.parse_userid(myid)
|
return hs.parse_userid(myid)
|
||||||
|
|
||||||
hs.get_auth().get_user_by_token = _get_user_by_token
|
room_member_handler = hs.handlers.room_member_handler = Mock(
|
||||||
|
spec=[
|
||||||
|
"get_rooms_for_user",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
hs.get_handlers().presence_handler = self.mock_handler
|
hs.get_auth().get_user_by_token = _get_user_by_token
|
||||||
|
|
||||||
hs.register_servlets()
|
hs.register_servlets()
|
||||||
|
|
||||||
@ -127,53 +165,67 @@ class PresenceListTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_get_my_list(self):
|
def test_get_my_list(self):
|
||||||
self.mock_handler.get_presence_list.return_value = defer.succeed(
|
self.datastore.get_presence_list.return_value = defer.succeed(
|
||||||
[{"observed_user": self.u_banana}]
|
[{"observed_user_id": "@banana:test"}],
|
||||||
)
|
)
|
||||||
|
|
||||||
(code, response) = yield self.mock_resource.trigger("GET",
|
(code, response) = yield self.mock_resource.trigger("GET",
|
||||||
"/presence_list/%s" % (myid), None)
|
"/presence_list/%s" % (myid), None)
|
||||||
|
|
||||||
self.assertEquals(200, code)
|
self.assertEquals(200, code)
|
||||||
self.assertEquals([{"user_id": "@banana:test"}], response)
|
self.assertEquals(
|
||||||
|
[{"user_id": "@banana:test", "state": OFFLINE}], response
|
||||||
|
)
|
||||||
|
|
||||||
|
self.datastore.get_presence_list.assert_called_with(
|
||||||
|
"apple", accepted=True
|
||||||
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_invite(self):
|
def test_invite(self):
|
||||||
self.mock_handler.send_invite.return_value = defer.succeed(())
|
self.datastore.add_presence_list_pending.return_value = (
|
||||||
|
defer.succeed(())
|
||||||
|
)
|
||||||
|
self.datastore.is_presence_visible.return_value = defer.succeed(
|
||||||
|
True
|
||||||
|
)
|
||||||
|
|
||||||
(code, response) = yield self.mock_resource.trigger("POST",
|
(code, response) = yield self.mock_resource.trigger("POST",
|
||||||
"/presence_list/%s" % (myid),
|
"/presence_list/%s" % (myid),
|
||||||
"""{
|
"""{"invite": ["@banana:test"]}"""
|
||||||
"invite": ["@banana:test"]
|
)
|
||||||
}""")
|
|
||||||
|
|
||||||
self.assertEquals(200, code)
|
self.assertEquals(200, code)
|
||||||
|
|
||||||
self.mock_handler.send_invite.assert_called_with(
|
self.datastore.add_presence_list_pending.assert_called_with(
|
||||||
observer_user=self.u_apple, observed_user=self.u_banana)
|
"apple", "@banana:test"
|
||||||
|
)
|
||||||
|
self.datastore.set_presence_list_accepted.assert_called_with(
|
||||||
|
"apple", "@banana:test"
|
||||||
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_drop(self):
|
def test_drop(self):
|
||||||
self.mock_handler.drop.return_value = defer.succeed(())
|
self.datastore.del_presence_list.return_value = (
|
||||||
|
defer.succeed(())
|
||||||
|
)
|
||||||
|
|
||||||
(code, response) = yield self.mock_resource.trigger("POST",
|
(code, response) = yield self.mock_resource.trigger("POST",
|
||||||
"/presence_list/%s" % (myid),
|
"/presence_list/%s" % (myid),
|
||||||
"""{
|
"""{"drop": ["@banana:test"]}"""
|
||||||
"drop": ["@banana:test"]
|
)
|
||||||
}""")
|
|
||||||
|
|
||||||
self.assertEquals(200, code)
|
self.assertEquals(200, code)
|
||||||
|
|
||||||
self.mock_handler.drop.assert_called_with(
|
self.datastore.del_presence_list.assert_called_with(
|
||||||
observer_user=self.u_apple, observed_user=self.u_banana)
|
"apple", "@banana:test"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class PresenceEventStreamTestCase(unittest.TestCase):
|
class PresenceEventStreamTestCase(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
|
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
|
||||||
|
|
||||||
# TODO: mocked data store
|
|
||||||
|
|
||||||
# HIDEOUS HACKERY
|
# HIDEOUS HACKERY
|
||||||
# TODO(paul): This should be injected in via the HomeServer DI system
|
# TODO(paul): This should be injected in via the HomeServer DI system
|
||||||
from synapse.handlers.events import EventStreamHandler
|
from synapse.handlers.events import EventStreamHandler
|
||||||
|
Loading…
Reference in New Issue
Block a user