test_presence.py 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from tests import unittest
  16. from twisted.internet import defer, reactor
  17. from mock import Mock, call, ANY, NonCallableMock
  18. import json
  19. from tests.utils import (
  20. MockHttpResource, MockClock, DeferredMockCallable, setup_test_homeserver
  21. )
  22. from synapse.api.constants import PresenceState
  23. from synapse.api.errors import SynapseError
  24. from synapse.handlers.presence import PresenceHandler, UserPresenceCache
  25. from synapse.streams.config import SourcePaginationConfig
  26. from synapse.storage.transactions import DestinationsTable
  27. from synapse.types import UserID
  28. OFFLINE = PresenceState.OFFLINE
  29. UNAVAILABLE = PresenceState.UNAVAILABLE
  30. ONLINE = PresenceState.ONLINE
  31. def _expect_edu(destination, edu_type, content, origin="test"):
  32. return {
  33. "origin": origin,
  34. "origin_server_ts": 1000000,
  35. "pdus": [],
  36. "edus": [
  37. {
  38. "edu_type": edu_type,
  39. "content": content,
  40. }
  41. ],
  42. "pdu_failures": [],
  43. }
  44. def _make_edu_json(origin, edu_type, content):
  45. return json.dumps(_expect_edu("test", edu_type, content, origin=origin))
  46. class JustPresenceHandlers(object):
  47. def __init__(self, hs):
  48. self.presence_handler = PresenceHandler(hs)
  49. class PresenceTestCase(unittest.TestCase):
  50. @defer.inlineCallbacks
  51. def setUp(self):
  52. self.clock = MockClock()
  53. self.mock_federation_resource = MockHttpResource()
  54. self.mock_http_client = Mock(spec=[])
  55. self.mock_http_client.put_json = DeferredMockCallable()
  56. hs_kwargs = {}
  57. if hasattr(self, "make_datastore_mock"):
  58. hs_kwargs["datastore"] = self.make_datastore_mock()
  59. hs = yield setup_test_homeserver(
  60. clock=self.clock,
  61. handlers=None,
  62. resource_for_federation=self.mock_federation_resource,
  63. http_client=self.mock_http_client,
  64. keyring=Mock(),
  65. **hs_kwargs
  66. )
  67. hs.handlers = JustPresenceHandlers(hs)
  68. self.datastore = hs.get_datastore()
  69. self.setUp_roommemberhandler_mocks(hs.handlers)
  70. self.handler = hs.get_handlers().presence_handler
  71. self.event_source = hs.get_event_sources().sources["presence"]
  72. self.distributor = hs.get_distributor()
  73. self.distributor.declare("user_joined_room")
  74. yield self.setUp_users(hs)
  75. def setUp_roommemberhandler_mocks(self, handlers):
  76. self.room_id = "a-room"
  77. self.room_members = []
  78. room_member_handler = handlers.room_member_handler = Mock(spec=[
  79. "get_rooms_for_user",
  80. "get_room_members",
  81. "fetch_room_distributions_into",
  82. ])
  83. self.room_member_handler = room_member_handler
  84. def get_rooms_for_user(user):
  85. if user in self.room_members:
  86. return defer.succeed([self.room_id])
  87. else:
  88. return defer.succeed([])
  89. room_member_handler.get_rooms_for_user = get_rooms_for_user
  90. def get_room_members(room_id):
  91. if room_id == self.room_id:
  92. return defer.succeed(self.room_members)
  93. else:
  94. return defer.succeed([])
  95. room_member_handler.get_room_members = get_room_members
  96. @defer.inlineCallbacks
  97. def fetch_room_distributions_into(room_id, localusers=None,
  98. remotedomains=None, ignore_user=None):
  99. members = yield get_room_members(room_id)
  100. for member in members:
  101. if ignore_user is not None and member == ignore_user:
  102. continue
  103. if member.is_mine:
  104. if localusers is not None:
  105. localusers.add(member)
  106. else:
  107. if remotedomains is not None:
  108. remotedomains.add(member.domain)
  109. room_member_handler.fetch_room_distributions_into = (
  110. fetch_room_distributions_into)
  111. self.setUp_datastore_room_mocks(self.datastore)
  112. def setUp_datastore_room_mocks(self, datastore):
  113. def get_room_hosts(room_id):
  114. if room_id == self.room_id:
  115. hosts = set([u.domain for u in self.room_members])
  116. return defer.succeed(hosts)
  117. else:
  118. return defer.succeed([])
  119. datastore.get_joined_hosts_for_room = get_room_hosts
  120. def user_rooms_intersect(userlist):
  121. room_member_ids = map(lambda u: u.to_string(), self.room_members)
  122. shared = all(map(lambda i: i in room_member_ids, userlist))
  123. return defer.succeed(shared)
  124. datastore.user_rooms_intersect = user_rooms_intersect
  125. @defer.inlineCallbacks
  126. def setUp_users(self, hs):
  127. # Some local users to test with
  128. self.u_apple = UserID.from_string("@apple:test")
  129. self.u_banana = UserID.from_string("@banana:test")
  130. self.u_clementine = UserID.from_string("@clementine:test")
  131. for u in self.u_apple, self.u_banana, self.u_clementine:
  132. yield self.datastore.create_presence(u.localpart)
  133. yield self.datastore.set_presence_state(
  134. self.u_apple.localpart, {"state": ONLINE, "status_msg": "Online"}
  135. )
  136. # ID of a local user that does not exist
  137. self.u_durian = UserID.from_string("@durian:test")
  138. # A remote user
  139. self.u_cabbage = UserID.from_string("@cabbage:elsewhere")
  140. class MockedDatastorePresenceTestCase(PresenceTestCase):
  141. def make_datastore_mock(self):
  142. datastore = Mock(spec=[
  143. # Bits that Federation needs
  144. "prep_send_transaction",
  145. "delivered_txn",
  146. "get_received_txn_response",
  147. "set_received_txn_response",
  148. "get_destination_retry_timings",
  149. ])
  150. self.setUp_datastore_federation_mocks(datastore)
  151. self.setUp_datastore_presence_mocks(datastore)
  152. return datastore
  153. def setUp_datastore_federation_mocks(self, datastore):
  154. datastore.get_destination_retry_timings.return_value = (
  155. defer.succeed(DestinationsTable.EntryType("", 0, 0))
  156. )
  157. def get_received_txn_response(*args):
  158. return defer.succeed(None)
  159. datastore.get_received_txn_response = get_received_txn_response
  160. def setUp_datastore_presence_mocks(self, datastore):
  161. self.current_user_state = {
  162. "apple": OFFLINE,
  163. "banana": OFFLINE,
  164. "clementine": OFFLINE,
  165. "fig": OFFLINE,
  166. }
  167. def get_presence_state(user_localpart):
  168. return defer.succeed(
  169. {"state": self.current_user_state[user_localpart],
  170. "status_msg": None,
  171. "mtime": 123456000}
  172. )
  173. datastore.get_presence_state = get_presence_state
  174. def set_presence_state(user_localpart, new_state):
  175. was = self.current_user_state[user_localpart]
  176. self.current_user_state[user_localpart] = new_state["state"]
  177. return defer.succeed({"state": was})
  178. datastore.set_presence_state = set_presence_state
  179. def get_presence_list(user_localpart, accepted):
  180. if not user_localpart in self.PRESENCE_LIST:
  181. return defer.succeed([])
  182. return defer.succeed([
  183. {"observed_user_id": u} for u in
  184. self.PRESENCE_LIST[user_localpart]])
  185. datastore.get_presence_list = get_presence_list
  186. def is_presence_visible(observed_localpart, observer_userid):
  187. return True
  188. datastore.is_presence_visible = is_presence_visible
  189. @defer.inlineCallbacks
  190. def setUp_users(self, hs):
  191. # Some local users to test with
  192. self.u_apple = UserID.from_string("@apple:test")
  193. self.u_banana = UserID.from_string("@banana:test")
  194. self.u_clementine = UserID.from_string("@clementine:test")
  195. self.u_durian = UserID.from_string("@durian:test")
  196. self.u_elderberry = UserID.from_string("@elderberry:test")
  197. self.u_fig = UserID.from_string("@fig:test")
  198. # Remote user
  199. self.u_onion = UserID.from_string("@onion:farm")
  200. self.u_potato = UserID.from_string("@potato:remote")
  201. yield
  202. class PresenceStateTestCase(PresenceTestCase):
  203. """ Tests presence management. """
  204. @defer.inlineCallbacks
  205. def setUp(self):
  206. yield super(PresenceStateTestCase, self).setUp()
  207. self.mock_start = Mock()
  208. self.mock_stop = Mock()
  209. self.handler.start_polling_presence = self.mock_start
  210. self.handler.stop_polling_presence = self.mock_stop
  211. @defer.inlineCallbacks
  212. def test_get_my_state(self):
  213. state = yield self.handler.get_state(
  214. target_user=self.u_apple, auth_user=self.u_apple
  215. )
  216. self.assertEquals(
  217. {"presence": ONLINE, "status_msg": "Online"},
  218. state
  219. )
  220. @defer.inlineCallbacks
  221. def test_get_allowed_state(self):
  222. yield self.datastore.allow_presence_visible(
  223. observed_localpart=self.u_apple.localpart,
  224. observer_userid=self.u_banana.to_string(),
  225. )
  226. state = yield self.handler.get_state(
  227. target_user=self.u_apple, auth_user=self.u_banana
  228. )
  229. self.assertEquals(
  230. {"presence": ONLINE, "status_msg": "Online"},
  231. state
  232. )
  233. @defer.inlineCallbacks
  234. def test_get_same_room_state(self):
  235. self.room_members = [self.u_apple, self.u_clementine]
  236. state = yield self.handler.get_state(
  237. target_user=self.u_apple, auth_user=self.u_clementine
  238. )
  239. self.assertEquals(
  240. {"presence": ONLINE, "status_msg": "Online"},
  241. state
  242. )
  243. @defer.inlineCallbacks
  244. def test_get_disallowed_state(self):
  245. self.room_members = []
  246. yield self.assertFailure(
  247. self.handler.get_state(
  248. target_user=self.u_apple, auth_user=self.u_clementine
  249. ),
  250. SynapseError
  251. )
  252. @defer.inlineCallbacks
  253. def test_set_my_state(self):
  254. yield self.handler.set_state(
  255. target_user=self.u_apple, auth_user=self.u_apple,
  256. state={"presence": UNAVAILABLE, "status_msg": "Away"})
  257. self.assertEquals(
  258. {"state": UNAVAILABLE,
  259. "status_msg": "Away",
  260. "mtime": 1000000},
  261. (yield self.datastore.get_presence_state(self.u_apple.localpart))
  262. )
  263. self.mock_start.assert_called_with(self.u_apple,
  264. state={
  265. "presence": UNAVAILABLE,
  266. "status_msg": "Away",
  267. "last_active": 1000000, # MockClock
  268. })
  269. yield self.handler.set_state(
  270. target_user=self.u_apple, auth_user=self.u_apple,
  271. state={"presence": OFFLINE})
  272. self.mock_stop.assert_called_with(self.u_apple)
  273. class PresenceInvitesTestCase(PresenceTestCase):
  274. """ Tests presence management. """
  275. @defer.inlineCallbacks
  276. def setUp(self):
  277. yield super(PresenceInvitesTestCase, self).setUp()
  278. self.mock_start = Mock()
  279. self.mock_stop = Mock()
  280. self.handler.start_polling_presence = self.mock_start
  281. self.handler.stop_polling_presence = self.mock_stop
  282. @defer.inlineCallbacks
  283. def test_invite_local(self):
  284. # TODO(paul): This test will likely break if/when real auth permissions
  285. # are added; for now the HS will always accept any invite
  286. yield self.handler.send_invite(
  287. observer_user=self.u_apple, observed_user=self.u_banana)
  288. self.assertEquals(
  289. [{"observed_user_id": "@banana:test", "accepted": 1}],
  290. (yield self.datastore.get_presence_list(self.u_apple.localpart))
  291. )
  292. self.assertTrue(
  293. (yield self.datastore.is_presence_visible(
  294. observed_localpart=self.u_banana.localpart,
  295. observer_userid=self.u_apple.to_string(),
  296. ))
  297. )
  298. self.mock_start.assert_called_with(
  299. self.u_apple, target_user=self.u_banana)
  300. @defer.inlineCallbacks
  301. def test_invite_local_nonexistant(self):
  302. yield self.handler.send_invite(
  303. observer_user=self.u_apple, observed_user=self.u_durian)
  304. self.assertEquals(
  305. [],
  306. (yield self.datastore.get_presence_list(self.u_apple.localpart))
  307. )
  308. @defer.inlineCallbacks
  309. def test_invite_remote(self):
  310. # Use a different destination, otherwise retry logic might fail the
  311. # request
  312. u_rocket = UserID.from_string("@rocket:there")
  313. put_json = self.mock_http_client.put_json
  314. put_json.expect_call_and_return(
  315. call("there",
  316. path="/_matrix/federation/v1/send/1000000/",
  317. data=_expect_edu("there", "m.presence_invite",
  318. content={
  319. "observer_user": "@apple:test",
  320. "observed_user": "@rocket:there",
  321. }
  322. ),
  323. json_data_callback=ANY,
  324. ),
  325. defer.succeed((200, "OK"))
  326. )
  327. yield self.handler.send_invite(
  328. observer_user=self.u_apple, observed_user=u_rocket)
  329. self.assertEquals(
  330. [{"observed_user_id": "@rocket:there", "accepted": 0}],
  331. (yield self.datastore.get_presence_list(self.u_apple.localpart))
  332. )
  333. yield put_json.await_calls()
  334. @defer.inlineCallbacks
  335. def test_accept_remote(self):
  336. # TODO(paul): This test will likely break if/when real auth permissions
  337. # are added; for now the HS will always accept any invite
  338. # Use a different destination, otherwise retry logic might fail the
  339. # request
  340. u_rocket = UserID.from_string("@rocket:moon")
  341. put_json = self.mock_http_client.put_json
  342. put_json.expect_call_and_return(
  343. call("moon",
  344. path="/_matrix/federation/v1/send/1000000/",
  345. data=_expect_edu("moon", "m.presence_accept",
  346. content={
  347. "observer_user": "@rocket:moon",
  348. "observed_user": "@apple:test",
  349. }
  350. ),
  351. json_data_callback=ANY,
  352. ),
  353. defer.succeed((200, "OK"))
  354. )
  355. yield self.mock_federation_resource.trigger("PUT",
  356. "/_matrix/federation/v1/send/1000000/",
  357. _make_edu_json("elsewhere", "m.presence_invite",
  358. content={
  359. "observer_user": "@rocket:moon",
  360. "observed_user": "@apple:test",
  361. }
  362. )
  363. )
  364. self.assertTrue(
  365. (yield self.datastore.is_presence_visible(
  366. observed_localpart=self.u_apple.localpart,
  367. observer_userid=u_rocket.to_string(),
  368. ))
  369. )
  370. yield put_json.await_calls()
  371. @defer.inlineCallbacks
  372. def test_invited_remote_nonexistant(self):
  373. # Use a different destination, otherwise retry logic might fail the
  374. # request
  375. u_rocket = UserID.from_string("@rocket:sun")
  376. put_json = self.mock_http_client.put_json
  377. put_json.expect_call_and_return(
  378. call("sun",
  379. path="/_matrix/federation/v1/send/1000000/",
  380. data=_expect_edu("sun", "m.presence_deny",
  381. content={
  382. "observer_user": "@rocket:sun",
  383. "observed_user": "@durian:test",
  384. }
  385. ),
  386. json_data_callback=ANY,
  387. ),
  388. defer.succeed((200, "OK"))
  389. )
  390. yield self.mock_federation_resource.trigger("PUT",
  391. "/_matrix/federation/v1/send/1000000/",
  392. _make_edu_json("sun", "m.presence_invite",
  393. content={
  394. "observer_user": "@rocket:sun",
  395. "observed_user": "@durian:test",
  396. }
  397. )
  398. )
  399. yield put_json.await_calls()
  400. @defer.inlineCallbacks
  401. def test_accepted_remote(self):
  402. yield self.datastore.add_presence_list_pending(
  403. observer_localpart=self.u_apple.localpart,
  404. observed_userid=self.u_cabbage.to_string(),
  405. )
  406. yield self.mock_federation_resource.trigger("PUT",
  407. "/_matrix/federation/v1/send/1000000/",
  408. _make_edu_json("elsewhere", "m.presence_accept",
  409. content={
  410. "observer_user": "@apple:test",
  411. "observed_user": "@cabbage:elsewhere",
  412. }
  413. )
  414. )
  415. self.assertEquals(
  416. [{"observed_user_id": "@cabbage:elsewhere", "accepted": 1}],
  417. (yield self.datastore.get_presence_list(self.u_apple.localpart))
  418. )
  419. self.mock_start.assert_called_with(
  420. self.u_apple, target_user=self.u_cabbage)
  421. @defer.inlineCallbacks
  422. def test_denied_remote(self):
  423. yield self.datastore.add_presence_list_pending(
  424. observer_localpart=self.u_apple.localpart,
  425. observed_userid="@eggplant:elsewhere",
  426. )
  427. yield self.mock_federation_resource.trigger("PUT",
  428. "/_matrix/federation/v1/send/1000000/",
  429. _make_edu_json("elsewhere", "m.presence_deny",
  430. content={
  431. "observer_user": "@apple:test",
  432. "observed_user": "@eggplant:elsewhere",
  433. }
  434. )
  435. )
  436. self.assertEquals(
  437. [],
  438. (yield self.datastore.get_presence_list(self.u_apple.localpart))
  439. )
  440. @defer.inlineCallbacks
  441. def test_drop_local(self):
  442. yield self.datastore.add_presence_list_pending(
  443. observer_localpart=self.u_apple.localpart,
  444. observed_userid=self.u_banana.to_string(),
  445. )
  446. yield self.datastore.set_presence_list_accepted(
  447. observer_localpart=self.u_apple.localpart,
  448. observed_userid=self.u_banana.to_string(),
  449. )
  450. yield self.handler.drop(
  451. observer_user=self.u_apple,
  452. observed_user=self.u_banana,
  453. )
  454. self.assertEquals(
  455. [],
  456. (yield self.datastore.get_presence_list(self.u_apple.localpart))
  457. )
  458. self.mock_stop.assert_called_with(
  459. self.u_apple, target_user=self.u_banana)
  460. @defer.inlineCallbacks
  461. def test_drop_remote(self):
  462. yield self.datastore.add_presence_list_pending(
  463. observer_localpart=self.u_apple.localpart,
  464. observed_userid=self.u_cabbage.to_string(),
  465. )
  466. yield self.datastore.set_presence_list_accepted(
  467. observer_localpart=self.u_apple.localpart,
  468. observed_userid=self.u_cabbage.to_string(),
  469. )
  470. yield self.handler.drop(
  471. observer_user=self.u_apple,
  472. observed_user=self.u_cabbage,
  473. )
  474. self.assertEquals(
  475. [],
  476. (yield self.datastore.get_presence_list(self.u_apple.localpart))
  477. )
  478. @defer.inlineCallbacks
  479. def test_get_presence_list(self):
  480. yield self.datastore.add_presence_list_pending(
  481. observer_localpart=self.u_apple.localpart,
  482. observed_userid=self.u_banana.to_string(),
  483. )
  484. yield self.datastore.set_presence_list_accepted(
  485. observer_localpart=self.u_apple.localpart,
  486. observed_userid=self.u_banana.to_string(),
  487. )
  488. presence = yield self.handler.get_presence_list(
  489. observer_user=self.u_apple)
  490. self.assertEquals([
  491. {"observed_user": self.u_banana,
  492. "presence": OFFLINE,
  493. "accepted": 1},
  494. ], presence)
  495. class PresencePushTestCase(MockedDatastorePresenceTestCase):
  496. """ Tests steady-state presence status updates.
  497. They assert that presence state update messages are pushed around the place
  498. when users change state, presuming that the watches are all established.
  499. These tests are MASSIVELY fragile currently as they poke internals of the
  500. presence handler; namely the _local_pushmap and _remote_recvmap.
  501. BE WARNED...
  502. """
  503. PRESENCE_LIST = {
  504. 'apple': [ "@banana:test", "@clementine:test" ],
  505. }
  506. @defer.inlineCallbacks
  507. def test_push_local(self):
  508. self.room_members = [self.u_apple, self.u_elderberry]
  509. self.datastore.set_presence_state.return_value = defer.succeed(
  510. {"state": ONLINE}
  511. )
  512. # TODO(paul): Gut-wrenching
  513. self.handler._user_cachemap[self.u_apple] = UserPresenceCache()
  514. self.handler._user_cachemap[self.u_apple].update(
  515. {"presence": OFFLINE}, serial=0
  516. )
  517. apple_set = self.handler._local_pushmap.setdefault("apple", set())
  518. apple_set.add(self.u_banana)
  519. apple_set.add(self.u_clementine)
  520. self.assertEquals(self.event_source.get_current_key(), 0)
  521. yield self.handler.set_state(self.u_apple, self.u_apple,
  522. {"presence": ONLINE}
  523. )
  524. # Apple sees self-reflection
  525. (events, _) = yield self.event_source.get_new_events_for_user(
  526. self.u_apple, 0, None
  527. )
  528. self.assertEquals(self.event_source.get_current_key(), 1)
  529. self.assertEquals(events,
  530. [
  531. {"type": "m.presence",
  532. "content": {
  533. "user_id": "@apple:test",
  534. "presence": ONLINE,
  535. "last_active_ago": 0,
  536. }},
  537. ],
  538. msg="Presence event should be visible to self-reflection"
  539. )
  540. config = SourcePaginationConfig(from_key=1, to_key=0)
  541. (chunk, _) = yield self.event_source.get_pagination_rows(
  542. self.u_apple, config, None
  543. )
  544. self.assertEquals(chunk,
  545. [
  546. {"type": "m.presence",
  547. "content": {
  548. "user_id": "@apple:test",
  549. "presence": ONLINE,
  550. "last_active_ago": 0,
  551. }},
  552. ]
  553. )
  554. # Banana sees it because of presence subscription
  555. (events, _) = yield self.event_source.get_new_events_for_user(
  556. self.u_banana, 0, None
  557. )
  558. self.assertEquals(self.event_source.get_current_key(), 1)
  559. self.assertEquals(events,
  560. [
  561. {"type": "m.presence",
  562. "content": {
  563. "user_id": "@apple:test",
  564. "presence": ONLINE,
  565. "last_active_ago": 0,
  566. }},
  567. ],
  568. msg="Presence event should be visible to explicit subscribers"
  569. )
  570. # Elderberry sees it because of same room
  571. (events, _) = yield self.event_source.get_new_events_for_user(
  572. self.u_elderberry, 0, None
  573. )
  574. self.assertEquals(self.event_source.get_current_key(), 1)
  575. self.assertEquals(events,
  576. [
  577. {"type": "m.presence",
  578. "content": {
  579. "user_id": "@apple:test",
  580. "presence": ONLINE,
  581. "last_active_ago": 0,
  582. }},
  583. ],
  584. msg="Presence event should be visible to other room members"
  585. )
  586. # Durian is not in the room, should not see this event
  587. (events, _) = yield self.event_source.get_new_events_for_user(
  588. self.u_durian, 0, None
  589. )
  590. self.assertEquals(self.event_source.get_current_key(), 1)
  591. self.assertEquals(events, [],
  592. msg="Presence event should not be visible to others"
  593. )
  594. presence = yield self.handler.get_presence_list(
  595. observer_user=self.u_apple, accepted=True)
  596. self.assertEquals(
  597. [
  598. {"observed_user": self.u_banana,
  599. "presence": OFFLINE},
  600. {"observed_user": self.u_clementine,
  601. "presence": OFFLINE},
  602. ],
  603. presence
  604. )
  605. # TODO(paul): Gut-wrenching
  606. banana_set = self.handler._local_pushmap.setdefault("banana", set())
  607. banana_set.add(self.u_apple)
  608. yield self.handler.set_state(self.u_banana, self.u_banana,
  609. {"presence": ONLINE}
  610. )
  611. self.clock.advance_time(2)
  612. presence = yield self.handler.get_presence_list(
  613. observer_user=self.u_apple, accepted=True)
  614. self.assertEquals([
  615. {"observed_user": self.u_banana,
  616. "presence": ONLINE,
  617. "last_active_ago": 2000},
  618. {"observed_user": self.u_clementine,
  619. "presence": OFFLINE},
  620. ], presence)
  621. (events, _) = yield self.event_source.get_new_events_for_user(
  622. self.u_apple, 1, None
  623. )
  624. self.assertEquals(self.event_source.get_current_key(), 2)
  625. self.assertEquals(events,
  626. [
  627. {"type": "m.presence",
  628. "content": {
  629. "user_id": "@banana:test",
  630. "presence": ONLINE,
  631. "last_active_ago": 2000
  632. }},
  633. ]
  634. )
  635. @defer.inlineCallbacks
  636. def test_push_remote(self):
  637. put_json = self.mock_http_client.put_json
  638. put_json.expect_call_and_return(
  639. call("farm",
  640. path=ANY, # Can't guarantee which txn ID will be which
  641. data=_expect_edu("farm", "m.presence",
  642. content={
  643. "push": [
  644. {"user_id": "@apple:test",
  645. "presence": u"online",
  646. "last_active_ago": 0},
  647. ],
  648. }
  649. ),
  650. json_data_callback=ANY,
  651. ),
  652. defer.succeed((200, "OK"))
  653. )
  654. put_json.expect_call_and_return(
  655. call("remote",
  656. path=ANY, # Can't guarantee which txn ID will be which
  657. data=_expect_edu("remote", "m.presence",
  658. content={
  659. "push": [
  660. {"user_id": "@apple:test",
  661. "presence": u"online",
  662. "last_active_ago": 0},
  663. ],
  664. }
  665. ),
  666. json_data_callback=ANY,
  667. ),
  668. defer.succeed((200, "OK"))
  669. )
  670. self.room_members = [self.u_apple, self.u_onion]
  671. self.datastore.set_presence_state.return_value = defer.succeed(
  672. {"state": ONLINE}
  673. )
  674. # TODO(paul): Gut-wrenching
  675. self.handler._user_cachemap[self.u_apple] = UserPresenceCache()
  676. self.handler._user_cachemap[self.u_apple].update(
  677. {"presence": OFFLINE}, serial=0
  678. )
  679. apple_set = self.handler._remote_sendmap.setdefault("apple", set())
  680. apple_set.add(self.u_potato.domain)
  681. yield self.handler.set_state(self.u_apple, self.u_apple,
  682. {"presence": ONLINE}
  683. )
  684. yield put_json.await_calls()
  685. @defer.inlineCallbacks
  686. def test_recv_remote(self):
  687. # TODO(paul): Gut-wrenching
  688. potato_set = self.handler._remote_recvmap.setdefault(self.u_potato,
  689. set())
  690. potato_set.add(self.u_apple)
  691. self.room_members = [self.u_banana, self.u_potato]
  692. self.assertEquals(self.event_source.get_current_key(), 0)
  693. yield self.mock_federation_resource.trigger("PUT",
  694. "/_matrix/federation/v1/send/1000000/",
  695. _make_edu_json("elsewhere", "m.presence",
  696. content={
  697. "push": [
  698. {"user_id": "@potato:remote",
  699. "presence": "online",
  700. "last_active_ago": 1000},
  701. ],
  702. }
  703. )
  704. )
  705. (events, _) = yield self.event_source.get_new_events_for_user(
  706. self.u_apple, 0, None
  707. )
  708. self.assertEquals(self.event_source.get_current_key(), 1)
  709. self.assertEquals(events,
  710. [
  711. {"type": "m.presence",
  712. "content": {
  713. "user_id": "@potato:remote",
  714. "presence": ONLINE,
  715. "last_active_ago": 1000,
  716. }}
  717. ]
  718. )
  719. self.clock.advance_time(2)
  720. state = yield self.handler.get_state(self.u_potato, self.u_apple)
  721. self.assertEquals(
  722. {"presence": ONLINE, "last_active_ago": 3000},
  723. state
  724. )
  725. @defer.inlineCallbacks
  726. def test_join_room_local(self):
  727. self.room_members = [self.u_apple, self.u_banana]
  728. self.assertEquals(self.event_source.get_current_key(), 0)
  729. # TODO(paul): Gut-wrenching
  730. self.handler._user_cachemap[self.u_clementine] = UserPresenceCache()
  731. self.handler._user_cachemap[self.u_clementine].update(
  732. {
  733. "presence": PresenceState.ONLINE,
  734. "last_active": self.clock.time_msec(),
  735. }, self.u_clementine
  736. )
  737. yield self.distributor.fire("user_joined_room", self.u_clementine,
  738. self.room_id
  739. )
  740. self.room_members.append(self.u_clementine)
  741. (events, _) = yield self.event_source.get_new_events_for_user(
  742. self.u_apple, 0, None
  743. )
  744. self.assertEquals(self.event_source.get_current_key(), 1)
  745. self.assertEquals(events,
  746. [
  747. {"type": "m.presence",
  748. "content": {
  749. "user_id": "@clementine:test",
  750. "presence": ONLINE,
  751. "last_active_ago": 0,
  752. }}
  753. ]
  754. )
  755. @defer.inlineCallbacks
  756. def test_join_room_remote(self):
  757. ## Sending local user state to a newly-joined remote user
  758. put_json = self.mock_http_client.put_json
  759. put_json.expect_call_and_return(
  760. call("remote",
  761. path=ANY, # Can't guarantee which txn ID will be which
  762. data=_expect_edu("remote", "m.presence",
  763. content={
  764. "push": [
  765. {"user_id": "@apple:test",
  766. "presence": "online"},
  767. ],
  768. }
  769. ),
  770. json_data_callback=ANY,
  771. ),
  772. defer.succeed((200, "OK"))
  773. )
  774. put_json.expect_call_and_return(
  775. call("remote",
  776. path=ANY, # Can't guarantee which txn ID will be which
  777. data=_expect_edu("remote", "m.presence",
  778. content={
  779. "push": [
  780. {"user_id": "@banana:test",
  781. "presence": "offline"},
  782. ],
  783. }
  784. ),
  785. json_data_callback=ANY,
  786. ),
  787. defer.succeed((200, "OK"))
  788. )
  789. # TODO(paul): Gut-wrenching
  790. self.handler._user_cachemap[self.u_apple] = UserPresenceCache()
  791. self.handler._user_cachemap[self.u_apple].update(
  792. {"presence": PresenceState.ONLINE}, self.u_apple)
  793. self.room_members = [self.u_apple, self.u_banana]
  794. yield self.distributor.fire("user_joined_room", self.u_potato,
  795. self.room_id
  796. )
  797. yield put_json.await_calls()
  798. ## Sending newly-joined local user state to remote users
  799. put_json.expect_call_and_return(
  800. call("remote",
  801. path="/_matrix/federation/v1/send/1000002/",
  802. data=_expect_edu("remote", "m.presence",
  803. content={
  804. "push": [
  805. {"user_id": "@clementine:test",
  806. "presence": "online"},
  807. ],
  808. }
  809. ),
  810. json_data_callback=ANY,
  811. ),
  812. defer.succeed((200, "OK"))
  813. )
  814. self.handler._user_cachemap[self.u_clementine] = UserPresenceCache()
  815. self.handler._user_cachemap[self.u_clementine].update(
  816. {"presence": ONLINE}, self.u_clementine)
  817. self.room_members.append(self.u_potato)
  818. yield self.distributor.fire("user_joined_room", self.u_clementine,
  819. self.room_id
  820. )
  821. put_json.await_calls()
  822. class PresencePollingTestCase(MockedDatastorePresenceTestCase):
  823. """ Tests presence status polling. """
  824. # For this test, we have three local users; apple is watching and is
  825. # watched by the other two, but the others don't watch each other.
  826. # Additionally clementine is watching a remote user.
  827. PRESENCE_LIST = {
  828. 'apple': [ "@banana:test", "@clementine:test" ],
  829. 'banana': [ "@apple:test" ],
  830. 'clementine': [ "@apple:test", "@potato:remote" ],
  831. 'fig': [ "@potato:remote" ],
  832. }
  833. @defer.inlineCallbacks
  834. def setUp(self):
  835. yield super(PresencePollingTestCase, self).setUp()
  836. self.mock_update_client = Mock()
  837. def update(*args,**kwargs):
  838. return defer.succeed(None)
  839. self.mock_update_client.side_effect = update
  840. self.handler.push_update_to_clients = self.mock_update_client
  841. @defer.inlineCallbacks
  842. def test_push_local(self):
  843. # apple goes online
  844. yield self.handler.set_state(
  845. target_user=self.u_apple, auth_user=self.u_apple,
  846. state={"presence": ONLINE}
  847. )
  848. # apple should see both banana and clementine currently offline
  849. self.mock_update_client.assert_has_calls([
  850. call(users_to_push=[self.u_apple],
  851. observed_user=self.u_banana,
  852. statuscache=ANY),
  853. call(users_to_push=[self.u_apple],
  854. observed_user=self.u_clementine,
  855. statuscache=ANY),
  856. ], any_order=True)
  857. # Gut-wrenching tests
  858. self.assertTrue("banana" in self.handler._local_pushmap)
  859. self.assertTrue(self.u_apple in self.handler._local_pushmap["banana"])
  860. self.assertTrue("clementine" in self.handler._local_pushmap)
  861. self.assertTrue(self.u_apple in self.handler._local_pushmap["clementine"])
  862. self.mock_update_client.reset_mock()
  863. # banana goes online
  864. yield self.handler.set_state(
  865. target_user=self.u_banana, auth_user=self.u_banana,
  866. state={"presence": ONLINE}
  867. )
  868. # apple and banana should now both see each other online
  869. self.mock_update_client.assert_has_calls([
  870. call(users_to_push=set([self.u_apple]),
  871. observed_user=self.u_banana,
  872. room_ids=[],
  873. statuscache=ANY),
  874. call(users_to_push=[self.u_banana],
  875. observed_user=self.u_apple,
  876. statuscache=ANY),
  877. ], any_order=True)
  878. self.assertTrue("apple" in self.handler._local_pushmap)
  879. self.assertTrue(self.u_banana in self.handler._local_pushmap["apple"])
  880. self.mock_update_client.reset_mock()
  881. # apple goes offline
  882. yield self.handler.set_state(
  883. target_user=self.u_apple, auth_user=self.u_apple,
  884. state={"presence": OFFLINE}
  885. )
  886. # banana should now be told apple is offline
  887. self.mock_update_client.assert_has_calls([
  888. call(users_to_push=set([self.u_banana, self.u_apple]),
  889. observed_user=self.u_apple,
  890. room_ids=[],
  891. statuscache=ANY),
  892. ], any_order=True)
  893. self.assertFalse("banana" in self.handler._local_pushmap)
  894. self.assertFalse("clementine" in self.handler._local_pushmap)
  895. @defer.inlineCallbacks
  896. def test_remote_poll_send(self):
  897. put_json = self.mock_http_client.put_json
  898. put_json.expect_call_and_return(
  899. call("remote",
  900. path=ANY,
  901. data=_expect_edu("remote", "m.presence",
  902. content={
  903. "poll": [ "@potato:remote" ],
  904. },
  905. ),
  906. json_data_callback=ANY,
  907. ),
  908. defer.succeed((200, "OK"))
  909. )
  910. put_json.expect_call_and_return(
  911. call("remote",
  912. path=ANY,
  913. data=_expect_edu("remote", "m.presence",
  914. content={
  915. "push": [ {
  916. "user_id": "@clementine:test",
  917. "presence": OFFLINE,
  918. }],
  919. },
  920. ),
  921. json_data_callback=ANY,
  922. ),
  923. defer.succeed((200, "OK"))
  924. )
  925. # clementine goes online
  926. yield self.handler.set_state(
  927. target_user=self.u_clementine, auth_user=self.u_clementine,
  928. state={"presence": ONLINE}
  929. )
  930. yield put_json.await_calls()
  931. # Gut-wrenching tests
  932. self.assertTrue(self.u_potato in self.handler._remote_recvmap,
  933. msg="expected potato to be in _remote_recvmap"
  934. )
  935. self.assertTrue(self.u_clementine in
  936. self.handler._remote_recvmap[self.u_potato])
  937. put_json.expect_call_and_return(
  938. call("remote",
  939. path=ANY,
  940. data=_expect_edu("remote", "m.presence",
  941. content={
  942. "push": [ {
  943. "user_id": "@fig:test",
  944. "presence": OFFLINE,
  945. }],
  946. },
  947. ),
  948. json_data_callback=ANY,
  949. ),
  950. defer.succeed((200, "OK"))
  951. )
  952. # fig goes online; shouldn't send a second poll
  953. yield self.handler.set_state(
  954. target_user=self.u_fig, auth_user=self.u_fig,
  955. state={"presence": ONLINE}
  956. )
  957. # reactor.iterate(delay=0)
  958. yield put_json.await_calls()
  959. # fig goes offline
  960. yield self.handler.set_state(
  961. target_user=self.u_fig, auth_user=self.u_fig,
  962. state={"presence": OFFLINE}
  963. )
  964. reactor.iterate(delay=0)
  965. put_json.assert_had_no_calls()
  966. put_json.expect_call_and_return(
  967. call("remote",
  968. path=ANY,
  969. data=_expect_edu("remote", "m.presence",
  970. content={
  971. "unpoll": [ "@potato:remote" ],
  972. },
  973. ),
  974. json_data_callback=ANY,
  975. ),
  976. defer.succeed((200, "OK"))
  977. )
  978. # clementine goes offline
  979. yield self.handler.set_state(
  980. target_user=self.u_clementine, auth_user=self.u_clementine,
  981. state={"presence": OFFLINE}
  982. )
  983. yield put_json.await_calls()
  984. self.assertFalse(self.u_potato in self.handler._remote_recvmap,
  985. msg="expected potato not to be in _remote_recvmap"
  986. )
  987. @defer.inlineCallbacks
  988. def test_remote_poll_receive(self):
  989. put_json = self.mock_http_client.put_json
  990. put_json.expect_call_and_return(
  991. call("remote",
  992. path="/_matrix/federation/v1/send/1000000/",
  993. data=_expect_edu("remote", "m.presence",
  994. content={
  995. "push": [
  996. {"user_id": "@banana:test",
  997. "presence": "offline",
  998. "status_msg": None},
  999. ],
  1000. },
  1001. ),
  1002. json_data_callback=ANY,
  1003. ),
  1004. defer.succeed((200, "OK"))
  1005. )
  1006. yield self.mock_federation_resource.trigger("PUT",
  1007. "/_matrix/federation/v1/send/1000000/",
  1008. _make_edu_json("remote", "m.presence",
  1009. content={
  1010. "poll": [ "@banana:test" ],
  1011. },
  1012. )
  1013. )
  1014. yield put_json.await_calls()
  1015. # Gut-wrenching tests
  1016. self.assertTrue(self.u_banana in self.handler._remote_sendmap)
  1017. yield self.mock_federation_resource.trigger("PUT",
  1018. "/_matrix/federation/v1/send/1000001/",
  1019. _make_edu_json("remote", "m.presence",
  1020. content={
  1021. "unpoll": [ "@banana:test" ],
  1022. }
  1023. )
  1024. )
  1025. # Gut-wrenching tests
  1026. self.assertFalse(self.u_banana in self.handler._remote_sendmap)