change pipeline api client to OAuthApiClient (#24080)

* change pipeline API client to OAuthApiClient
This commit is contained in:
Syed Muhammad Dawoud Sheraz Ali
2020-05-28 19:48:14 +05:00
committed by GitHub
parent 9bdc20b0c2
commit 9929b52907
3 changed files with 32 additions and 43 deletions

View File

@@ -31,30 +31,34 @@ def update_3rd_party_transcription_service_credentials(**credentials_payload):
pipeline_integration = VideoPipelineIntegration.current()
if pipeline_integration.enabled:
try:
video_pipeline_user = pipeline_integration.get_service_user()
oauth_client = Application.objects.get(name=pipeline_integration.client_name)
except ObjectDoesNotExist:
return error_response, is_updated
client = create_video_pipeline_api_client(
user=video_pipeline_user,
api_client_id=oauth_client.client_id,
api_client_secret=oauth_client.client_secret,
api_url=pipeline_integration.api_url
oauth_client.client_id,
oauth_client.client_secret
)
error_message = "Unable to update transcript credentials -- org={}, provider={}, response={}"
try:
client.transcript_credentials.post(credentials_payload)
is_updated = True
response = client.request("POST", pipeline_integration.api_url, json=credentials_payload)
if response.ok:
is_updated = True
else:
is_updated = False
error_response = json.loads(response.text)
log.error(error_message.format(
credentials_payload.get('org'),
credentials_payload.get('provider'),
response.text
))
except HttpClientError as ex:
is_updated = False
log.exception(
('[video-pipeline-service] Unable to update transcript credentials '
u'-- org=%s -- provider=%s -- response=%s.'),
log.exception(error_message.format(
credentials_payload.get('org'),
credentials_payload.get('provider'),
ex.content,
)
ex.content
))
error_response = json.loads(ex.content)
return error_response, is_updated

View File

@@ -34,15 +34,6 @@ class TestAPIUtils(VideoPipelineIntegrationMixin, TestCase):
__, is_updated = update_3rd_party_transcription_service_credentials()
self.assertFalse(is_updated)
def test_update_transcription_service_credentials_with_unknown_user(self):
"""
Test updating the credentials when expected service user is not registered.
"""
self.pipeline_integration.service_username = 'non_existent_user'
self.pipeline_integration.save()
__, is_updated = update_3rd_party_transcription_service_credentials()
self.assertFalse(is_updated)
def test_update_transcription_service_credentials_with_unknown_oauth_client(self):
"""
Test updating the credentials when expected oauth cleint is not present.
@@ -63,33 +54,31 @@ class TestAPIUtils(VideoPipelineIntegrationMixin, TestCase):
}
)
@patch('openedx.core.djangoapps.video_pipeline.api.log')
@patch('openedx.core.djangoapps.video_pipeline.utils.EdxRestApiClient')
@patch('openedx.core.djangoapps.video_pipeline.utils.OAuthAPIClient')
def test_update_transcription_service_credentials(self, credentials_payload, mock_client, mock_logger):
"""
Tests that the update transcription service credentials api util works as expected.
"""
# Mock the post request
mock_credentials_endpoint = mock_client.return_value.transcript_credentials
mock_client.request.return_value.ok = True
# Try updating the transcription service credentials
error_response, is_updated = update_3rd_party_transcription_service_credentials(**credentials_payload)
mock_credentials_endpoint.post.assert_called_with(credentials_payload)
# Making sure log.exception is not called.
self.assertDictEqual(error_response, {})
self.assertFalse(mock_logger.exception.called)
self.assertTrue(is_updated)
@patch('openedx.core.djangoapps.video_pipeline.api.log')
@patch('openedx.core.djangoapps.video_pipeline.utils.EdxRestApiClient')
@patch('openedx.core.djangoapps.video_pipeline.utils.OAuthAPIClient')
def test_update_transcription_service_credentials_exceptions(self, mock_client, mock_logger):
"""
Tests that the update transcription service credentials logs the exception occurring
during communication with edx-video-pipeline.
"""
error_content = '{"error_type": "1"}'
# Mock the post request
mock_credentials_endpoint = mock_client.return_value.transcript_credentials
mock_credentials_endpoint.post = Mock(side_effect=HttpClientError(content=error_content))
mock_client.return_value.request = Mock(side_effect=HttpClientError(content=error_content))
# try updating the transcription service credentials
credentials_payload = {
'org': 'mit',
@@ -98,13 +87,13 @@ class TestAPIUtils(VideoPipelineIntegrationMixin, TestCase):
}
error_response, is_updated = update_3rd_party_transcription_service_credentials(**credentials_payload)
mock_credentials_endpoint.post.assert_called_with(credentials_payload)
# Assert the results.
self.assertFalse(is_updated)
self.assertDictEqual(error_response, json.loads(error_content))
mock_logger.exception.assert_called_with(
u'[video-pipeline-service] Unable to update transcript credentials -- org=%s -- provider=%s -- response=%s.',
credentials_payload['org'],
credentials_payload['provider'],
error_content
'Unable to update transcript credentials -- org={}, provider={}, response={}'.format(
credentials_payload['org'],
credentials_payload['provider'],
error_content
)
)

View File

@@ -1,21 +1,17 @@
"""
Utils for video_pipeline app.
"""
from django.conf import settings
from edx_rest_api_client.client import EdxRestApiClient
from openedx.core.djangoapps.oauth_dispatch.jwt import create_jwt_for_user
from edx_rest_api_client.client import OAuthAPIClient
def create_video_pipeline_api_client(user, api_client_id, api_client_secret, api_url):
def create_video_pipeline_api_client(api_client_id, api_client_secret):
"""
Returns an API client which can be used to make Video Pipeline API requests.
Arguments:
user(User): A requesting user.
api_client_id(unicode): Video pipeline client id.
api_client_secret(unicode): Video pipeline client secret.
api_url(unicode): It is video pipeline's API URL.
"""
jwt_token = create_jwt_for_user(user, secret=api_client_secret, aud=api_client_id)
return EdxRestApiClient(api_url, jwt=jwt_token)
return OAuthAPIClient(settings.LMS_ROOT_URL, api_client_id, api_client_secret)