test_filebased.py 5.66 KB
Newer Older
1 2 3
"""
Test Microsite filebased backends.
"""
4
import unittest
5 6 7
from mock import patch

from django.test import TestCase
8 9
from django.conf import settings
from django.core.urlresolvers import reverse
10 11 12

from microsite_configuration.backends.base import (
    BaseMicrositeBackend,
13
    BaseMicrositeTemplateBackend,
14 15
)
from microsite_configuration import microsite
16 17 18
from student.tests.factories import CourseEnrollmentFactory, UserFactory
from xmodule.modulestore.tests.factories import CourseFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
19 20 21 22 23 24 25 26 27 28 29 30 31 32


@patch(
    'microsite_configuration.microsite.BACKEND',
    microsite.get_backend(
        'microsite_configuration.backends.filebased.FilebasedMicrositeBackend', BaseMicrositeBackend
    )
)
class FilebasedMicrositeBackendTests(TestCase):
    """
    Go through and test the FilebasedMicrositeBackend class
    """
    def setUp(self):
        super(FilebasedMicrositeBackendTests, self).setUp()
33
        self.microsite_subdomain = 'test-site'
34 35 36 37 38 39 40 41 42 43

    def tearDown(self):
        super(FilebasedMicrositeBackendTests, self).tearDown()
        microsite.clear()

    def test_get_value(self):
        """
        Tests microsite.get_value works as expected.
        """
        microsite.set_by_domain(self.microsite_subdomain)
44
        self.assertEqual(microsite.get_value('platform_name'), 'Test Site')
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

    def test_is_request_in_microsite(self):
        """
        Tests microsite.is_request_in_microsite works as expected.
        """
        microsite.set_by_domain(self.microsite_subdomain)
        self.assertTrue(microsite.is_request_in_microsite())

    def test_has_override_value(self):
        """
        Tests microsite.has_override_value works as expected.
        """
        microsite.set_by_domain(self.microsite_subdomain)
        self.assertTrue(microsite.has_override_value('platform_name'))

    def test_get_value_for_org(self):
        """
        Tests microsite.get_value_for_org works as expected.
        """
        microsite.set_by_domain(self.microsite_subdomain)
        self.assertEqual(
66 67
            microsite.get_value_for_org('TestSiteX', 'platform_name'),
            'Test Site'
68 69 70 71 72 73
        )

        # if no config is set
        microsite.clear()
        with patch('django.conf.settings.MICROSITE_CONFIGURATION', False):
            self.assertEqual(
74
                microsite.get_value_for_org('TestSiteX', 'platform_name', 'Default Value'),
75 76 77 78 79 80 81 82 83 84
                'Default Value'
            )

    def test_get_all_orgs(self):
        """
        Tests microsite.get_all_orgs works as expected.
        """
        microsite.set_by_domain(self.microsite_subdomain)
        self.assertEqual(
            microsite.get_all_orgs(),
85
            set(['TestSiteX', 'LogistrationX'])
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        )

        # if no config is set
        microsite.clear()
        with patch('django.conf.settings.MICROSITE_CONFIGURATION', False):
            self.assertEqual(
                microsite.get_all_orgs(),
                set()
            )

    def test_clear(self):
        """
        Tests microsite.clear works as expected.
        """
        microsite.set_by_domain(self.microsite_subdomain)
        self.assertEqual(
            microsite.get_value('platform_name'),
103
            'Test Site'
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        )
        microsite.clear()
        self.assertIsNone(microsite.get_value('platform_name'))

    def test_get_all_configs(self):
        """
        Tests microsite.get_all_config works as expected.
        """
        microsite.set_by_domain(self.microsite_subdomain)
        configs = microsite.get_all_config()
        self.assertEqual(len(configs.keys()), 3)

    def test_set_config_by_domain(self):
        """
        Tests microsite.set_config_by_domain works as expected.
        """
        microsite.clear()
        # if microsite config does not exist default config should be used
        microsite.set_by_domain('unknown')
        self.assertEqual(microsite.get_value('university'), 'default_university')
124

125 126 127 128 129 130 131 132 133
    def test_has_configuration_set(self):
        """
        Tests microsite.has_configuration_set works as expected.
        """
        self.assertTrue(microsite.BACKEND.has_configuration_set())

        with patch('django.conf.settings.MICROSITE_CONFIGURATION', {}):
            self.assertFalse(microsite.BACKEND.has_configuration_set())

134 135 136 137 138 139 140 141 142 143 144 145 146 147

@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()
148
        self.microsite_subdomain = 'test-site'
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        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")