test_http.py 5.3 KB

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