test_load.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. def test_generates_and_loads_macaroon_secret_key(self):
  33. self.generate_config()
  34. with open(self.file,
  35. "r") as f:
  36. raw = yaml.load(f)
  37. self.assertIn("macaroon_secret_key", raw)
  38. config = HomeServerConfig.load_config("", ["-c", self.file])
  39. self.assertTrue(
  40. hasattr(config, "macaroon_secret_key"),
  41. "Want config to have attr macaroon_secret_key"
  42. )
  43. if len(config.macaroon_secret_key) < 5:
  44. self.fail(
  45. "Want macaroon secret key to be string of at least length 5,"
  46. "was: %r" % (config.macaroon_secret_key,)
  47. )
  48. def test_load_succeeds_if_macaroon_secret_key_missing(self):
  49. self.generate_config_and_remove_lines_containing("macaroon")
  50. config1 = HomeServerConfig.load_config("", ["-c", self.file])
  51. config2 = HomeServerConfig.load_config("", ["-c", self.file])
  52. self.assertEqual(config1.macaroon_secret_key, config2.macaroon_secret_key)
  53. def test_disable_registration(self):
  54. self.generate_config()
  55. self.add_lines_to_config([
  56. "enable_registration: true",
  57. "disable_registration: true",
  58. ])
  59. # Check that disable_registration clobbers enable_registration.
  60. config = HomeServerConfig.load_config("", ["-c", self.file])
  61. self.assertFalse(config.enable_registration)
  62. # Check that either config value is clobbered by the command line.
  63. config = HomeServerConfig.load_config("", [
  64. "-c", self.file, "--enable-registration"
  65. ])
  66. self.assertTrue(config.enable_registration)
  67. def generate_config(self):
  68. HomeServerConfig.load_config("", [
  69. "--generate-config",
  70. "-c", self.file,
  71. "--report-stats=yes",
  72. "-H", "lemurs.win"
  73. ])
  74. def generate_config_and_remove_lines_containing(self, needle):
  75. self.generate_config()
  76. with open(self.file, "r") as f:
  77. contents = f.readlines()
  78. contents = [l for l in contents if needle not in l]
  79. with open(self.file, "w") as f:
  80. f.write("".join(contents))
  81. def add_lines_to_config(self, lines):
  82. with open(self.file, "a") as f:
  83. for line in lines:
  84. f.write(line + "\n")