Adding the declaration of the settings object to openedx.conf to be able to import it from a nicer location Resolving quality violations Merging dicts with the settings definition when they exist in the microsite configuration Using a cache to improve the perfomance of quering any dictionary in the microsite definition Ignoring the invalid-name pylint warning since the names must be kept thsi way to stay the same as the ones in django. Removing the default dict argument as per https://docs.python.org/2/tutorial/controlflow.html#default-argument-values Extracting the implementation of the microsite to a selectable backend. Leaving the function startup.enable_microsites for backwards compatibilityy Adding a database backend Using a cache to improve the perfomance of quering any dictionary in the microsite definition. Changed the database backend so that it extends the settings file backend and removed all the unnecessary methods. Using the backend provider for the get_dict function some tweeks and some initial unit tests Using getattr as a function insteal of calling the underlying __getattr__ directly Adding an ModelAdmin object for the microsite model in the django-admin panel refactor enable_microsites() consolidate/refactor some shared code add config to aws.py and add migration files fix tests Changes to get the backends to run after the refactor add archiving capabilities to microsites. Also make a few notes about performance improvements to make fix tests Making the query to find if microsites exist in the database faster add ORG to microsite mapping tables and some performance improvements allow for Mako templates to be pulled from the database fix tests For the database template backend the uri of the template does not use the filesystem relative path Fixing pylint violations Added caching of the templates stored in the database Fixing pylint errors fix pylint Clearing the cache on model save Fixing pylint errors rebased and added test coverage rebased cdodge/microsite-improvements branch with master and added test coverage added missing migration fix quality violations add more test coverage mattdrayer: Add microsite_configuration to cms.INSTALLED_APPS added microsite settings to cms/envs/test.py run session cookie tests only in LMS fixed broken tests putting middleware changes back Preventing the template_backend to be called on requests which have no microsite changes to address feedback from mjfrey changed BaseMicrositeBackend to AbstractBaseMicrositeBackend changes after feedback from mattdrayer fixed broken tests and quality violations Allowing the backend to handle the enable_pre_startup routine Typos and docstrings Adressing feedback Fixing python tests add comment to explain why we need enable_microsites_pre_startup()
82 lines
3.3 KiB
Python
82 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Test Microsite middleware.
|
|
"""
|
|
import ddt
|
|
import unittest
|
|
from mock import patch
|
|
|
|
from django.conf import settings
|
|
from django.test.client import Client
|
|
from django.test.utils import override_settings
|
|
|
|
from student.tests.factories import UserFactory
|
|
from microsite_configuration.microsite import (
|
|
get_backend,
|
|
)
|
|
from microsite_configuration.backends.base import BaseMicrositeBackend
|
|
from microsite_configuration.tests.tests import (
|
|
DatabaseMicrositeTestCase,
|
|
side_effect_for_get_value,
|
|
MICROSITE_BACKENDS,
|
|
)
|
|
|
|
|
|
# NOTE: We set SESSION_SAVE_EVERY_REQUEST to True in order to make sure
|
|
# Sessions are always started on every request
|
|
# pylint: disable=no-member, protected-access
|
|
@ddt.ddt
|
|
@override_settings(SESSION_SAVE_EVERY_REQUEST=True)
|
|
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
|
|
class MicrositeSessionCookieTests(DatabaseMicrositeTestCase):
|
|
"""
|
|
Tests regarding the session cookie management in the middlware for Microsites
|
|
"""
|
|
|
|
def setUp(self):
|
|
super(MicrositeSessionCookieTests, self).setUp()
|
|
# Create a test client, and log it in so that it will save some session
|
|
# data.
|
|
self.user = UserFactory.create()
|
|
self.user.set_password('password')
|
|
self.user.save()
|
|
self.client = Client()
|
|
self.client.login(username=self.user.username, password="password")
|
|
|
|
@ddt.data(*MICROSITE_BACKENDS)
|
|
def test_session_cookie_domain_no_microsite(self, site_backend):
|
|
"""
|
|
Tests that non-microsite behaves according to default behavior
|
|
"""
|
|
with patch('microsite_configuration.microsite.BACKEND',
|
|
get_backend(site_backend, BaseMicrositeBackend)):
|
|
response = self.client.get('/')
|
|
self.assertNotIn('test_microsite.localhost', str(response.cookies['sessionid']))
|
|
self.assertNotIn('Domain', str(response.cookies['sessionid']))
|
|
|
|
@ddt.data(*MICROSITE_BACKENDS)
|
|
def test_session_cookie_domain(self, site_backend):
|
|
"""
|
|
Makes sure that the cookie being set in a Microsite
|
|
is the one specially overridden in configuration
|
|
"""
|
|
with patch('microsite_configuration.microsite.BACKEND',
|
|
get_backend(site_backend, BaseMicrositeBackend)):
|
|
response = self.client.get('/', HTTP_HOST=settings.MICROSITE_TEST_HOSTNAME)
|
|
self.assertIn('test_microsite.localhost', str(response.cookies['sessionid']))
|
|
|
|
@ddt.data(*MICROSITE_BACKENDS)
|
|
def test_microsite_none_cookie_domain(self, site_backend):
|
|
"""
|
|
Tests to make sure that a Microsite that specifies None for 'SESSION_COOKIE_DOMAIN' does not
|
|
set a domain on the session cookie
|
|
"""
|
|
|
|
with patch('microsite_configuration.microsite.get_value') as mock_get_value:
|
|
mock_get_value.side_effect = side_effect_for_get_value('SESSION_COOKIE_DOMAIN', None)
|
|
with patch('microsite_configuration.microsite.BACKEND',
|
|
get_backend(site_backend, BaseMicrositeBackend)):
|
|
response = self.client.get('/', HTTP_HOST=settings.MICROSITE_TEST_HOSTNAME)
|
|
self.assertNotIn('test_microsite.localhost', str(response.cookies['sessionid']))
|
|
self.assertNotIn('Domain', str(response.cookies['sessionid']))
|