test_base.py 5.3 KB

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