2014-08-12 10:10:52 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-08-12 22:32:18 -04:00
|
|
|
# Copyright 2014 matrix.org
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
"""Tests REST events for /presence paths."""
|
|
|
|
|
|
|
|
from twisted.trial import unittest
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
from mock import Mock
|
|
|
|
import logging
|
|
|
|
|
2014-08-18 09:03:07 -04:00
|
|
|
from ..utils import MockHttpResource
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
from synapse.api.constants import PresenceState
|
2014-08-18 11:52:29 -04:00
|
|
|
from synapse.handlers.presence import PresenceHandler
|
2014-08-12 10:10:52 -04:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
|
|
|
|
|
|
|
logging.getLogger().addHandler(logging.NullHandler())
|
|
|
|
|
|
|
|
|
|
|
|
OFFLINE = PresenceState.OFFLINE
|
2014-08-13 09:31:48 -04:00
|
|
|
UNAVAILABLE = PresenceState.UNAVAILABLE
|
2014-08-12 10:10:52 -04:00
|
|
|
ONLINE = PresenceState.ONLINE
|
|
|
|
|
|
|
|
|
|
|
|
myid = "@apple:test"
|
|
|
|
PATH_PREFIX = "/matrix/client/api/v1"
|
|
|
|
|
|
|
|
|
2014-08-18 11:52:29 -04:00
|
|
|
class JustPresenceHandlers(object):
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.presence_handler = PresenceHandler(hs)
|
|
|
|
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
class PresenceStateTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2014-08-18 09:03:07 -04:00
|
|
|
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
hs = HomeServer("test",
|
|
|
|
db_pool=None,
|
2014-08-18 11:52:29 -04:00
|
|
|
datastore=Mock(spec=[
|
|
|
|
"get_presence_state",
|
|
|
|
"set_presence_state",
|
|
|
|
]),
|
2014-08-12 10:10:52 -04:00
|
|
|
http_client=None,
|
2014-08-18 09:03:07 -04:00
|
|
|
resource_for_client=self.mock_resource,
|
|
|
|
resource_for_federation=self.mock_resource,
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
2014-08-18 11:52:29 -04:00
|
|
|
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
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
def _get_user_by_token(token=None):
|
|
|
|
return hs.parse_userid(myid)
|
|
|
|
|
|
|
|
hs.get_auth().get_user_by_token = _get_user_by_token
|
|
|
|
|
2014-08-18 11:52:29 -04:00
|
|
|
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
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
hs.register_servlets()
|
|
|
|
|
|
|
|
self.u_apple = hs.parse_userid(myid)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_my_status(self):
|
2014-08-18 11:52:29 -04:00
|
|
|
mocked_get = self.datastore.get_presence_state
|
2014-08-12 10:10:52 -04:00
|
|
|
mocked_get.return_value = defer.succeed(
|
2014-08-18 11:52:29 -04:00
|
|
|
{"state": ONLINE, "status_msg": "Available"}
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-18 09:03:07 -04:00
|
|
|
(code, response) = yield self.mock_resource.trigger("GET",
|
2014-08-12 10:10:52 -04:00
|
|
|
"/presence/%s/status" % (myid), None)
|
|
|
|
|
|
|
|
self.assertEquals(200, code)
|
|
|
|
self.assertEquals({"state": ONLINE, "status_msg": "Available"},
|
|
|
|
response)
|
2014-08-18 11:52:29 -04:00
|
|
|
mocked_get.assert_called_with("apple")
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_set_my_status(self):
|
2014-08-18 11:52:29 -04:00
|
|
|
mocked_set = self.datastore.set_presence_state
|
|
|
|
mocked_set.return_value = defer.succeed({"state": OFFLINE})
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-18 09:03:07 -04:00
|
|
|
(code, response) = yield self.mock_resource.trigger("PUT",
|
2014-08-12 10:10:52 -04:00
|
|
|
"/presence/%s/status" % (myid),
|
2014-08-13 09:31:48 -04:00
|
|
|
'{"state": "unavailable", "status_msg": "Away"}')
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
self.assertEquals(200, code)
|
2014-08-18 11:52:29 -04:00
|
|
|
mocked_set.assert_called_with("apple",
|
|
|
|
{"state": UNAVAILABLE, "status_msg": "Away"})
|
2014-08-22 13:40:31 -04:00
|
|
|
test_set_my_status.skip = "Presence polling is disabled"
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PresenceListTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2014-08-18 09:03:07 -04:00
|
|
|
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
hs = HomeServer("test",
|
|
|
|
db_pool=None,
|
2014-08-18 11:52:29 -04:00
|
|
|
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",
|
|
|
|
]),
|
2014-08-12 10:10:52 -04:00
|
|
|
http_client=None,
|
2014-08-18 09:03:07 -04:00
|
|
|
resource_for_client=self.mock_resource,
|
|
|
|
resource_for_federation=self.mock_resource
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
2014-08-18 11:52:29 -04:00
|
|
|
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
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
def _get_user_by_token(token=None):
|
|
|
|
return hs.parse_userid(myid)
|
|
|
|
|
2014-08-18 11:52:29 -04:00
|
|
|
room_member_handler = hs.handlers.room_member_handler = Mock(
|
|
|
|
spec=[
|
|
|
|
"get_rooms_for_user",
|
|
|
|
]
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-18 11:52:29 -04:00
|
|
|
hs.get_auth().get_user_by_token = _get_user_by_token
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
hs.register_servlets()
|
|
|
|
|
|
|
|
self.u_apple = hs.parse_userid("@apple:test")
|
|
|
|
self.u_banana = hs.parse_userid("@banana:test")
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_my_list(self):
|
2014-08-18 11:52:29 -04:00
|
|
|
self.datastore.get_presence_list.return_value = defer.succeed(
|
|
|
|
[{"observed_user_id": "@banana:test"}],
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
|
|
|
|
2014-08-18 09:03:07 -04:00
|
|
|
(code, response) = yield self.mock_resource.trigger("GET",
|
2014-08-12 10:10:52 -04:00
|
|
|
"/presence_list/%s" % (myid), None)
|
|
|
|
|
|
|
|
self.assertEquals(200, code)
|
2014-08-18 11:52:29 -04:00
|
|
|
self.assertEquals(
|
|
|
|
[{"user_id": "@banana:test", "state": OFFLINE}], response
|
|
|
|
)
|
|
|
|
|
|
|
|
self.datastore.get_presence_list.assert_called_with(
|
|
|
|
"apple", accepted=True
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_invite(self):
|
2014-08-18 11:52:29 -04:00
|
|
|
self.datastore.add_presence_list_pending.return_value = (
|
|
|
|
defer.succeed(())
|
|
|
|
)
|
|
|
|
self.datastore.is_presence_visible.return_value = defer.succeed(
|
|
|
|
True
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-18 09:03:07 -04:00
|
|
|
(code, response) = yield self.mock_resource.trigger("POST",
|
2014-08-18 11:52:29 -04:00
|
|
|
"/presence_list/%s" % (myid),
|
|
|
|
"""{"invite": ["@banana:test"]}"""
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
self.assertEquals(200, code)
|
|
|
|
|
2014-08-18 11:52:29 -04:00
|
|
|
self.datastore.add_presence_list_pending.assert_called_with(
|
|
|
|
"apple", "@banana:test"
|
|
|
|
)
|
|
|
|
self.datastore.set_presence_list_accepted.assert_called_with(
|
|
|
|
"apple", "@banana:test"
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_drop(self):
|
2014-08-18 11:52:29 -04:00
|
|
|
self.datastore.del_presence_list.return_value = (
|
|
|
|
defer.succeed(())
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-18 09:03:07 -04:00
|
|
|
(code, response) = yield self.mock_resource.trigger("POST",
|
2014-08-18 11:52:29 -04:00
|
|
|
"/presence_list/%s" % (myid),
|
|
|
|
"""{"drop": ["@banana:test"]}"""
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
self.assertEquals(200, code)
|
|
|
|
|
2014-08-18 11:52:29 -04:00
|
|
|
self.datastore.del_presence_list.assert_called_with(
|
|
|
|
"apple", "@banana:test"
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PresenceEventStreamTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2014-08-18 09:03:07 -04:00
|
|
|
self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
# HIDEOUS HACKERY
|
|
|
|
# TODO(paul): This should be injected in via the HomeServer DI system
|
|
|
|
from synapse.handlers.events import EventStreamHandler
|
|
|
|
from synapse.handlers.presence import PresenceStreamData
|
|
|
|
EventStreamHandler.stream_data_classes = [
|
|
|
|
PresenceStreamData
|
|
|
|
]
|
|
|
|
|
|
|
|
hs = HomeServer("test",
|
|
|
|
db_pool=None,
|
|
|
|
http_client=None,
|
2014-08-18 09:03:07 -04:00
|
|
|
resource_for_client=self.mock_resource,
|
|
|
|
resource_for_federation=self.mock_resource,
|
2014-08-12 10:10:52 -04:00
|
|
|
datastore=Mock(spec=[
|
|
|
|
"set_presence_state",
|
|
|
|
"get_presence_list",
|
|
|
|
]),
|
|
|
|
clock=Mock(spec=[
|
|
|
|
"call_later",
|
|
|
|
"cancel_call_later",
|
|
|
|
"time_msec",
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
|
|
|
|
hs.get_clock().time_msec.return_value = 1000000
|
|
|
|
|
|
|
|
def _get_user_by_req(req=None):
|
|
|
|
return hs.parse_userid(myid)
|
|
|
|
|
|
|
|
hs.get_auth().get_user_by_req = _get_user_by_req
|
|
|
|
|
|
|
|
hs.register_servlets()
|
|
|
|
|
|
|
|
hs.handlers.room_member_handler = Mock(spec=[
|
|
|
|
"get_rooms_for_user",
|
|
|
|
])
|
|
|
|
hs.handlers.room_member_handler.get_rooms_for_user = (
|
|
|
|
lambda u: defer.succeed([]))
|
|
|
|
|
|
|
|
self.mock_datastore = hs.get_datastore()
|
|
|
|
self.presence = hs.get_handlers().presence_handler
|
|
|
|
|
|
|
|
self.u_apple = hs.parse_userid("@apple:test")
|
|
|
|
self.u_banana = hs.parse_userid("@banana:test")
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_shortpoll(self):
|
|
|
|
self.mock_datastore.set_presence_state.return_value = defer.succeed(
|
|
|
|
{"state": ONLINE})
|
|
|
|
self.mock_datastore.get_presence_list.return_value = defer.succeed(
|
|
|
|
[])
|
|
|
|
|
2014-08-18 09:03:07 -04:00
|
|
|
(code, response) = yield self.mock_resource.trigger("GET",
|
2014-08-12 10:10:52 -04:00
|
|
|
"/events?timeout=0", None)
|
|
|
|
|
|
|
|
self.assertEquals(200, code)
|
|
|
|
|
|
|
|
# We've forced there to be only one data stream so the tokens will
|
|
|
|
# all be ours
|
|
|
|
|
|
|
|
# I'll already get my own presence state change
|
2014-08-19 10:52:20 -04:00
|
|
|
self.assertEquals({"start": "1", "end": "1", "chunk": []}, response)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
self.mock_datastore.set_presence_state.return_value = defer.succeed(
|
|
|
|
{"state": ONLINE})
|
|
|
|
self.mock_datastore.get_presence_list.return_value = defer.succeed(
|
|
|
|
[])
|
|
|
|
|
|
|
|
yield self.presence.set_state(self.u_banana, self.u_banana,
|
|
|
|
state={"state": ONLINE})
|
|
|
|
|
2014-08-18 09:03:07 -04:00
|
|
|
(code, response) = yield self.mock_resource.trigger("GET",
|
2014-08-12 10:10:52 -04:00
|
|
|
"/events?from=1&timeout=0", None)
|
|
|
|
|
|
|
|
self.assertEquals(200, code)
|
|
|
|
self.assertEquals({"start": "1", "end": "2", "chunk": [
|
|
|
|
{"type": "m.presence",
|
2014-08-13 14:18:55 -04:00
|
|
|
"content": {
|
|
|
|
"user_id": "@banana:test",
|
|
|
|
"state": ONLINE,
|
|
|
|
"mtime_age": 0,
|
|
|
|
}},
|
2014-08-12 10:10:52 -04:00
|
|
|
]}, response)
|
2014-08-22 13:40:31 -04:00
|
|
|
test_shortpoll.skip = "Presence polling is disabled"
|