Commit d77025c8 by Calen Pennington

Use a contextmanager to cleanup after manipulating the current request in third_party_auth tests

parent 86902e87
...@@ -5,6 +5,7 @@ import unittest ...@@ -5,6 +5,7 @@ import unittest
import json import json
import mock import mock
from contextlib import contextmanager
from django import test from django import test
from django.contrib import auth from django.contrib import auth
from django.contrib.auth import models as auth_models from django.contrib.auth import models as auth_models
...@@ -17,7 +18,6 @@ from social import actions, exceptions ...@@ -17,7 +18,6 @@ from social import actions, exceptions
from social.apps.django_app import utils as social_utils from social.apps.django_app import utils as social_utils
from social.apps.django_app import views as social_views from social.apps.django_app import views as social_views
from edxmako.tests import mako_middleware_process_request
from lms.djangoapps.commerce.tests import TEST_API_URL, TEST_API_SIGNING_KEY from lms.djangoapps.commerce.tests import TEST_API_URL, TEST_API_SIGNING_KEY
from student import models as student_models from student import models as student_models
from student import views as student_views from student import views as student_views
...@@ -526,6 +526,13 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -526,6 +526,13 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
return request, strategy return request, strategy
@contextmanager
def _patch_edxmako_current_request(self, request):
"""Make ``request`` be the current request for edxmako template rendering."""
with mock.patch('edxmako.request_context.get_current_request', return_value=request):
yield
def get_user_by_email(self, strategy, email): def get_user_by_email(self, strategy, email):
"""Gets a user by email, using the given strategy.""" """Gets a user by email, using the given strategy."""
return strategy.storage.user.user_model().objects.get(email=email) return strategy.storage.user.user_model().objects.get(email=email)
...@@ -579,7 +586,6 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -579,7 +586,6 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
pipeline.get_login_url(self.provider.provider_id, pipeline.AUTH_ENTRY_LOGIN)) pipeline.get_login_url(self.provider.provider_id, pipeline.AUTH_ENTRY_LOGIN))
actions.do_complete(request.backend, social_views._do_login) # pylint: disable=protected-access actions.do_complete(request.backend, social_views._do_login) # pylint: disable=protected-access
mako_middleware_process_request(strategy.request)
student_views.signin_user(strategy.request) student_views.signin_user(strategy.request)
student_views.login_user(strategy.request) student_views.login_user(strategy.request)
actions.do_complete(request.backend, social_views._do_login) # pylint: disable=protected-access actions.do_complete(request.backend, social_views._do_login) # pylint: disable=protected-access
...@@ -627,7 +633,7 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -627,7 +633,7 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
pipeline.get_login_url(self.provider.provider_id, pipeline.AUTH_ENTRY_LOGIN)) pipeline.get_login_url(self.provider.provider_id, pipeline.AUTH_ENTRY_LOGIN))
actions.do_complete(request.backend, social_views._do_login) # pylint: disable=protected-access actions.do_complete(request.backend, social_views._do_login) # pylint: disable=protected-access
mako_middleware_process_request(strategy.request) with self._patch_edxmako_current_request(strategy.request):
student_views.signin_user(strategy.request) student_views.signin_user(strategy.request)
student_views.login_user(strategy.request) student_views.login_user(strategy.request)
actions.do_complete(request.backend, social_views._do_login, user=user) # pylint: disable=protected-access actions.do_complete(request.backend, social_views._do_login, user=user) # pylint: disable=protected-access
...@@ -686,7 +692,7 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -686,7 +692,7 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
self.client.get(pipeline.get_login_url(self.provider.provider_id, pipeline.AUTH_ENTRY_LOGIN)) self.client.get(pipeline.get_login_url(self.provider.provider_id, pipeline.AUTH_ENTRY_LOGIN))
actions.do_complete(request.backend, social_views._do_login) # pylint: disable=protected-access actions.do_complete(request.backend, social_views._do_login) # pylint: disable=protected-access
mako_middleware_process_request(strategy.request) with self._patch_edxmako_current_request(strategy.request):
student_views.signin_user(strategy.request) student_views.signin_user(strategy.request)
student_views.login_user(strategy.request) student_views.login_user(strategy.request)
actions.do_complete(request.backend, social_views._do_login, user=user) # pylint: disable=protected-access actions.do_complete(request.backend, social_views._do_login, user=user) # pylint: disable=protected-access
...@@ -727,9 +733,9 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -727,9 +733,9 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
# pylint: disable=protected-access # pylint: disable=protected-access
self.assert_redirect_to_login_looks_correct(actions.do_complete(request.backend, social_views._do_login)) self.assert_redirect_to_login_looks_correct(actions.do_complete(request.backend, social_views._do_login))
mako_middleware_process_request(strategy.request)
# At this point we know the pipeline has resumed correctly. Next we # At this point we know the pipeline has resumed correctly. Next we
# fire off the view that displays the login form and posts it via JS. # fire off the view that displays the login form and posts it via JS.
with self._patch_edxmako_current_request(strategy.request):
self.assert_login_response_in_pipeline_looks_correct(student_views.signin_user(strategy.request)) self.assert_login_response_in_pipeline_looks_correct(student_views.signin_user(strategy.request))
# Next, we invoke the view that handles the POST, and expect it # Next, we invoke the view that handles the POST, and expect it
...@@ -760,7 +766,7 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -760,7 +766,7 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
user.is_active = False user.is_active = False
user.save() user.save()
mako_middleware_process_request(strategy.request) with self._patch_edxmako_current_request(strategy.request):
self.assert_json_failure_response_is_inactive_account(student_views.login_user(strategy.request)) self.assert_json_failure_response_is_inactive_account(student_views.login_user(strategy.request))
def test_signin_fails_if_no_account_associated(self): def test_signin_fails_if_no_account_associated(self):
...@@ -806,9 +812,9 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -806,9 +812,9 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
# pylint: disable=protected-access # pylint: disable=protected-access
self.assert_redirect_to_register_looks_correct(actions.do_complete(request.backend, social_views._do_login)) self.assert_redirect_to_register_looks_correct(actions.do_complete(request.backend, social_views._do_login))
mako_middleware_process_request(strategy.request)
# At this point we know the pipeline has resumed correctly. Next we # At this point we know the pipeline has resumed correctly. Next we
# fire off the view that displays the registration form. # fire off the view that displays the registration form.
with self._patch_edxmako_current_request(request):
self.assert_register_response_in_pipeline_looks_correct( self.assert_register_response_in_pipeline_looks_correct(
student_views.register_user(strategy.request), pipeline.get(request)['kwargs']) student_views.register_user(strategy.request), pipeline.get(request)['kwargs'])
...@@ -828,6 +834,7 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -828,6 +834,7 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
# ...but when we invoke create_account the existing edX view will make # ...but when we invoke create_account the existing edX view will make
# it, but not social auths. The pipeline creates those later. # it, but not social auths. The pipeline creates those later.
with self._patch_edxmako_current_request(strategy.request):
self.assert_json_success_response_looks_correct(student_views.create_account(strategy.request)) self.assert_json_success_response_looks_correct(student_views.create_account(strategy.request))
# We've overridden the user's password, so authenticate() with the old # We've overridden the user's password, so authenticate() with the old
# value won't work: # value won't work:
...@@ -876,9 +883,11 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -876,9 +883,11 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
# pylint: disable=protected-access # pylint: disable=protected-access
self.assert_redirect_to_register_looks_correct(actions.do_complete(backend, social_views._do_login)) self.assert_redirect_to_register_looks_correct(actions.do_complete(backend, social_views._do_login))
mako_middleware_process_request(strategy.request) with self._patch_edxmako_current_request(request):
self.assert_register_response_in_pipeline_looks_correct( self.assert_register_response_in_pipeline_looks_correct(
student_views.register_user(strategy.request), pipeline.get(request)['kwargs']) student_views.register_user(strategy.request), pipeline.get(request)['kwargs'])
with self._patch_edxmako_current_request(strategy.request):
strategy.request.POST = self.get_registration_post_vars() strategy.request.POST = self.get_registration_post_vars()
# Create twice: once successfully, and once causing a collision. # Create twice: once successfully, and once causing a collision.
student_views.create_account(strategy.request) student_views.create_account(strategy.request)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment