From 4cc7628696be0be35dc6eb6af0a496959d263a42 Mon Sep 17 00:00:00 2001
From: Clinton Blackburn <cblackburn@edx.org>
Date: Mon, 6 Feb 2017 19:52:52 -0500
Subject: [PATCH] Upgraded to Python Social Auth 0.2.21 to resolve migration issue

---
 cms/startup.py                                           |  6 +-----
 common/djangoapps/third_party_auth/saml.py               |  5 -----
 common/djangoapps/third_party_auth/tests/specs/base.py   |  4 +++-
 common/djangoapps/third_party_auth/tests/utils.py        |  5 ++---
 lms/envs/common.py                                       |  5 -----
 lms/startup.py                                           |  6 +-----
 openedx/core/djangoapps/monkey_patch/third_party_auth.py | 38 --------------------------------------
 requirements/edx/base.txt                                |  7 +------
 8 files changed, 8 insertions(+), 68 deletions(-)
 delete mode 100644 openedx/core/djangoapps/monkey_patch/third_party_auth.py

diff --git a/cms/startup.py b/cms/startup.py
index c00da4f..c37f315 100644
--- a/cms/startup.py
+++ b/cms/startup.py
@@ -10,10 +10,7 @@ settings.INSTALLED_APPS  # pylint: disable=pointless-statement
 
 from openedx.core.lib.django_startup import autostartup
 import django
-from openedx.core.djangoapps.monkey_patch import (
-    third_party_auth,
-    django_db_models_options
-)
+from openedx.core.djangoapps.monkey_patch import django_db_models_options
 from openedx.core.lib.xblock_utils import xblock_local_resource_url
 
 import xmodule.x_module
@@ -28,7 +25,6 @@ def run():
     """
     Executed during django startup
     """
-    third_party_auth.patch()
     django_db_models_options.patch()
 
     # Comprehensive theming needs to be set up before django startup,
diff --git a/common/djangoapps/third_party_auth/saml.py b/common/djangoapps/third_party_auth/saml.py
index 7a90a32..95747e9 100644
--- a/common/djangoapps/third_party_auth/saml.py
+++ b/common/djangoapps/third_party_auth/saml.py
@@ -44,11 +44,6 @@ class SAMLAuthBackend(SAMLAuth):  # pylint: disable=abstract-method
             log.error('SAML authentication is not enabled')
             raise Http404
 
-        # TODO: remove this check once the fix is merged upstream:
-        # https://github.com/omab/python-social-auth/pull/821
-        if 'idp' not in self.strategy.request_data():
-            raise AuthMissingParameter(self, 'idp')
-
         return super(SAMLAuthBackend, self).auth_url()
 
     def _check_entitlements(self, idp, attributes):
diff --git a/common/djangoapps/third_party_auth/tests/specs/base.py b/common/djangoapps/third_party_auth/tests/specs/base.py
index 887f9b0..98566d0 100644
--- a/common/djangoapps/third_party_auth/tests/specs/base.py
+++ b/common/djangoapps/third_party_auth/tests/specs/base.py
@@ -413,8 +413,10 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
     def assert_redirect_to_dashboard_looks_correct(self, response):
         """Asserts a response would redirect to /dashboard."""
         self.assertEqual(302, response.status_code)
+        # NOTE: Ideally we should use assertRedirects(), however it errors out due to the hostname, testserver,
+        # not being properly set. This may be an issue with the call made by PSA, but we are not certain.
         # pylint: disable=protected-access
-        self.assertEqual(auth_settings._SOCIAL_AUTH_LOGIN_REDIRECT_URL, response.get('Location'))
+        self.assertTrue(response.get('Location').endswith(django_settings.SOCIAL_AUTH_LOGIN_REDIRECT_URL))
 
     def assert_redirect_to_login_looks_correct(self, response):
         """Asserts a response would redirect to /login."""
diff --git a/common/djangoapps/third_party_auth/tests/utils.py b/common/djangoapps/third_party_auth/tests/utils.py
index 48de8ce..81435ee 100644
--- a/common/djangoapps/third_party_auth/tests/utils.py
+++ b/common/djangoapps/third_party_auth/tests/utils.py
@@ -2,11 +2,10 @@
 import json
 
 import httpretty
-
 from provider.constants import PUBLIC
 from provider.oauth2.models import Client
 from social.apps.django_app.default.models import UserSocialAuth
-
+from social.backends.facebook import FacebookOAuth2
 from student.tests.factories import UserFactory
 
 from .testutil import ThirdPartyAuthTestMixin
