2014-08-12 16:10:52 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-03 18:29:13 +02:00
|
|
|
# Copyright 2014 OpenMarket Ltd
|
2014-08-13 04:32:18 +02: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 16:10:52 +02:00
|
|
|
|
2014-09-12 19:24:53 +02:00
|
|
|
from tests import unittest
|
2014-08-28 17:43:55 +02:00
|
|
|
from twisted.internet import defer, reactor
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2015-01-23 12:47:15 +01:00
|
|
|
from mock import Mock, call, ANY, NonCallableMock
|
2014-08-19 15:25:17 +02:00
|
|
|
import json
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-09-11 14:57:17 +02:00
|
|
|
from tests.utils import (
|
2015-02-11 12:37:30 +01:00
|
|
|
MockHttpResource, MockClock, DeferredMockCallable, setup_test_homeserver
|
2014-09-11 14:57:17 +02:00
|
|
|
)
|
2014-08-13 20:18:55 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
from synapse.api.constants import PresenceState
|
|
|
|
from synapse.api.errors import SynapseError
|
|
|
|
from synapse.handlers.presence import PresenceHandler, UserPresenceCache
|
2014-10-29 17:15:48 +01:00
|
|
|
from synapse.streams.config import SourcePaginationConfig
|
2014-12-10 00:53:07 +01:00
|
|
|
from synapse.storage.transactions import DestinationsTable
|
2015-01-23 12:47:15 +01:00
|
|
|
from synapse.types import UserID
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
OFFLINE = PresenceState.OFFLINE
|
2014-08-13 15:31:48 +02:00
|
|
|
UNAVAILABLE = PresenceState.UNAVAILABLE
|
2014-08-12 16:10:52 +02:00
|
|
|
ONLINE = PresenceState.ONLINE
|
|
|
|
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
def _expect_edu(destination, edu_type, content, origin="test"):
|
|
|
|
return {
|
|
|
|
"origin": origin,
|
2014-10-17 18:12:25 +02:00
|
|
|
"origin_server_ts": 1000000,
|
2014-08-19 15:25:17 +02:00
|
|
|
"pdus": [],
|
|
|
|
"edus": [
|
|
|
|
{
|
|
|
|
"edu_type": edu_type,
|
|
|
|
"content": content,
|
|
|
|
}
|
|
|
|
],
|
2014-11-05 12:10:54 +01:00
|
|
|
"pdu_failures": [],
|
2014-08-19 15:25:17 +02:00
|
|
|
}
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
def _make_edu_json(origin, edu_type, content):
|
|
|
|
return json.dumps(_expect_edu("test", edu_type, content, origin=origin))
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class JustPresenceHandlers(object):
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.presence_handler = PresenceHandler(hs)
|
|
|
|
|
|
|
|
|
2015-01-21 17:58:16 +01:00
|
|
|
class PresenceTestCase(unittest.TestCase):
|
2014-09-11 14:57:17 +02:00
|
|
|
@defer.inlineCallbacks
|
2014-08-12 16:10:52 +02:00
|
|
|
def setUp(self):
|
2015-01-21 17:58:16 +01:00
|
|
|
self.clock = MockClock()
|
|
|
|
|
|
|
|
self.mock_federation_resource = MockHttpResource()
|
|
|
|
|
|
|
|
self.mock_http_client = Mock(spec=[])
|
|
|
|
self.mock_http_client.put_json = DeferredMockCallable()
|
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
hs_kwargs = {}
|
|
|
|
if hasattr(self, "make_datastore_mock"):
|
|
|
|
hs_kwargs["datastore"] = self.make_datastore_mock()
|
|
|
|
|
2015-02-11 12:37:30 +01:00
|
|
|
hs = yield setup_test_homeserver(
|
2015-01-21 17:58:16 +01:00
|
|
|
clock=self.clock,
|
2014-09-11 14:57:17 +02:00
|
|
|
handlers=None,
|
2015-01-21 17:58:16 +01:00
|
|
|
resource_for_federation=self.mock_federation_resource,
|
|
|
|
http_client=self.mock_http_client,
|
2014-09-30 16:15:10 +02:00
|
|
|
keyring=Mock(),
|
2015-01-22 18:54:52 +01:00
|
|
|
**hs_kwargs
|
2014-09-11 14:57:17 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
hs.handlers = JustPresenceHandlers(hs)
|
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
self.datastore = hs.get_datastore()
|
2015-01-21 17:58:16 +01:00
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
self.setUp_roommemberhandler_mocks(hs.handlers)
|
2014-09-11 14:57:17 +02:00
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
self.handler = hs.get_handlers().presence_handler
|
|
|
|
self.event_source = hs.get_event_sources().sources["presence"]
|
2015-01-21 17:58:16 +01:00
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
self.distributor = hs.get_distributor()
|
|
|
|
self.distributor.declare("user_joined_room")
|
2015-01-21 17:58:16 +01:00
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
yield self.setUp_users(hs)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
def setUp_roommemberhandler_mocks(self, handlers):
|
2015-01-21 21:01:57 +01:00
|
|
|
self.room_id = "a-room"
|
2014-08-18 14:41:43 +02:00
|
|
|
self.room_members = []
|
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
room_member_handler = handlers.room_member_handler = Mock(spec=[
|
2015-03-09 18:01:11 +01:00
|
|
|
"get_joined_rooms_for_user",
|
2015-01-22 18:54:52 +01:00
|
|
|
"get_room_members",
|
|
|
|
"fetch_room_distributions_into",
|
|
|
|
])
|
|
|
|
self.room_member_handler = room_member_handler
|
|
|
|
|
2014-08-18 14:41:43 +02:00
|
|
|
def get_rooms_for_user(user):
|
|
|
|
if user in self.room_members:
|
2015-01-21 21:01:57 +01:00
|
|
|
return defer.succeed([self.room_id])
|
2014-08-18 14:41:43 +02:00
|
|
|
else:
|
|
|
|
return defer.succeed([])
|
2015-03-09 18:01:11 +01:00
|
|
|
room_member_handler.get_joined_rooms_for_user = get_rooms_for_user
|
2014-08-18 14:41:43 +02:00
|
|
|
|
|
|
|
def get_room_members(room_id):
|
2015-01-21 21:01:57 +01:00
|
|
|
if room_id == self.room_id:
|
2014-08-18 14:41:43 +02:00
|
|
|
return defer.succeed(self.room_members)
|
|
|
|
else:
|
|
|
|
return defer.succeed([])
|
|
|
|
room_member_handler.get_room_members = get_room_members
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def fetch_room_distributions_into(room_id, localusers=None,
|
|
|
|
remotedomains=None, ignore_user=None):
|
|
|
|
|
|
|
|
members = yield get_room_members(room_id)
|
|
|
|
for member in members:
|
|
|
|
if ignore_user is not None and member == ignore_user:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if member.is_mine:
|
|
|
|
if localusers is not None:
|
|
|
|
localusers.add(member)
|
|
|
|
else:
|
|
|
|
if remotedomains is not None:
|
|
|
|
remotedomains.add(member.domain)
|
|
|
|
room_member_handler.fetch_room_distributions_into = (
|
|
|
|
fetch_room_distributions_into)
|
|
|
|
|
|
|
|
self.setUp_datastore_room_mocks(self.datastore)
|
|
|
|
|
|
|
|
def setUp_datastore_room_mocks(self, datastore):
|
|
|
|
def get_room_hosts(room_id):
|
|
|
|
if room_id == self.room_id:
|
|
|
|
hosts = set([u.domain for u in self.room_members])
|
|
|
|
return defer.succeed(hosts)
|
|
|
|
else:
|
|
|
|
return defer.succeed([])
|
|
|
|
datastore.get_joined_hosts_for_room = get_room_hosts
|
|
|
|
|
2014-09-02 17:16:51 +02:00
|
|
|
def user_rooms_intersect(userlist):
|
2014-09-02 17:29:04 +02:00
|
|
|
room_member_ids = map(lambda u: u.to_string(), self.room_members)
|
|
|
|
|
|
|
|
shared = all(map(lambda i: i in room_member_ids, userlist))
|
2014-08-27 19:29:45 +02:00
|
|
|
return defer.succeed(shared)
|
2015-01-22 18:54:52 +01:00
|
|
|
datastore.user_rooms_intersect = user_rooms_intersect
|
2014-08-27 19:29:45 +02:00
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp_users(self, hs):
|
|
|
|
# Some local users to test with
|
2015-01-23 12:47:15 +01: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 16:10:52 +02:00
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
for u in self.u_apple, self.u_banana, self.u_clementine:
|
|
|
|
yield self.datastore.create_presence(u.localpart)
|
|
|
|
|
|
|
|
yield self.datastore.set_presence_state(
|
|
|
|
self.u_apple.localpart, {"state": ONLINE, "status_msg": "Online"}
|
|
|
|
)
|
|
|
|
|
|
|
|
# ID of a local user that does not exist
|
2015-01-23 12:47:15 +01:00
|
|
|
self.u_durian = UserID.from_string("@durian:test")
|
2015-01-22 18:54:52 +01:00
|
|
|
|
|
|
|
# A remote user
|
2015-01-23 12:47:15 +01:00
|
|
|
self.u_cabbage = UserID.from_string("@cabbage:elsewhere")
|
2015-01-22 18:54:52 +01:00
|
|
|
|
|
|
|
|
2015-01-22 21:06:08 +01:00
|
|
|
class MockedDatastorePresenceTestCase(PresenceTestCase):
|
2015-01-22 18:54:52 +01:00
|
|
|
def make_datastore_mock(self):
|
|
|
|
datastore = Mock(spec=[
|
|
|
|
# Bits that Federation needs
|
|
|
|
"prep_send_transaction",
|
|
|
|
"delivered_txn",
|
|
|
|
"get_received_txn_response",
|
|
|
|
"set_received_txn_response",
|
|
|
|
"get_destination_retry_timings",
|
|
|
|
])
|
|
|
|
|
|
|
|
self.setUp_datastore_federation_mocks(datastore)
|
|
|
|
self.setUp_datastore_presence_mocks(datastore)
|
|
|
|
|
|
|
|
return datastore
|
|
|
|
|
|
|
|
def setUp_datastore_federation_mocks(self, datastore):
|
2015-04-01 15:12:33 +02:00
|
|
|
retry_timings_res = {
|
|
|
|
"destination": "",
|
|
|
|
"retry_last_ts": 0,
|
|
|
|
"retry_interval": 0,
|
|
|
|
}
|
2015-01-22 18:54:52 +01:00
|
|
|
datastore.get_destination_retry_timings.return_value = (
|
2015-04-01 15:12:33 +02:00
|
|
|
defer.succeed(retry_timings_res)
|
2015-01-22 18:54:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_received_txn_response(*args):
|
|
|
|
return defer.succeed(None)
|
|
|
|
datastore.get_received_txn_response = get_received_txn_response
|
|
|
|
|
|
|
|
def setUp_datastore_presence_mocks(self, datastore):
|
|
|
|
self.current_user_state = {
|
|
|
|
"apple": OFFLINE,
|
|
|
|
"banana": OFFLINE,
|
|
|
|
"clementine": OFFLINE,
|
|
|
|
"fig": OFFLINE,
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_presence_state(user_localpart):
|
|
|
|
return defer.succeed(
|
|
|
|
{"state": self.current_user_state[user_localpart],
|
|
|
|
"status_msg": None,
|
|
|
|
"mtime": 123456000}
|
|
|
|
)
|
|
|
|
datastore.get_presence_state = get_presence_state
|
|
|
|
|
|
|
|
def set_presence_state(user_localpart, new_state):
|
|
|
|
was = self.current_user_state[user_localpart]
|
|
|
|
self.current_user_state[user_localpart] = new_state["state"]
|
|
|
|
return defer.succeed({"state": was})
|
|
|
|
datastore.set_presence_state = set_presence_state
|
|
|
|
|
|
|
|
def get_presence_list(user_localpart, accepted):
|
|
|
|
if not user_localpart in self.PRESENCE_LIST:
|
|
|
|
return defer.succeed([])
|
|
|
|
return defer.succeed([
|
2015-05-22 17:22:54 +02:00
|
|
|
{"observed_user_id": u, "accepted": accepted} for u in
|
2015-01-22 18:54:52 +01:00
|
|
|
self.PRESENCE_LIST[user_localpart]])
|
|
|
|
datastore.get_presence_list = get_presence_list
|
|
|
|
|
|
|
|
def is_presence_visible(observed_localpart, observer_userid):
|
|
|
|
return True
|
|
|
|
datastore.is_presence_visible = is_presence_visible
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp_users(self, hs):
|
|
|
|
# Some local users to test with
|
2015-01-23 12:47:15 +01: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")
|
|
|
|
self.u_durian = UserID.from_string("@durian:test")
|
|
|
|
self.u_elderberry = UserID.from_string("@elderberry:test")
|
|
|
|
self.u_fig = UserID.from_string("@fig:test")
|
2015-01-22 18:54:52 +01:00
|
|
|
|
|
|
|
# Remote user
|
2015-01-23 12:47:15 +01:00
|
|
|
self.u_onion = UserID.from_string("@onion:farm")
|
|
|
|
self.u_potato = UserID.from_string("@potato:remote")
|
2015-01-22 18:54:52 +01:00
|
|
|
|
|
|
|
yield
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2015-01-21 17:58:16 +01:00
|
|
|
|
|
|
|
class PresenceStateTestCase(PresenceTestCase):
|
|
|
|
""" Tests presence management. """
|
2015-01-22 18:54:52 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp(self):
|
|
|
|
yield super(PresenceStateTestCase, self).setUp()
|
|
|
|
|
|
|
|
self.mock_start = Mock()
|
|
|
|
self.mock_stop = Mock()
|
|
|
|
|
|
|
|
self.handler.start_polling_presence = self.mock_start
|
|
|
|
self.handler.stop_polling_presence = self.mock_stop
|
2015-01-21 17:58:16 +01:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_my_state(self):
|
|
|
|
state = yield self.handler.get_state(
|
|
|
|
target_user=self.u_apple, auth_user=self.u_apple
|
|
|
|
)
|
|
|
|
|
2014-09-01 15:45:35 +02:00
|
|
|
self.assertEquals(
|
2014-09-03 16:37:10 +02:00
|
|
|
{"presence": ONLINE, "status_msg": "Online"},
|
2014-08-12 16:10:52 +02:00
|
|
|
state
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_allowed_state(self):
|
2015-01-22 18:54:52 +01:00
|
|
|
yield self.datastore.allow_presence_visible(
|
2014-09-11 14:57:17 +02:00
|
|
|
observed_localpart=self.u_apple.localpart,
|
|
|
|
observer_userid=self.u_banana.to_string(),
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
state = yield self.handler.get_state(
|
|
|
|
target_user=self.u_apple, auth_user=self.u_banana
|
|
|
|
)
|
|
|
|
|
2014-09-01 15:45:35 +02:00
|
|
|
self.assertEquals(
|
2014-09-03 16:37:10 +02:00
|
|
|
{"presence": ONLINE, "status_msg": "Online"},
|
2014-08-12 16:10:52 +02:00
|
|
|
state
|
|
|
|
)
|
|
|
|
|
2014-08-18 14:41:43 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_same_room_state(self):
|
|
|
|
self.room_members = [self.u_apple, self.u_clementine]
|
|
|
|
|
|
|
|
state = yield self.handler.get_state(
|
|
|
|
target_user=self.u_apple, auth_user=self.u_clementine
|
|
|
|
)
|
|
|
|
|
2014-09-01 15:45:35 +02:00
|
|
|
self.assertEquals(
|
2014-09-03 16:37:10 +02:00
|
|
|
{"presence": ONLINE, "status_msg": "Online"},
|
2014-09-01 15:45:35 +02:00
|
|
|
state
|
|
|
|
)
|
2014-08-18 14:41:43 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_disallowed_state(self):
|
2014-08-18 14:41:43 +02:00
|
|
|
self.room_members = []
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
yield self.assertFailure(
|
|
|
|
self.handler.get_state(
|
|
|
|
target_user=self.u_apple, auth_user=self.u_clementine
|
|
|
|
),
|
|
|
|
SynapseError
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_set_my_state(self):
|
|
|
|
yield self.handler.set_state(
|
|
|
|
target_user=self.u_apple, auth_user=self.u_apple,
|
2014-09-01 15:45:35 +02:00
|
|
|
state={"presence": UNAVAILABLE, "status_msg": "Away"})
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-09-11 14:57:17 +02:00
|
|
|
self.assertEquals(
|
|
|
|
{"state": UNAVAILABLE,
|
|
|
|
"status_msg": "Away",
|
|
|
|
"mtime": 1000000},
|
2015-01-22 18:54:52 +01:00
|
|
|
(yield self.datastore.get_presence_state(self.u_apple.localpart))
|
2014-09-03 16:37:10 +02:00
|
|
|
)
|
2014-09-11 14:57:17 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
self.mock_start.assert_called_with(self.u_apple,
|
2014-08-13 20:18:55 +02:00
|
|
|
state={
|
2014-09-01 15:45:35 +02:00
|
|
|
"presence": UNAVAILABLE,
|
2014-08-13 20:18:55 +02:00
|
|
|
"status_msg": "Away",
|
2014-09-01 16:38:37 +02:00
|
|
|
"last_active": 1000000, # MockClock
|
2014-08-13 20:18:55 +02:00
|
|
|
})
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
yield self.handler.set_state(
|
|
|
|
target_user=self.u_apple, auth_user=self.u_apple,
|
2014-09-01 15:45:35 +02:00
|
|
|
state={"presence": OFFLINE})
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
self.mock_stop.assert_called_with(self.u_apple)
|
|
|
|
|
|
|
|
|
2015-01-21 17:58:16 +01:00
|
|
|
class PresenceInvitesTestCase(PresenceTestCase):
|
2014-08-12 16:10:52 +02:00
|
|
|
""" Tests presence management. """
|
2015-01-22 18:54:52 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp(self):
|
|
|
|
yield super(PresenceInvitesTestCase, self).setUp()
|
|
|
|
|
|
|
|
self.mock_start = Mock()
|
|
|
|
self.mock_stop = Mock()
|
|
|
|
|
|
|
|
self.handler.start_polling_presence = self.mock_start
|
|
|
|
self.handler.stop_polling_presence = self.mock_stop
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_invite_local(self):
|
|
|
|
# TODO(paul): This test will likely break if/when real auth permissions
|
|
|
|
# are added; for now the HS will always accept any invite
|
|
|
|
|
2015-12-01 17:06:17 +01:00
|
|
|
yield self.handler.send_presence_invite(
|
2014-08-12 16:10:52 +02:00
|
|
|
observer_user=self.u_apple, observed_user=self.u_banana)
|
|
|
|
|
2014-09-11 17:22:44 +02:00
|
|
|
self.assertEquals(
|
|
|
|
[{"observed_user_id": "@banana:test", "accepted": 1}],
|
2015-01-22 18:54:52 +01:00
|
|
|
(yield self.datastore.get_presence_list(self.u_apple.localpart))
|
2014-09-11 17:22:44 +02:00
|
|
|
)
|
|
|
|
self.assertTrue(
|
2015-01-22 18:54:52 +01:00
|
|
|
(yield self.datastore.is_presence_visible(
|
2014-09-11 17:22:44 +02:00
|
|
|
observed_localpart=self.u_banana.localpart,
|
|
|
|
observer_userid=self.u_apple.to_string(),
|
|
|
|
))
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
self.mock_start.assert_called_with(
|
|
|
|
self.u_apple, target_user=self.u_banana)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_invite_local_nonexistant(self):
|
2015-12-01 17:06:17 +01:00
|
|
|
yield self.handler.send_presence_invite(
|
2014-08-12 16:10:52 +02:00
|
|
|
observer_user=self.u_apple, observed_user=self.u_durian)
|
|
|
|
|
2014-09-11 17:22:44 +02:00
|
|
|
self.assertEquals(
|
|
|
|
[],
|
2015-01-22 18:54:52 +01:00
|
|
|
(yield self.datastore.get_presence_list(self.u_apple.localpart))
|
2014-09-11 17:22:44 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_invite_remote(self):
|
2015-02-23 16:14:09 +01:00
|
|
|
# Use a different destination, otherwise retry logic might fail the
|
|
|
|
# request
|
|
|
|
u_rocket = UserID.from_string("@rocket:there")
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
put_json = self.mock_http_client.put_json
|
|
|
|
put_json.expect_call_and_return(
|
2015-02-23 16:14:09 +01:00
|
|
|
call("there",
|
2014-08-31 15:51:37 +02:00
|
|
|
path="/_matrix/federation/v1/send/1000000/",
|
2015-02-23 16:14:09 +01:00
|
|
|
data=_expect_edu("there", "m.presence_invite",
|
2014-08-19 15:25:17 +02:00
|
|
|
content={
|
|
|
|
"observer_user": "@apple:test",
|
2015-02-23 16:14:09 +01:00
|
|
|
"observed_user": "@rocket:there",
|
2014-08-19 15:25:17 +02:00
|
|
|
}
|
2014-09-15 16:05:12 +02:00
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-19 15:25:17 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2015-12-01 17:06:17 +01:00
|
|
|
yield self.handler.send_presence_invite(
|
2015-02-23 16:14:09 +01:00
|
|
|
observer_user=self.u_apple, observed_user=u_rocket)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-09-11 17:22:44 +02:00
|
|
|
self.assertEquals(
|
2015-02-23 16:14:09 +01:00
|
|
|
[{"observed_user_id": "@rocket:there", "accepted": 0}],
|
2015-01-22 18:54:52 +01:00
|
|
|
(yield self.datastore.get_presence_list(self.u_apple.localpart))
|
2014-09-11 17:22:44 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield put_json.await_calls()
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_accept_remote(self):
|
|
|
|
# TODO(paul): This test will likely break if/when real auth permissions
|
|
|
|
# are added; for now the HS will always accept any invite
|
2015-02-23 16:14:09 +01:00
|
|
|
|
|
|
|
# Use a different destination, otherwise retry logic might fail the
|
|
|
|
# request
|
|
|
|
u_rocket = UserID.from_string("@rocket:moon")
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
put_json = self.mock_http_client.put_json
|
|
|
|
put_json.expect_call_and_return(
|
2015-02-23 16:14:09 +01:00
|
|
|
call("moon",
|
2014-08-31 15:51:37 +02:00
|
|
|
path="/_matrix/federation/v1/send/1000000/",
|
2015-02-23 16:14:09 +01:00
|
|
|
data=_expect_edu("moon", "m.presence_accept",
|
2014-08-19 15:25:17 +02:00
|
|
|
content={
|
2015-02-23 16:14:09 +01:00
|
|
|
"observer_user": "@rocket:moon",
|
2014-08-19 15:25:17 +02:00
|
|
|
"observed_user": "@apple:test",
|
|
|
|
}
|
2014-09-15 16:05:12 +02:00
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-19 15:25:17 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield self.mock_federation_resource.trigger("PUT",
|
2014-08-31 15:51:37 +02:00
|
|
|
"/_matrix/federation/v1/send/1000000/",
|
2014-08-19 15:25:17 +02:00
|
|
|
_make_edu_json("elsewhere", "m.presence_invite",
|
|
|
|
content={
|
2015-02-23 16:14:09 +01:00
|
|
|
"observer_user": "@rocket:moon",
|
2014-08-12 16:10:52 +02:00
|
|
|
"observed_user": "@apple:test",
|
|
|
|
}
|
2014-08-19 15:25:17 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
2014-09-11 17:22:44 +02:00
|
|
|
self.assertTrue(
|
2015-01-22 18:54:52 +01:00
|
|
|
(yield self.datastore.is_presence_visible(
|
2014-09-11 17:22:44 +02:00
|
|
|
observed_localpart=self.u_apple.localpart,
|
2015-02-23 16:14:09 +01:00
|
|
|
observer_userid=u_rocket.to_string(),
|
2014-09-11 17:22:44 +02:00
|
|
|
))
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield put_json.await_calls()
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_invited_remote_nonexistant(self):
|
2015-02-23 16:14:09 +01:00
|
|
|
# Use a different destination, otherwise retry logic might fail the
|
|
|
|
# request
|
|
|
|
u_rocket = UserID.from_string("@rocket:sun")
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
put_json = self.mock_http_client.put_json
|
|
|
|
put_json.expect_call_and_return(
|
2015-02-23 16:14:09 +01:00
|
|
|
call("sun",
|
2014-08-31 15:51:37 +02:00
|
|
|
path="/_matrix/federation/v1/send/1000000/",
|
2015-02-23 16:14:09 +01:00
|
|
|
data=_expect_edu("sun", "m.presence_deny",
|
2014-08-19 15:25:17 +02:00
|
|
|
content={
|
2015-02-23 16:14:09 +01:00
|
|
|
"observer_user": "@rocket:sun",
|
2014-08-19 15:25:17 +02:00
|
|
|
"observed_user": "@durian:test",
|
|
|
|
}
|
2014-09-15 16:05:12 +02:00
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-19 15:25:17 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield self.mock_federation_resource.trigger("PUT",
|
2014-08-31 15:51:37 +02:00
|
|
|
"/_matrix/federation/v1/send/1000000/",
|
2015-02-23 16:14:09 +01:00
|
|
|
_make_edu_json("sun", "m.presence_invite",
|
2014-08-12 16:10:52 +02:00
|
|
|
content={
|
2015-02-23 16:14:09 +01:00
|
|
|
"observer_user": "@rocket:sun",
|
2014-08-12 16:10:52 +02:00
|
|
|
"observed_user": "@durian:test",
|
|
|
|
}
|
2014-08-19 15:25:17 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield put_json.await_calls()
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_accepted_remote(self):
|
2015-01-22 18:54:52 +01:00
|
|
|
yield self.datastore.add_presence_list_pending(
|
2014-09-11 17:22:44 +02:00
|
|
|
observer_localpart=self.u_apple.localpart,
|
|
|
|
observed_userid=self.u_cabbage.to_string(),
|
|
|
|
)
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield self.mock_federation_resource.trigger("PUT",
|
2014-08-31 15:51:37 +02:00
|
|
|
"/_matrix/federation/v1/send/1000000/",
|
2014-08-19 15:25:17 +02:00
|
|
|
_make_edu_json("elsewhere", "m.presence_accept",
|
|
|
|
content={
|
2014-08-12 16:10:52 +02:00
|
|
|
"observer_user": "@apple:test",
|
|
|
|
"observed_user": "@cabbage:elsewhere",
|
|
|
|
}
|
2014-08-19 15:25:17 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
2014-09-11 17:22:44 +02:00
|
|
|
self.assertEquals(
|
|
|
|
[{"observed_user_id": "@cabbage:elsewhere", "accepted": 1}],
|
2015-01-22 18:54:52 +01:00
|
|
|
(yield self.datastore.get_presence_list(self.u_apple.localpart))
|
2014-09-11 17:22:44 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
self.mock_start.assert_called_with(
|
|
|
|
self.u_apple, target_user=self.u_cabbage)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_denied_remote(self):
|
2015-01-22 18:54:52 +01:00
|
|
|
yield self.datastore.add_presence_list_pending(
|
2014-09-11 17:22:44 +02:00
|
|
|
observer_localpart=self.u_apple.localpart,
|
|
|
|
observed_userid="@eggplant:elsewhere",
|
|
|
|
)
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield self.mock_federation_resource.trigger("PUT",
|
2014-08-31 15:51:37 +02:00
|
|
|
"/_matrix/federation/v1/send/1000000/",
|
2014-08-19 15:25:17 +02:00
|
|
|
_make_edu_json("elsewhere", "m.presence_deny",
|
|
|
|
content={
|
2014-08-12 16:10:52 +02:00
|
|
|
"observer_user": "@apple:test",
|
|
|
|
"observed_user": "@eggplant:elsewhere",
|
|
|
|
}
|
2014-08-19 15:25:17 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
2014-09-11 17:22:44 +02:00
|
|
|
self.assertEquals(
|
|
|
|
[],
|
2015-01-22 18:54:52 +01:00
|
|
|
(yield self.datastore.get_presence_list(self.u_apple.localpart))
|
2014-09-11 17:22:44 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_drop_local(self):
|
2015-01-22 18:54:52 +01:00
|
|
|
yield self.datastore.add_presence_list_pending(
|
2014-09-11 17:22:44 +02:00
|
|
|
observer_localpart=self.u_apple.localpart,
|
|
|
|
observed_userid=self.u_banana.to_string(),
|
|
|
|
)
|
2015-01-22 18:54:52 +01:00
|
|
|
yield self.datastore.set_presence_list_accepted(
|
2014-09-11 17:22:44 +02:00
|
|
|
observer_localpart=self.u_apple.localpart,
|
|
|
|
observed_userid=self.u_banana.to_string(),
|
|
|
|
)
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
yield self.handler.drop(
|
2014-09-11 17:22:44 +02:00
|
|
|
observer_user=self.u_apple,
|
|
|
|
observed_user=self.u_banana,
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-09-11 17:22:44 +02:00
|
|
|
self.assertEquals(
|
|
|
|
[],
|
2015-01-22 18:54:52 +01:00
|
|
|
(yield self.datastore.get_presence_list(self.u_apple.localpart))
|
2014-09-11 17:22:44 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
self.mock_stop.assert_called_with(
|
|
|
|
self.u_apple, target_user=self.u_banana)
|
|
|
|
|
2014-08-18 17:43:18 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_drop_remote(self):
|
2015-01-22 18:54:52 +01:00
|
|
|
yield self.datastore.add_presence_list_pending(
|
2014-09-11 17:22:44 +02:00
|
|
|
observer_localpart=self.u_apple.localpart,
|
|
|
|
observed_userid=self.u_cabbage.to_string(),
|
|
|
|
)
|
2015-01-22 18:54:52 +01:00
|
|
|
yield self.datastore.set_presence_list_accepted(
|
2014-09-11 17:22:44 +02:00
|
|
|
observer_localpart=self.u_apple.localpart,
|
|
|
|
observed_userid=self.u_cabbage.to_string(),
|
|
|
|
)
|
|
|
|
|
2014-08-18 17:43:18 +02:00
|
|
|
yield self.handler.drop(
|
2014-09-11 17:22:44 +02:00
|
|
|
observer_user=self.u_apple,
|
|
|
|
observed_user=self.u_cabbage,
|
|
|
|
)
|
2014-08-18 17:43:18 +02:00
|
|
|
|
2014-09-11 17:22:44 +02:00
|
|
|
self.assertEquals(
|
|
|
|
[],
|
2015-01-22 18:54:52 +01:00
|
|
|
(yield self.datastore.get_presence_list(self.u_apple.localpart))
|
2014-09-11 17:22:44 +02:00
|
|
|
)
|
2014-08-18 17:43:18 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_presence_list(self):
|
2015-01-22 18:54:52 +01:00
|
|
|
yield self.datastore.add_presence_list_pending(
|
2014-09-11 17:22:44 +02:00
|
|
|
observer_localpart=self.u_apple.localpart,
|
|
|
|
observed_userid=self.u_banana.to_string(),
|
2014-09-01 15:45:35 +02:00
|
|
|
)
|
2015-01-22 18:54:52 +01:00
|
|
|
yield self.datastore.set_presence_list_accepted(
|
2014-09-11 17:22:44 +02:00
|
|
|
observer_localpart=self.u_apple.localpart,
|
|
|
|
observed_userid=self.u_banana.to_string(),
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
presence = yield self.handler.get_presence_list(
|
2014-09-11 17:22:44 +02:00
|
|
|
observer_user=self.u_apple)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-09-01 15:45:35 +02:00
|
|
|
self.assertEquals([
|
|
|
|
{"observed_user": self.u_banana,
|
2014-09-11 17:22:44 +02:00
|
|
|
"presence": OFFLINE,
|
|
|
|
"accepted": 1},
|
2014-09-01 15:45:35 +02:00
|
|
|
], presence)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
|
2015-01-22 21:06:08 +01:00
|
|
|
class PresencePushTestCase(MockedDatastorePresenceTestCase):
|
2014-08-12 16:10:52 +02:00
|
|
|
""" Tests steady-state presence status updates.
|
|
|
|
|
|
|
|
They assert that presence state update messages are pushed around the place
|
|
|
|
when users change state, presuming that the watches are all established.
|
|
|
|
|
|
|
|
These tests are MASSIVELY fragile currently as they poke internals of the
|
|
|
|
presence handler; namely the _local_pushmap and _remote_recvmap.
|
|
|
|
BE WARNED...
|
|
|
|
"""
|
2015-01-22 18:54:52 +01:00
|
|
|
PRESENCE_LIST = {
|
|
|
|
'apple': [ "@banana:test", "@clementine:test" ],
|
2015-05-20 14:22:18 +02:00
|
|
|
'banana': [ "@apple:test" ],
|
2015-01-22 18:54:52 +01:00
|
|
|
}
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_push_local(self):
|
|
|
|
self.room_members = [self.u_apple, self.u_elderberry]
|
|
|
|
|
|
|
|
self.datastore.set_presence_state.return_value = defer.succeed(
|
2014-09-03 16:37:10 +02:00
|
|
|
{"state": ONLINE}
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
# TODO(paul): Gut-wrenching
|
|
|
|
self.handler._user_cachemap[self.u_apple] = UserPresenceCache()
|
2014-09-01 16:38:37 +02:00
|
|
|
self.handler._user_cachemap[self.u_apple].update(
|
|
|
|
{"presence": OFFLINE}, serial=0
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
apple_set = self.handler._local_pushmap.setdefault("apple", set())
|
|
|
|
apple_set.add(self.u_banana)
|
|
|
|
apple_set.add(self.u_clementine)
|
|
|
|
|
2014-08-29 20:53:33 +02:00
|
|
|
self.assertEquals(self.event_source.get_current_key(), 0)
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
yield self.handler.set_state(self.u_apple, self.u_apple,
|
2014-09-01 15:45:35 +02:00
|
|
|
{"presence": ONLINE}
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2015-11-05 15:32:26 +01:00
|
|
|
# Apple sees self-reflection even without room_id
|
|
|
|
(events, _) = yield self.event_source.get_new_events(
|
|
|
|
user=self.u_apple,
|
|
|
|
from_key=0,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(self.event_source.get_current_key(), 1)
|
|
|
|
self.assertEquals(events,
|
|
|
|
[
|
|
|
|
{"type": "m.presence",
|
|
|
|
"content": {
|
|
|
|
"user_id": "@apple:test",
|
|
|
|
"presence": ONLINE,
|
|
|
|
"last_active_ago": 0,
|
|
|
|
}},
|
|
|
|
],
|
|
|
|
msg="Presence event should be visible to self-reflection"
|
|
|
|
)
|
|
|
|
|
2014-09-02 17:29:04 +02:00
|
|
|
# Apple sees self-reflection
|
2015-11-05 15:32:26 +01:00
|
|
|
(events, _) = yield self.event_source.get_new_events(
|
|
|
|
user=self.u_apple,
|
|
|
|
from_key=0,
|
|
|
|
room_ids=[self.room_id],
|
2014-09-01 23:30:09 +02:00
|
|
|
)
|
|
|
|
|
2014-08-29 20:53:33 +02:00
|
|
|
self.assertEquals(self.event_source.get_current_key(), 1)
|
2014-09-01 23:30:09 +02:00
|
|
|
self.assertEquals(events,
|
2014-08-29 20:53:33 +02:00
|
|
|
[
|
|
|
|
{"type": "m.presence",
|
|
|
|
"content": {
|
|
|
|
"user_id": "@apple:test",
|
2014-09-01 15:45:35 +02:00
|
|
|
"presence": ONLINE,
|
2014-09-01 16:38:37 +02:00
|
|
|
"last_active_ago": 0,
|
2014-08-29 20:53:33 +02:00
|
|
|
}},
|
|
|
|
],
|
2014-09-02 17:29:04 +02:00
|
|
|
msg="Presence event should be visible to self-reflection"
|
|
|
|
)
|
|
|
|
|
2014-10-29 17:15:48 +01:00
|
|
|
config = SourcePaginationConfig(from_key=1, to_key=0)
|
|
|
|
(chunk, _) = yield self.event_source.get_pagination_rows(
|
|
|
|
self.u_apple, config, None
|
|
|
|
)
|
|
|
|
self.assertEquals(chunk,
|
|
|
|
[
|
|
|
|
{"type": "m.presence",
|
|
|
|
"content": {
|
|
|
|
"user_id": "@apple:test",
|
|
|
|
"presence": ONLINE,
|
|
|
|
"last_active_ago": 0,
|
|
|
|
}},
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2014-09-02 17:29:04 +02:00
|
|
|
# Banana sees it because of presence subscription
|
2015-11-05 15:32:26 +01:00
|
|
|
(events, _) = yield self.event_source.get_new_events(
|
|
|
|
user=self.u_banana,
|
|
|
|
from_key=0,
|
|
|
|
room_ids=[self.room_id],
|
2014-09-02 17:29:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(self.event_source.get_current_key(), 1)
|
|
|
|
self.assertEquals(events,
|
|
|
|
[
|
|
|
|
{"type": "m.presence",
|
|
|
|
"content": {
|
|
|
|
"user_id": "@apple:test",
|
|
|
|
"presence": ONLINE,
|
|
|
|
"last_active_ago": 0,
|
|
|
|
}},
|
|
|
|
],
|
|
|
|
msg="Presence event should be visible to explicit subscribers"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Elderberry sees it because of same room
|
2015-11-05 15:32:26 +01:00
|
|
|
(events, _) = yield self.event_source.get_new_events(
|
|
|
|
user=self.u_elderberry,
|
|
|
|
from_key=0,
|
|
|
|
room_ids=[self.room_id],
|
2014-09-02 17:29:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(self.event_source.get_current_key(), 1)
|
|
|
|
self.assertEquals(events,
|
|
|
|
[
|
|
|
|
{"type": "m.presence",
|
|
|
|
"content": {
|
|
|
|
"user_id": "@apple:test",
|
|
|
|
"presence": ONLINE,
|
|
|
|
"last_active_ago": 0,
|
|
|
|
}},
|
|
|
|
],
|
|
|
|
msg="Presence event should be visible to other room members"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Durian is not in the room, should not see this event
|
2015-11-05 15:32:26 +01:00
|
|
|
(events, _) = yield self.event_source.get_new_events(
|
|
|
|
user=self.u_durian,
|
|
|
|
from_key=0,
|
|
|
|
room_ids=[],
|
2014-09-02 17:29:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(self.event_source.get_current_key(), 1)
|
|
|
|
self.assertEquals(events, [],
|
|
|
|
msg="Presence event should not be visible to others"
|
2014-08-29 20:53:33 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
presence = yield self.handler.get_presence_list(
|
|
|
|
observer_user=self.u_apple, accepted=True)
|
|
|
|
|
2014-09-01 15:45:35 +02:00
|
|
|
self.assertEquals(
|
|
|
|
[
|
2015-05-22 17:22:54 +02:00
|
|
|
{"observed_user": self.u_banana,
|
|
|
|
"presence": OFFLINE,
|
|
|
|
"accepted": True},
|
2014-09-01 15:45:35 +02:00
|
|
|
{"observed_user": self.u_clementine,
|
2015-05-22 17:22:54 +02:00
|
|
|
"presence": OFFLINE,
|
|
|
|
"accepted": True},
|
2014-09-01 15:45:35 +02:00
|
|
|
],
|
|
|
|
presence
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-09-02 17:29:04 +02:00
|
|
|
# TODO(paul): Gut-wrenching
|
|
|
|
banana_set = self.handler._local_pushmap.setdefault("banana", set())
|
|
|
|
banana_set.add(self.u_apple)
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
yield self.handler.set_state(self.u_banana, self.u_banana,
|
2014-09-01 15:45:35 +02:00
|
|
|
{"presence": ONLINE}
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-08-13 20:18:55 +02:00
|
|
|
self.clock.advance_time(2)
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
presence = yield self.handler.get_presence_list(
|
|
|
|
observer_user=self.u_apple, accepted=True)
|
|
|
|
|
|
|
|
self.assertEquals([
|
2014-08-13 20:18:55 +02:00
|
|
|
{"observed_user": self.u_banana,
|
2014-09-01 15:45:35 +02:00
|
|
|
"presence": ONLINE,
|
2015-05-22 17:22:54 +02:00
|
|
|
"last_active_ago": 2000,
|
|
|
|
"accepted": True},
|
2014-08-13 20:18:55 +02:00
|
|
|
{"observed_user": self.u_clementine,
|
2015-05-22 17:22:54 +02:00
|
|
|
"presence": OFFLINE,
|
|
|
|
"accepted": True},
|
2014-08-13 20:18:55 +02:00
|
|
|
], presence)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2015-11-05 15:32:26 +01:00
|
|
|
(events, _) = yield self.event_source.get_new_events(
|
|
|
|
user=self.u_apple,
|
|
|
|
from_key=1,
|
2014-09-01 23:30:09 +02:00
|
|
|
)
|
|
|
|
|
2014-08-29 20:53:33 +02:00
|
|
|
self.assertEquals(self.event_source.get_current_key(), 2)
|
2014-09-01 23:30:09 +02:00
|
|
|
self.assertEquals(events,
|
2014-08-29 20:53:33 +02:00
|
|
|
[
|
|
|
|
{"type": "m.presence",
|
|
|
|
"content": {
|
|
|
|
"user_id": "@banana:test",
|
2014-09-01 15:45:35 +02:00
|
|
|
"presence": ONLINE,
|
2014-09-01 16:38:37 +02:00
|
|
|
"last_active_ago": 2000
|
2014-08-29 20:53:33 +02:00
|
|
|
}},
|
|
|
|
]
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_push_remote(self):
|
2014-08-19 15:25:17 +02:00
|
|
|
put_json = self.mock_http_client.put_json
|
|
|
|
put_json.expect_call_and_return(
|
|
|
|
call("farm",
|
|
|
|
path=ANY, # Can't guarantee which txn ID will be which
|
|
|
|
data=_expect_edu("farm", "m.presence",
|
2014-08-12 16:10:52 +02:00
|
|
|
content={
|
|
|
|
"push": [
|
|
|
|
{"user_id": "@apple:test",
|
2014-09-01 15:45:35 +02:00
|
|
|
"presence": u"online",
|
2014-09-01 16:38:37 +02:00
|
|
|
"last_active_ago": 0},
|
2014-08-12 16:10:52 +02:00
|
|
|
],
|
2014-08-19 15:25:17 +02:00
|
|
|
}
|
2014-09-15 16:05:12 +02:00
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-19 15:25:17 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
2014-09-02 16:38:48 +02:00
|
|
|
put_json.expect_call_and_return(
|
|
|
|
call("remote",
|
|
|
|
path=ANY, # Can't guarantee which txn ID will be which
|
|
|
|
data=_expect_edu("remote", "m.presence",
|
|
|
|
content={
|
|
|
|
"push": [
|
|
|
|
{"user_id": "@apple:test",
|
|
|
|
"presence": u"online",
|
|
|
|
"last_active_ago": 0},
|
|
|
|
],
|
|
|
|
}
|
2014-09-15 16:05:12 +02:00
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-09-02 16:38:48 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
2014-08-19 15:25:17 +02:00
|
|
|
|
|
|
|
self.room_members = [self.u_apple, self.u_onion]
|
|
|
|
|
|
|
|
self.datastore.set_presence_state.return_value = defer.succeed(
|
|
|
|
{"state": ONLINE}
|
|
|
|
)
|
|
|
|
|
|
|
|
# TODO(paul): Gut-wrenching
|
|
|
|
self.handler._user_cachemap[self.u_apple] = UserPresenceCache()
|
2014-09-01 16:38:37 +02:00
|
|
|
self.handler._user_cachemap[self.u_apple].update(
|
|
|
|
{"presence": OFFLINE}, serial=0
|
|
|
|
)
|
2014-08-19 15:25:17 +02:00
|
|
|
apple_set = self.handler._remote_sendmap.setdefault("apple", set())
|
|
|
|
apple_set.add(self.u_potato.domain)
|
|
|
|
|
|
|
|
yield self.handler.set_state(self.u_apple, self.u_apple,
|
2014-09-01 15:45:35 +02:00
|
|
|
{"presence": ONLINE}
|
2014-08-19 15:25:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
yield put_json.await_calls()
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_recv_remote(self):
|
2015-05-20 14:22:18 +02:00
|
|
|
self.room_members = [self.u_apple, self.u_banana, self.u_potato]
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-08-29 20:53:33 +02:00
|
|
|
self.assertEquals(self.event_source.get_current_key(), 0)
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield self.mock_federation_resource.trigger("PUT",
|
2014-08-31 15:51:37 +02:00
|
|
|
"/_matrix/federation/v1/send/1000000/",
|
2014-08-19 15:25:17 +02:00
|
|
|
_make_edu_json("elsewhere", "m.presence",
|
|
|
|
content={
|
2014-08-12 16:10:52 +02:00
|
|
|
"push": [
|
|
|
|
{"user_id": "@potato:remote",
|
2014-09-03 16:37:10 +02:00
|
|
|
"presence": "online",
|
2014-09-01 16:38:37 +02:00
|
|
|
"last_active_ago": 1000},
|
2014-08-12 16:10:52 +02:00
|
|
|
],
|
|
|
|
}
|
2014-08-19 15:25:17 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
2015-11-05 15:32:26 +01:00
|
|
|
(events, _) = yield self.event_source.get_new_events(
|
|
|
|
user=self.u_apple,
|
|
|
|
from_key=0,
|
|
|
|
room_ids=[self.room_id],
|
2014-09-01 23:30:09 +02:00
|
|
|
)
|
|
|
|
|
2014-08-29 20:53:33 +02:00
|
|
|
self.assertEquals(self.event_source.get_current_key(), 1)
|
2014-09-01 23:30:09 +02:00
|
|
|
self.assertEquals(events,
|
2014-08-29 20:53:33 +02:00
|
|
|
[
|
|
|
|
{"type": "m.presence",
|
|
|
|
"content": {
|
|
|
|
"user_id": "@potato:remote",
|
2014-09-01 15:45:35 +02:00
|
|
|
"presence": ONLINE,
|
2014-09-01 16:38:37 +02:00
|
|
|
"last_active_ago": 1000,
|
2014-08-29 20:53:33 +02:00
|
|
|
}}
|
|
|
|
]
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-08-13 20:18:55 +02:00
|
|
|
self.clock.advance_time(2)
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
state = yield self.handler.get_state(self.u_potato, self.u_apple)
|
|
|
|
|
2014-09-01 15:45:35 +02:00
|
|
|
self.assertEquals(
|
2014-09-03 16:37:10 +02:00
|
|
|
{"presence": ONLINE, "last_active_ago": 3000},
|
2014-09-01 15:45:35 +02:00
|
|
|
state
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2015-04-23 19:40:19 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_recv_remote_offline(self):
|
|
|
|
""" Various tests relating to SYN-261 """
|
|
|
|
|
2015-05-20 14:22:18 +02:00
|
|
|
self.room_members = [self.u_apple, self.u_banana, self.u_potato]
|
2015-04-23 19:40:19 +02:00
|
|
|
|
|
|
|
self.assertEquals(self.event_source.get_current_key(), 0)
|
|
|
|
|
|
|
|
yield self.mock_federation_resource.trigger("PUT",
|
|
|
|
"/_matrix/federation/v1/send/1000000/",
|
|
|
|
_make_edu_json("elsewhere", "m.presence",
|
|
|
|
content={
|
|
|
|
"push": [
|
|
|
|
{"user_id": "@potato:remote",
|
|
|
|
"presence": "offline"},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(self.event_source.get_current_key(), 1)
|
|
|
|
|
2015-11-05 15:32:26 +01:00
|
|
|
(events, _) = yield self.event_source.get_new_events(
|
|
|
|
user=self.u_apple,
|
|
|
|
from_key=0,
|
|
|
|
room_ids=[self.room_id,]
|
2015-04-23 19:40:19 +02:00
|
|
|
)
|
|
|
|
self.assertEquals(events,
|
|
|
|
[
|
|
|
|
{"type": "m.presence",
|
|
|
|
"content": {
|
|
|
|
"user_id": "@potato:remote",
|
|
|
|
"presence": OFFLINE,
|
|
|
|
}}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2015-04-23 20:01:37 +02:00
|
|
|
yield self.mock_federation_resource.trigger("PUT",
|
|
|
|
"/_matrix/federation/v1/send/1000001/",
|
|
|
|
_make_edu_json("elsewhere", "m.presence",
|
|
|
|
content={
|
|
|
|
"push": [
|
|
|
|
{"user_id": "@potato:remote",
|
|
|
|
"presence": "online"},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(self.event_source.get_current_key(), 2)
|
|
|
|
|
2015-11-05 15:32:26 +01:00
|
|
|
(events, _) = yield self.event_source.get_new_events(
|
|
|
|
user=self.u_apple,
|
|
|
|
from_key=0,
|
|
|
|
room_ids=[self.room_id,]
|
2015-04-23 20:01:37 +02:00
|
|
|
)
|
|
|
|
self.assertEquals(events,
|
|
|
|
[
|
|
|
|
{"type": "m.presence",
|
|
|
|
"content": {
|
|
|
|
"user_id": "@potato:remote",
|
|
|
|
"presence": ONLINE,
|
|
|
|
}}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_join_room_local(self):
|
|
|
|
self.room_members = [self.u_apple, self.u_banana]
|
|
|
|
|
2014-08-29 20:53:33 +02:00
|
|
|
self.assertEquals(self.event_source.get_current_key(), 0)
|
|
|
|
|
|
|
|
# TODO(paul): Gut-wrenching
|
|
|
|
self.handler._user_cachemap[self.u_clementine] = UserPresenceCache()
|
|
|
|
self.handler._user_cachemap[self.u_clementine].update(
|
|
|
|
{
|
2014-09-01 15:45:35 +02:00
|
|
|
"presence": PresenceState.ONLINE,
|
2014-09-01 16:38:37 +02:00
|
|
|
"last_active": self.clock.time_msec(),
|
2014-08-29 20:53:33 +02:00
|
|
|
}, self.u_clementine
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.distributor.fire("user_joined_room", self.u_clementine,
|
2015-01-21 21:01:57 +01:00
|
|
|
self.room_id
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
2014-09-02 17:29:04 +02:00
|
|
|
self.room_members.append(self.u_clementine)
|
|
|
|
|
2015-11-05 15:32:26 +01:00
|
|
|
(events, _) = yield self.event_source.get_new_events(
|
|
|
|
user=self.u_apple,
|
|
|
|
from_key=0,
|
2014-09-01 23:30:09 +02:00
|
|
|
)
|
|
|
|
|
2014-08-29 20:53:33 +02:00
|
|
|
self.assertEquals(self.event_source.get_current_key(), 1)
|
2014-09-01 23:30:09 +02:00
|
|
|
self.assertEquals(events,
|
2014-08-29 20:53:33 +02:00
|
|
|
[
|
|
|
|
{"type": "m.presence",
|
|
|
|
"content": {
|
|
|
|
"user_id": "@clementine:test",
|
2014-09-01 15:45:35 +02:00
|
|
|
"presence": ONLINE,
|
2014-09-01 16:38:37 +02:00
|
|
|
"last_active_ago": 0,
|
2014-08-29 20:53:33 +02:00
|
|
|
}}
|
|
|
|
]
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_join_room_remote(self):
|
|
|
|
## Sending local user state to a newly-joined remote user
|
2014-08-19 15:25:17 +02:00
|
|
|
put_json = self.mock_http_client.put_json
|
|
|
|
put_json.expect_call_and_return(
|
|
|
|
call("remote",
|
|
|
|
path=ANY, # Can't guarantee which txn ID will be which
|
|
|
|
data=_expect_edu("remote", "m.presence",
|
|
|
|
content={
|
|
|
|
"push": [
|
|
|
|
{"user_id": "@apple:test",
|
2014-09-03 16:37:10 +02:00
|
|
|
"presence": "online"},
|
2014-08-19 15:25:17 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-19 15:25:17 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
|
|
|
put_json.expect_call_and_return(
|
|
|
|
call("remote",
|
|
|
|
path=ANY, # Can't guarantee which txn ID will be which
|
|
|
|
data=_expect_edu("remote", "m.presence",
|
|
|
|
content={
|
|
|
|
"push": [
|
|
|
|
{"user_id": "@banana:test",
|
2014-09-03 16:37:10 +02:00
|
|
|
"presence": "offline"},
|
2014-08-19 15:25:17 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-19 15:25:17 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
# TODO(paul): Gut-wrenching
|
|
|
|
self.handler._user_cachemap[self.u_apple] = UserPresenceCache()
|
|
|
|
self.handler._user_cachemap[self.u_apple].update(
|
2014-09-01 15:45:35 +02:00
|
|
|
{"presence": PresenceState.ONLINE}, self.u_apple)
|
2014-08-12 16:10:52 +02:00
|
|
|
self.room_members = [self.u_apple, self.u_banana]
|
|
|
|
|
|
|
|
yield self.distributor.fire("user_joined_room", self.u_potato,
|
2015-01-21 21:01:57 +01:00
|
|
|
self.room_id
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield put_json.await_calls()
|
|
|
|
|
|
|
|
## Sending newly-joined local user state to remote users
|
|
|
|
|
|
|
|
put_json.expect_call_and_return(
|
|
|
|
call("remote",
|
2014-08-31 15:51:37 +02:00
|
|
|
path="/_matrix/federation/v1/send/1000002/",
|
2014-08-19 15:25:17 +02:00
|
|
|
data=_expect_edu("remote", "m.presence",
|
2014-08-12 16:10:52 +02:00
|
|
|
content={
|
|
|
|
"push": [
|
2014-08-19 15:25:17 +02:00
|
|
|
{"user_id": "@clementine:test",
|
2014-09-03 16:37:10 +02:00
|
|
|
"presence": "online"},
|
2014-08-12 16:10:52 +02:00
|
|
|
],
|
2014-08-19 15:25:17 +02:00
|
|
|
}
|
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-19 15:25:17 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
self.handler._user_cachemap[self.u_clementine] = UserPresenceCache()
|
|
|
|
self.handler._user_cachemap[self.u_clementine].update(
|
2014-09-01 15:45:35 +02:00
|
|
|
{"presence": ONLINE}, self.u_clementine)
|
2014-08-12 16:10:52 +02:00
|
|
|
self.room_members.append(self.u_potato)
|
|
|
|
|
|
|
|
yield self.distributor.fire("user_joined_room", self.u_clementine,
|
2015-01-21 21:01:57 +01:00
|
|
|
self.room_id
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
put_json.await_calls()
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
|
2015-01-22 21:06:08 +01:00
|
|
|
class PresencePollingTestCase(MockedDatastorePresenceTestCase):
|
2014-08-12 16:10:52 +02:00
|
|
|
""" Tests presence status polling. """
|
|
|
|
|
|
|
|
# For this test, we have three local users; apple is watching and is
|
|
|
|
# watched by the other two, but the others don't watch each other.
|
|
|
|
# Additionally clementine is watching a remote user.
|
|
|
|
PRESENCE_LIST = {
|
|
|
|
'apple': [ "@banana:test", "@clementine:test" ],
|
|
|
|
'banana': [ "@apple:test" ],
|
|
|
|
'clementine': [ "@apple:test", "@potato:remote" ],
|
2014-08-28 17:43:55 +02:00
|
|
|
'fig': [ "@potato:remote" ],
|
2014-08-12 16:10:52 +02:00
|
|
|
}
|
|
|
|
|
2015-01-22 18:54:52 +01:00
|
|
|
@defer.inlineCallbacks
|
2014-08-12 16:10:52 +02:00
|
|
|
def setUp(self):
|
2015-01-22 18:54:52 +01:00
|
|
|
yield super(PresencePollingTestCase, self).setUp()
|
2014-08-19 15:25:17 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
self.mock_update_client = Mock()
|
2014-08-29 13:08:33 +02:00
|
|
|
|
|
|
|
def update(*args,**kwargs):
|
|
|
|
return defer.succeed(None)
|
|
|
|
self.mock_update_client.side_effect = update
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
self.handler.push_update_to_clients = self.mock_update_client
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_push_local(self):
|
|
|
|
# apple goes online
|
|
|
|
yield self.handler.set_state(
|
2014-08-19 15:25:17 +02:00
|
|
|
target_user=self.u_apple, auth_user=self.u_apple,
|
2014-09-01 15:45:35 +02:00
|
|
|
state={"presence": ONLINE}
|
2014-08-19 15:25:17 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
# apple should see both banana and clementine currently offline
|
|
|
|
self.mock_update_client.assert_has_calls([
|
2015-05-14 15:59:31 +02:00
|
|
|
call(users_to_push=[self.u_apple]),
|
|
|
|
call(users_to_push=[self.u_apple]),
|
2014-08-12 16:10:52 +02:00
|
|
|
], any_order=True)
|
|
|
|
|
|
|
|
# Gut-wrenching tests
|
|
|
|
self.assertTrue("banana" in self.handler._local_pushmap)
|
|
|
|
self.assertTrue(self.u_apple in self.handler._local_pushmap["banana"])
|
|
|
|
self.assertTrue("clementine" in self.handler._local_pushmap)
|
|
|
|
self.assertTrue(self.u_apple in self.handler._local_pushmap["clementine"])
|
|
|
|
|
|
|
|
self.mock_update_client.reset_mock()
|
|
|
|
|
|
|
|
# banana goes online
|
|
|
|
yield self.handler.set_state(
|
2014-09-01 15:45:35 +02:00
|
|
|
target_user=self.u_banana, auth_user=self.u_banana,
|
|
|
|
state={"presence": ONLINE}
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
# apple and banana should now both see each other online
|
|
|
|
self.mock_update_client.assert_has_calls([
|
2015-05-14 15:59:31 +02:00
|
|
|
call(users_to_push=set([self.u_apple]), room_ids=[]),
|
|
|
|
call(users_to_push=[self.u_banana]),
|
2014-08-12 16:10:52 +02:00
|
|
|
], any_order=True)
|
|
|
|
|
|
|
|
self.assertTrue("apple" in self.handler._local_pushmap)
|
|
|
|
self.assertTrue(self.u_banana in self.handler._local_pushmap["apple"])
|
|
|
|
|
|
|
|
self.mock_update_client.reset_mock()
|
|
|
|
|
|
|
|
# apple goes offline
|
|
|
|
yield self.handler.set_state(
|
2014-09-01 15:45:35 +02:00
|
|
|
target_user=self.u_apple, auth_user=self.u_apple,
|
|
|
|
state={"presence": OFFLINE}
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
# banana should now be told apple is offline
|
|
|
|
self.mock_update_client.assert_has_calls([
|
2015-05-14 15:59:31 +02:00
|
|
|
call(users_to_push=set([self.u_banana, self.u_apple]), room_ids=[]),
|
2014-08-12 16:10:52 +02:00
|
|
|
], any_order=True)
|
|
|
|
|
|
|
|
self.assertFalse("banana" in self.handler._local_pushmap)
|
|
|
|
self.assertFalse("clementine" in self.handler._local_pushmap)
|
2014-08-22 19:40:31 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_remote_poll_send(self):
|
2014-08-19 15:25:17 +02:00
|
|
|
put_json = self.mock_http_client.put_json
|
|
|
|
put_json.expect_call_and_return(
|
|
|
|
call("remote",
|
2014-08-29 13:08:33 +02:00
|
|
|
path=ANY,
|
2014-08-19 15:25:17 +02:00
|
|
|
data=_expect_edu("remote", "m.presence",
|
|
|
|
content={
|
|
|
|
"poll": [ "@potato:remote" ],
|
|
|
|
},
|
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-19 15:25:17 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
|
|
|
|
2014-08-29 13:08:33 +02:00
|
|
|
put_json.expect_call_and_return(
|
|
|
|
call("remote",
|
|
|
|
path=ANY,
|
|
|
|
data=_expect_edu("remote", "m.presence",
|
|
|
|
content={
|
2014-11-19 18:30:35 +01:00
|
|
|
"push": [ {
|
|
|
|
"user_id": "@clementine:test",
|
|
|
|
"presence": OFFLINE,
|
|
|
|
}],
|
2014-08-29 13:08:33 +02:00
|
|
|
},
|
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-29 13:08:33 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
# clementine goes online
|
|
|
|
yield self.handler.set_state(
|
2014-09-01 15:45:35 +02:00
|
|
|
target_user=self.u_clementine, auth_user=self.u_clementine,
|
|
|
|
state={"presence": ONLINE}
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield put_json.await_calls()
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
# Gut-wrenching tests
|
2014-08-28 17:43:55 +02:00
|
|
|
self.assertTrue(self.u_potato in self.handler._remote_recvmap,
|
|
|
|
msg="expected potato to be in _remote_recvmap"
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
self.assertTrue(self.u_clementine in
|
|
|
|
self.handler._remote_recvmap[self.u_potato])
|
|
|
|
|
2014-08-29 13:08:33 +02:00
|
|
|
|
|
|
|
put_json.expect_call_and_return(
|
|
|
|
call("remote",
|
|
|
|
path=ANY,
|
|
|
|
data=_expect_edu("remote", "m.presence",
|
|
|
|
content={
|
2014-11-19 18:30:35 +01:00
|
|
|
"push": [ {
|
|
|
|
"user_id": "@fig:test",
|
|
|
|
"presence": OFFLINE,
|
|
|
|
}],
|
2014-08-29 13:08:33 +02:00
|
|
|
},
|
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-29 13:08:33 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
|
|
|
|
2014-08-28 17:43:55 +02:00
|
|
|
# fig goes online; shouldn't send a second poll
|
|
|
|
yield self.handler.set_state(
|
|
|
|
target_user=self.u_fig, auth_user=self.u_fig,
|
2014-09-01 15:45:35 +02:00
|
|
|
state={"presence": ONLINE}
|
2014-08-28 17:43:55 +02:00
|
|
|
)
|
|
|
|
|
2014-08-29 13:08:33 +02:00
|
|
|
# reactor.iterate(delay=0)
|
2014-08-28 17:43:55 +02:00
|
|
|
|
2014-08-29 13:08:33 +02:00
|
|
|
yield put_json.await_calls()
|
2014-08-28 17:43:55 +02:00
|
|
|
|
|
|
|
# fig goes offline
|
|
|
|
yield self.handler.set_state(
|
|
|
|
target_user=self.u_fig, auth_user=self.u_fig,
|
2014-09-01 15:45:35 +02:00
|
|
|
state={"presence": OFFLINE}
|
2014-08-28 17:43:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
reactor.iterate(delay=0)
|
|
|
|
|
|
|
|
put_json.assert_had_no_calls()
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
put_json.expect_call_and_return(
|
|
|
|
call("remote",
|
2014-08-29 13:08:33 +02:00
|
|
|
path=ANY,
|
2014-08-19 15:25:17 +02:00
|
|
|
data=_expect_edu("remote", "m.presence",
|
|
|
|
content={
|
|
|
|
"unpoll": [ "@potato:remote" ],
|
|
|
|
},
|
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-19 15:25:17 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
# clementine goes offline
|
|
|
|
yield self.handler.set_state(
|
2014-09-01 15:45:35 +02:00
|
|
|
target_user=self.u_clementine, auth_user=self.u_clementine,
|
|
|
|
state={"presence": OFFLINE}
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-08-29 13:08:33 +02:00
|
|
|
yield put_json.await_calls()
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-08-28 17:43:55 +02:00
|
|
|
self.assertFalse(self.u_potato in self.handler._remote_recvmap,
|
|
|
|
msg="expected potato not to be in _remote_recvmap"
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_remote_poll_receive(self):
|
2014-08-19 15:25:17 +02:00
|
|
|
put_json = self.mock_http_client.put_json
|
|
|
|
put_json.expect_call_and_return(
|
|
|
|
call("remote",
|
2014-08-31 15:51:37 +02:00
|
|
|
path="/_matrix/federation/v1/send/1000000/",
|
2014-08-19 15:25:17 +02:00
|
|
|
data=_expect_edu("remote", "m.presence",
|
|
|
|
content={
|
|
|
|
"push": [
|
|
|
|
{"user_id": "@banana:test",
|
2014-09-01 15:45:35 +02:00
|
|
|
"presence": "offline",
|
2014-08-19 15:25:17 +02:00
|
|
|
"status_msg": None},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
),
|
2014-10-13 12:49:40 +02:00
|
|
|
json_data_callback=ANY,
|
2015-11-17 19:38:48 +01:00
|
|
|
long_retries=True,
|
2014-08-19 15:25:17 +02:00
|
|
|
),
|
|
|
|
defer.succeed((200, "OK"))
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.mock_federation_resource.trigger("PUT",
|
2014-08-31 15:51:37 +02:00
|
|
|
"/_matrix/federation/v1/send/1000000/",
|
2014-08-19 15:25:17 +02:00
|
|
|
_make_edu_json("remote", "m.presence",
|
|
|
|
content={
|
2014-08-12 16:10:52 +02:00
|
|
|
"poll": [ "@banana:test" ],
|
2014-08-19 15:25:17 +02:00
|
|
|
},
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield put_json.await_calls()
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
# Gut-wrenching tests
|
|
|
|
self.assertTrue(self.u_banana in self.handler._remote_sendmap)
|
|
|
|
|
2014-08-19 15:25:17 +02:00
|
|
|
yield self.mock_federation_resource.trigger("PUT",
|
2014-08-31 15:51:37 +02:00
|
|
|
"/_matrix/federation/v1/send/1000001/",
|
2014-08-19 15:25:17 +02:00
|
|
|
_make_edu_json("remote", "m.presence",
|
2014-08-12 16:10:52 +02:00
|
|
|
content={
|
|
|
|
"unpoll": [ "@banana:test" ],
|
|
|
|
}
|
2014-08-19 15:25:17 +02:00
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Gut-wrenching tests
|
|
|
|
self.assertFalse(self.u_banana in self.handler._remote_sendmap)
|