test_load.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright 2016 OpenMarket Ltd
  2. # Copyright 2021 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 yaml
  16. from synapse.config import ConfigError
  17. from synapse.config.homeserver import HomeServerConfig
  18. from tests.config.utils import ConfigFileTestCase
  19. class ConfigLoadingFileTestCase(ConfigFileTestCase):
  20. def test_load_fails_if_server_name_missing(self) -> None:
  21. self.generate_config_and_remove_lines_containing("server_name")
  22. with self.assertRaises(ConfigError):
  23. HomeServerConfig.load_config("", ["-c", self.config_file])
  24. with self.assertRaises(ConfigError):
  25. HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
  26. def test_generates_and_loads_macaroon_secret_key(self) -> None:
  27. self.generate_config()
  28. with open(self.config_file) as f:
  29. raw = yaml.safe_load(f)
  30. self.assertIn("macaroon_secret_key", raw)
  31. config = HomeServerConfig.load_config("", ["-c", self.config_file])
  32. self.assertTrue(
  33. hasattr(config.key, "macaroon_secret_key"),
  34. "Want config to have attr macaroon_secret_key",
  35. )
  36. if len(config.key.macaroon_secret_key) < 5:
  37. self.fail(
  38. "Want macaroon secret key to be string of at least length 5,"
  39. "was: %r" % (config.key.macaroon_secret_key,)
  40. )
  41. config2 = HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
  42. assert config2 is not None
  43. self.assertTrue(
  44. hasattr(config2.key, "macaroon_secret_key"),
  45. "Want config to have attr macaroon_secret_key",
  46. )
  47. if len(config2.key.macaroon_secret_key) < 5:
  48. self.fail(
  49. "Want macaroon secret key to be string of at least length 5,"
  50. "was: %r" % (config2.key.macaroon_secret_key,)
  51. )
  52. def test_load_succeeds_if_macaroon_secret_key_missing(self) -> None:
  53. self.generate_config_and_remove_lines_containing("macaroon")
  54. config1 = HomeServerConfig.load_config("", ["-c", self.config_file])
  55. config2 = HomeServerConfig.load_config("", ["-c", self.config_file])
  56. config3 = HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
  57. assert config1 is not None
  58. assert config2 is not None
  59. assert config3 is not None
  60. self.assertEqual(
  61. config1.key.macaroon_secret_key, config2.key.macaroon_secret_key
  62. )
  63. self.assertEqual(
  64. config1.key.macaroon_secret_key, config3.key.macaroon_secret_key
  65. )
  66. def test_disable_registration(self) -> None:
  67. self.generate_config()
  68. self.add_lines_to_config(
  69. ["enable_registration: true", "disable_registration: true"]
  70. )
  71. # Check that disable_registration clobbers enable_registration.
  72. config = HomeServerConfig.load_config("", ["-c", self.config_file])
  73. self.assertFalse(config.registration.enable_registration)
  74. config2 = HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
  75. assert config2 is not None
  76. self.assertFalse(config2.registration.enable_registration)
  77. # Check that either config value is clobbered by the command line.
  78. config3 = HomeServerConfig.load_or_generate_config(
  79. "", ["-c", self.config_file, "--enable-registration"]
  80. )
  81. assert config3 is not None
  82. self.assertTrue(config3.registration.enable_registration)
  83. def test_stats_enabled(self) -> None:
  84. self.generate_config_and_remove_lines_containing("enable_metrics")
  85. self.add_lines_to_config(["enable_metrics: true"])
  86. # The default Metrics Flags are off by default.
  87. config = HomeServerConfig.load_config("", ["-c", self.config_file])
  88. self.assertFalse(config.metrics.metrics_flags.known_servers)
  89. def test_depreciated_identity_server_flag_throws_error(self) -> None:
  90. self.generate_config()
  91. # Needed to ensure that actual key/value pair added below don't end up on a line with a comment
  92. self.add_lines_to_config([" "])
  93. # Check that presence of "trust_identity_server_for_password" throws config error
  94. self.add_lines_to_config(["trust_identity_server_for_password_resets: true"])
  95. with self.assertRaises(ConfigError):
  96. HomeServerConfig.load_config("", ["-c", self.config_file])