Commit 2f0bcab9 by Bill DeRusha

Add client_credential flow to refresh management cmd

ECOM-3602
parent 330ea8bc
import logging
from django.core.management import BaseCommand, CommandError
from django.conf import settings
from django.core.management import BaseCommand
from edx_rest_api_client.client import EdxRestApiClient
from course_discovery.apps.courses.models import Course
......@@ -23,8 +25,16 @@ class Command(BaseCommand):
access_token = options.get('access_token')
if not access_token:
msg = 'Courses cannot be migrated if no access token is supplied.'
logger.error(msg)
raise CommandError(msg)
logger.info('No access token provided. Retrieving access token using client_credential flow...')
try:
access_token, __ = EdxRestApiClient.get_oauth_access_token(
'{root}/access_token'.format(root=settings.SOCIAL_AUTH_EDX_OIDC_URL_ROOT),
settings.SOCIAL_AUTH_EDX_OIDC_KEY,
settings.SOCIAL_AUTH_EDX_OIDC_SECRET
)
except Exception:
logger.exception('No access token provided or acquired through client_credential flow.')
raise
Course.refresh_all(access_token=access_token)
import mock
from django.core.management import CommandError, call_command
""" Tests for Refresh All Courses management command. """
from django.core.management import call_command
from django.test import TestCase
from django.test.utils import override_settings
from edx_rest_api_client.client import EdxRestApiClient
import httpretty
from mock import patch
from course_discovery.apps.courses.models import Course
@override_settings(
SOCIAL_AUTH_EDX_OIDC_URL_ROOT="http://auth-url.com/oauth2",
SOCIAL_AUTH_EDX_OIDC_KEY="client_id",
SOCIAL_AUTH_EDX_OIDC_SECRET="client_secret"
)
class RefreshAllCoursesCommandTests(TestCase):
""" Tests for refresh_all_courses management command. """
cmd = 'refresh_all_courses'
def test_call(self):
""" Verify the management command calls Course.refresh_all(). """
def test_call_with_access_token(self):
""" Verify the management command calls Course.refresh_all() with access token. """
access_token = 'secret'
with mock.patch('course_discovery.apps.courses.models.Course.refresh_all') as mock_refresh:
with patch.object(Course, 'refresh_all') as mock_refresh:
call_command(self.cmd, access_token=access_token)
mock_refresh.assert_called_once_with(access_token=access_token)
def test_call_without_access_token(self):
""" Verify the command requires an access token. """
with self.assertRaisesRegex(CommandError, 'Courses cannot be migrated if no access token is supplied.'):
call_command(self.cmd)
@httpretty.activate
def test_call_with_client_credentials(self):
""" Verify the management command calls Course.refresh_all() with client credentials. """
access_token = 'secret'
with patch.object(EdxRestApiClient, 'get_oauth_access_token') as mock_access_token:
mock_access_token.return_value = (access_token, None)
with patch.object(Course, 'refresh_all') as mock_refresh:
call_command(self.cmd)
mock_refresh.assert_called_once_with(access_token=access_token)
@httpretty.activate
def test_call_with_client_credentials_error(self):
""" Verify the command requires an access token to complete. """
with patch.object(EdxRestApiClient, 'get_oauth_access_token') as mock_access_token:
mock_access_token.side_effect = Exception()
with self.assertRaises(Exception):
call_command(self.cmd)
Getting Started
===============
If you have not already done so, create/activate a `virtualenv`_. Unless otherwise stated, assume all terminal code
If you have not already done so, create/activate a `virtualenv`_ running Python 3. Unless otherwise stated, assume all terminal code
below is executed within the virtualenv.
.. _virtualenv: https://virtualenvwrapper.readthedocs.org/en/latest/
......
......@@ -5,6 +5,6 @@ djangorestframework == 3.3.1
djangorestframework-jwt==1.7.2
django-rest-swagger[reST]==0.3.4
edx-auth-backends == 0.1.3
edx-rest-api-client==1.2.1
edx-rest-api-client==1.5.0
elasticsearch>=1.0.0,<2.0.0
pytz == 2015.7
......@@ -7,6 +7,7 @@ django-dynamic-fixture == 1.8.5
django-nose == 1.4.2
edx-lint == 0.4.0
factory-boy==2.6.0
httpretty==0.8.14
mock == 1.3.0
nose-ignore-docstring == 0.2
pep8 == 1.6.2
......
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