test_load.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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, "macaroon_secret_key"),
  43. "Want config to have attr macaroon_secret_key",
  44. )
  45. if len(config.macaroon_secret_key) < 5:
  46. self.fail(
  47. "Want macaroon secret key to be string of at least length 5,"
  48. "was: %r" % (config.macaroon_secret_key,)
  49. )
  50. config = HomeServerConfig.load_or_generate_config("", ["-c", self.file])
  51. self.assertTrue(
  52. hasattr(config, "macaroon_secret_key"),
  53. "Want config to have attr macaroon_secret_key",
  54. )
  55. if len(config.macaroon_secret_key) < 5:
  56. self.fail(
  57. "Want macaroon secret key to be string of at least length 5,"
  58. "was: %r" % (config.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(config1.macaroon_secret_key, config2.macaroon_secret_key)
  66. self.assertEqual(config1.macaroon_secret_key, config3.macaroon_secret_key)
  67. def test_disable_registration(self):
  68. self.generate_config()
  69. self.add_lines_to_config(
  70. ["enable_registration: true", "disable_registration: true"]
  71. )
  72. # Check that disable_registration clobbers enable_registration.
  73. config = HomeServerConfig.load_config("", ["-c", self.file])
  74. self.assertFalse(config.enable_registration)
  75. config = HomeServerConfig.load_or_generate_config("", ["-c", self.file])
  76. self.assertFalse(config.enable_registration)
  77. # Check that either config value is clobbered by the command line.
  78. config = HomeServerConfig.load_or_generate_config(
  79. "", ["-c", self.file, "--enable-registration"]
  80. )
  81. self.assertTrue(config.enable_registration)
  82. def test_stats_enabled(self):
  83. self.generate_config_and_remove_lines_containing("enable_metrics")
  84. self.add_lines_to_config(["enable_metrics: true"])
  85. # The default Metrics Flags are off by default.
  86. config = HomeServerConfig.load_config("", ["-c", self.file])
  87. self.assertFalse(config.metrics_flags.known_servers)
  88. def generate_config(self):
  89. with redirect_stdout(StringIO()):
  90. HomeServerConfig.load_or_generate_config(
  91. "",
  92. [
  93. "--generate-config",
  94. "-c",
  95. self.file,
  96. "--report-stats=yes",
  97. "-H",
  98. "lemurs.win",
  99. ],
  100. )
  101. def generate_config_and_remove_lines_containing(self, needle):
  102. self.generate_config()
  103. with open(self.file) as f:
  104. contents = f.readlines()
  105. contents = [line for line in contents if needle not in line]
  106. with open(self.file, "w") as f:
  107. f.write("".join(contents))
  108. def add_lines_to_config(self, lines):
  109. with open(self.file, "a") as f:
  110. for line in lines:
  111. f.write(line + "\n")