test_load.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright 2016 OpenMarket Ltd
  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 os.path
  15. import shutil
  16. import tempfile
  17. from contextlib import redirect_stdout
  18. from io import StringIO
  19. import yaml
  20. from synapse.config import ConfigError
  21. from synapse.config.homeserver import HomeServerConfig
  22. from tests import unittest
  23. class ConfigLoadingTestCase(unittest.TestCase):
  24. def setUp(self):
  25. self.dir = tempfile.mkdtemp()
  26. self.file = os.path.join(self.dir, "homeserver.yaml")
  27. def tearDown(self):
  28. shutil.rmtree(self.dir)
  29. def test_load_fails_if_server_name_missing(self):
  30. self.generate_config_and_remove_lines_containing("server_name")
  31. with self.assertRaises(ConfigError):
  32. HomeServerConfig.load_config("", ["-c", self.file])
  33. with self.assertRaises(ConfigError):
  34. HomeServerConfig.load_or_generate_config("", ["-c", self.file])
  35. def test_generates_and_loads_macaroon_secret_key(self):
  36. self.generate_config()
  37. with open(self.file) as f:
  38. raw = yaml.safe_load(f)
  39. self.assertIn("macaroon_secret_key", raw)
  40. config = HomeServerConfig.load_config("", ["-c", self.file])
  41. self.assertTrue(
  42. hasattr(config.key, "macaroon_secret_key"),
  43. "Want config to have attr macaroon_secret_key",
  44. )
  45. if len(config.key.macaroon_secret_key) < 5:
  46. self.fail(
  47. "Want macaroon secret key to be string of at least length 5,"
  48. "was: %r" % (config.key.macaroon_secret_key,)
  49. )
  50. config = HomeServerConfig.load_or_generate_config("", ["-c", self.file])
  51. self.assertTrue(
  52. hasattr(config.key, "macaroon_secret_key"),
  53. "Want config to have attr macaroon_secret_key",
  54. )
  55. if len(config.key.macaroon_secret_key) < 5:
  56. self.fail(
  57. "Want macaroon secret key to be string of at least length 5,"
  58. "was: %r" % (config.key.macaroon_secret_key,)
  59. )
  60. def test_load_succeeds_if_macaroon_secret_key_missing(self):
  61. self.generate_config_and_remove_lines_containing("macaroon")
  62. config1 = HomeServerConfig.load_config("", ["-c", self.file])
  63. config2 = HomeServerConfig.load_config("", ["-c", self.file])
  64. config3 = HomeServerConfig.load_or_generate_config("", ["-c", self.file])
  65. self.assertEqual(
  66. config1.key.macaroon_secret_key, config2.key.macaroon_secret_key
  67. )
  68. self.assertEqual(
  69. config1.key.macaroon_secret_key, config3.key.macaroon_secret_key
  70. )
  71. def test_disable_registration(self):
  72. self.generate_config()
  73. self.add_lines_to_config(
  74. ["enable_registration: true", "disable_registration: true"]
  75. )
  76. # Check that disable_registration clobbers enable_registration.
  77. config = HomeServerConfig.load_config("", ["-c", self.file])
  78. self.assertFalse(config.registration.enable_registration)
  79. config = HomeServerConfig.load_or_generate_config("", ["-c", self.file])
  80. self.assertFalse(config.registration.enable_registration)
  81. # Check that either config value is clobbered by the command line.
  82. config = HomeServerConfig.load_or_generate_config(
  83. "", ["-c", self.file, "--enable-registration"]
  84. )
  85. self.assertTrue(config.registration.enable_registration)
  86. def test_stats_enabled(self):
  87. self.generate_config_and_remove_lines_containing("enable_metrics")
  88. self.add_lines_to_config(["enable_metrics: true"])
  89. # The default Metrics Flags are off by default.
  90. config = HomeServerConfig.load_config("", ["-c", self.file])
  91. self.assertFalse(config.metrics.metrics_flags.known_servers)
  92. def generate_config(self):
  93. with redirect_stdout(StringIO()):
  94. HomeServerConfig.load_or_generate_config(
  95. "",
  96. [
  97. "--generate-config",
  98. "-c",
  99. self.file,
  100. "--report-stats=yes",
  101. "-H",
  102. "lemurs.win",
  103. ],
  104. )
  105. def generate_config_and_remove_lines_containing(self, needle):
  106. self.generate_config()
  107. with open(self.file) as f:
  108. contents = f.readlines()
  109. contents = [line for line in contents if needle not in line]
  110. with open(self.file, "w") as f:
  111. f.write("".join(contents))
  112. def add_lines_to_config(self, lines):
  113. with open(self.file, "a") as f:
  114. for line in lines:
  115. f.write(line + "\n")