2016-09-20 09:34:28 +00:00
|
|
|
import io
|
2011-07-06 13:40:56 +00:00
|
|
|
import os
|
2011-06-09 12:24:09 +00:00
|
|
|
import shutil
|
|
|
|
|
import tempfile
|
2011-07-01 13:44:09 +00:00
|
|
|
|
2012-03-30 13:11:57 +00:00
|
|
|
from django.conf import settings as django_settings
|
2011-07-08 12:46:41 +00:00
|
|
|
from django.core.cache.backends.base import BaseCache
|
2011-04-11 17:18:55 +00:00
|
|
|
from django.core.management import call_command
|
2016-09-20 08:31:26 +00:00
|
|
|
from django.template import loader, TemplateDoesNotExist
|
2010-06-24 09:48:08 +00:00
|
|
|
from django.test import TestCase
|
2011-04-11 17:18:55 +00:00
|
|
|
|
2010-06-24 09:48:08 +00:00
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
|
|
2011-07-01 13:44:09 +00:00
|
|
|
from dbtemplates.conf import settings
|
2011-06-15 14:20:30 +00:00
|
|
|
from dbtemplates.models import Template
|
2012-05-06 13:52:17 +00:00
|
|
|
from dbtemplates.utils.cache import get_cache_backend, get_cache_key
|
2011-08-02 19:11:28 +00:00
|
|
|
from dbtemplates.utils.template import (get_template_source,
|
|
|
|
|
check_template_syntax)
|
2011-06-09 12:24:09 +00:00
|
|
|
from dbtemplates.management.commands.sync_templates import (FILES_TO_DATABASE,
|
|
|
|
|
DATABASE_TO_FILES)
|
2010-06-24 09:48:08 +00:00
|
|
|
|
2012-01-08 23:01:17 +00:00
|
|
|
|
2010-06-24 09:48:08 +00:00
|
|
|
class DbTemplatesTestCase(TestCase):
|
|
|
|
|
def setUp(self):
|
2012-01-08 23:01:17 +00:00
|
|
|
self.old_template_loaders = settings.TEMPLATE_LOADERS
|
|
|
|
|
if 'dbtemplates.loader.Loader' not in settings.TEMPLATE_LOADERS:
|
2012-02-25 21:47:05 +00:00
|
|
|
loader.template_source_loaders = None
|
2019-01-26 00:18:32 +00:00
|
|
|
settings.TEMPLATE_LOADERS = list(settings.TEMPLATE_LOADERS) + [
|
|
|
|
|
'dbtemplates.loader.Loader'
|
|
|
|
|
]
|
2012-01-08 23:01:17 +00:00
|
|
|
|
2011-07-01 13:50:04 +00:00
|
|
|
self.site1, created1 = Site.objects.get_or_create(
|
|
|
|
|
domain="example.com", name="example.com")
|
|
|
|
|
self.site2, created2 = Site.objects.get_or_create(
|
|
|
|
|
domain="example.org", name="example.org")
|
|
|
|
|
self.t1, _ = Template.objects.get_or_create(
|
|
|
|
|
name='base.html', content='base')
|
|
|
|
|
self.t2, _ = Template.objects.get_or_create(
|
|
|
|
|
name='sub.html', content='sub')
|
2011-07-01 13:44:09 +00:00
|
|
|
self.t2.sites.add(self.site2)
|
2010-06-24 09:48:08 +00:00
|
|
|
|
2012-01-08 23:01:17 +00:00
|
|
|
def tearDown(self):
|
2012-02-25 21:47:05 +00:00
|
|
|
loader.template_source_loaders = None
|
2012-01-08 23:01:17 +00:00
|
|
|
settings.TEMPLATE_LOADERS = self.old_template_loaders
|
|
|
|
|
|
2017-12-09 17:40:57 +00:00
|
|
|
def test_basics(self):
|
2011-07-01 13:44:09 +00:00
|
|
|
self.assertEqual(list(self.t1.sites.all()), [self.site1])
|
|
|
|
|
self.assertTrue("base" in self.t1.content)
|
2011-07-01 13:50:04 +00:00
|
|
|
self.assertEqual(list(Template.objects.filter(sites=self.site1)),
|
|
|
|
|
[self.t1, self.t2])
|
2011-07-01 13:44:09 +00:00
|
|
|
self.assertEqual(list(self.t2.sites.all()), [self.site1, self.site2])
|
2010-06-24 09:48:08 +00:00
|
|
|
|
2011-07-01 13:44:09 +00:00
|
|
|
def test_empty_sites(self):
|
|
|
|
|
old_add_default_site = settings.DBTEMPLATES_ADD_DEFAULT_SITE
|
2010-09-23 01:33:19 +00:00
|
|
|
try:
|
2011-07-01 13:44:09 +00:00
|
|
|
settings.DBTEMPLATES_ADD_DEFAULT_SITE = False
|
2011-07-01 13:50:04 +00:00
|
|
|
self.t3 = Template.objects.create(
|
|
|
|
|
name='footer.html', content='footer')
|
2011-07-01 13:44:09 +00:00
|
|
|
self.assertEqual(list(self.t3.sites.all()), [])
|
2010-09-23 01:33:19 +00:00
|
|
|
finally:
|
2011-07-01 13:44:09 +00:00
|
|
|
settings.DBTEMPLATES_ADD_DEFAULT_SITE = old_add_default_site
|
|
|
|
|
|
2012-03-30 13:11:57 +00:00
|
|
|
def test_load_templates_sites(self):
|
|
|
|
|
old_add_default_site = settings.DBTEMPLATES_ADD_DEFAULT_SITE
|
|
|
|
|
old_site_id = django_settings.SITE_ID
|
|
|
|
|
try:
|
|
|
|
|
settings.DBTEMPLATES_ADD_DEFAULT_SITE = False
|
|
|
|
|
t_site1 = Template.objects.create(
|
|
|
|
|
name='copyright.html', content='(c) example.com')
|
|
|
|
|
t_site1.sites.add(self.site1)
|
|
|
|
|
t_site2 = Template.objects.create(
|
|
|
|
|
name='copyright.html', content='(c) example.org')
|
|
|
|
|
t_site2.sites.add(self.site2)
|
|
|
|
|
|
|
|
|
|
django_settings.SITE_ID = Site.objects.create(
|
|
|
|
|
domain="example.net", name="example.net").id
|
|
|
|
|
Site.objects.clear_cache()
|
|
|
|
|
|
2012-05-07 07:04:31 +00:00
|
|
|
self.assertRaises(TemplateDoesNotExist,
|
|
|
|
|
loader.get_template, "copyright.html")
|
2012-03-30 13:11:57 +00:00
|
|
|
finally:
|
|
|
|
|
django_settings.SITE_ID = old_site_id
|
|
|
|
|
settings.DBTEMPLATES_ADD_DEFAULT_SITE = old_add_default_site
|
|
|
|
|
|
2011-07-01 13:44:09 +00:00
|
|
|
def test_load_templates(self):
|
2016-09-20 08:31:26 +00:00
|
|
|
result = loader.get_template("base.html").render()
|
2011-07-01 13:44:09 +00:00
|
|
|
self.assertEqual(result, 'base')
|
2016-09-20 08:31:26 +00:00
|
|
|
result2 = loader.get_template("sub.html").render()
|
2011-07-01 13:44:09 +00:00
|
|
|
self.assertEqual(result2, 'sub')
|
2010-09-23 01:33:19 +00:00
|
|
|
|
2010-06-24 09:48:08 +00:00
|
|
|
def test_error_templates_creation(self):
|
|
|
|
|
call_command('create_error_templates', force=True, verbosity=0)
|
|
|
|
|
self.assertEqual(list(Template.objects.filter(sites=self.site1)),
|
2011-07-01 13:44:09 +00:00
|
|
|
list(Template.objects.filter()))
|
2011-07-06 13:40:56 +00:00
|
|
|
self.assertTrue(Template.objects.filter(name='404.html').exists())
|
2010-06-24 09:59:55 +00:00
|
|
|
|
|
|
|
|
def test_automatic_sync(self):
|
2011-04-11 17:18:55 +00:00
|
|
|
admin_base_template = get_template_source('admin/base.html')
|
2010-06-24 09:59:55 +00:00
|
|
|
template = Template.objects.create(name='admin/base.html')
|
|
|
|
|
self.assertEqual(admin_base_template, template.content)
|
2011-07-06 13:40:56 +00:00
|
|
|
|
|
|
|
|
def test_sync_templates(self):
|
2016-09-20 08:31:18 +00:00
|
|
|
old_template_dirs = settings.TEMPLATES[0].get('DIRS', [])
|
2011-06-09 12:24:09 +00:00
|
|
|
temp_template_dir = tempfile.mkdtemp('dbtemplates')
|
|
|
|
|
temp_template_path = os.path.join(temp_template_dir, 'temp_test.html')
|
2016-09-20 09:34:28 +00:00
|
|
|
temp_template = io.open(temp_template_path, 'w', encoding='utf-8')
|
2011-07-06 13:40:56 +00:00
|
|
|
try:
|
2016-09-20 09:46:13 +00:00
|
|
|
temp_template.write(u'temp test')
|
2016-09-20 08:31:18 +00:00
|
|
|
settings.TEMPLATES[0]['DIRS'] = (temp_template_dir,)
|
2015-06-15 22:07:27 +00:00
|
|
|
# these works well if is not settings patched at runtime
|
|
|
|
|
# for supporting django < 1.7 tests we must patch dirs in runtime
|
|
|
|
|
from dbtemplates.management.commands import sync_templates
|
2016-09-20 08:31:18 +00:00
|
|
|
sync_templates.DIRS = settings.TEMPLATES[0]['DIRS']
|
2015-06-15 22:07:27 +00:00
|
|
|
|
2012-01-08 23:01:17 +00:00
|
|
|
self.assertFalse(
|
|
|
|
|
Template.objects.filter(name='temp_test.html').exists())
|
2013-04-04 09:33:00 +00:00
|
|
|
call_command('sync_templates', force=True,
|
|
|
|
|
verbosity=0, overwrite=FILES_TO_DATABASE)
|
2012-01-08 23:01:17 +00:00
|
|
|
self.assertTrue(
|
|
|
|
|
Template.objects.filter(name='temp_test.html').exists())
|
2011-06-09 12:24:09 +00:00
|
|
|
|
|
|
|
|
t = Template.objects.get(name='temp_test.html')
|
2016-09-20 09:46:13 +00:00
|
|
|
t.content = u'temp test modified'
|
2011-06-09 12:24:09 +00:00
|
|
|
t.save()
|
2013-04-04 09:33:00 +00:00
|
|
|
call_command('sync_templates', force=True,
|
|
|
|
|
verbosity=0, overwrite=DATABASE_TO_FILES)
|
2016-09-20 10:02:03 +00:00
|
|
|
self.assertEqual(u'temp test modified',
|
|
|
|
|
io.open(temp_template_path,
|
|
|
|
|
encoding='utf-8').read())
|
2011-06-09 12:24:09 +00:00
|
|
|
|
2012-01-08 23:01:17 +00:00
|
|
|
call_command('sync_templates', force=True, verbosity=0,
|
|
|
|
|
delete=True, overwrite=DATABASE_TO_FILES)
|
2011-06-09 12:24:09 +00:00
|
|
|
self.assertTrue(os.path.exists(temp_template_path))
|
2012-01-08 23:01:17 +00:00
|
|
|
self.assertFalse(
|
|
|
|
|
Template.objects.filter(name='temp_test.html').exists())
|
2011-07-06 13:40:56 +00:00
|
|
|
finally:
|
2011-06-09 12:24:09 +00:00
|
|
|
temp_template.close()
|
2016-09-20 08:31:18 +00:00
|
|
|
settings.TEMPLATES[0]['DIRS'] = old_template_dirs
|
2011-06-09 12:24:09 +00:00
|
|
|
shutil.rmtree(temp_template_dir)
|
2011-07-08 12:46:41 +00:00
|
|
|
|
|
|
|
|
def test_get_cache(self):
|
|
|
|
|
self.assertTrue(isinstance(get_cache_backend(), BaseCache))
|
2011-08-02 19:11:28 +00:00
|
|
|
|
|
|
|
|
def test_check_template_syntax(self):
|
|
|
|
|
bad_template, _ = Template.objects.get_or_create(
|
|
|
|
|
name='bad.html', content='{% if foo %}Bar')
|
|
|
|
|
good_template, _ = Template.objects.get_or_create(
|
|
|
|
|
name='good.html', content='{% if foo %}Bar{% endif %}')
|
2011-08-15 11:05:34 +00:00
|
|
|
self.assertFalse(check_template_syntax(bad_template)[0])
|
|
|
|
|
self.assertTrue(check_template_syntax(good_template)[0])
|
2012-05-06 13:52:17 +00:00
|
|
|
|
|
|
|
|
def test_get_cache_name(self):
|
2012-05-07 12:06:54 +00:00
|
|
|
self.assertEqual(get_cache_key('name with spaces'),
|
|
|
|
|
'dbtemplates::name-with-spaces::1')
|