Files
edx-platform/common/djangoapps/third_party_auth/tests/test_middleware.py
Feanil Patel 9cf2f9f298 Run 2to3 -f future . -w
This will remove imports from __future__ that are no longer needed.

https://docs.python.org/3.5/library/2to3.html#2to3fixer-future
2019-12-30 10:35:30 -05:00

45 lines
1.6 KiB
Python

"""
Tests for third party auth middleware
"""
import mock
from django.contrib.messages.middleware import MessageMiddleware
from django.http import HttpResponse
from django.test.client import RequestFactory
from requests.exceptions import HTTPError
from openedx.core.djangolib.testing.utils import skip_unless_lms
from student.helpers import get_next_url_for_login_page
from third_party_auth.middleware import ExceptionMiddleware
from third_party_auth.tests.testutil import TestCase
class ThirdPartyAuthMiddlewareTestCase(TestCase):
"""Tests that ExceptionMiddleware is correctly redirected"""
@skip_unless_lms
@mock.patch('django.conf.settings.MESSAGE_STORAGE', 'django.contrib.messages.storage.cookie.CookieStorage')
def test_http_exception_redirection(self):
"""
Test ExceptionMiddleware is correctly redirected to login page
when PSA raises HttpError exception.
"""
request = RequestFactory().get("dummy_url")
next_url = get_next_url_for_login_page(request)
login_url = '/login?next=' + next_url
request.META['HTTP_REFERER'] = 'http://example.com:8000/login'
exception = HTTPError()
exception.response = HttpResponse(status=502)
# Add error message for error in auth pipeline
MessageMiddleware().process_request(request)
response = ExceptionMiddleware().process_exception(
request, exception
)
target_url = response.url
self.assertEqual(response.status_code, 302)
self.assertTrue(target_url.endswith(login_url))