check for too long data in task_input field

This commit is contained in:
noraiz-anwar
2019-07-25 16:27:19 +05:00
committed by Noraiz Anwar
parent 544c68b1ff
commit ffeff8db3d
3 changed files with 34 additions and 2 deletions

View File

@@ -185,7 +185,7 @@ def common_exceptions_400(func):
message = _('User does not exist.')
except MultipleObjectsReturned:
message = _('Found a conflict with given identifier. Please try an alternative identifier')
except (AlreadyRunningError, QueueConnectionError) as err:
except (AlreadyRunningError, QueueConnectionError, AttributeError) as err:
message = six.text_type(err)
if use_json:

View File

@@ -28,6 +28,7 @@ from django.conf import settings
from django.contrib.auth.models import User
from django.core.files.base import ContentFile
from django.db import models, transaction
from django.utils.translation import ugettext as _
from opaque_keys.edx.django.models import CourseKeyField
from six import text_type
@@ -38,6 +39,7 @@ logger = logging.getLogger(__name__)
# define custom states used by InstructorTask
QUEUING = 'QUEUING'
PROGRESS = 'PROGRESS'
TASK_INPUT_LENGTH = 10000
class InstructorTask(models.Model):
@@ -101,6 +103,17 @@ class InstructorTask(models.Model):
task_id = str(uuid4())
json_task_input = json.dumps(task_input)
# check length of task_input, and return an exception if it's too long
if len(json_task_input) > TASK_INPUT_LENGTH:
logger.error(
u'Task input longer than: `%s` for `%s` of course: `%s`',
TASK_INPUT_LENGTH,
task_type,
course_id
)
error_msg = _('An error has occurred. Task was not created.')
raise AttributeError(error_msg)
# create the task, then save it:
instructor_task = cls(
course_id=course_id,

View File

@@ -14,10 +14,29 @@ from mock import patch
from opaque_keys.edx.locator import CourseLocator
from common.test.utils import MockS3Mixin
from lms.djangoapps.instructor_task.models import ReportStore
from lms.djangoapps.instructor_task.models import InstructorTask, ReportStore, TASK_INPUT_LENGTH
from lms.djangoapps.instructor_task.tests.test_base import TestReportMixin
class TestInstructorTasksModel(TestCase):
"""
Test validations in instructor task model
"""
def test_task_input_valid_length(self):
"""
Test allowed length of task_input field
"""
task_input = 's' * TASK_INPUT_LENGTH
with self.assertRaises(AttributeError):
InstructorTask.create(
course_id='dummy_course_id',
task_type='dummy type',
task_key='dummy key',
task_input=task_input,
requester='dummy requester',
)
class ReportStoreTestMixin(object):
"""
Mixin for report store tests.