test_base.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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 tempfile
  16. from synapse.config import ConfigError
  17. from synapse.util.stringutils import random_string
  18. from tests import unittest
  19. class BaseConfigTestCase(unittest.HomeserverTestCase):
  20. def prepare(self, reactor, clock, hs):
  21. self.hs = hs
  22. def test_loading_missing_templates(self):
  23. # Use a temporary directory that exists on the system, but that isn't likely to
  24. # contain template files
  25. with tempfile.TemporaryDirectory() as tmp_dir:
  26. # Attempt to load an HTML template from our custom template directory
  27. template = self.hs.config.read_templates(["sso_error.html"], (tmp_dir,))[0]
  28. # If no errors, we should've gotten the default template instead
  29. # Render the template
  30. a_random_string = random_string(5)
  31. html_content = template.render({"error_description": a_random_string})
  32. # Check that our string exists in the template
  33. self.assertIn(
  34. a_random_string,
  35. html_content,
  36. "Template file did not contain our test string",
  37. )
  38. def test_loading_custom_templates(self):
  39. # Use a temporary directory that exists on the system
  40. with tempfile.TemporaryDirectory() as tmp_dir:
  41. # Create a temporary bogus template file
  42. with tempfile.NamedTemporaryFile(dir=tmp_dir) as tmp_template:
  43. # Get temporary file's filename
  44. template_filename = os.path.basename(tmp_template.name)
  45. # Write a custom HTML template
  46. contents = b"{{ test_variable }}"
  47. tmp_template.write(contents)
  48. tmp_template.flush()
  49. # Attempt to load the template from our custom template directory
  50. template = (
  51. self.hs.config.read_templates([template_filename], (tmp_dir,))
  52. )[0]
  53. # Render the template
  54. a_random_string = random_string(5)
  55. html_content = template.render({"test_variable": a_random_string})
  56. # Check that our string exists in the template
  57. self.assertIn(
  58. a_random_string,
  59. html_content,
  60. "Template file did not contain our test string",
  61. )
  62. def test_multiple_custom_template_directories(self):
  63. """Tests that directories are searched in the right order if multiple custom
  64. template directories are provided.
  65. """
  66. # Create two temporary directories on the filesystem.
  67. tempdirs = [
  68. tempfile.TemporaryDirectory(),
  69. tempfile.TemporaryDirectory(),
  70. ]
  71. # Create one template in each directory, whose content is the index of the
  72. # directory in the list.
  73. template_filename = "my_template.html.j2"
  74. for i in range(len(tempdirs)):
  75. tempdir = tempdirs[i]
  76. template_path = os.path.join(tempdir.name, template_filename)
  77. with open(template_path, "w") as fp:
  78. fp.write(str(i))
  79. fp.flush()
  80. # Retrieve the template.
  81. template = (
  82. self.hs.config.read_templates(
  83. [template_filename],
  84. (td.name for td in tempdirs),
  85. )
  86. )[0]
  87. # Test that we got the template we dropped in the first directory in the list.
  88. self.assertEqual(template.render(), "0")
  89. # Add another template, this one only in the second directory in the list, so we
  90. # can test that the second directory is still searched into when no matching file
  91. # could be found in the first one.
  92. other_template_name = "my_other_template.html.j2"
  93. other_template_path = os.path.join(tempdirs[1].name, other_template_name)
  94. with open(other_template_path, "w") as fp:
  95. fp.write("hello world")
  96. fp.flush()
  97. # Retrieve the template.
  98. template = (
  99. self.hs.config.read_templates(
  100. [other_template_name],
  101. (td.name for td in tempdirs),
  102. )
  103. )[0]
  104. # Test that the file has the expected content.
  105. self.assertEqual(template.render(), "hello world")
  106. # Cleanup the temporary directories manually since we're not using a context
  107. # manager.
  108. for td in tempdirs:
  109. td.cleanup()
  110. def test_loading_template_from_nonexistent_custom_directory(self):
  111. with self.assertRaises(ConfigError):
  112. self.hs.config.read_templates(
  113. ["some_filename.html"], ("a_nonexistent_directory",)
  114. )