|
| 1 | +# Copyright The IETF Trust 2022, All Rights Reserved |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +from django.template import Context, Origin, Template |
| 4 | +from django.test import override_settings |
| 5 | + |
| 6 | +from ietf.utils.test_utils import TestCase |
| 7 | +import debug # pyflakes: ignore |
| 8 | + |
| 9 | + |
| 10 | +@override_settings(BASE_DIR='/fake/base/') |
| 11 | +class OriginTests(TestCase): |
| 12 | + def test_origin_not_shown_in_production(self): |
| 13 | + template = Template( |
| 14 | + '{% load origin %}{% origin %}', |
| 15 | + origin=Origin('/fake/base/templates/my-template.html'), |
| 16 | + ) |
| 17 | + with override_settings(SERVER_MODE='production'): |
| 18 | + self.assertEqual(template.render(Context()), '') |
| 19 | + |
| 20 | + def test_origin_shown_in_development_and_test(self): |
| 21 | + template = Template( |
| 22 | + '{% load origin %}{% origin %}', |
| 23 | + origin=Origin('/fake/base/templates/my-template.html'), |
| 24 | + ) |
| 25 | + for mode in ['development', 'test']: |
| 26 | + with override_settings(SERVER_MODE=mode): |
| 27 | + output = template.render(Context()) |
| 28 | + self.assertIn('templates/my-template.html', output) |
| 29 | + for component in ['fake', 'base']: |
| 30 | + self.assertNotIn(component, output, 'Reported path should be relative to BASE_DIR') |
| 31 | + |
| 32 | + def test_origin_outside_base_dir(self): |
| 33 | + template = Template( |
| 34 | + '{% load origin %}{% origin %}', |
| 35 | + origin=Origin('/different/templates/my-template.html'), |
| 36 | + ) |
| 37 | + with override_settings(SERVER_MODE='development'): |
| 38 | + for component in ['fake', 'base', 'different', 'templates']: |
| 39 | + output = template.render(Context()) |
| 40 | + self.assertNotIn(component, output, |
| 41 | + 'Full path components should not be revealed in html') |
0 commit comments