From d77025c873f50c286ca27961d2767785e956978e Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Thu, 26 May 2016 14:26:47 -0400 Subject: [PATCH] Use a contextmanager to cleanup after manipulating the current request in third_party_auth tests --- .../third_party_auth/tests/specs/base.py | 57 +++++++++++-------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/common/djangoapps/third_party_auth/tests/specs/base.py b/common/djangoapps/third_party_auth/tests/specs/base.py index ee879390b4..fa8fa991f0 100644 --- a/common/djangoapps/third_party_auth/tests/specs/base.py +++ b/common/djangoapps/third_party_auth/tests/specs/base.py @@ -5,6 +5,7 @@ import unittest import json import mock +from contextlib import contextmanager from django import test from django.contrib import auth from django.contrib.auth import models as auth_models @@ -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 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 student import models as student_models from student import views as student_views @@ -526,6 +526,13 @@ class IntegrationTest(testutil.TestCase, test.TestCase): 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): """Gets a user by email, using the given strategy.""" return strategy.storage.user.user_model().objects.get(email=email) @@ -579,7 +586,6 @@ class IntegrationTest(testutil.TestCase, test.TestCase): 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 - mako_middleware_process_request(strategy.request) student_views.signin_user(strategy.request) student_views.login_user(strategy.request) actions.do_complete(request.backend, social_views._do_login) # pylint: disable=protected-access @@ -627,10 +633,10 @@ class IntegrationTest(testutil.TestCase, test.TestCase): 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 - mako_middleware_process_request(strategy.request) - student_views.signin_user(strategy.request) - student_views.login_user(strategy.request) - actions.do_complete(request.backend, social_views._do_login, user=user) # pylint: disable=protected-access + with self._patch_edxmako_current_request(strategy.request): + student_views.signin_user(strategy.request) + student_views.login_user(strategy.request) + actions.do_complete(request.backend, social_views._do_login, user=user) # pylint: disable=protected-access # First we expect that we're in the linked state, with a backend entry. self.assert_account_settings_context_looks_correct(account_settings_context(request), user, linked=True) @@ -686,10 +692,10 @@ class IntegrationTest(testutil.TestCase, test.TestCase): 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 - mako_middleware_process_request(strategy.request) - student_views.signin_user(strategy.request) - student_views.login_user(strategy.request) - actions.do_complete(request.backend, social_views._do_login, user=user) # pylint: disable=protected-access + with self._patch_edxmako_current_request(strategy.request): + student_views.signin_user(strategy.request) + student_views.login_user(strategy.request) + actions.do_complete(request.backend, social_views._do_login, user=user) # pylint: disable=protected-access # Monkey-patch storage for messaging; pylint: disable=protected-access request._messages = fallback.FallbackStorage(request) @@ -727,10 +733,10 @@ class IntegrationTest(testutil.TestCase, test.TestCase): # pylint: disable=protected-access 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 # fire off the view that displays the login form and posts it via JS. - self.assert_login_response_in_pipeline_looks_correct(student_views.signin_user(strategy.request)) + with self._patch_edxmako_current_request(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 # redirects to /auth/complete. In the browser ajax handlers will @@ -760,8 +766,8 @@ class IntegrationTest(testutil.TestCase, test.TestCase): user.is_active = False user.save() - mako_middleware_process_request(strategy.request) - self.assert_json_failure_response_is_inactive_account(student_views.login_user(strategy.request)) + with self._patch_edxmako_current_request(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): _, strategy = self.get_request_and_strategy( @@ -806,11 +812,11 @@ class IntegrationTest(testutil.TestCase, test.TestCase): # pylint: disable=protected-access 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 # fire off the view that displays the registration form. - self.assert_register_response_in_pipeline_looks_correct( - student_views.register_user(strategy.request), pipeline.get(request)['kwargs']) + with self._patch_edxmako_current_request(request): + self.assert_register_response_in_pipeline_looks_correct( + student_views.register_user(strategy.request), pipeline.get(request)['kwargs']) # Next, we invoke the view that handles the POST. Not all providers # supply email. Manually add it as the user would have to; this @@ -828,7 +834,8 @@ class IntegrationTest(testutil.TestCase, test.TestCase): # ...but when we invoke create_account the existing edX view will make # it, but not social auths. The pipeline creates those later. - self.assert_json_success_response_looks_correct(student_views.create_account(strategy.request)) + with self._patch_edxmako_current_request(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 # value won't work: created_user = self.get_user_by_email(strategy, email) @@ -876,12 +883,14 @@ class IntegrationTest(testutil.TestCase, test.TestCase): # pylint: disable=protected-access self.assert_redirect_to_register_looks_correct(actions.do_complete(backend, social_views._do_login)) - mako_middleware_process_request(strategy.request) - self.assert_register_response_in_pipeline_looks_correct( - student_views.register_user(strategy.request), pipeline.get(request)['kwargs']) - strategy.request.POST = self.get_registration_post_vars() - # Create twice: once successfully, and once causing a collision. - student_views.create_account(strategy.request) + with self._patch_edxmako_current_request(request): + self.assert_register_response_in_pipeline_looks_correct( + 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() + # Create twice: once successfully, and once causing a collision. + student_views.create_account(strategy.request) self.assert_json_failure_response_is_username_collision(student_views.create_account(strategy.request)) def test_pipeline_raises_auth_entry_error_if_auth_entry_invalid(self):