test_comprehensive_theming.py 3.61 KB
Newer Older
David Baumgold committed
1 2 3 4 5 6
"""Tests of comprehensive theming."""

from django.conf import settings
from django.test import TestCase

from path import path           # pylint: disable=no-name-in-module
7
from django.contrib import staticfiles
David Baumgold committed
8

9
from openedx.core.djangoapps.theming.test_util import with_comprehensive_theme
David Baumgold committed
10 11 12 13 14 15 16 17 18 19
from openedx.core.lib.tempdir import mkdtemp_clean


class TestComprehensiveTheming(TestCase):
    """Test comprehensive theming."""

    def setUp(self):
        super(TestComprehensiveTheming, self).setUp()

        # Clear the internal staticfiles caches, to get test isolation.
20
        staticfiles.finders.get_finder.cache_clear()
David Baumgold committed
21

22
    @with_comprehensive_theme(settings.REPO_ROOT / 'themes/red-theme')
David Baumgold committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    def test_red_footer(self):
        resp = self.client.get('/')
        self.assertEqual(resp.status_code, 200)
        # This string comes from footer.html
        self.assertContains(resp, "super-ugly")
        # This string comes from header.html
        self.assertContains(resp, "This file is only for demonstration, and is horrendous!")

    def test_theme_outside_repo(self):
        # Need to create a temporary theme, and defer decorating the function
        # until it is done, which leads to this strange nested-function style
        # of test.

        # Make a temp directory as a theme.
        tmp_theme = path(mkdtemp_clean())
        template_dir = tmp_theme / "lms/templates"
        template_dir.makedirs()
        with open(template_dir / "footer.html", "w") as footer:
            footer.write("<footer>TEMPORARY THEME</footer>")

43
        @with_comprehensive_theme(tmp_theme)
David Baumgold committed
44 45 46 47 48 49 50 51 52 53 54 55 56
        def do_the_test(self):
            """A function to do the work so we can use the decorator."""
            resp = self.client.get('/')
            self.assertEqual(resp.status_code, 200)
            self.assertContains(resp, "TEMPORARY THEME")

        do_the_test(self)

    def test_theme_adjusts_staticfiles_search_path(self):
        # Test that a theme adds itself to the staticfiles search path.
        before_finders = list(settings.STATICFILES_FINDERS)
        before_dirs = list(settings.STATICFILES_DIRS)

57
        @with_comprehensive_theme(settings.REPO_ROOT / 'themes/red-theme')
David Baumgold committed
58 59 60 61 62 63 64 65 66 67 68 69
        def do_the_test(self):
            """A function to do the work so we can use the decorator."""
            self.assertEqual(list(settings.STATICFILES_FINDERS), before_finders)
            self.assertEqual(settings.STATICFILES_DIRS[0], settings.REPO_ROOT / 'themes/red-theme/lms/static')
            self.assertEqual(settings.STATICFILES_DIRS[1:], before_dirs)

        do_the_test(self)

    def test_default_logo_image(self):
        result = staticfiles.finders.find('images/logo.png')
        self.assertEqual(result, settings.REPO_ROOT / 'lms/static/images/logo.png')

70
    @with_comprehensive_theme(settings.REPO_ROOT / 'themes/red-theme')
David Baumgold committed
71 72 73
    def test_overridden_logo_image(self):
        result = staticfiles.finders.find('images/logo.png')
        self.assertEqual(result, settings.REPO_ROOT / 'themes/red-theme/lms/static/images/logo.png')
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

    def test_default_favicon(self):
        """
        Test default favicon is served if no theme is applied
        """
        result = staticfiles.finders.find('images/favicon.ico')
        self.assertEqual(result, settings.REPO_ROOT / 'lms/static/images/favicon.ico')

    @with_comprehensive_theme(settings.REPO_ROOT / 'themes/red-theme')
    def test_overridden_favicon(self):
        """
        Test comprehensive theme override on favicon image.
        """
        result = staticfiles.finders.find('images/favicon.ico')
        self.assertEqual(result, settings.REPO_ROOT / 'themes/red-theme/lms/static/images/favicon.ico')