1
0

test_client_reader_shard.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from synapse.rest.client import register
  16. from tests.replication._base import BaseMultiWorkerStreamTestCase
  17. from tests.server import make_request
  18. logger = logging.getLogger(__name__)
  19. class ClientReaderTestCase(BaseMultiWorkerStreamTestCase):
  20. """Test using one or more generic workers for registration."""
  21. servlets = [register.register_servlets]
  22. def _get_worker_hs_config(self) -> dict:
  23. config = self.default_config()
  24. config["worker_app"] = "synapse.app.generic_worker"
  25. config["worker_replication_host"] = "testserv"
  26. config["worker_replication_http_port"] = "8765"
  27. return config
  28. def test_register_single_worker(self) -> None:
  29. """Test that registration works when using a single generic worker."""
  30. worker_hs = self.make_worker_hs("synapse.app.generic_worker")
  31. site = self._hs_to_site[worker_hs]
  32. channel_1 = make_request(
  33. self.reactor,
  34. site,
  35. "POST",
  36. "register",
  37. {"username": "user", "type": "m.login.password", "password": "bar"},
  38. )
  39. self.assertEqual(channel_1.code, 401)
  40. # Grab the session
  41. session = channel_1.json_body["session"]
  42. # also complete the dummy auth
  43. channel_2 = make_request(
  44. self.reactor,
  45. site,
  46. "POST",
  47. "register",
  48. {"auth": {"session": session, "type": "m.login.dummy"}},
  49. )
  50. self.assertEqual(channel_2.code, 200)
  51. # We're given a registered user.
  52. self.assertEqual(channel_2.json_body["user_id"], "@user:test")
  53. def test_register_multi_worker(self) -> None:
  54. """Test that registration works when using multiple generic workers."""
  55. worker_hs_1 = self.make_worker_hs("synapse.app.generic_worker")
  56. worker_hs_2 = self.make_worker_hs("synapse.app.generic_worker")
  57. site_1 = self._hs_to_site[worker_hs_1]
  58. channel_1 = make_request(
  59. self.reactor,
  60. site_1,
  61. "POST",
  62. "register",
  63. {"username": "user", "type": "m.login.password", "password": "bar"},
  64. )
  65. self.assertEqual(channel_1.code, 401)
  66. # Grab the session
  67. session = channel_1.json_body["session"]
  68. # also complete the dummy auth
  69. site_2 = self._hs_to_site[worker_hs_2]
  70. channel_2 = make_request(
  71. self.reactor,
  72. site_2,
  73. "POST",
  74. "register",
  75. {"auth": {"session": session, "type": "m.login.dummy"}},
  76. )
  77. self.assertEqual(channel_2.code, 200)
  78. # We're given a registered user.
  79. self.assertEqual(channel_2.json_body["user_id"], "@user:test")