Unverified Commit 60c7bdc9 by M. Rehan Committed by GitHub

Merge pull request #58 from edx/mrehan/error-types

Introduce error types in credentials api
parents 7d9cf989 498a73ef
PEP8_THRESHOLD=49 PEP8_THRESHOLD=46
PYLINT_THRESHOLD=761 PYLINT_THRESHOLD=761
production-requirements: production-requirements:
......
"""
Module containing all the Enumerations used in the API.
"""
class TranscriptionProviderErrorType:
"""
Transcription provider's errors enumeration.
"""
INVALID_CREDENTIALS = 1
INVALID_PROVIDER = 2
MISSING_REQUIRED_ATTRIBUTES = 3
...@@ -239,8 +239,7 @@ class Cielo24TranscriptTests(APITestCase): ...@@ -239,8 +239,7 @@ class Cielo24TranscriptTests(APITestCase):
self.assertTrue( self.assertTrue(
responses.calls[0].request.url, responses.calls[0].request.url,
('http://api.cielo24.com/job/get_caption' ('http://api.cielo24.com/job/get_caption'
'?api_token=i_am_key&job_id=%28100%2C%29&caption_format=SRT&v={cielo_api_version}' '?api_token=i_am_key&job_id=%28100%2C%29&caption_format=SRT&v={cielo_api_version}').format(
).format(
cielo_api_version=transcripts.CIELO24_API_VERSION cielo_api_version=transcripts.CIELO24_API_VERSION
) )
) )
......
...@@ -9,9 +9,11 @@ from mock import patch ...@@ -9,9 +9,11 @@ from mock import patch
from rest_framework import status from rest_framework import status
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
from VEDA_OS01.enums import TranscriptionProviderErrorType
from VEDA_OS01.models import TranscriptCredentials, TranscriptProvider from VEDA_OS01.models import TranscriptCredentials, TranscriptProvider
from VEDA_OS01.views import CIELO24_LOGIN_URL from VEDA_OS01.views import CIELO24_LOGIN_URL
@ddt @ddt
class TranscriptCredentialsTest(APITestCase): class TranscriptCredentialsTest(APITestCase):
""" """
...@@ -48,8 +50,6 @@ class TranscriptCredentialsTest(APITestCase): ...@@ -48,8 +50,6 @@ class TranscriptCredentialsTest(APITestCase):
) )
response_status_code = response.status_code response_status_code = response.status_code
response = json.loads(response.content)
self.assertEqual(response_status_code, status.HTTP_401_UNAUTHORIZED) self.assertEqual(response_status_code, status.HTTP_401_UNAUTHORIZED)
@data( @data(
...@@ -76,7 +76,10 @@ class TranscriptCredentialsTest(APITestCase): ...@@ -76,7 +76,10 @@ class TranscriptCredentialsTest(APITestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
response = json.loads(response.content) response = json.loads(response.content)
self.assertEqual(response['message'], 'Invalid provider {provider}.'.format(provider=provider)) self.assertDictEqual(response, {
'message': 'Invalid provider {provider}.'.format(provider=provider),
'error_type': TranscriptionProviderErrorType.INVALID_PROVIDER
})
@data( @data(
( (
...@@ -133,7 +136,10 @@ class TranscriptCredentialsTest(APITestCase): ...@@ -133,7 +136,10 @@ class TranscriptCredentialsTest(APITestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
response = json.loads(response.content) response = json.loads(response.content)
self.assertEqual(response['message'], error_message) self.assertDictEqual(response, {
'message': error_message,
'error_type': TranscriptionProviderErrorType.MISSING_REQUIRED_ATTRIBUTES
})
@data( @data(
{ {
...@@ -206,7 +212,10 @@ class TranscriptCredentialsTest(APITestCase): ...@@ -206,7 +212,10 @@ class TranscriptCredentialsTest(APITestCase):
) )
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
response = json.loads(response.content) response = json.loads(response.content)
self.assertEqual(response['message'], error_message) self.assertDictEqual(response, {
'message': error_message,
'error_type': TranscriptionProviderErrorType.INVALID_CREDENTIALS
})
mock_logger.warning.assert_called_with( mock_logger.warning.assert_called_with(
'[Transcript Credentials] Unable to get api token -- response %s -- status %s.', '[Transcript Credentials] Unable to get api token -- response %s -- status %s.',
json.dumps({'error': error_message}), json.dumps({'error': error_message}),
......
...@@ -16,6 +16,7 @@ from rest_framework.views import APIView ...@@ -16,6 +16,7 @@ from rest_framework.views import APIView
from api import token_finisher from api import token_finisher
from VEDA import utils from VEDA import utils
from VEDA_OS01.enums import TranscriptionProviderErrorType
from VEDA_OS01.models import Course, Video, URL, Encode, TranscriptCredentials, TranscriptProvider from VEDA_OS01.models import Course, Video, URL, Encode, TranscriptCredentials, TranscriptProvider
from VEDA_OS01.serializers import CourseSerializer, EncodeSerializer, VideoSerializer, URLSerializer from VEDA_OS01.serializers import CourseSerializer, EncodeSerializer, VideoSerializer, URLSerializer
from VEDA_OS01.transcripts import CIELO24_API_VERSION from VEDA_OS01.transcripts import CIELO24_API_VERSION
...@@ -30,6 +31,7 @@ CIELO24_LOGIN_URL = utils.build_url( ...@@ -30,6 +31,7 @@ CIELO24_LOGIN_URL = utils.build_url(
'/account/login' '/account/login'
) )
class CourseViewSet(viewsets.ModelViewSet): class CourseViewSet(viewsets.ModelViewSet):
queryset = Course.objects.all() queryset = Course.objects.all()
...@@ -159,7 +161,7 @@ class TranscriptCredentialsView(APIView): ...@@ -159,7 +161,7 @@ class TranscriptCredentialsView(APIView):
missing=' and '.join(missing) missing=' and '.join(missing)
) )
return error_message return TranscriptionProviderErrorType.MISSING_REQUIRED_ATTRIBUTES, error_message
def validate_transcript_credentials(self, provider, **credentials): def validate_transcript_credentials(self, provider, **credentials):
""" """
...@@ -171,11 +173,11 @@ class TranscriptCredentialsView(APIView): ...@@ -171,11 +173,11 @@ class TranscriptCredentialsView(APIView):
3PlayMedia - 'api_key' and 'api_secret_key' are required. 3PlayMedia - 'api_key' and 'api_secret_key' are required.
Cielo24 - Valid 'api_key' and 'username' are required. Cielo24 - Valid 'api_key' and 'username' are required.
""" """
error_message, validated_credentials = '', {} error_type, error_message, validated_credentials = None, '', {}
if provider in [TranscriptProvider.CIELO24, TranscriptProvider.THREE_PLAY]: if provider in [TranscriptProvider.CIELO24, TranscriptProvider.THREE_PLAY]:
if provider == TranscriptProvider.CIELO24: if provider == TranscriptProvider.CIELO24:
must_have_props = ('org', 'api_key', 'username') must_have_props = ('org', 'api_key', 'username')
error_message = self.validate_missing_attributes(provider, must_have_props, credentials) error_type, error_message = self.validate_missing_attributes(provider, must_have_props, credentials)
if not error_message: if not error_message:
# Get cielo api token and store it in api_key. # Get cielo api token and store it in api_key.
...@@ -187,9 +189,10 @@ class TranscriptCredentialsView(APIView): ...@@ -187,9 +189,10 @@ class TranscriptCredentialsView(APIView):
}) })
else: else:
error_message = u'Invalid credentials supplied.' error_message = u'Invalid credentials supplied.'
error_type = TranscriptionProviderErrorType.INVALID_CREDENTIALS
else: else:
must_have_props = ('org', 'api_key', 'api_secret_key') must_have_props = ('org', 'api_key', 'api_secret_key')
error_message = self.validate_missing_attributes(provider, must_have_props, credentials) error_type, error_message = self.validate_missing_attributes(provider, must_have_props, credentials)
if not error_message: if not error_message:
validated_credentials.update({ validated_credentials.update({
'org': credentials['org'], 'org': credentials['org'],
...@@ -198,8 +201,9 @@ class TranscriptCredentialsView(APIView): ...@@ -198,8 +201,9 @@ class TranscriptCredentialsView(APIView):
}) })
else: else:
error_message = u'Invalid provider {provider}.'.format(provider=provider) error_message = u'Invalid provider {provider}.'.format(provider=provider)
error_type = TranscriptionProviderErrorType.INVALID_PROVIDER
return error_message, validated_credentials return error_type, error_message, validated_credentials
def post(self, request): def post(self, request):
""" """
...@@ -239,16 +243,19 @@ class TranscriptCredentialsView(APIView): ...@@ -239,16 +243,19 @@ class TranscriptCredentialsView(APIView):
In case of error: In case of error:
Return response with error message and 400 status code (HTTP 400 Bad Request). Return response with error message and 400 status code (HTTP 400 Bad Request).
{ {
"error_type": INVALID_CREDENTIALS
"message": "Error message." "message": "Error message."
} }
""" """
# Validate credentials # Validate credentials
provider = request.data.pop('provider', None) provider = request.data.pop('provider', None)
error_message, validated_credentials = self.validate_transcript_credentials(provider=provider, **request.data) error_type, error_message, validated_credentials = self.validate_transcript_credentials(
provider=provider, **request.data
)
if error_message: if error_message:
return Response( return Response(
status=status.HTTP_400_BAD_REQUEST, status=status.HTTP_400_BAD_REQUEST,
data=dict(message=error_message) data=dict(error_type=error_type, message=error_message)
) )
TranscriptCredentials.objects.update_or_create( TranscriptCredentials.objects.update_or_create(
......
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