test_middleware.py 1.46 KB
Newer Older
1 2 3 4 5
"""
Tests for courseware middleware
"""

from django.http import Http404
6
from django.test.client import RequestFactory
7
from nose.plugins.attrib import attr
8

9 10
from lms.djangoapps.courseware.exceptions import Redirect
from lms.djangoapps.courseware.middleware import RedirectMiddleware
11 12
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory
13

14

15
@attr(shard=1)
16
class CoursewareMiddlewareTestCase(SharedModuleStoreTestCase):
17 18
    """Tests that courseware middleware is correctly redirected"""

19 20 21 22 23
    @classmethod
    def setUpClass(cls):
        super(CoursewareMiddlewareTestCase, cls).setUpClass()
        cls.course = CourseFactory.create()

24 25 26
    def test_process_404(self):
        """A 404 should not trigger anything"""
        request = RequestFactory().get("dummy_url")
27
        response = RedirectMiddleware().process_exception(
28 29 30
            request, Http404()
        )
        self.assertIsNone(response)
31 32 33 34 35 36 37 38 39 40 41 42 43 44

    def test_redirect_exceptions(self):
        """
        Unit tests for handling of Redirect exceptions.
        """
        request = RequestFactory().get("dummy_url")
        test_url = '/test_url'
        exception = Redirect(test_url)
        response = RedirectMiddleware().process_exception(
            request, exception
        )
        self.assertEqual(response.status_code, 302)
        target_url = response._headers['location'][1]
        self.assertTrue(target_url.endswith(test_url))