Newer
Older
Paul "LeoNerd" Evans
committed
http_client=self.mock_http_client,
)
hs.handlers = JustPresenceHandlers(hs)
self.datastore = hs.get_datastore()
Paul "LeoNerd" Evans
committed
def get_received_txn_response(*args):
return defer.succeed(None)
self.datastore.get_received_txn_response = get_received_txn_response
Erik Johnston
committed
def update(*args,**kwargs):
# print "mock_update_client: Args=%s, kwargs=%s" %(args, kwargs,)
return defer.succeed(None)
self.mock_update_client.side_effect = update
self.handler = hs.get_handlers().presence_handler
self.handler.push_update_to_clients = self.mock_update_client
hs.handlers.room_member_handler = Mock(spec=[
"get_rooms_for_user",
])
# For this test no users are ever in rooms
def get_rooms_for_user(user):
return defer.succeed([])
hs.handlers.room_member_handler.get_rooms_for_user = get_rooms_for_user
# Mocked database state
# Local users always start offline
self.current_user_state = {
Paul "LeoNerd" Evans
committed
"apple": OFFLINE,
"banana": OFFLINE,
"clementine": OFFLINE,
"fig": OFFLINE,
}
def get_presence_state(user_localpart):
return defer.succeed(
{"state": self.current_user_state[user_localpart],
Paul "LeoNerd" Evans
committed
"status_msg": None,
"mtime": 123456000}
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
)
self.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})
self.datastore.set_presence_state = set_presence_state
def get_presence_list(user_localpart, accepted):
return defer.succeed([
{"observed_user_id": u} for u in
self.PRESENCE_LIST[user_localpart]])
self.datastore.get_presence_list = get_presence_list
def is_presence_visible(observed_localpart, observer_userid):
return True
self.datastore.is_presence_visible = is_presence_visible
# Local users
self.u_apple = hs.parse_userid("@apple:test")
self.u_banana = hs.parse_userid("@banana:test")
self.u_clementine = hs.parse_userid("@clementine:test")
Paul "LeoNerd" Evans
committed
self.u_fig = hs.parse_userid("@fig:test")
# Remote users
self.u_potato = hs.parse_userid("@potato:remote")
@defer.inlineCallbacks
def test_push_local(self):
# apple goes online
yield self.handler.set_state(
Paul "LeoNerd" Evans
committed
target_user=self.u_apple, auth_user=self.u_apple,
Paul "LeoNerd" Evans
committed
state={"presence": ONLINE}
Paul "LeoNerd" Evans
committed
)
# apple should see both banana and clementine currently offline
self.mock_update_client.assert_has_calls([
Erik Johnston
committed
call(users_to_push=[self.u_apple],
observed_user=self.u_banana,
statuscache=ANY),
Erik Johnston
committed
call(users_to_push=[self.u_apple],
observed_user=self.u_clementine,
statuscache=ANY),
], 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(
Paul "LeoNerd" Evans
committed
target_user=self.u_banana, auth_user=self.u_banana,
state={"presence": ONLINE}
)
# apple and banana should now both see each other online
self.mock_update_client.assert_has_calls([
Erik Johnston
committed
call(users_to_push=set([self.u_apple]),
Erik Johnston
committed
room_ids=[],
Erik Johnston
committed
call(users_to_push=[self.u_banana],
observed_user=self.u_apple,
statuscache=ANY),
], 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(
Paul "LeoNerd" Evans
committed
target_user=self.u_apple, auth_user=self.u_apple,
state={"presence": OFFLINE}
)
# banana should now be told apple is offline
self.mock_update_client.assert_has_calls([
Erik Johnston
committed
call(users_to_push=set([self.u_banana, self.u_apple]),
Erik Johnston
committed
room_ids=[],
statuscache=ANY),
], any_order=True)
self.assertFalse("banana" in self.handler._local_pushmap)
self.assertFalse("clementine" in self.handler._local_pushmap)
@defer.inlineCallbacks
def test_remote_poll_send(self):
Paul "LeoNerd" Evans
committed
put_json = self.mock_http_client.put_json
put_json.expect_call_and_return(
call("remote",
Erik Johnston
committed
path=ANY,
Paul "LeoNerd" Evans
committed
data=_expect_edu("remote", "m.presence",
content={
"poll": [ "@potato:remote" ],
},
),
),
defer.succeed((200, "OK"))
)
Erik Johnston
committed
put_json.expect_call_and_return(
call("remote",
path=ANY,
data=_expect_edu("remote", "m.presence",
content={
"push": [ {"user_id": "@clementine:test" }],
},
),
),
defer.succeed((200, "OK"))
)
# clementine goes online
yield self.handler.set_state(
Paul "LeoNerd" Evans
committed
target_user=self.u_clementine, auth_user=self.u_clementine,
state={"presence": ONLINE}
)
Paul "LeoNerd" Evans
committed
yield put_json.await_calls()
Paul "LeoNerd" Evans
committed
self.assertTrue(self.u_potato in self.handler._remote_recvmap,
msg="expected potato to be in _remote_recvmap"
)
self.assertTrue(self.u_clementine in
self.handler._remote_recvmap[self.u_potato])
Erik Johnston
committed
put_json.expect_call_and_return(
call("remote",
path=ANY,
data=_expect_edu("remote", "m.presence",
content={
"push": [ {"user_id": "@fig:test" }],
},
),
),
defer.succeed((200, "OK"))
)
Paul "LeoNerd" Evans
committed
# fig goes online; shouldn't send a second poll
yield self.handler.set_state(
target_user=self.u_fig, auth_user=self.u_fig,
Paul "LeoNerd" Evans
committed
state={"presence": ONLINE}
Paul "LeoNerd" Evans
committed
)
Erik Johnston
committed
# reactor.iterate(delay=0)
Paul "LeoNerd" Evans
committed
Erik Johnston
committed
yield put_json.await_calls()
Paul "LeoNerd" Evans
committed
# fig goes offline
yield self.handler.set_state(
target_user=self.u_fig, auth_user=self.u_fig,
Paul "LeoNerd" Evans
committed
state={"presence": OFFLINE}
Paul "LeoNerd" Evans
committed
)
reactor.iterate(delay=0)
put_json.assert_had_no_calls()
Paul "LeoNerd" Evans
committed
put_json.expect_call_and_return(
call("remote",
Erik Johnston
committed
path=ANY,
Paul "LeoNerd" Evans
committed
data=_expect_edu("remote", "m.presence",
content={
"unpoll": [ "@potato:remote" ],
},
),
),
defer.succeed((200, "OK"))
)
# clementine goes offline
yield self.handler.set_state(
Paul "LeoNerd" Evans
committed
target_user=self.u_clementine, auth_user=self.u_clementine,
state={"presence": OFFLINE}
)
Erik Johnston
committed
yield put_json.await_calls()
Paul "LeoNerd" Evans
committed
self.assertFalse(self.u_potato in self.handler._remote_recvmap,
msg="expected potato not to be in _remote_recvmap"
)
@defer.inlineCallbacks
def test_remote_poll_receive(self):
Paul "LeoNerd" Evans
committed
put_json = self.mock_http_client.put_json
put_json.expect_call_and_return(
call("remote",
Matthew Hodgson
committed
path="/_matrix/federation/v1/send/1000000/",
Paul "LeoNerd" Evans
committed
data=_expect_edu("remote", "m.presence",
content={
"push": [
{"user_id": "@banana:test",
Paul "LeoNerd" Evans
committed
"presence": "offline",
Paul "LeoNerd" Evans
committed
"status_msg": None},
],
},
),
),
defer.succeed((200, "OK"))
)
yield self.mock_federation_resource.trigger("PUT",
Matthew Hodgson
committed
"/_matrix/federation/v1/send/1000000/",
Paul "LeoNerd" Evans
committed
_make_edu_json("remote", "m.presence",
content={
Paul "LeoNerd" Evans
committed
},
)
Paul "LeoNerd" Evans
committed
yield put_json.await_calls()
# Gut-wrenching tests
self.assertTrue(self.u_banana in self.handler._remote_sendmap)
Paul "LeoNerd" Evans
committed
yield self.mock_federation_resource.trigger("PUT",
Matthew Hodgson
committed
"/_matrix/federation/v1/send/1000001/",
Paul "LeoNerd" Evans
committed
_make_edu_json("remote", "m.presence",
content={
"unpoll": [ "@banana:test" ],
}
Paul "LeoNerd" Evans
committed
)
)
# Gut-wrenching tests
self.assertFalse(self.u_banana in self.handler._remote_sendmap)