diff --git a/common/djangoapps/student/tests/test_email.py b/common/djangoapps/student/tests/test_email.py index 38824a1f47ea21cbcbf5e91f691853f804d8c5da..5146c5ce2fc00ac01d6713686112f23e81295072 100644 --- a/common/djangoapps/student/tests/test_email.py +++ b/common/djangoapps/student/tests/test_email.py @@ -150,6 +150,32 @@ class ActivationEmailTests(CacheIsolationTestCase): for fragment in body_fragments: self.assertIn(fragment, msg.body) + def test_do_not_send_email_and_do_activate(self): + """ + Tests that when an inactive user logs-in using the social auth, + an activation email is not sent. + """ + pipeline_partial = { + 'kwargs': { + 'social': { + 'uid': 'fake uid' + } + } + } + user = UserFactory(is_active=False) + Registration().register(user) + request = RequestFactory().get(settings.SOCIAL_AUTH_INACTIVE_USER_URL) + request.user = user + with patch('student.views.management.compose_and_send_activation_email') as email: + with patch('third_party_auth.provider.Registry.get_from_pipeline') as reg: + with patch('third_party_auth.pipeline.get', return_value=pipeline_partial): + with patch('third_party_auth.pipeline.running', return_value=True): + with patch('third_party_auth.is_enabled', return_value=True): + reg.skip_email_verification = True + inactive_user_view(request) + self.assertEquals(user.is_active, True) + self.assertEquals(email.called, False, msg='method should not have been called') + @patch('student.tasks.log') def test_send_email_to_inactive_user(self, mock_log): """ @@ -161,12 +187,13 @@ class ActivationEmailTests(CacheIsolationTestCase): request = RequestFactory().get(settings.SOCIAL_AUTH_INACTIVE_USER_URL) request.user = inactive_user with patch('edxmako.request_context.get_current_request', return_value=request): - inactive_user_view(request) - mock_log.info.assert_called_with( - "Activation Email has been sent to User {user_email}".format( - user_email=inactive_user.email + with patch('third_party_auth.pipeline.running', return_value=False): + inactive_user_view(request) + mock_log.info.assert_called_with( + "Activation Email has been sent to User {user_email}".format( + user_email=inactive_user.email + ) ) - ) @patch('student.views.login.render_to_string', Mock(side_effect=mock_render_to_string, autospec=True)) diff --git a/common/djangoapps/third_party_auth/views.py b/common/djangoapps/third_party_auth/views.py index 4360bef5ada32f711929741e95bedb7df147cc3c..deaabc593c005fb0b38875a803e957769a2b59dc 100644 --- a/common/djangoapps/third_party_auth/views.py +++ b/common/djangoapps/third_party_auth/views.py @@ -12,6 +12,8 @@ from social_core.utils import setting_name from student.models import UserProfile from student.views import compose_and_send_activation_email +import third_party_auth +from third_party_auth import pipeline, provider from .models import SAMLConfiguration, SAMLProviderConfig @@ -27,12 +29,27 @@ def inactive_user_view(request): The reason this view exists is that if we don't define this as the SOCIAL_AUTH_INACTIVE_USER_URL, inactive users will get sent to LOGIN_ERROR_URL, which we don't want. + + If the third_party_provider.skip_email_verification is set then the user is activated + and verification email is not sent """ # 'next' may be set to '/account/finish_auth/.../' if this user needs to be auto-enrolled # in a course. Otherwise, just redirect them to the dashboard, which displays a message # about activating their account. - profile = UserProfile.objects.get(user=request.user) - compose_and_send_activation_email(request.user, profile) + user = request.user + profile = UserProfile.objects.get(user=user) + activated = user.is_active + # If the user is registering via 3rd party auth, track which provider they use + if third_party_auth.is_enabled() and pipeline.running(request): + running_pipeline = pipeline.get(request) + third_party_provider = provider.Registry.get_from_pipeline(running_pipeline) + if third_party_provider.skip_email_verification and not activated: + user.is_active = True + user.save() + activated = True + if not activated: + compose_and_send_activation_email(user, profile) + return redirect(request.GET.get('next', 'dashboard'))