Commit 249b1061 by Michael Frey

Merge pull request #744 from edx/mjfrey/course-discovery-util

Utility to access course discovery via edx-rest-api
parents e7aba82b 1ce09d49
import mock import mock
from django.conf import settings
from edx_rest_api_client.auth import SuppliedJwtAuth
from edx_rest_api_client.client import EdxRestApiClient
from ecommerce.core.exceptions import MissingRequestError from ecommerce.core.exceptions import MissingRequestError
from ecommerce.core.url_utils import get_ecommerce_url, get_lms_url from ecommerce.core.url_utils import(get_course_catalog_api_client,
get_ecommerce_url, get_lms_url,
get_oauth2_provider_url)
from ecommerce.tests.testcases import TestCase from ecommerce.tests.testcases import TestCase
...@@ -15,3 +21,17 @@ class UrlUtilityTests(TestCase): ...@@ -15,3 +21,17 @@ class UrlUtilityTests(TestCase):
def test_get_lms_url_with_no_current_request(self): def test_get_lms_url_with_no_current_request(self):
with self.assertRaises(MissingRequestError): with self.assertRaises(MissingRequestError):
get_lms_url() get_lms_url()
@mock.patch.object(EdxRestApiClient, 'get_oauth_access_token')
def test_get_course_catalog_api_client(self, mock_method):
mock_method.return_value = ('auth-token', 'now')
api = get_course_catalog_api_client(self.site)
mock_method.assert_called_with('{root}/access_token'.format(root=get_oauth2_provider_url()),
self.site.siteconfiguration.oauth_settings['SOCIAL_AUTH_EDX_OIDC_KEY'],
self.site.siteconfiguration.oauth_settings['SOCIAL_AUTH_EDX_OIDC_SECRET'],
token_type='jwt')
api_session = api._store['session'] # pylint: disable=protected-access
self.assertEqual('auth-token', api_session.auth.token)
self.assertEqual(SuppliedJwtAuth, type(getattr(api_session, 'auth')))
self.assertEqual(settings.COURSE_CATALOG_API_URL, api._store['base_url']) # pylint: disable=protected-access
from urlparse import urljoin from urlparse import urljoin
from django.conf import settings
from edx_rest_api_client.client import EdxRestApiClient
from threadlocals.threadlocals import get_current_request from threadlocals.threadlocals import get_current_request
from ecommerce.core.exceptions import MissingRequestError from ecommerce.core.exceptions import MissingRequestError
...@@ -50,3 +52,27 @@ def get_lms_url(path=''): ...@@ -50,3 +52,27 @@ def get_lms_url(path=''):
def get_oauth2_provider_url(): def get_oauth2_provider_url():
return get_lms_url('/oauth2') return get_lms_url('/oauth2')
def get_course_catalog_api_client(site):
"""
Returns an API client to access the Course Catalog service.
Arguments:
site (Site): The site for which to retrieve settings.
Returns:
EdxRestApiClient: The client to access the Course Catalog service.
"""
access_token, __ = EdxRestApiClient.get_oauth_access_token(
'{root}/access_token'.format(root=get_oauth2_provider_url()),
site.siteconfiguration.oauth_settings['SOCIAL_AUTH_EDX_OIDC_KEY'],
site.siteconfiguration.oauth_settings['SOCIAL_AUTH_EDX_OIDC_SECRET'],
token_type='jwt'
)
course_catalog_client = EdxRestApiClient(
settings.COURSE_CATALOG_API_URL,
jwt=access_token
)
return course_catalog_client
...@@ -498,6 +498,9 @@ SUPPORT_URL = 'SET-ME-PLEASE' ...@@ -498,6 +498,9 @@ SUPPORT_URL = 'SET-ME-PLEASE'
# Path to the receipt page # Path to the receipt page
RECEIPT_PAGE_PATH = '/commerce/checkout/receipt/' RECEIPT_PAGE_PATH = '/commerce/checkout/receipt/'
# URL for Course Catalog service
COURSE_CATALOG_API_URL = 'http://localhost:8008/api/v1/'
# Black-listed course modes not allowed to create coupons with # Black-listed course modes not allowed to create coupons with
BLACK_LIST_COUPON_COURSE_MODES = [u'audit', u'honor'] BLACK_LIST_COUPON_COURSE_MODES = [u'audit', u'honor']
......
...@@ -257,7 +257,11 @@ class SiteMixin(object): ...@@ -257,7 +257,11 @@ class SiteMixin(object):
partner__name='edX', partner__name='edX',
site__id=settings.SITE_ID, site__id=settings.SITE_ID,
site__domain=domain, site__domain=domain,
segment_key='fake_segment_key' segment_key='fake_segment_key',
oauth_settings={
'SOCIAL_AUTH_EDX_OIDC_KEY': 'key',
'SOCIAL_AUTH_EDX_OIDC_SECRET': 'secret'
}
) )
self.partner = site_configuration.partner self.partner = site_configuration.partner
self.site = site_configuration.site self.site = site_configuration.site
......
...@@ -18,7 +18,7 @@ edx-django-sites-extensions==1.0.0 ...@@ -18,7 +18,7 @@ edx-django-sites-extensions==1.0.0
edx-drf-extensions==0.5.1 edx-drf-extensions==0.5.1
edx-ecommerce-worker==0.3.3 edx-ecommerce-worker==0.3.3
edx-opaque-keys==0.3.1 edx-opaque-keys==0.3.1
edx-rest-api-client==1.5.0 edx-rest-api-client==1.6.0
jsonfield==1.0.3 jsonfield==1.0.3
libsass==0.9.2 libsass==0.9.2
ndg-httpsclient==0.4.0 ndg-httpsclient==0.4.0
......
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