1
0

test_base.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_loading_template_from_nonexistent_custom_directory(self):
  63. with self.assertRaises(ConfigError):
  64. self.hs.config.read_templates(
  65. ["some_filename.html"], "a_nonexistent_directory"
  66. )