1
0

test_http.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, proxied_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({"user_name": user_id})
  82. )
  83. pushers = list(pushers)
  84. self.assertEqual(len(pushers), 1)
  85. last_stream_ordering = pushers[0]["last_stream_ordering"]
  86. # Advance time a bit, so the pusher will register something has happened
  87. self.pump()
  88. # It hasn't succeeded yet, so the stream ordering shouldn't have moved
  89. pushers = self.get_success(
  90. self.hs.get_datastore().get_pushers_by({"user_name": user_id})
  91. )
  92. pushers = list(pushers)
  93. self.assertEqual(len(pushers), 1)
  94. self.assertEqual(last_stream_ordering, pushers[0]["last_stream_ordering"])
  95. # One push was attempted to be sent -- it'll be the first message
  96. self.assertEqual(len(self.push_attempts), 1)
  97. self.assertEqual(self.push_attempts[0][1], "example.com")
  98. self.assertEqual(
  99. self.push_attempts[0][2]["notification"]["content"]["body"], "Hi!"
  100. )
  101. # Make the push succeed
  102. self.push_attempts[0][0].callback({})
  103. self.pump()
  104. # The stream ordering has increased
  105. pushers = self.get_success(
  106. self.hs.get_datastore().get_pushers_by({"user_name": user_id})
  107. )
  108. pushers = list(pushers)
  109. self.assertEqual(len(pushers), 1)
  110. self.assertTrue(pushers[0]["last_stream_ordering"] > last_stream_ordering)
  111. last_stream_ordering = pushers[0]["last_stream_ordering"]
  112. # Now it'll try and send the second push message, which will be the second one
  113. self.assertEqual(len(self.push_attempts), 2)
  114. self.assertEqual(self.push_attempts[1][1], "example.com")
  115. self.assertEqual(
  116. self.push_attempts[1][2]["notification"]["content"]["body"], "There!"
  117. )
  118. # Make the second push succeed
  119. self.push_attempts[1][0].callback({})
  120. self.pump()
  121. # The stream ordering has increased, again
  122. pushers = self.get_success(
  123. self.hs.get_datastore().get_pushers_by({"user_name": user_id})
  124. )
  125. pushers = list(pushers)
  126. self.assertEqual(len(pushers), 1)
  127. self.assertTrue(pushers[0]["last_stream_ordering"] > last_stream_ordering)