1
0

test_load.py 4.4 KB

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