1
0

test_base.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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 tempfile
  17. from synapse.config import ConfigError
  18. from synapse.util.stringutils import random_string
  19. from tests import unittest
  20. class BaseConfigTestCase(unittest.HomeserverTestCase):
  21. def prepare(self, reactor, clock, hs):
  22. self.hs = hs
  23. def test_loading_missing_templates(self):
  24. # Use a temporary directory that exists on the system, but that isn't likely to
  25. # contain template files
  26. with tempfile.TemporaryDirectory() as tmp_dir:
  27. # Attempt to load an HTML template from our custom template directory
  28. template = self.hs.config.read_templates(["sso_error.html"], tmp_dir)[0]
  29. # If no errors, we should've gotten the default template instead
  30. # Render the template
  31. a_random_string = random_string(5)
  32. html_content = template.render({"error_description": a_random_string})
  33. # Check that our string exists in the template
  34. self.assertIn(
  35. a_random_string,
  36. html_content,
  37. "Template file did not contain our test string",
  38. )
  39. def test_loading_custom_templates(self):
  40. # Use a temporary directory that exists on the system
  41. with tempfile.TemporaryDirectory() as tmp_dir:
  42. # Create a temporary bogus template file
  43. with tempfile.NamedTemporaryFile(dir=tmp_dir) as tmp_template:
  44. # Get temporary file's filename
  45. template_filename = os.path.basename(tmp_template.name)
  46. # Write a custom HTML template
  47. contents = b"{{ test_variable }}"
  48. tmp_template.write(contents)
  49. tmp_template.flush()
  50. # Attempt to load the template from our custom template directory
  51. template = (
  52. self.hs.config.read_templates([template_filename], tmp_dir)
  53. )[0]
  54. # Render the template
  55. a_random_string = random_string(5)
  56. html_content = template.render({"test_variable": a_random_string})
  57. # Check that our string exists in the template
  58. self.assertIn(
  59. a_random_string,
  60. html_content,
  61. "Template file did not contain our test string",
  62. )
  63. def test_loading_template_from_nonexistent_custom_directory(self):
  64. with self.assertRaises(ConfigError):
  65. self.hs.config.read_templates(
  66. ["some_filename.html"], "a_nonexistent_directory"
  67. )