Commit 6073ea2e by Nimisha Asthagiri Committed by GitHub

Merge pull request #13658 from edx/common_cleanup/auth_exchange

Move auth_exchange from common to openedx/core
parents 24d29be3 4c0f85b4
...@@ -21,7 +21,7 @@ from django.utils.http import int_to_base36 ...@@ -21,7 +21,7 @@ from django.utils.http import int_to_base36
from mock import Mock, patch from mock import Mock, patch
import ddt import ddt
from lms.djangoapps.oauth_dispatch.tests import factories as dot_factories from openedx.core.djangoapps.oauth_dispatch.tests import factories as dot_factories
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase from openedx.core.djangolib.testing.utils import CacheIsolationTestCase
from student.views import password_reset, password_reset_confirm_wrapper, SETTING_CHANGE_INITIATED from student.views import password_reset, password_reset_confirm_wrapper, SETTING_CHANGE_INITIATED
from student.tests.factories import UserFactory from student.tests.factories import UserFactory
......
...@@ -35,7 +35,7 @@ from commerce.models import CommerceConfiguration ...@@ -35,7 +35,7 @@ from commerce.models import CommerceConfiguration
from commerce.tests import TEST_API_URL, TEST_API_SIGNING_KEY, factories from commerce.tests import TEST_API_URL, TEST_API_SIGNING_KEY, factories
from commerce.tests.mocks import mock_get_orders from commerce.tests.mocks import mock_get_orders
from course_modes.models import CourseMode from course_modes.models import CourseMode
from lms.djangoapps.oauth_dispatch.tests import factories as dot_factories from openedx.core.djangoapps.oauth_dispatch.tests import factories as dot_factories
from openedx.core.djangoapps.programs.tests.mixins import ProgramsApiConfigMixin from openedx.core.djangoapps.programs.tests.mixins import ProgramsApiConfigMixin
from openedx.core.djangoapps.user_api.accounts.api import activate_account, create_account from openedx.core.djangoapps.user_api.accounts.api import activate_account, create_account
from openedx.core.djangoapps.user_api.accounts import EMAIL_MAX_LENGTH from openedx.core.djangoapps.user_api.accounts import EMAIL_MAX_LENGTH
......
...@@ -440,7 +440,7 @@ OAUTH_EXPIRE_PUBLIC_CLIENT_DAYS = 30 ...@@ -440,7 +440,7 @@ OAUTH_EXPIRE_PUBLIC_CLIENT_DAYS = 30
################################## DJANGO OAUTH TOOLKIT ####################################### ################################## DJANGO OAUTH TOOLKIT #######################################
OAUTH2_PROVIDER = { OAUTH2_PROVIDER = {
'OAUTH2_VALIDATOR_CLASS': 'lms.djangoapps.oauth_dispatch.dot_overrides.EdxOAuth2Validator', 'OAUTH2_VALIDATOR_CLASS': 'openedx.core.djangoapps.oauth_dispatch.dot_overrides.EdxOAuth2Validator',
'SCOPES': { 'SCOPES': {
'read': 'Read scope', 'read': 'Read scope',
'write': 'Write scope', 'write': 'Write scope',
...@@ -1927,7 +1927,7 @@ INSTALLED_APPS = ( ...@@ -1927,7 +1927,7 @@ INSTALLED_APPS = (
# django-oauth-toolkit # django-oauth-toolkit
'oauth2_provider', 'oauth2_provider',
'lms.djangoapps.oauth_dispatch.apps.OAuthDispatchAppConfig', 'openedx.core.djangoapps.oauth_dispatch.apps.OAuthDispatchAppConfig',
'third_party_auth', 'third_party_auth',
...@@ -1937,7 +1937,7 @@ INSTALLED_APPS = ( ...@@ -1937,7 +1937,7 @@ INSTALLED_APPS = (
# defined by oauth_provider. If those tables don't exist, an error can occur. # defined by oauth_provider. If those tables don't exist, an error can occur.
'oauth_provider', 'oauth_provider',
'auth_exchange', 'openedx.core.djangoapps.auth_exchange',
# For the wiki # For the wiki
'wiki', # The new django-wiki from benjaoming 'wiki', # The new django-wiki from benjaoming
......
...@@ -8,10 +8,10 @@ from django.views.generic.base import RedirectView ...@@ -8,10 +8,10 @@ from django.views.generic.base import RedirectView
from ratelimitbackend import admin from ratelimitbackend import admin
from django.conf.urls.static import static from django.conf.urls.static import static
import auth_exchange.views
from courseware.views.views import EnrollStaffView from courseware.views.views import EnrollStaffView
from config_models.views import ConfigurationModelCurrentAPIView from config_models.views import ConfigurationModelCurrentAPIView
from courseware.views.index import CoursewareIndex from courseware.views.index import CoursewareIndex
from openedx.core.djangoapps.auth_exchange.views import LoginWithAccessTokenView
from openedx.core.djangoapps.catalog.models import CatalogIntegration from openedx.core.djangoapps.catalog.models import CatalogIntegration
from openedx.core.djangoapps.programs.models import ProgramsApiConfig from openedx.core.djangoapps.programs.models import ProgramsApiConfig
from openedx.core.djangoapps.self_paced.models import SelfPacedConfiguration from openedx.core.djangoapps.self_paced.models import SelfPacedConfiguration
...@@ -860,7 +860,7 @@ if settings.FEATURES.get('ENABLE_OAUTH2_PROVIDER'): ...@@ -860,7 +860,7 @@ if settings.FEATURES.get('ENABLE_OAUTH2_PROVIDER'):
urlpatterns += ( urlpatterns += (
# These URLs dispatch to django-oauth-toolkit or django-oauth2-provider as appropriate. # These URLs dispatch to django-oauth-toolkit or django-oauth2-provider as appropriate.
# Developers should use these routes, to maintain compatibility for existing client code # Developers should use these routes, to maintain compatibility for existing client code
url(r'^oauth2/', include('lms.djangoapps.oauth_dispatch.urls')), url(r'^oauth2/', include('openedx.core.djangoapps.oauth_dispatch.urls')),
# These URLs contain the django-oauth2-provider default behavior. It exists to provide # These URLs contain the django-oauth2-provider default behavior. It exists to provide
# URLs for django-oauth2-provider to call using reverse() with the oauth2 namespace, and # URLs for django-oauth2-provider to call using reverse() with the oauth2 namespace, and
# also to maintain support for views that have not yet been wrapped in dispatch views. # also to maintain support for views that have not yet been wrapped in dispatch views.
...@@ -938,7 +938,7 @@ if settings.FEATURES.get('ENABLE_OAUTH2_PROVIDER'): ...@@ -938,7 +938,7 @@ if settings.FEATURES.get('ENABLE_OAUTH2_PROVIDER'):
urlpatterns += ( urlpatterns += (
url( url(
r'^oauth2/login/$', r'^oauth2/login/$',
auth_exchange.views.LoginWithAccessTokenView.as_view(), LoginWithAccessTokenView.as_view(),
name="login_with_access_token" name="login_with_access_token"
), ),
) )
......
...@@ -8,8 +8,8 @@ Django-OAuth2-Provider. ...@@ -8,8 +8,8 @@ Django-OAuth2-Provider.
from unittest import skip, expectedFailure from unittest import skip, expectedFailure
from django.test.client import RequestFactory from django.test.client import RequestFactory
from lms.djangoapps.oauth_dispatch import adapters from openedx.core.djangoapps.oauth_dispatch import adapters
from lms.djangoapps.oauth_dispatch.tests.constants import DUMMY_REDIRECT_URL from openedx.core.djangoapps.oauth_dispatch.tests.constants import DUMMY_REDIRECT_URL
from ..views import DOTAccessTokenExchangeView from ..views import DOTAccessTokenExchangeView
......
...@@ -70,7 +70,10 @@ class AccessTokenExchangeViewTest(AccessTokenExchangeTestMixin): ...@@ -70,7 +70,10 @@ class AccessTokenExchangeViewTest(AccessTokenExchangeTestMixin):
self._setup_provider_response(success=True) self._setup_provider_response(success=True)
for single_access_token in [True, False]: for single_access_token in [True, False]:
with mock.patch("auth_exchange.views.constants.SINGLE_ACCESS_TOKEN", single_access_token): with mock.patch(
"openedx.core.djangoapps.auth_exchange.views.constants.SINGLE_ACCESS_TOKEN",
single_access_token,
):
first_response = self.client.post(self.url, self.data) first_response = self.client.post(self.url, self.data)
second_response = self.client.post(self.url, self.data) second_response = self.client.post(self.url, self.data)
self.assertEqual(first_response.status_code, 200) self.assertEqual(first_response.status_code, 200)
......
...@@ -26,8 +26,8 @@ from rest_framework.response import Response ...@@ -26,8 +26,8 @@ from rest_framework.response import Response
from rest_framework.views import APIView from rest_framework.views import APIView
import social.apps.django_app.utils as social_utils import social.apps.django_app.utils as social_utils
from auth_exchange.forms import AccessTokenExchangeForm from openedx.core.djangoapps.auth_exchange.forms import AccessTokenExchangeForm
from lms.djangoapps.oauth_dispatch import adapters from openedx.core.djangoapps.oauth_dispatch import adapters
from openedx.core.lib.api.authentication import OAuth2AuthenticationAllowInactiveUser from openedx.core.lib.api.authentication import OAuth2AuthenticationAllowInactiveUser
...@@ -53,7 +53,7 @@ class AccessTokenExchangeBase(APIView): ...@@ -53,7 +53,7 @@ class AccessTokenExchangeBase(APIView):
""" """
form = AccessTokenExchangeForm(request=request, oauth2_adapter=self.oauth2_adapter, data=request.POST) # pylint: disable=no-member form = AccessTokenExchangeForm(request=request, oauth2_adapter=self.oauth2_adapter, data=request.POST) # pylint: disable=no-member
if not form.is_valid(): if not form.is_valid():
return self.error_response(form.errors) return self.error_response(form.errors) # pylint: disable=no-member
user = form.cleaned_data["user"] user = form.cleaned_data["user"]
scope = form.cleaned_data["scope"] scope = form.cleaned_data["scope"]
......
...@@ -11,4 +11,4 @@ class OAuthDispatchAppConfig(AppConfig): ...@@ -11,4 +11,4 @@ class OAuthDispatchAppConfig(AppConfig):
""" """
OAuthDispatch Configuration OAuthDispatch Configuration
""" """
name = u'lms.djangoapps.oauth_dispatch' name = u'openedx.core.djangoapps.oauth_dispatch'
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import json import json
import unittest
from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.test import TestCase from django.test import TestCase
from edx_oauth2_provider.tests.factories import ClientFactory from edx_oauth2_provider.tests.factories import ClientFactory
...@@ -15,6 +17,7 @@ from .constants import DUMMY_REDIRECT_URL ...@@ -15,6 +17,7 @@ from .constants import DUMMY_REDIRECT_URL
from ..adapters import DOTAdapter from ..adapters import DOTAdapter
@unittest.skipUnless(settings.FEATURES.get("ENABLE_OAUTH2_PROVIDER"), "OAuth2 not enabled")
class ClientCredentialsTest(mixins.AccessTokenMixin, TestCase): class ClientCredentialsTest(mixins.AccessTokenMixin, TestCase):
""" Tests validating the client credentials grant behavior. """ """ Tests validating the client credentials grant behavior. """
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
from django.test import TestCase from django.test import TestCase
from oauth2_provider.models import Application, AccessToken, RefreshToken from oauth2_provider.models import Application, AccessToken, RefreshToken
from lms.djangoapps.oauth_dispatch.tests import factories from openedx.core.djangoapps.oauth_dispatch.tests import factories
from student.tests.factories import UserFactory from student.tests.factories import UserFactory
......
...@@ -5,30 +5,33 @@ Tests for Blocks Views ...@@ -5,30 +5,33 @@ Tests for Blocks Views
import json import json
import ddt import ddt
from django.conf import settings
from django.test import RequestFactory, TestCase from django.test import RequestFactory, TestCase
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
import httpretty import httpretty
from provider import constants from provider import constants
import unittest
from student.tests.factories import UserFactory from student.tests.factories import UserFactory
from third_party_auth.tests.utils import ThirdPartyOAuthTestMixin, ThirdPartyOAuthTestMixinGoogle from third_party_auth.tests.utils import ThirdPartyOAuthTestMixin, ThirdPartyOAuthTestMixinGoogle
from .constants import DUMMY_REDIRECT_URL from .constants import DUMMY_REDIRECT_URL
from .. import adapters from .. import adapters
from .. import views if settings.FEATURES.get("ENABLE_OAUTH2_PROVIDER"):
from .. import views
from . import mixins from . import mixins
@unittest.skipUnless(settings.FEATURES.get("ENABLE_OAUTH2_PROVIDER"), "OAuth2 not enabled")
class _DispatchingViewTestCase(TestCase): class _DispatchingViewTestCase(TestCase):
""" """
Base class for tests that exercise DispatchingViews. Base class for tests that exercise DispatchingViews.
Subclasses need to define self.url.
""" """
dop_adapter = adapters.DOPAdapter() dop_adapter = adapters.DOPAdapter()
dot_adapter = adapters.DOTAdapter() dot_adapter = adapters.DOTAdapter()
view_class = None
url = None
def setUp(self): def setUp(self):
super(_DispatchingViewTestCase, self).setUp() super(_DispatchingViewTestCase, self).setUp()
self.user = UserFactory() self.user = UserFactory()
...@@ -50,7 +53,7 @@ class _DispatchingViewTestCase(TestCase): ...@@ -50,7 +53,7 @@ class _DispatchingViewTestCase(TestCase):
Call the view with a POST request objectwith the appropriate format, Call the view with a POST request objectwith the appropriate format,
returning the response object. returning the response object.
""" """
return self.client.post(self.url, self._post_body(user, client, token_type)) return self.client.post(self.url, self._post_body(user, client, token_type)) # pylint: disable=no-member
def _post_body(self, user, client, token_type=None): def _post_body(self, user, client, token_type=None):
""" """
...@@ -64,9 +67,10 @@ class TestAccessTokenView(mixins.AccessTokenMixin, _DispatchingViewTestCase): ...@@ -64,9 +67,10 @@ class TestAccessTokenView(mixins.AccessTokenMixin, _DispatchingViewTestCase):
""" """
Test class for AccessTokenView Test class for AccessTokenView
""" """
def setUp(self):
view_class = views.AccessTokenView self.url = reverse('access_token')
url = reverse('access_token') self.view_class = views.AccessTokenView
super(TestAccessTokenView, self).setUp()
def _post_body(self, user, client, token_type=None): def _post_body(self, user, client, token_type=None):
""" """
...@@ -124,9 +128,10 @@ class TestAccessTokenExchangeView(ThirdPartyOAuthTestMixinGoogle, ThirdPartyOAut ...@@ -124,9 +128,10 @@ class TestAccessTokenExchangeView(ThirdPartyOAuthTestMixinGoogle, ThirdPartyOAut
""" """
Test class for AccessTokenExchangeView Test class for AccessTokenExchangeView
""" """
def setUp(self):
view_class = views.AccessTokenExchangeView self.url = reverse('exchange_access_token', kwargs={'backend': 'google-oauth2'})
url = reverse('exchange_access_token', kwargs={'backend': 'google-oauth2'}) self.view_class = views.AccessTokenExchangeView
super(TestAccessTokenExchangeView, self).setUp()
def _post_body(self, user, client, token_type=None): def _post_body(self, user, client, token_type=None):
return { return {
...@@ -143,6 +148,7 @@ class TestAccessTokenExchangeView(ThirdPartyOAuthTestMixinGoogle, ThirdPartyOAut ...@@ -143,6 +148,7 @@ class TestAccessTokenExchangeView(ThirdPartyOAuthTestMixinGoogle, ThirdPartyOAut
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
# pylint: disable=abstract-method
@ddt.ddt @ddt.ddt
class TestAuthorizationView(_DispatchingViewTestCase): class TestAuthorizationView(_DispatchingViewTestCase):
""" """
...@@ -231,6 +237,7 @@ class TestAuthorizationView(_DispatchingViewTestCase): ...@@ -231,6 +237,7 @@ class TestAuthorizationView(_DispatchingViewTestCase):
return response.redirect_chain[-1][0] return response.redirect_chain[-1][0]
@unittest.skipUnless(settings.FEATURES.get("ENABLE_OAUTH2_PROVIDER"), "OAuth2 not enabled")
class TestViewDispatch(TestCase): class TestViewDispatch(TestCase):
""" """
Test that the DispatchingView dispatches the right way. Test that the DispatchingView dispatches the right way.
...@@ -324,12 +331,11 @@ class TestRevokeTokenView(_DispatchingViewTestCase): # pylint: disable=abstract ...@@ -324,12 +331,11 @@ class TestRevokeTokenView(_DispatchingViewTestCase): # pylint: disable=abstract
""" """
Test class for RevokeTokenView Test class for RevokeTokenView
""" """
login_with_access_token_url = reverse("login_with_access_token")
revoke_token_url = reverse('revoke_token')
access_token_url = reverse('access_token')
def setUp(self): def setUp(self):
self.login_with_access_token_url = reverse("login_with_access_token")
self.revoke_token_url = reverse('revoke_token')
self.access_token_url = reverse('access_token')
super(TestRevokeTokenView, self).setUp() super(TestRevokeTokenView, self).setUp()
response = self.client.post(self.access_token_url, self.access_token_post_body_with_password()) response = self.client.post(self.access_token_url, self.access_token_post_body_with_password())
access_token_data = json.loads(response.content) access_token_data = json.loads(response.content)
......
...@@ -7,11 +7,11 @@ from __future__ import unicode_literals ...@@ -7,11 +7,11 @@ from __future__ import unicode_literals
import json import json
from auth_exchange import views as auth_exchange_views
from django.views.generic import View from django.views.generic import View
from edx_oauth2_provider import views as dop_views # django-oauth2-provider views from edx_oauth2_provider import views as dop_views # django-oauth2-provider views
from oauth2_provider import models as dot_models, views as dot_views # django-oauth-toolkit from oauth2_provider import models as dot_models, views as dot_views # django-oauth-toolkit
from openedx.core.djangoapps.auth_exchange import views as auth_exchange_views
from openedx.core.lib.token_utils import JwtBuilder from openedx.core.lib.token_utils import JwtBuilder
from . import adapters from . import adapters
......
...@@ -27,7 +27,7 @@ from rest_framework.views import APIView ...@@ -27,7 +27,7 @@ from rest_framework.views import APIView
from rest_framework_oauth import permissions from rest_framework_oauth import permissions
from rest_framework_oauth.compat import oauth2_provider, oauth2_provider_scope from rest_framework_oauth.compat import oauth2_provider, oauth2_provider_scope
from lms.djangoapps.oauth_dispatch import adapters from openedx.core.djangoapps.oauth_dispatch import adapters
from openedx.core.lib.api import authentication from openedx.core.lib.api import authentication
factory = APIRequestFactory() # pylint: disable=invalid-name factory = APIRequestFactory() # pylint: disable=invalid-name
......
...@@ -4,7 +4,7 @@ from django.test import TestCase ...@@ -4,7 +4,7 @@ from django.test import TestCase
import jwt import jwt
from nose.plugins.attrib import attr from nose.plugins.attrib import attr
from lms.djangoapps.oauth_dispatch.tests import mixins from openedx.core.djangoapps.oauth_dispatch.tests import mixins
from openedx.core.lib.token_utils import JwtBuilder from openedx.core.lib.token_utils import JwtBuilder
from student.tests.factories import UserFactory, UserProfileFactory from student.tests.factories import UserFactory, UserProfileFactory
......
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