1
0

test_client_reader_shard.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. return config
  26. def test_register_single_worker(self) -> None:
  27. """Test that registration works when using a single generic worker."""
  28. worker_hs = self.make_worker_hs("synapse.app.generic_worker")
  29. site = self._hs_to_site[worker_hs]
  30. channel_1 = make_request(
  31. self.reactor,
  32. site,
  33. "POST",
  34. "register",
  35. {"username": "user", "type": "m.login.password", "password": "bar"},
  36. )
  37. self.assertEqual(channel_1.code, 401)
  38. # Grab the session
  39. session = channel_1.json_body["session"]
  40. # also complete the dummy auth
  41. channel_2 = make_request(
  42. self.reactor,
  43. site,
  44. "POST",
  45. "register",
  46. {"auth": {"session": session, "type": "m.login.dummy"}},
  47. )
  48. self.assertEqual(channel_2.code, 200)
  49. # We're given a registered user.
  50. self.assertEqual(channel_2.json_body["user_id"], "@user:test")
  51. def test_register_multi_worker(self) -> None:
  52. """Test that registration works when using multiple generic workers."""
  53. worker_hs_1 = self.make_worker_hs("synapse.app.generic_worker")
  54. worker_hs_2 = self.make_worker_hs("synapse.app.generic_worker")
  55. site_1 = self._hs_to_site[worker_hs_1]
  56. channel_1 = make_request(
  57. self.reactor,
  58. site_1,
  59. "POST",
  60. "register",
  61. {"username": "user", "type": "m.login.password", "password": "bar"},
  62. )
  63. self.assertEqual(channel_1.code, 401)
  64. # Grab the session
  65. session = channel_1.json_body["session"]
  66. # also complete the dummy auth
  67. site_2 = self._hs_to_site[worker_hs_2]
  68. channel_2 = make_request(
  69. self.reactor,
  70. site_2,
  71. "POST",
  72. "register",
  73. {"auth": {"session": session, "type": "m.login.dummy"}},
  74. )
  75. self.assertEqual(channel_2.code, 200)
  76. # We're given a registered user.
  77. self.assertEqual(channel_2.json_body["user_id"], "@user:test")