Commits

Anonymous committed 8814b67

Added a testcase for the templating system.

Comments (0)

Files changed (6)

solace/templating.py

 DEFAULT_THEME_PATH = [path.join(path.dirname(__file__), 'themes')]
 
 
-def split_path_savely(template):
+def split_path_safely(template):
+    """Splits up a path into individual components.  If one of the
+    components is unsafe on the file system, `None` is returned:
+
+    >>> from solace.templating import split_path_safely
+    >>> split_path_safely("foo/bar/baz")
+    ['foo', 'bar', 'baz']
+    >>> split_path_safely("foo/bar/baz/../meh")
+    >>> split_path_safely("/meh/muh")
+    ['meh', 'muh']
+    >>> split_path_safely("/blafasel/.muh")
+    ['blafasel', '.muh']
+    >>> split_path_safely("/blafasel/./x")
+    ['blafasel', 'x']
+    """
     pieces = []
     for piece in template.split('/'):
         if path.sep in piece \
     global _theme
     _theme = None
 
+    # if we have a cache, clear it.  This makes sure that imports no
+    # longer point to the old theme's layout files etc.
+    cache = jinja_env.cache
+    if cache:
+        cache.clear()
+
 
 class Theme(object):
     """Represents a theme."""
 
     def open_resource(self, filename):
         """Opens a resource from the static folder as fd."""
-        pieces = split_path_savely(filename)
+        pieces = split_path_safely(filename)
         if pieces is not None:
             fn = path.join(self.folder, 'static', *pieces)
             if path.isfile(fn):
         if template[:1] == '!':
             template = template[1:]
         else:
-            pieces = split_path_savely(template)
+            pieces = split_path_safely(template)
             if pieces is None:
                 raise TemplateNotFound()
             theme = get_theme()

solace/tests/__init__.py

     """Subclass of the standard test case that creates and drops the database."""
 
     def setUp(self):
-        from solace import database, settings
+        from solace import database, settings, templating
         from solace.application import application
         self.__old_settings = dict(settings.__dict__)
         settings.DATABASE_URI = 'sqlite:///' + TEST_DATABASE
 
 
 def suite():
-    from solace.tests import models, querycount, kb_views, core_views
+    from solace.tests import models, querycount, kb_views, core_views, \
+         templating
     suite = unittest.TestSuite()
     suite.addTest(models.suite())
     suite.addTest(querycount.suite())
     suite.addTest(kb_views.suite())
     suite.addTest(core_views.suite())
+    suite.addTest(templating.suite())
     return suite

solace/tests/templating.py

+# -*- coding: utf-8 -*-
+"""
+    solace.tests.templating
+    ~~~~~~~~~~~~~~~~~~~~~~~
+
+    Tests the templating features.
+
+    :copyright: (c) 2009 by Plurk Inc., see AUTHORS for more details.
+    :license: BSD, see LICENSE for more details.
+"""
+from os.path import dirname, join
+import unittest
+import doctest
+
+from solace.tests import SolaceTestCase
+from solace import templating, models, settings
+
+
+class TemplatingTestCase(SolaceTestCase):
+
+    def test_simple_render(self):
+        """Basic Template rendering."""
+        me = models.User('me', 'me@example.com')
+        rv = templating.render_template('mails/activate_user.txt', user=me,
+                                        confirmation_url='MEH')
+        self.assert_('Hi me!' in rv)
+        self.assert_('MEH' in rv)
+        self.assert_('See you soon on Plurk Solace' in rv)
+
+    def test_theme_switches(self):
+        """Theme based template switches."""
+        settings.THEME_PATH.append(dirname(__file__))
+        settings.THEME = 'test_theme'
+        templating.refresh_theme()
+
+        resp = self.client.get('/en/')
+        self.assert_('I AM THE TEST THEME HEAD' in resp.data)
+        self.assert_('_themes/test_theme' in resp.data)
+
+
+def suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(TemplatingTestCase))
+    suite.addTest(doctest.DocTestSuite(templating))
+    return suite
+
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='suite')

solace/tests/test_theme/static/foo.css

+/* I AM FOO */

solace/tests/test_theme/templates/layout.html

+{% extends "!layout.html" %}
+{% block html_head %}
+{{- super() }}
+{{ theme.packs.default }}
+<!-- I AM THE TEST THEME HEAD BLOCK -->
+{%- endblock %}

solace/tests/test_theme/theme.ini

+[theme]
+name = Test Theme
+
+[packs]
+default = foo.css