test_registration_config.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright 2021 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 synapse.app.homeserver
  15. from synapse.config import ConfigError
  16. from synapse.config.homeserver import HomeServerConfig
  17. from tests.config.utils import ConfigFileTestCase
  18. from tests.utils import default_config
  19. class RegistrationConfigTestCase(ConfigFileTestCase):
  20. def test_session_lifetime_must_not_be_exceeded_by_smaller_lifetimes(self):
  21. """
  22. session_lifetime should logically be larger than, or at least as large as,
  23. all the different token lifetimes.
  24. Test that the user is faced with configuration errors if they make it
  25. smaller, as that configuration doesn't make sense.
  26. """
  27. config_dict = default_config("test")
  28. # First test all the error conditions
  29. with self.assertRaises(ConfigError):
  30. HomeServerConfig().parse_config_dict(
  31. {
  32. "session_lifetime": "30m",
  33. "nonrefreshable_access_token_lifetime": "31m",
  34. **config_dict,
  35. },
  36. "",
  37. "",
  38. )
  39. with self.assertRaises(ConfigError):
  40. HomeServerConfig().parse_config_dict(
  41. {
  42. "session_lifetime": "30m",
  43. "refreshable_access_token_lifetime": "31m",
  44. **config_dict,
  45. },
  46. "",
  47. "",
  48. )
  49. with self.assertRaises(ConfigError):
  50. HomeServerConfig().parse_config_dict(
  51. {
  52. "session_lifetime": "30m",
  53. "refresh_token_lifetime": "31m",
  54. **config_dict,
  55. },
  56. "",
  57. "",
  58. )
  59. # Then test all the fine conditions
  60. HomeServerConfig().parse_config_dict(
  61. {
  62. "session_lifetime": "31m",
  63. "nonrefreshable_access_token_lifetime": "31m",
  64. **config_dict,
  65. },
  66. "",
  67. "",
  68. )
  69. HomeServerConfig().parse_config_dict(
  70. {
  71. "session_lifetime": "31m",
  72. "refreshable_access_token_lifetime": "31m",
  73. **config_dict,
  74. },
  75. "",
  76. "",
  77. )
  78. HomeServerConfig().parse_config_dict(
  79. {"session_lifetime": "31m", "refresh_token_lifetime": "31m", **config_dict},
  80. "",
  81. "",
  82. )
  83. def test_refuse_to_start_if_open_registration_and_no_verification(self):
  84. self.generate_config()
  85. self.add_lines_to_config(
  86. [
  87. " ",
  88. "enable_registration: true",
  89. "registrations_require_3pid: []",
  90. "enable_registration_captcha: false",
  91. "registration_requires_token: false",
  92. ]
  93. )
  94. # Test that allowing open registration without verification raises an error
  95. with self.assertRaises(ConfigError):
  96. synapse.app.homeserver.setup(["-c", self.config_file])