test_http.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. # Copyright 2018 New Vector
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Dict, List, Optional, Tuple
  15. from unittest.mock import Mock
  16. from twisted.internet.defer import Deferred
  17. from twisted.test.proto_helpers import MemoryReactor
  18. import synapse.rest.admin
  19. from synapse.logging.context import make_deferred_yieldable
  20. from synapse.push import PusherConfigException
  21. from synapse.rest.client import login, push_rule, receipts, room
  22. from synapse.server import HomeServer
  23. from synapse.types import JsonDict
  24. from synapse.util import Clock
  25. from tests.unittest import HomeserverTestCase, override_config
  26. class HTTPPusherTests(HomeserverTestCase):
  27. servlets = [
  28. synapse.rest.admin.register_servlets_for_client_rest_resource,
  29. room.register_servlets,
  30. login.register_servlets,
  31. receipts.register_servlets,
  32. push_rule.register_servlets,
  33. ]
  34. user_id = True
  35. hijack_auth = False
  36. def default_config(self) -> Dict[str, Any]:
  37. config = super().default_config()
  38. config["start_pushers"] = True
  39. return config
  40. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  41. self.push_attempts: List[Tuple[Deferred, str, dict]] = []
  42. m = Mock()
  43. def post_json_get_json(url, body):
  44. d: Deferred = Deferred()
  45. self.push_attempts.append((d, url, body))
  46. return make_deferred_yieldable(d)
  47. m.post_json_get_json = post_json_get_json
  48. hs = self.setup_test_homeserver(proxied_blacklisted_http_client=m)
  49. return hs
  50. def test_invalid_configuration(self) -> None:
  51. """Invalid push configurations should be rejected."""
  52. # Register the user who gets notified
  53. user_id = self.register_user("user", "pass")
  54. access_token = self.login("user", "pass")
  55. # Register the pusher
  56. user_tuple = self.get_success(
  57. self.hs.get_datastores().main.get_user_by_access_token(access_token)
  58. )
  59. token_id = user_tuple.token_id
  60. def test_data(data: Optional[JsonDict]) -> None:
  61. self.get_failure(
  62. self.hs.get_pusherpool().add_pusher(
  63. user_id=user_id,
  64. access_token=token_id,
  65. kind="http",
  66. app_id="m.http",
  67. app_display_name="HTTP Push Notifications",
  68. device_display_name="pushy push",
  69. pushkey="a@example.com",
  70. lang=None,
  71. data=data,
  72. ),
  73. PusherConfigException,
  74. )
  75. # Data must be provided with a URL.
  76. test_data(None)
  77. test_data({})
  78. test_data({"url": 1})
  79. # A bare domain name isn't accepted.
  80. test_data({"url": "example.com"})
  81. # A URL without a path isn't accepted.
  82. test_data({"url": "http://example.com"})
  83. # A url with an incorrect path isn't accepted.
  84. test_data({"url": "http://example.com/foo"})
  85. def test_sends_http(self) -> None:
  86. """
  87. The HTTP pusher will send pushes for each message to a HTTP endpoint
  88. when configured to do so.
  89. """
  90. # Register the user who gets notified
  91. user_id = self.register_user("user", "pass")
  92. access_token = self.login("user", "pass")
  93. # Register the user who sends the message
  94. other_user_id = self.register_user("otheruser", "pass")
  95. other_access_token = self.login("otheruser", "pass")
  96. # Register the pusher
  97. user_tuple = self.get_success(
  98. self.hs.get_datastores().main.get_user_by_access_token(access_token)
  99. )
  100. token_id = user_tuple.token_id
  101. self.get_success(
  102. self.hs.get_pusherpool().add_pusher(
  103. user_id=user_id,
  104. access_token=token_id,
  105. kind="http",
  106. app_id="m.http",
  107. app_display_name="HTTP Push Notifications",
  108. device_display_name="pushy push",
  109. pushkey="a@example.com",
  110. lang=None,
  111. data={"url": "http://example.com/_matrix/push/v1/notify"},
  112. )
  113. )
  114. # Create a room
  115. room = self.helper.create_room_as(user_id, tok=access_token)
  116. # The other user joins
  117. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  118. # The other user sends some messages
  119. self.helper.send(room, body="Hi!", tok=other_access_token)
  120. self.helper.send(room, body="There!", tok=other_access_token)
  121. # Get the stream ordering before it gets sent
  122. pushers = self.get_success(
  123. self.hs.get_datastores().main.get_pushers_by({"user_name": user_id})
  124. )
  125. pushers = list(pushers)
  126. self.assertEqual(len(pushers), 1)
  127. last_stream_ordering = pushers[0].last_stream_ordering
  128. # Advance time a bit, so the pusher will register something has happened
  129. self.pump()
  130. # It hasn't succeeded yet, so the stream ordering shouldn't have moved
  131. pushers = self.get_success(
  132. self.hs.get_datastores().main.get_pushers_by({"user_name": user_id})
  133. )
  134. pushers = list(pushers)
  135. self.assertEqual(len(pushers), 1)
  136. self.assertEqual(last_stream_ordering, pushers[0].last_stream_ordering)
  137. # One push was attempted to be sent -- it'll be the first message
  138. self.assertEqual(len(self.push_attempts), 1)
  139. self.assertEqual(
  140. self.push_attempts[0][1], "http://example.com/_matrix/push/v1/notify"
  141. )
  142. self.assertEqual(
  143. self.push_attempts[0][2]["notification"]["content"]["body"], "Hi!"
  144. )
  145. # Make the push succeed
  146. self.push_attempts[0][0].callback({})
  147. self.pump()
  148. # The stream ordering has increased
  149. pushers = self.get_success(
  150. self.hs.get_datastores().main.get_pushers_by({"user_name": user_id})
  151. )
  152. pushers = list(pushers)
  153. self.assertEqual(len(pushers), 1)
  154. self.assertTrue(pushers[0].last_stream_ordering > last_stream_ordering)
  155. last_stream_ordering = pushers[0].last_stream_ordering
  156. # Now it'll try and send the second push message, which will be the second one
  157. self.assertEqual(len(self.push_attempts), 2)
  158. self.assertEqual(
  159. self.push_attempts[1][1], "http://example.com/_matrix/push/v1/notify"
  160. )
  161. self.assertEqual(
  162. self.push_attempts[1][2]["notification"]["content"]["body"], "There!"
  163. )
  164. # Make the second push succeed
  165. self.push_attempts[1][0].callback({})
  166. self.pump()
  167. # The stream ordering has increased, again
  168. pushers = self.get_success(
  169. self.hs.get_datastores().main.get_pushers_by({"user_name": user_id})
  170. )
  171. pushers = list(pushers)
  172. self.assertEqual(len(pushers), 1)
  173. self.assertTrue(pushers[0].last_stream_ordering > last_stream_ordering)
  174. def test_sends_high_priority_for_encrypted(self) -> None:
  175. """
  176. The HTTP pusher will send pushes at high priority if they correspond
  177. to an encrypted message.
  178. This will happen both in 1:1 rooms and larger rooms.
  179. """
  180. # Register the user who gets notified
  181. user_id = self.register_user("user", "pass")
  182. access_token = self.login("user", "pass")
  183. # Register the user who sends the message
  184. other_user_id = self.register_user("otheruser", "pass")
  185. other_access_token = self.login("otheruser", "pass")
  186. # Register a third user
  187. yet_another_user_id = self.register_user("yetanotheruser", "pass")
  188. yet_another_access_token = self.login("yetanotheruser", "pass")
  189. # Create a room
  190. room = self.helper.create_room_as(user_id, tok=access_token)
  191. # The other user joins
  192. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  193. # Register the pusher
  194. user_tuple = self.get_success(
  195. self.hs.get_datastores().main.get_user_by_access_token(access_token)
  196. )
  197. token_id = user_tuple.token_id
  198. self.get_success(
  199. self.hs.get_pusherpool().add_pusher(
  200. user_id=user_id,
  201. access_token=token_id,
  202. kind="http",
  203. app_id="m.http",
  204. app_display_name="HTTP Push Notifications",
  205. device_display_name="pushy push",
  206. pushkey="a@example.com",
  207. lang=None,
  208. data={"url": "http://example.com/_matrix/push/v1/notify"},
  209. )
  210. )
  211. # Send an encrypted event
  212. # I know there'd normally be set-up of an encrypted room first
  213. # but this will do for our purposes
  214. self.helper.send_event(
  215. room,
  216. "m.room.encrypted",
  217. content={
  218. "algorithm": "m.megolm.v1.aes-sha2",
  219. "sender_key": "6lImKbzK51MzWLwHh8tUM3UBBSBrLlgup/OOCGTvumM",
  220. "ciphertext": "AwgAErABoRxwpMipdgiwXgu46rHiWQ0DmRj0qUlPrMraBUDk"
  221. "leTnJRljpuc7IOhsYbLY3uo2WI0ab/ob41sV+3JEIhODJPqH"
  222. "TK7cEZaIL+/up9e+dT9VGF5kRTWinzjkeqO8FU5kfdRjm+3w"
  223. "0sy3o1OCpXXCfO+faPhbV/0HuK4ndx1G+myNfK1Nk/CxfMcT"
  224. "BT+zDS/Df/QePAHVbrr9uuGB7fW8ogW/ulnydgZPRluusFGv"
  225. "J3+cg9LoPpZPAmv5Me3ec7NtdlfN0oDZ0gk3TiNkkhsxDG9Y"
  226. "YcNzl78USI0q8+kOV26Bu5dOBpU4WOuojXZHJlP5lMgdzLLl"
  227. "EQ0",
  228. "session_id": "IigqfNWLL+ez/Is+Duwp2s4HuCZhFG9b9CZKTYHtQ4A",
  229. "device_id": "AHQDUSTAAA",
  230. },
  231. tok=other_access_token,
  232. )
  233. # Advance time a bit, so the pusher will register something has happened
  234. self.pump()
  235. # Make the push succeed
  236. self.push_attempts[0][0].callback({})
  237. self.pump()
  238. # Check our push made it with high priority
  239. self.assertEqual(len(self.push_attempts), 1)
  240. self.assertEqual(
  241. self.push_attempts[0][1], "http://example.com/_matrix/push/v1/notify"
  242. )
  243. self.assertEqual(self.push_attempts[0][2]["notification"]["prio"], "high")
  244. # Add yet another person — we want to make this room not a 1:1
  245. # (as encrypted messages in a 1:1 currently have tweaks applied
  246. # so it doesn't properly exercise the condition of all encrypted
  247. # messages need to be high).
  248. self.helper.join(
  249. room=room, user=yet_another_user_id, tok=yet_another_access_token
  250. )
  251. # Check no push notifications are sent regarding the membership changes
  252. # (that would confuse the test)
  253. self.pump()
  254. self.assertEqual(len(self.push_attempts), 1)
  255. # Send another encrypted event
  256. self.helper.send_event(
  257. room,
  258. "m.room.encrypted",
  259. content={
  260. "ciphertext": "AwgAEoABtEuic/2DF6oIpNH+q/PonzlhXOVho8dTv0tzFr5m"
  261. "9vTo50yabx3nxsRlP2WxSqa8I07YftP+EKWCWJvTkg6o7zXq"
  262. "6CK+GVvLQOVgK50SfvjHqJXN+z1VEqj+5mkZVN/cAgJzoxcH"
  263. "zFHkwDPJC8kQs47IHd8EO9KBUK4v6+NQ1uE/BIak4qAf9aS/"
  264. "kI+f0gjn9IY9K6LXlah82A/iRyrIrxkCkE/n0VfvLhaWFecC"
  265. "sAWTcMLoF6fh1Jpke95mljbmFSpsSd/eEQw",
  266. "device_id": "SRCFTWTHXO",
  267. "session_id": "eMA+bhGczuTz1C5cJR1YbmrnnC6Goni4lbvS5vJ1nG4",
  268. "algorithm": "m.megolm.v1.aes-sha2",
  269. "sender_key": "rC/XSIAiYrVGSuaHMop8/pTZbku4sQKBZwRwukgnN1c",
  270. },
  271. tok=other_access_token,
  272. )
  273. # Advance time a bit, so the pusher will register something has happened
  274. self.pump()
  275. self.assertEqual(len(self.push_attempts), 2)
  276. self.assertEqual(
  277. self.push_attempts[1][1], "http://example.com/_matrix/push/v1/notify"
  278. )
  279. self.assertEqual(self.push_attempts[1][2]["notification"]["prio"], "high")
  280. def test_sends_high_priority_for_one_to_one_only(self) -> None:
  281. """
  282. The HTTP pusher will send pushes at high priority if they correspond
  283. to a message in a one-to-one room.
  284. """
  285. # Register the user who gets notified
  286. user_id = self.register_user("user", "pass")
  287. access_token = self.login("user", "pass")
  288. # Register the user who sends the message
  289. other_user_id = self.register_user("otheruser", "pass")
  290. other_access_token = self.login("otheruser", "pass")
  291. # Register a third user
  292. yet_another_user_id = self.register_user("yetanotheruser", "pass")
  293. yet_another_access_token = self.login("yetanotheruser", "pass")
  294. # Create a room
  295. room = self.helper.create_room_as(user_id, tok=access_token)
  296. # The other user joins
  297. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  298. # Register the pusher
  299. user_tuple = self.get_success(
  300. self.hs.get_datastores().main.get_user_by_access_token(access_token)
  301. )
  302. token_id = user_tuple.token_id
  303. self.get_success(
  304. self.hs.get_pusherpool().add_pusher(
  305. user_id=user_id,
  306. access_token=token_id,
  307. kind="http",
  308. app_id="m.http",
  309. app_display_name="HTTP Push Notifications",
  310. device_display_name="pushy push",
  311. pushkey="a@example.com",
  312. lang=None,
  313. data={"url": "http://example.com/_matrix/push/v1/notify"},
  314. )
  315. )
  316. # Send a message
  317. self.helper.send(room, body="Hi!", tok=other_access_token)
  318. # Advance time a bit, so the pusher will register something has happened
  319. self.pump()
  320. # Make the push succeed
  321. self.push_attempts[0][0].callback({})
  322. self.pump()
  323. # Check our push made it with high priority — this is a one-to-one room
  324. self.assertEqual(len(self.push_attempts), 1)
  325. self.assertEqual(
  326. self.push_attempts[0][1], "http://example.com/_matrix/push/v1/notify"
  327. )
  328. self.assertEqual(self.push_attempts[0][2]["notification"]["prio"], "high")
  329. # Yet another user joins
  330. self.helper.join(
  331. room=room, user=yet_another_user_id, tok=yet_another_access_token
  332. )
  333. # Check no push notifications are sent regarding the membership changes
  334. # (that would confuse the test)
  335. self.pump()
  336. self.assertEqual(len(self.push_attempts), 1)
  337. # Send another event
  338. self.helper.send(room, body="Welcome!", tok=other_access_token)
  339. # Advance time a bit, so the pusher will register something has happened
  340. self.pump()
  341. self.assertEqual(len(self.push_attempts), 2)
  342. self.assertEqual(
  343. self.push_attempts[1][1], "http://example.com/_matrix/push/v1/notify"
  344. )
  345. # check that this is low-priority
  346. self.assertEqual(self.push_attempts[1][2]["notification"]["prio"], "low")
  347. def test_sends_high_priority_for_mention(self) -> None:
  348. """
  349. The HTTP pusher will send pushes at high priority if they correspond
  350. to a message containing the user's display name.
  351. """
  352. # Register the user who gets notified
  353. user_id = self.register_user("user", "pass")
  354. access_token = self.login("user", "pass")
  355. # Register the user who sends the message
  356. other_user_id = self.register_user("otheruser", "pass")
  357. other_access_token = self.login("otheruser", "pass")
  358. # Register a third user
  359. yet_another_user_id = self.register_user("yetanotheruser", "pass")
  360. yet_another_access_token = self.login("yetanotheruser", "pass")
  361. # Create a room
  362. room = self.helper.create_room_as(user_id, tok=access_token)
  363. # The other users join
  364. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  365. self.helper.join(
  366. room=room, user=yet_another_user_id, tok=yet_another_access_token
  367. )
  368. # Register the pusher
  369. user_tuple = self.get_success(
  370. self.hs.get_datastores().main.get_user_by_access_token(access_token)
  371. )
  372. token_id = user_tuple.token_id
  373. self.get_success(
  374. self.hs.get_pusherpool().add_pusher(
  375. user_id=user_id,
  376. access_token=token_id,
  377. kind="http",
  378. app_id="m.http",
  379. app_display_name="HTTP Push Notifications",
  380. device_display_name="pushy push",
  381. pushkey="a@example.com",
  382. lang=None,
  383. data={"url": "http://example.com/_matrix/push/v1/notify"},
  384. )
  385. )
  386. # Send a message
  387. self.helper.send(room, body="Oh, user, hello!", tok=other_access_token)
  388. # Advance time a bit, so the pusher will register something has happened
  389. self.pump()
  390. # Make the push succeed
  391. self.push_attempts[0][0].callback({})
  392. self.pump()
  393. # Check our push made it with high priority
  394. self.assertEqual(len(self.push_attempts), 1)
  395. self.assertEqual(
  396. self.push_attempts[0][1], "http://example.com/_matrix/push/v1/notify"
  397. )
  398. self.assertEqual(self.push_attempts[0][2]["notification"]["prio"], "high")
  399. # Send another event, this time with no mention
  400. self.helper.send(room, body="Are you there?", tok=other_access_token)
  401. # Advance time a bit, so the pusher will register something has happened
  402. self.pump()
  403. self.assertEqual(len(self.push_attempts), 2)
  404. self.assertEqual(
  405. self.push_attempts[1][1], "http://example.com/_matrix/push/v1/notify"
  406. )
  407. # check that this is low-priority
  408. self.assertEqual(self.push_attempts[1][2]["notification"]["prio"], "low")
  409. def test_sends_high_priority_for_atroom(self) -> None:
  410. """
  411. The HTTP pusher will send pushes at high priority if they correspond
  412. to a message that contains @room.
  413. """
  414. # Register the user who gets notified
  415. user_id = self.register_user("user", "pass")
  416. access_token = self.login("user", "pass")
  417. # Register the user who sends the message
  418. other_user_id = self.register_user("otheruser", "pass")
  419. other_access_token = self.login("otheruser", "pass")
  420. # Register a third user
  421. yet_another_user_id = self.register_user("yetanotheruser", "pass")
  422. yet_another_access_token = self.login("yetanotheruser", "pass")
  423. # Create a room (as other_user so the power levels are compatible with
  424. # other_user sending @room).
  425. room = self.helper.create_room_as(other_user_id, tok=other_access_token)
  426. # The other users join
  427. self.helper.join(room=room, user=user_id, tok=access_token)
  428. self.helper.join(
  429. room=room, user=yet_another_user_id, tok=yet_another_access_token
  430. )
  431. # Register the pusher
  432. user_tuple = self.get_success(
  433. self.hs.get_datastores().main.get_user_by_access_token(access_token)
  434. )
  435. token_id = user_tuple.token_id
  436. self.get_success(
  437. self.hs.get_pusherpool().add_pusher(
  438. user_id=user_id,
  439. access_token=token_id,
  440. kind="http",
  441. app_id="m.http",
  442. app_display_name="HTTP Push Notifications",
  443. device_display_name="pushy push",
  444. pushkey="a@example.com",
  445. lang=None,
  446. data={"url": "http://example.com/_matrix/push/v1/notify"},
  447. )
  448. )
  449. # Send a message
  450. self.helper.send(
  451. room,
  452. body="@room eeek! There's a spider on the table!",
  453. tok=other_access_token,
  454. )
  455. # Advance time a bit, so the pusher will register something has happened
  456. self.pump()
  457. # Make the push succeed
  458. self.push_attempts[0][0].callback({})
  459. self.pump()
  460. # Check our push made it with high priority
  461. self.assertEqual(len(self.push_attempts), 1)
  462. self.assertEqual(
  463. self.push_attempts[0][1], "http://example.com/_matrix/push/v1/notify"
  464. )
  465. self.assertEqual(self.push_attempts[0][2]["notification"]["prio"], "high")
  466. # Send another event, this time as someone without the power of @room
  467. self.helper.send(
  468. room, body="@room the spider is gone", tok=yet_another_access_token
  469. )
  470. # Advance time a bit, so the pusher will register something has happened
  471. self.pump()
  472. self.assertEqual(len(self.push_attempts), 2)
  473. self.assertEqual(
  474. self.push_attempts[1][1], "http://example.com/_matrix/push/v1/notify"
  475. )
  476. # check that this is low-priority
  477. self.assertEqual(self.push_attempts[1][2]["notification"]["prio"], "low")
  478. def test_push_unread_count_group_by_room(self) -> None:
  479. """
  480. The HTTP pusher will group unread count by number of unread rooms.
  481. """
  482. # Carry out common push count tests and setup
  483. self._test_push_unread_count()
  484. # Carry out our option-value specific test
  485. #
  486. # This push should still only contain an unread count of 1 (for 1 unread room)
  487. self._check_push_attempt(6, 1)
  488. @override_config({"push": {"group_unread_count_by_room": False}})
  489. def test_push_unread_count_message_count(self) -> None:
  490. """
  491. The HTTP pusher will send the total unread message count.
  492. """
  493. # Carry out common push count tests and setup
  494. self._test_push_unread_count()
  495. # Carry out our option-value specific test
  496. #
  497. # We're counting every unread message, so there should now be 3 since the
  498. # last read receipt
  499. self._check_push_attempt(6, 3)
  500. def _test_push_unread_count(self) -> None:
  501. """
  502. Tests that the correct unread count appears in sent push notifications
  503. Note that:
  504. * Sending messages will cause push notifications to go out to relevant users
  505. * Sending a read receipt will cause the HTTP pusher to check whether the unread
  506. count has changed since the last push notification. If so, a "badge update"
  507. notification goes out to the user that sent the receipt
  508. """
  509. # Register the user who gets notified
  510. user_id = self.register_user("user", "pass")
  511. access_token = self.login("user", "pass")
  512. # Register the user who sends the message
  513. other_user_id = self.register_user("other_user", "pass")
  514. other_access_token = self.login("other_user", "pass")
  515. # Create a room (as other_user)
  516. room_id = self.helper.create_room_as(other_user_id, tok=other_access_token)
  517. # The user to get notified joins
  518. self.helper.join(room=room_id, user=user_id, tok=access_token)
  519. # Register the pusher
  520. user_tuple = self.get_success(
  521. self.hs.get_datastores().main.get_user_by_access_token(access_token)
  522. )
  523. token_id = user_tuple.token_id
  524. self.get_success(
  525. self.hs.get_pusherpool().add_pusher(
  526. user_id=user_id,
  527. access_token=token_id,
  528. kind="http",
  529. app_id="m.http",
  530. app_display_name="HTTP Push Notifications",
  531. device_display_name="pushy push",
  532. pushkey="a@example.com",
  533. lang=None,
  534. data={"url": "http://example.com/_matrix/push/v1/notify"},
  535. )
  536. )
  537. # Send a message
  538. response = self.helper.send(
  539. room_id, body="Hello there!", tok=other_access_token
  540. )
  541. # To get an unread count, the user who is getting notified has to have a read
  542. # position in the room. We'll set the read position to this event in a moment
  543. first_message_event_id = response["event_id"]
  544. expected_push_attempts = 1
  545. self._check_push_attempt(expected_push_attempts, 0)
  546. self._send_read_request(access_token, first_message_event_id, room_id)
  547. # Unread count has not changed. Therefore, ensure that read request does not
  548. # trigger a push notification.
  549. self.assertEqual(len(self.push_attempts), 1)
  550. # Send another message
  551. response2 = self.helper.send(
  552. room_id, body="How's the weather today?", tok=other_access_token
  553. )
  554. second_message_event_id = response2["event_id"]
  555. expected_push_attempts += 1
  556. self._check_push_attempt(expected_push_attempts, 1)
  557. self._send_read_request(access_token, second_message_event_id, room_id)
  558. expected_push_attempts += 1
  559. self._check_push_attempt(expected_push_attempts, 0)
  560. # If we're grouping by room, sending more messages shouldn't increase the
  561. # unread count, as they're all being sent in the same room. Otherwise, it
  562. # should. Therefore, the last call to _check_push_attempt is done in the
  563. # caller method.
  564. self.helper.send(room_id, body="Hello?", tok=other_access_token)
  565. expected_push_attempts += 1
  566. self._advance_time_and_make_push_succeed(expected_push_attempts)
  567. self.helper.send(room_id, body="Hello??", tok=other_access_token)
  568. expected_push_attempts += 1
  569. self._advance_time_and_make_push_succeed(expected_push_attempts)
  570. self.helper.send(room_id, body="HELLO???", tok=other_access_token)
  571. def _advance_time_and_make_push_succeed(self, expected_push_attempts: int) -> None:
  572. self.pump()
  573. self.push_attempts[expected_push_attempts - 1][0].callback({})
  574. def _check_push_attempt(
  575. self, expected_push_attempts: int, expected_unread_count_last_push: int
  576. ) -> None:
  577. """
  578. Makes sure that the last expected push attempt succeeds and checks whether
  579. it contains the expected unread count.
  580. """
  581. self._advance_time_and_make_push_succeed(expected_push_attempts)
  582. # Check our push made it
  583. self.assertEqual(len(self.push_attempts), expected_push_attempts)
  584. _, push_url, push_body = self.push_attempts[expected_push_attempts - 1]
  585. self.assertEqual(
  586. push_url,
  587. "http://example.com/_matrix/push/v1/notify",
  588. )
  589. # Check that the unread count for the room is 0
  590. #
  591. # The unread count is zero as the user has no read receipt in the room yet
  592. self.assertEqual(
  593. push_body["notification"]["counts"]["unread"],
  594. expected_unread_count_last_push,
  595. )
  596. def _send_read_request(
  597. self, access_token: str, message_event_id: str, room_id: str
  598. ) -> None:
  599. # Now set the user's read receipt position to the first event
  600. #
  601. # This will actually trigger a new notification to be sent out so that
  602. # even if the user does not receive another message, their unread
  603. # count goes down
  604. channel = self.make_request(
  605. "POST",
  606. "/rooms/%s/receipt/m.read/%s" % (room_id, message_event_id),
  607. {},
  608. access_token=access_token,
  609. )
  610. self.assertEqual(channel.code, 200, channel.json_body)
  611. def _make_user_with_pusher(self, username: str) -> Tuple[str, str]:
  612. user_id = self.register_user(username, "pass")
  613. access_token = self.login(username, "pass")
  614. # Register the pusher
  615. user_tuple = self.get_success(
  616. self.hs.get_datastores().main.get_user_by_access_token(access_token)
  617. )
  618. token_id = user_tuple.token_id
  619. self.get_success(
  620. self.hs.get_pusherpool().add_pusher(
  621. user_id=user_id,
  622. access_token=token_id,
  623. kind="http",
  624. app_id="m.http",
  625. app_display_name="HTTP Push Notifications",
  626. device_display_name="pushy push",
  627. pushkey="a@example.com",
  628. lang=None,
  629. data={"url": "http://example.com/_matrix/push/v1/notify"},
  630. )
  631. )
  632. return user_id, access_token
  633. def test_dont_notify_rule_overrides_message(self) -> None:
  634. """
  635. The override push rule will suppress notification
  636. """
  637. user_id, access_token = self._make_user_with_pusher("user")
  638. other_user_id, other_access_token = self._make_user_with_pusher("otheruser")
  639. # Create a room
  640. room = self.helper.create_room_as(user_id, tok=access_token)
  641. # Disable user notifications for this room -> user
  642. body = {
  643. "conditions": [{"kind": "event_match", "key": "room_id", "pattern": room}],
  644. "actions": ["dont_notify"],
  645. }
  646. channel = self.make_request(
  647. "PUT",
  648. "/pushrules/global/override/best.friend",
  649. body,
  650. access_token=access_token,
  651. )
  652. self.assertEqual(channel.code, 200)
  653. # Check we start with no pushes
  654. self.assertEqual(len(self.push_attempts), 0)
  655. # The other user joins
  656. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  657. # The other user sends a message (ignored by dont_notify push rule set above)
  658. self.helper.send(room, body="Hi!", tok=other_access_token)
  659. self.assertEqual(len(self.push_attempts), 0)
  660. # The user sends a message back (sends a notification)
  661. self.helper.send(room, body="Hello", tok=access_token)
  662. self.assertEqual(len(self.push_attempts), 1)