diff --git a/common/djangoapps/microsite_configuration/backends/base.py b/common/djangoapps/microsite_configuration/backends/base.py index 20cc25e136..e95f995196 100644 --- a/common/djangoapps/microsite_configuration/backends/base.py +++ b/common/djangoapps/microsite_configuration/backends/base.py @@ -303,7 +303,7 @@ class BaseMicrositeTemplateBackend(object): configuration of microsite on filesystem. """ - def get_template_path(self, relative_path, **kwargs): + def get_template_path(self, template_path, **kwargs): """ Returns a path (string) to a Mako template, which can either be in an override or will just return what is passed in which is expected to be a string @@ -312,7 +312,6 @@ class BaseMicrositeTemplateBackend(object): from microsite_configuration.microsite import get_value as microsite_get_value microsite_template_path = microsite_get_value('template_dir', None) - if not microsite_template_path: microsite_template_path = '/'.join([ settings.MICROSITE_ROOT_DIR, @@ -320,6 +319,7 @@ class BaseMicrositeTemplateBackend(object): 'templates', ]) + relative_path = template_path[1:] if template_path.startswith('/') else template_path search_path = os.path.join(microsite_template_path, relative_path) if os.path.isfile(search_path): path = '/{0}/templates/{1}'.format( @@ -328,7 +328,7 @@ class BaseMicrositeTemplateBackend(object): ) return path else: - return relative_path + return template_path def get_template(self, uri): """ diff --git a/common/djangoapps/microsite_configuration/tests/backends/test_filebased.py b/common/djangoapps/microsite_configuration/tests/backends/test_filebased.py index 99a103d42c..3d219ae209 100644 --- a/common/djangoapps/microsite_configuration/tests/backends/test_filebased.py +++ b/common/djangoapps/microsite_configuration/tests/backends/test_filebased.py @@ -1,14 +1,21 @@ """ Test Microsite filebased backends. """ +import unittest from mock import patch from django.test import TestCase +from django.conf import settings +from django.core.urlresolvers import reverse from microsite_configuration.backends.base import ( BaseMicrositeBackend, + BaseMicrositeTemplateBackend, ) from microsite_configuration import microsite +from student.tests.factories import CourseEnrollmentFactory, UserFactory +from xmodule.modulestore.tests.factories import CourseFactory +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase @patch( @@ -114,3 +121,40 @@ class FilebasedMicrositeBackendTests(TestCase): # if microsite config does not exist default config should be used microsite.set_by_domain('unknown') self.assertEqual(microsite.get_value('university'), 'default_university') + + +@patch( + 'microsite_configuration.microsite.TEMPLATES_BACKEND', + microsite.get_backend( + 'microsite_configuration.backends.filebased.FilebasedMicrositeTemplateBackend', BaseMicrositeTemplateBackend + ) +) +@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') +class FilebasedMicrositeTemplateBackendTests(ModuleStoreTestCase): + """ + Go through and test the FilebasedMicrositeTemplateBackend class + """ + def setUp(self): + super(FilebasedMicrositeTemplateBackendTests, self).setUp() + self.microsite_subdomain = 'testmicrosite' + self.course = CourseFactory.create() + self.user = UserFactory.create(username="Bob", email="bob@example.com", password="edx") + self.client.login(username=self.user.username, password="edx") + + def test_get_template_path(self): + """ + Tests get template path works for both relative and absolute paths. + """ + microsite.set_by_domain(self.microsite_subdomain) + CourseEnrollmentFactory( + course_id=self.course.id, + user=self.user + ) + + response = self.client.get( + reverse('syllabus', args=[unicode(self.course.id)]), + HTTP_HOST=settings.MICROSITE_TEST_HOSTNAME, + ) + + self.assertContains(response, "Microsite relative path template contents") + self.assertContains(response, "Microsite absolute path template contents") diff --git a/common/test/test_microsites/test_microsite/templates/courseware/syllabus.html b/common/test/test_microsites/test_microsite/templates/courseware/syllabus.html new file mode 100644 index 0000000000..284f5e8a05 --- /dev/null +++ b/common/test/test_microsites/test_microsite/templates/courseware/syllabus.html @@ -0,0 +1,5 @@ +## mako +<%namespace name='static' file='/static_content.html'/> +<%include file="${static.get_template_path('courseware/test_relative_path.html')}" /> +<%include file="${static.get_template_path('/courseware/test_absolute_path.html')}" /> + diff --git a/common/test/test_microsites/test_microsite/templates/courseware/test_absolute_path.html b/common/test/test_microsites/test_microsite/templates/courseware/test_absolute_path.html new file mode 100644 index 0000000000..710c751e79 --- /dev/null +++ b/common/test/test_microsites/test_microsite/templates/courseware/test_absolute_path.html @@ -0,0 +1,3 @@ +## mako +<%namespace name='static' file='/static_content.html'/> +
Microsite absolute path template contents
\ No newline at end of file diff --git a/common/test/test_microsites/test_microsite/templates/courseware/test_relative_path.html b/common/test/test_microsites/test_microsite/templates/courseware/test_relative_path.html new file mode 100644 index 0000000000..d010ef3f97 --- /dev/null +++ b/common/test/test_microsites/test_microsite/templates/courseware/test_relative_path.html @@ -0,0 +1,3 @@ +## mako +<%namespace name='static' file='/static_content.html'/> +
Microsite relative path template contents
\ No newline at end of file