1
0

test_client_reader_shard.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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. import logging
  16. from synapse.api.constants import LoginType
  17. from synapse.http.site import SynapseRequest
  18. from synapse.rest.client.v2_alpha import register
  19. from tests.replication._base import BaseMultiWorkerStreamTestCase
  20. from tests.rest.client.v2_alpha.test_auth import DummyRecaptchaChecker
  21. from tests.server import FakeChannel
  22. logger = logging.getLogger(__name__)
  23. class ClientReaderTestCase(BaseMultiWorkerStreamTestCase):
  24. """Base class for tests of the replication streams"""
  25. servlets = [register.register_servlets]
  26. def prepare(self, reactor, clock, hs):
  27. self.recaptcha_checker = DummyRecaptchaChecker(hs)
  28. auth_handler = hs.get_auth_handler()
  29. auth_handler.checkers[LoginType.RECAPTCHA] = self.recaptcha_checker
  30. def _get_worker_hs_config(self) -> dict:
  31. config = self.default_config()
  32. config["worker_app"] = "synapse.app.client_reader"
  33. config["worker_replication_host"] = "testserv"
  34. config["worker_replication_http_port"] = "8765"
  35. return config
  36. def test_register_single_worker(self):
  37. """Test that registration works when using a single client reader worker.
  38. """
  39. worker_hs = self.make_worker_hs("synapse.app.client_reader")
  40. request_1, channel_1 = self.make_request(
  41. "POST",
  42. "register",
  43. {"username": "user", "type": "m.login.password", "password": "bar"},
  44. ) # type: SynapseRequest, FakeChannel
  45. self.render_on_worker(worker_hs, request_1)
  46. self.assertEqual(request_1.code, 401)
  47. # Grab the session
  48. session = channel_1.json_body["session"]
  49. # also complete the dummy auth
  50. request_2, channel_2 = self.make_request(
  51. "POST", "register", {"auth": {"session": session, "type": "m.login.dummy"}}
  52. ) # type: SynapseRequest, FakeChannel
  53. self.render_on_worker(worker_hs, request_2)
  54. self.assertEqual(request_2.code, 200)
  55. # We're given a registered user.
  56. self.assertEqual(channel_2.json_body["user_id"], "@user:test")
  57. def test_register_multi_worker(self):
  58. """Test that registration works when using multiple client reader workers.
  59. """
  60. worker_hs_1 = self.make_worker_hs("synapse.app.client_reader")
  61. worker_hs_2 = self.make_worker_hs("synapse.app.client_reader")
  62. request_1, channel_1 = self.make_request(
  63. "POST",
  64. "register",
  65. {"username": "user", "type": "m.login.password", "password": "bar"},
  66. ) # type: SynapseRequest, FakeChannel
  67. self.render_on_worker(worker_hs_1, request_1)
  68. self.assertEqual(request_1.code, 401)
  69. # Grab the session
  70. session = channel_1.json_body["session"]
  71. # also complete the dummy auth
  72. request_2, channel_2 = self.make_request(
  73. "POST", "register", {"auth": {"session": session, "type": "m.login.dummy"}}
  74. ) # type: SynapseRequest, FakeChannel
  75. self.render_on_worker(worker_hs_2, request_2)
  76. self.assertEqual(request_2.code, 200)
  77. # We're given a registered user.
  78. self.assertEqual(channel_2.json_body["user_id"], "@user:test")