test_load.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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,
  37. "r") as f:
  38. raw = yaml.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",
  71. "disable_registration: true",
  72. ])
  73. # Check that disable_registration clobbers enable_registration.
  74. config = HomeServerConfig.load_config("", ["-c", self.file])
  75. self.assertFalse(config.enable_registration)
  76. config = HomeServerConfig.load_or_generate_config("", ["-c", self.file])
  77. self.assertFalse(config.enable_registration)
  78. # Check that either config value is clobbered by the command line.
  79. config = HomeServerConfig.load_or_generate_config("", [
  80. "-c", self.file, "--enable-registration"
  81. ])
  82. self.assertTrue(config.enable_registration)
  83. def generate_config(self):
  84. HomeServerConfig.load_or_generate_config("", [
  85. "--generate-config",
  86. "-c", self.file,
  87. "--report-stats=yes",
  88. "-H", "lemurs.win"
  89. ])
  90. def generate_config_and_remove_lines_containing(self, needle):
  91. self.generate_config()
  92. with open(self.file, "r") as f:
  93. contents = f.readlines()
  94. contents = [l for l in contents if needle not in l]
  95. with open(self.file, "w") as f:
  96. f.write("".join(contents))
  97. def add_lines_to_config(self, lines):
  98. with open(self.file, "a") as f:
  99. for line in lines:
  100. f.write(line + "\n")