test_http.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector
  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 mock import Mock
  16. from twisted.internet.defer import Deferred
  17. from synapse.rest.client.v1 import admin, login, room
  18. from synapse.util.logcontext import make_deferred_yieldable
  19. from tests.unittest import HomeserverTestCase
  20. try:
  21. from synapse.push.mailer import load_jinja2_templates
  22. except Exception:
  23. load_jinja2_templates = None
  24. class HTTPPusherTests(HomeserverTestCase):
  25. skip = "No Jinja installed" if not load_jinja2_templates else None
  26. servlets = [
  27. admin.register_servlets,
  28. room.register_servlets,
  29. login.register_servlets,
  30. ]
  31. user_id = True
  32. hijack_auth = False
  33. def make_homeserver(self, reactor, clock):
  34. self.push_attempts = []
  35. m = Mock()
  36. def post_json_get_json(url, body):
  37. d = Deferred()
  38. self.push_attempts.append((d, url, body))
  39. return make_deferred_yieldable(d)
  40. m.post_json_get_json = post_json_get_json
  41. config = self.default_config()
  42. config.start_pushers = True
  43. hs = self.setup_test_homeserver(config=config, simple_http_client=m)
  44. return hs
  45. def test_sends_http(self):
  46. """
  47. The HTTP pusher will send pushes for each message to a HTTP endpoint
  48. when configured to do so.
  49. """
  50. # Register the user who gets notified
  51. user_id = self.register_user("user", "pass")
  52. access_token = self.login("user", "pass")
  53. # Register the user who sends the message
  54. other_user_id = self.register_user("otheruser", "pass")
  55. other_access_token = self.login("otheruser", "pass")
  56. # Register the pusher
  57. user_tuple = self.get_success(
  58. self.hs.get_datastore().get_user_by_access_token(access_token)
  59. )
  60. token_id = user_tuple["token_id"]
  61. self.get_success(
  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={"url": "example.com"},
  72. )
  73. )
  74. # Create a room
  75. room = self.helper.create_room_as(user_id, tok=access_token)
  76. # Invite the other person
  77. self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
  78. # The other user joins
  79. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  80. # The other user sends some messages
  81. self.helper.send(room, body="Hi!", tok=other_access_token)
  82. self.helper.send(room, body="There!", tok=other_access_token)
  83. # Get the stream ordering before it gets sent
  84. pushers = self.get_success(
  85. self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
  86. )
  87. self.assertEqual(len(pushers), 1)
  88. last_stream_ordering = pushers[0]["last_stream_ordering"]
  89. # Advance time a bit, so the pusher will register something has happened
  90. self.pump()
  91. # It hasn't succeeded yet, so the stream ordering shouldn't have moved
  92. pushers = self.get_success(
  93. self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
  94. )
  95. self.assertEqual(len(pushers), 1)
  96. self.assertEqual(last_stream_ordering, pushers[0]["last_stream_ordering"])
  97. # One push was attempted to be sent -- it'll be the first message
  98. self.assertEqual(len(self.push_attempts), 1)
  99. self.assertEqual(self.push_attempts[0][1], "example.com")
  100. self.assertEqual(
  101. self.push_attempts[0][2]["notification"]["content"]["body"], "Hi!"
  102. )
  103. # Make the push succeed
  104. self.push_attempts[0][0].callback({})
  105. self.pump()
  106. # The stream ordering has increased
  107. pushers = self.get_success(
  108. self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
  109. )
  110. self.assertEqual(len(pushers), 1)
  111. self.assertTrue(pushers[0]["last_stream_ordering"] > last_stream_ordering)
  112. last_stream_ordering = pushers[0]["last_stream_ordering"]
  113. # Now it'll try and send the second push message, which will be the second one
  114. self.assertEqual(len(self.push_attempts), 2)
  115. self.assertEqual(self.push_attempts[1][1], "example.com")
  116. self.assertEqual(
  117. self.push_attempts[1][2]["notification"]["content"]["body"], "There!"
  118. )
  119. # Make the second push succeed
  120. self.push_attempts[1][0].callback({})
  121. self.pump()
  122. # The stream ordering has increased, again
  123. pushers = self.get_success(
  124. self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
  125. )
  126. self.assertEqual(len(pushers), 1)
  127. self.assertTrue(pushers[0]["last_stream_ordering"] > last_stream_ordering)