2014-08-12 08:10:52 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-12 20:32:18 -06:00
|
|
|
#
|
|
|
|
# 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 08:10:52 -06:00
|
|
|
"""This file contains tests of the "presence-like" data that is shared between
|
|
|
|
presence and profiles; namely, the displayname and avatar_url."""
|
|
|
|
|
2014-09-12 11:24:53 -06:00
|
|
|
from tests import unittest
|
2014-08-12 08:10:52 -06:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2014-12-19 10:49:39 -07:00
|
|
|
from mock import Mock, call, ANY, NonCallableMock
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2015-02-11 04:37:30 -07:00
|
|
|
from ..utils import MockClock, setup_test_homeserver
|
2014-08-13 12:18:55 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
from synapse.api.constants import PresenceState
|
|
|
|
from synapse.handlers.presence import PresenceHandler
|
|
|
|
from synapse.handlers.profile import ProfileHandler
|
2015-01-23 04:47:15 -07:00
|
|
|
from synapse.types import UserID
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
|
|
|
|
OFFLINE = PresenceState.OFFLINE
|
2014-08-13 07:31:48 -06:00
|
|
|
UNAVAILABLE = PresenceState.UNAVAILABLE
|
2014-08-12 08:10:52 -06:00
|
|
|
ONLINE = PresenceState.ONLINE
|
|
|
|
|
|
|
|
|
|
|
|
class MockReplication(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.edu_handlers = {}
|
|
|
|
|
|
|
|
def register_edu_handler(self, edu_type, handler):
|
|
|
|
self.edu_handlers[edu_type] = handler
|
|
|
|
|
2014-08-13 10:12:50 -06:00
|
|
|
def register_query_handler(self, query_type, handler):
|
|
|
|
pass
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
def received_edu(self, origin, edu_type, content):
|
|
|
|
self.edu_handlers[edu_type](origin, content)
|
|
|
|
|
|
|
|
|
|
|
|
class PresenceAndProfileHandlers(object):
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.presence_handler = PresenceHandler(hs)
|
|
|
|
self.profile_handler = ProfileHandler(hs)
|
|
|
|
|
|
|
|
|
|
|
|
class PresenceProfilelikeDataTestCase(unittest.TestCase):
|
|
|
|
|
2015-02-11 04:37:30 -07:00
|
|
|
@defer.inlineCallbacks
|
2014-08-12 08:10:52 -06:00
|
|
|
def setUp(self):
|
2015-02-11 04:37:30 -07:00
|
|
|
hs = yield setup_test_homeserver(
|
|
|
|
clock=MockClock(),
|
|
|
|
datastore=Mock(spec=[
|
|
|
|
"set_presence_state",
|
|
|
|
"is_presence_visible",
|
|
|
|
"set_profile_displayname",
|
2015-03-09 11:01:11 -06:00
|
|
|
"get_rooms_for_user",
|
2015-02-11 04:37:30 -07:00
|
|
|
]),
|
|
|
|
handlers=None,
|
|
|
|
resource_for_federation=Mock(),
|
|
|
|
http_client=None,
|
|
|
|
replication_layer=MockReplication(),
|
|
|
|
ratelimiter=NonCallableMock(spec_set=[
|
2014-12-19 10:49:39 -07:00
|
|
|
"send_message",
|
2015-02-11 04:37:30 -07:00
|
|
|
]),
|
2014-12-19 10:49:39 -07:00
|
|
|
)
|
|
|
|
self.ratelimiter = hs.get_ratelimiter()
|
|
|
|
self.ratelimiter.send_message.return_value = (True, 0)
|
2014-08-12 08:10:52 -06:00
|
|
|
hs.handlers = PresenceAndProfileHandlers(hs)
|
|
|
|
|
|
|
|
self.datastore = hs.get_datastore()
|
|
|
|
|
|
|
|
self.replication = hs.get_replication_layer()
|
|
|
|
self.replication.send_edu = Mock()
|
2014-08-29 05:08:33 -06:00
|
|
|
|
|
|
|
def send_edu(*args, **kwargs):
|
|
|
|
# print "send_edu: %s, %s" % (args, kwargs)
|
|
|
|
return defer.succeed((200, "OK"))
|
|
|
|
self.replication.send_edu.side_effect = send_edu
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
def get_profile_displayname(user_localpart):
|
|
|
|
return defer.succeed("Frank")
|
|
|
|
self.datastore.get_profile_displayname = get_profile_displayname
|
|
|
|
|
2014-08-13 12:07:22 -06:00
|
|
|
def is_presence_visible(*args, **kwargs):
|
|
|
|
return defer.succeed(False)
|
|
|
|
self.datastore.is_presence_visible = is_presence_visible
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
def get_profile_avatar_url(user_localpart):
|
|
|
|
return defer.succeed("http://foo")
|
|
|
|
self.datastore.get_profile_avatar_url = get_profile_avatar_url
|
|
|
|
|
2014-08-29 05:08:33 -06:00
|
|
|
self.presence_list = [
|
2015-05-22 09:22:54 -06:00
|
|
|
{"observed_user_id": "@banana:test", "accepted": True},
|
|
|
|
{"observed_user_id": "@clementine:test", "accepted": True},
|
2014-08-29 05:08:33 -06:00
|
|
|
]
|
2014-08-12 08:10:52 -06:00
|
|
|
def get_presence_list(user_localpart, accepted=None):
|
2014-08-29 05:08:33 -06:00
|
|
|
return defer.succeed(self.presence_list)
|
2014-08-12 08:10:52 -06:00
|
|
|
self.datastore.get_presence_list = get_presence_list
|
|
|
|
|
2014-09-02 09:16:51 -06:00
|
|
|
def user_rooms_intersect(userlist):
|
2014-08-27 11:29:45 -06:00
|
|
|
return defer.succeed(False)
|
2014-09-02 09:16:51 -06:00
|
|
|
self.datastore.user_rooms_intersect = user_rooms_intersect
|
2014-08-27 11:29:45 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
self.handlers = hs.get_handlers()
|
|
|
|
|
|
|
|
self.mock_update_client = Mock()
|
2014-08-29 05:08:33 -06:00
|
|
|
def update(*args, **kwargs):
|
|
|
|
# print "mock_update_client: %s, %s" %(args, kwargs)
|
|
|
|
return defer.succeed(None)
|
|
|
|
self.mock_update_client.side_effect = update
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
self.handlers.presence_handler.push_update_to_clients = (
|
|
|
|
self.mock_update_client)
|
|
|
|
|
|
|
|
hs.handlers.room_member_handler = Mock(spec=[
|
2015-03-09 11:01:11 -06:00
|
|
|
"get_joined_rooms_for_user",
|
2014-08-12 08:10:52 -06:00
|
|
|
])
|
2015-03-09 11:01:11 -06:00
|
|
|
hs.handlers.room_member_handler.get_joined_rooms_for_user = (
|
2014-08-12 08:10:52 -06:00
|
|
|
lambda u: defer.succeed([]))
|
|
|
|
|
|
|
|
# Some local users to test with
|
2015-01-23 04:47:15 -07:00
|
|
|
self.u_apple = UserID.from_string("@apple:test")
|
|
|
|
self.u_banana = UserID.from_string("@banana:test")
|
|
|
|
self.u_clementine = UserID.from_string("@clementine:test")
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
# Remote user
|
2015-01-23 04:47:15 -07:00
|
|
|
self.u_potato = UserID.from_string("@potato:remote")
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2014-09-17 08:05:09 -06:00
|
|
|
self.mock_get_joined = (
|
2015-03-09 11:01:11 -06:00
|
|
|
self.datastore.get_rooms_for_user
|
2014-09-17 08:05:09 -06:00
|
|
|
)
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_set_my_state(self):
|
2014-08-29 05:08:33 -06:00
|
|
|
self.presence_list = [
|
2015-05-22 09:22:54 -06:00
|
|
|
{"observed_user_id": "@banana:test", "accepted": True},
|
|
|
|
{"observed_user_id": "@clementine:test", "accepted": True},
|
2014-08-29 05:08:33 -06:00
|
|
|
]
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
mocked_set = self.datastore.set_presence_state
|
|
|
|
mocked_set.return_value = defer.succeed({"state": OFFLINE})
|
|
|
|
|
|
|
|
yield self.handlers.presence_handler.set_state(
|
|
|
|
target_user=self.u_apple, auth_user=self.u_apple,
|
2014-09-03 08:37:10 -06:00
|
|
|
state={"presence": UNAVAILABLE, "status_msg": "Away"})
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
mocked_set.assert_called_with("apple",
|
2014-09-03 08:37:10 -06:00
|
|
|
{"state": UNAVAILABLE, "status_msg": "Away"}
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_push_local(self):
|
2014-09-17 08:05:09 -06:00
|
|
|
def get_joined(*args):
|
|
|
|
return defer.succeed([])
|
|
|
|
|
|
|
|
self.mock_get_joined.side_effect = get_joined
|
|
|
|
|
2014-08-29 05:08:33 -06:00
|
|
|
self.presence_list = [
|
2015-05-22 09:22:54 -06:00
|
|
|
{"observed_user_id": "@banana:test", "accepted": True},
|
|
|
|
{"observed_user_id": "@clementine:test", "accepted": True},
|
2014-08-29 05:08:33 -06:00
|
|
|
]
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
self.datastore.set_presence_state.return_value = defer.succeed(
|
2014-09-03 08:37:10 -06:00
|
|
|
{"state": ONLINE}
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
# TODO(paul): Gut-wrenching
|
|
|
|
from synapse.handlers.presence import UserPresenceCache
|
|
|
|
self.handlers.presence_handler._user_cachemap[self.u_apple] = (
|
2014-09-01 08:38:37 -06:00
|
|
|
UserPresenceCache()
|
|
|
|
)
|
|
|
|
self.handlers.presence_handler._user_cachemap[self.u_apple].update(
|
|
|
|
{"presence": OFFLINE}, serial=0
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
apple_set = self.handlers.presence_handler._local_pushmap.setdefault(
|
|
|
|
"apple", set())
|
|
|
|
apple_set.add(self.u_banana)
|
|
|
|
apple_set.add(self.u_clementine)
|
|
|
|
|
|
|
|
yield self.handlers.presence_handler.set_state(self.u_apple,
|
2014-09-03 08:37:10 -06:00
|
|
|
self.u_apple, {"presence": ONLINE}
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
yield self.handlers.presence_handler.set_state(self.u_banana,
|
2014-09-03 08:37:10 -06:00
|
|
|
self.u_banana, {"presence": ONLINE}
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
presence = yield self.handlers.presence_handler.get_presence_list(
|
|
|
|
observer_user=self.u_apple, accepted=True)
|
|
|
|
|
|
|
|
self.assertEquals([
|
2014-08-13 12:18:55 -06:00
|
|
|
{"observed_user": self.u_banana,
|
2014-09-01 07:45:35 -06:00
|
|
|
"presence": ONLINE,
|
2014-09-01 08:38:37 -06:00
|
|
|
"last_active_ago": 0,
|
2014-08-13 12:18:55 -06:00
|
|
|
"displayname": "Frank",
|
2015-05-22 09:22:54 -06:00
|
|
|
"avatar_url": "http://foo",
|
|
|
|
"accepted": True},
|
2014-08-13 12:18:55 -06:00
|
|
|
{"observed_user": self.u_clementine,
|
2015-05-22 09:22:54 -06:00
|
|
|
"presence": OFFLINE,
|
|
|
|
"accepted": True}
|
2014-09-03 08:37:10 -06:00
|
|
|
], presence)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
self.mock_update_client.assert_has_calls([
|
2015-05-14 07:59:31 -06:00
|
|
|
call(
|
|
|
|
users_to_push={self.u_apple, self.u_banana, self.u_clementine},
|
|
|
|
room_ids=[]
|
|
|
|
),
|
2014-08-12 08:10:52 -06:00
|
|
|
], any_order=True)
|
|
|
|
|
|
|
|
self.mock_update_client.reset_mock()
|
|
|
|
|
|
|
|
self.datastore.set_profile_displayname.return_value = defer.succeed(
|
|
|
|
None)
|
|
|
|
|
|
|
|
yield self.handlers.profile_handler.set_displayname(self.u_apple,
|
|
|
|
self.u_apple, "I am an Apple")
|
|
|
|
|
|
|
|
self.mock_update_client.assert_has_calls([
|
2015-05-14 07:59:31 -06:00
|
|
|
call(
|
|
|
|
users_to_push={self.u_apple, self.u_banana, self.u_clementine},
|
2014-08-29 05:08:33 -06:00
|
|
|
room_ids=[],
|
2015-05-14 07:59:31 -06:00
|
|
|
),
|
2014-08-12 08:10:52 -06:00
|
|
|
], any_order=True)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_push_remote(self):
|
2014-08-29 05:08:33 -06:00
|
|
|
self.presence_list = [
|
2015-05-22 09:22:54 -06:00
|
|
|
{"observed_user_id": "@potato:remote", "accepted": True},
|
2014-08-29 05:08:33 -06:00
|
|
|
]
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
self.datastore.set_presence_state.return_value = defer.succeed(
|
2014-09-03 08:37:10 -06:00
|
|
|
{"state": ONLINE}
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
# TODO(paul): Gut-wrenching
|
|
|
|
from synapse.handlers.presence import UserPresenceCache
|
|
|
|
self.handlers.presence_handler._user_cachemap[self.u_apple] = (
|
2014-09-01 08:38:37 -06:00
|
|
|
UserPresenceCache()
|
|
|
|
)
|
|
|
|
self.handlers.presence_handler._user_cachemap[self.u_apple].update(
|
|
|
|
{"presence": OFFLINE}, serial=0
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
apple_set = self.handlers.presence_handler._remote_sendmap.setdefault(
|
|
|
|
"apple", set())
|
|
|
|
apple_set.add(self.u_potato.domain)
|
|
|
|
|
|
|
|
yield self.handlers.presence_handler.set_state(self.u_apple,
|
2014-09-03 08:37:10 -06:00
|
|
|
self.u_apple, {"presence": ONLINE}
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
self.replication.send_edu.assert_called_with(
|
|
|
|
destination="remote",
|
|
|
|
edu_type="m.presence",
|
|
|
|
content={
|
|
|
|
"push": [
|
|
|
|
{"user_id": "@apple:test",
|
2014-09-01 07:45:35 -06:00
|
|
|
"presence": "online",
|
2014-09-01 08:38:37 -06:00
|
|
|
"last_active_ago": 0,
|
2014-08-12 08:10:52 -06:00
|
|
|
"displayname": "Frank",
|
|
|
|
"avatar_url": "http://foo"},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_recv_remote(self):
|
2014-08-29 05:08:33 -06:00
|
|
|
self.presence_list = [
|
|
|
|
{"observed_user_id": "@banana:test"},
|
|
|
|
{"observed_user_id": "@clementine:test"},
|
|
|
|
]
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
# TODO(paul): Gut-wrenching
|
|
|
|
potato_set = self.handlers.presence_handler._remote_recvmap.setdefault(
|
2014-09-03 08:37:10 -06:00
|
|
|
self.u_potato, set()
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
potato_set.add(self.u_apple)
|
|
|
|
|
|
|
|
yield self.replication.received_edu(
|
2014-09-03 08:37:10 -06:00
|
|
|
"remote", "m.presence", {
|
|
|
|
"push": [
|
|
|
|
{"user_id": "@potato:remote",
|
|
|
|
"presence": "online",
|
|
|
|
"displayname": "Frank",
|
|
|
|
"avatar_url": "http://foo"},
|
|
|
|
],
|
|
|
|
}
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
self.mock_update_client.assert_called_with(
|
2014-08-29 05:08:33 -06:00
|
|
|
users_to_push=set([self.u_apple]),
|
|
|
|
room_ids=[],
|
2015-05-14 07:59:31 -06:00
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
state = yield self.handlers.presence_handler.get_state(self.u_potato,
|
|
|
|
self.u_apple)
|
|
|
|
|
2014-09-01 07:45:35 -06:00
|
|
|
self.assertEquals(
|
|
|
|
{"presence": ONLINE,
|
|
|
|
"displayname": "Frank",
|
|
|
|
"avatar_url": "http://foo"},
|
|
|
|
state)
|