test_client_reader_shard.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.rest.client.v2_alpha import register
  17. from tests.replication._base import BaseMultiWorkerStreamTestCase
  18. from tests.server import make_request
  19. logger = logging.getLogger(__name__)
  20. class ClientReaderTestCase(BaseMultiWorkerStreamTestCase):
  21. """Test using one or more client readers for registration."""
  22. servlets = [register.register_servlets]
  23. def _get_worker_hs_config(self) -> dict:
  24. config = self.default_config()
  25. config["worker_app"] = "synapse.app.client_reader"
  26. config["worker_replication_host"] = "testserv"
  27. config["worker_replication_http_port"] = "8765"
  28. return config
  29. def test_register_single_worker(self):
  30. """Test that registration works when using a single client reader worker.
  31. """
  32. worker_hs = self.make_worker_hs("synapse.app.client_reader")
  33. site = self._hs_to_site[worker_hs]
  34. channel_1 = make_request(
  35. self.reactor,
  36. site,
  37. "POST",
  38. "register",
  39. {"username": "user", "type": "m.login.password", "password": "bar"},
  40. )
  41. self.assertEqual(channel_1.code, 401)
  42. # Grab the session
  43. session = channel_1.json_body["session"]
  44. # also complete the dummy auth
  45. channel_2 = make_request(
  46. self.reactor,
  47. site,
  48. "POST",
  49. "register",
  50. {"auth": {"session": session, "type": "m.login.dummy"}},
  51. )
  52. self.assertEqual(channel_2.code, 200)
  53. # We're given a registered user.
  54. self.assertEqual(channel_2.json_body["user_id"], "@user:test")
  55. def test_register_multi_worker(self):
  56. """Test that registration works when using multiple client reader workers.
  57. """
  58. worker_hs_1 = self.make_worker_hs("synapse.app.client_reader")
  59. worker_hs_2 = self.make_worker_hs("synapse.app.client_reader")
  60. site_1 = self._hs_to_site[worker_hs_1]
  61. channel_1 = make_request(
  62. self.reactor,
  63. site_1,
  64. "POST",
  65. "register",
  66. {"username": "user", "type": "m.login.password", "password": "bar"},
  67. )
  68. self.assertEqual(channel_1.code, 401)
  69. # Grab the session
  70. session = channel_1.json_body["session"]
  71. # also complete the dummy auth
  72. site_2 = self._hs_to_site[worker_hs_2]
  73. channel_2 = make_request(
  74. self.reactor,
  75. site_2,
  76. "POST",
  77. "register",
  78. {"auth": {"session": session, "type": "m.login.dummy"}},
  79. )
  80. self.assertEqual(channel_2.code, 200)
  81. # We're given a registered user.
  82. self.assertEqual(channel_2.json_body["user_id"], "@user:test")