@@ -81,7 +80,7 @@ class ThirdPartyOAuthTestMixin(ThirdPartyAuthTestMixin):
 class ThirdPartyOAuthTestMixinFacebook(object):
     """Tests oauth with the Facebook backend"""
     BACKEND = "facebook"
-    USER_URL = "https://graph.facebook.com/v2.3/me"
+    USER_URL = FacebookOAuth2.USER_DATA_URL
     # In facebook responses, the "id" field is used as the user's identifier
     UID_FIELD = "id"
 
diff --git a/lms/envs/common.py b/lms/envs/common.py
index 41b1f84..ac03fe7 100644
--- a/lms/envs/common.py
+++ b/lms/envs/common.py
@@ -2175,11 +2175,6 @@ INSTALLED_APPS = (
     'database_fixups',
 )
 
-# Migrations which are not in the standard module "migrations"
-MIGRATION_MODULES = {
-    'social.apps.django_app.default': 'social.apps.django_app.default.south_migrations'
-}
-
 ######################### CSRF #########################################
 
 # Forwards-compatibility with Django 1.7
diff --git a/lms/startup.py b/lms/startup.py
index 0d44ae6..d4d594d 100644
--- a/lms/startup.py
+++ b/lms/startup.py
@@ -12,10 +12,7 @@ settings.INSTALLED_APPS  # pylint: disable=pointless-statement
 from openedx.core.lib.django_startup import autostartup
 import logging
 import analytics
-from openedx.core.djangoapps.monkey_patch import (
-    third_party_auth,
-    django_db_models_options
-)
+from openedx.core.djangoapps.monkey_patch import django_db_models_options
 
 import xmodule.x_module
 import lms_xblock.runtime
@@ -33,7 +30,6 @@ def run():
     """
     Executed during django startup
     """
-    third_party_auth.patch()
     django_db_models_options.patch()
 
     # To override the settings before executing the autostartup() for python-social-auth
diff --git a/openedx/core/djangoapps/monkey_patch/third_party_auth.py b/openedx/core/djangoapps/monkey_patch/third_party_auth.py
deleted file mode 100644
index a78f832..0000000
--- a/openedx/core/djangoapps/monkey_patch/third_party_auth.py
+++ /dev/null
@@ -1,38 +0,0 @@
-"""
-Monkey patch implementation for a python_social_auth Django ORM method that is not Django 1.8-compatible.
-Remove once the module fully supports Django 1.8!
-"""
-
-from django.db import transaction
-from social.storage.django_orm import DjangoUserMixin
-from social.apps.django_app.default.models import (
-    UserSocialAuth, Nonce, Association, Code
-)
-
-
-def patch():
-    """
-    Monkey-patch the DjangoUserMixin class.
-    """
-    def create_social_auth_wrapper(wrapped_func):
-        # pylint: disable=missing-docstring
-        wrapped_func = wrapped_func.__func__
-
-        def _create_social_auth(*args, **kwargs):
-            # The entire reason for this monkey-patch is to wrap the create_social_auth call
-            # in an atomic transaction. The call can sometime raise an IntegrityError, which is
-            # caught and dealt with by python_social_auth - but not inside of an atomic transaction.
-            # In Django 1.8, unless the exception is raised in an atomic transaction, the transaction
-            # becomes unusable after the IntegrityError exception is raised.
-            with transaction.atomic():
-                return wrapped_func(*args, **kwargs)
-        return classmethod(_create_social_auth)
-
-    DjangoUserMixin.create_social_auth = create_social_auth_wrapper(DjangoUserMixin.create_social_auth)
-
-    # Monkey-patch some social auth models' Meta class to squelch Django19 warnings.
-    # pylint: disable=protected-access
-    UserSocialAuth._meta.app_label = "default"
-    Nonce._meta.app_label = "default"
-    Association._meta.app_label = "default"
-    Code._meta.app_label = "default"
diff --git a/requirements/edx/base.txt b/requirements/edx/base.txt
index 8463272..3347c02 100644
--- a/requirements/edx/base.txt
+++ b/requirements/edx/base.txt
@@ -90,12 +90,7 @@ python-memcached==1.48
 django-memcached-hashring==0.1.2
 python-openid==2.2.5
 python-dateutil==2.1
-
-# This module gets monkey-patched in third_party_auth.py to fix a Django 1.8 incompatibility.
-# When this dependency gets upgraded, the monkey patch should be removed, if possible.
-# We can also remove the fix to auth_url in third_party_auth/saml.py when that fix is included upstream.
-python-social-auth==0.2.12
-
+python-social-auth==0.2.21
 pytz==2016.7
 pysrt==0.4.7
 PyYAML==3.10
--
libgit2 0.26.0