test_load.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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):
  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):
  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. config = HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
  42. self.assertTrue(
  43. hasattr(config.key, "macaroon_secret_key"),
  44. "Want config to have attr macaroon_secret_key",
  45. )
  46. if len(config.key.macaroon_secret_key) < 5:
  47. self.fail(
  48. "Want macaroon secret key to be string of at least length 5,"
  49. "was: %r" % (config.key.macaroon_secret_key,)
  50. )
  51. def test_load_succeeds_if_macaroon_secret_key_missing(self):
  52. self.generate_config_and_remove_lines_containing("macaroon")
  53. config1 = HomeServerConfig.load_config("", ["-c", self.config_file])
  54. config2 = HomeServerConfig.load_config("", ["-c", self.config_file])
  55. config3 = HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
  56. self.assertEqual(
  57. config1.key.macaroon_secret_key, config2.key.macaroon_secret_key
  58. )
  59. self.assertEqual(
  60. config1.key.macaroon_secret_key, config3.key.macaroon_secret_key
  61. )
  62. def test_disable_registration(self):
  63. self.generate_config()
  64. self.add_lines_to_config(
  65. ["enable_registration: true", "disable_registration: true"]
  66. )
  67. # Check that disable_registration clobbers enable_registration.
  68. config = HomeServerConfig.load_config("", ["-c", self.config_file])
  69. self.assertFalse(config.registration.enable_registration)
  70. config = HomeServerConfig.load_or_generate_config("", ["-c", self.config_file])
  71. self.assertFalse(config.registration.enable_registration)
  72. # Check that either config value is clobbered by the command line.
  73. config = HomeServerConfig.load_or_generate_config(
  74. "", ["-c", self.config_file, "--enable-registration"]
  75. )
  76. self.assertTrue(config.registration.enable_registration)
  77. def test_stats_enabled(self):
  78. self.generate_config_and_remove_lines_containing("enable_metrics")
  79. self.add_lines_to_config(["enable_metrics: true"])
  80. # The default Metrics Flags are off by default.
  81. config = HomeServerConfig.load_config("", ["-c", self.config_file])
  82. self.assertFalse(config.metrics.metrics_flags.known_servers)
  83. def test_depreciated_identity_server_flag_throws_error(self):
  84. self.generate_config()
  85. # Needed to ensure that actual key/value pair added below don't end up on a line with a comment
  86. self.add_lines_to_config([" "])
  87. # Check that presence of "trust_identity_server_for_password" throws config error
  88. self.add_lines_to_config(["trust_identity_server_for_password_resets: true"])
  89. with self.assertRaises(ConfigError):
  90. HomeServerConfig.load_config("", ["-c", self.config_file])