Commit 919b7b7b by Awais Jibran

fix-empty-file-name-in-response.

parent fb9de0a5
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -626,6 +626,10 @@ describe("OpenAssessment.ResponseView", function() { ...@@ -626,6 +626,10 @@ describe("OpenAssessment.ResponseView", function() {
function getFileUploadField() { function getFileUploadField() {
return $(view.element).find('.file__upload').first(); return $(view.element).find('.file__upload').first();
} }
function expectFileUploadButton(disabled) {
view.checkFilesDescriptions();
expect(getFileUploadField().is(':disabled')).toEqual(disabled);
}
spyOn(view, 'updateFilesDescriptionsFields').and.callThrough(); spyOn(view, 'updateFilesDescriptionsFields').and.callThrough();
var files = [{type: 'image/jpeg', size: 1024, name: 'picture1.jpg', data: ''}, var files = [{type: 'image/jpeg', size: 1024, name: 'picture1.jpg', data: ''},
...@@ -634,26 +638,36 @@ describe("OpenAssessment.ResponseView", function() { ...@@ -634,26 +638,36 @@ describe("OpenAssessment.ResponseView", function() {
expect(getFileUploadField().is(':disabled')).toEqual(true); expect(getFileUploadField().is(':disabled')).toEqual(true);
expect(view.updateFilesDescriptionsFields).toHaveBeenCalledWith(files, undefined, 'image'); expect(view.updateFilesDescriptionsFields).toHaveBeenCalledWith(files, undefined, 'image');
var firstDescriptionField1 = $(view.element).find('.file__description__0').first();
var firstDescriptionField2 = $(view.element).find('.file__description__1').first();
// set the first description field (the second is still empty) // Only set the first description field and the second field remain empty.
// and check that upload button is disabled // and check that upload button is disabled
var firstDescriptionField1 = $(view.element).find('.file__description__0').first(); $(firstDescriptionField1).val('test1');
$(firstDescriptionField1).val('test'); $(firstDescriptionField2).val('');
view.checkFilesDescriptions(); expectFileUploadButton(true);
expect(getFileUploadField().is(':disabled')).toEqual(true);
// set the second description field (now both descriptions are not empty) // Set the second description field to be only spaces (given first description has value).
// and check that upload button is disabled
$(firstDescriptionField2).val(' ');
expectFileUploadButton(true)
// Set the both description fields to contain only spaces.
// and check that upload button is disabled
$(firstDescriptionField1).val(' ');
$(firstDescriptionField2).val(' ');
expectFileUploadButton(true)
// set the both description field to contain non empty values.
// and check that upload button is enabled // and check that upload button is enabled
var firstDescriptionField2 = $(view.element).find('.file__description__1').first(); $(firstDescriptionField1).val('test1');
$(firstDescriptionField2).val('test2'); $(firstDescriptionField2).val('test2');
view.checkFilesDescriptions(); expectFileUploadButton(false)
expect(getFileUploadField().is(':disabled')).toEqual(false);
// remove value in the first upload field // remove value in the first upload field
// and check that upload button is disabled // and check that upload button is disabled
$(firstDescriptionField1).val(''); $(firstDescriptionField1).val('');
view.checkFilesDescriptions(); expectFileUploadButton(true)
expect(getFileUploadField().is(':disabled')).toEqual(true);
}); });
it("removes description fields after files upload", function() { it("removes description fields after files upload", function() {
......
...@@ -666,7 +666,7 @@ OpenAssessment.ResponseView.prototype = { ...@@ -666,7 +666,7 @@ OpenAssessment.ResponseView.prototype = {
var filesDescriptions = []; var filesDescriptions = [];
$(this.element).find('.file__description').each(function() { $(this.element).find('.file__description').each(function() {
var filesDescriptionVal = $(this).val(); var filesDescriptionVal = $.trim($(this).val());
if (filesDescriptionVal) { if (filesDescriptionVal) {
filesDescriptions.push(filesDescriptionVal); filesDescriptions.push(filesDescriptionVal);
} else { } else {
......
...@@ -400,24 +400,20 @@ class SubmissionMixin(object): ...@@ -400,24 +400,20 @@ class SubmissionMixin(object):
""" """
urls = [] urls = []
if 'file_keys' in submission['answer']: if 'file_keys' in submission['answer']:
keys = submission['answer'].get('file_keys', []) file_keys = submission['answer'].get('file_keys', [])
descriptions = submission['answer'].get('files_descriptions', []) descriptions = submission['answer'].get('files_descriptions', [])
for idx, key in enumerate(keys): for idx, key in enumerate(file_keys):
url = self._get_url_by_file_key(key) file_download_url = self._get_url_by_file_key(key)
if url: if file_download_url:
description = '' file_description = descriptions[idx].strip() if idx < len(descriptions) else ''
try: urls.append((file_download_url, file_description))
description = descriptions[idx]
except IndexError:
pass
urls.append((url, description))
else: else:
break break
elif 'file_key' in submission['answer']: elif 'file_key' in submission['answer']:
key = submission['answer'].get('file_key', '') key = submission['answer'].get('file_key', '')
url = self._get_url_by_file_key(key) file_download_url = self._get_url_by_file_key(key)
if url: if file_download_url:
urls.append((url, '')) urls.append((file_download_url, ''))
return urls return urls
@staticmethod @staticmethod
......
...@@ -175,8 +175,6 @@ class SubmissionTest(XBlockHandlerTestCase): ...@@ -175,8 +175,6 @@ class SubmissionTest(XBlockHandlerTestCase):
resp = self.request(xblock, 'download_url', json.dumps(dict()), response_format='json') resp = self.request(xblock, 'download_url', json.dumps(dict()), response_format='json')
self.assertTrue(resp['success']) self.assertTrue(resp['success'])
print download_url
print resp['url']
self.assertEqual(download_url, resp['url']) self.assertEqual(download_url, resp['url'])
@mock_s3 @mock_s3
...@@ -212,11 +210,10 @@ class SubmissionTest(XBlockHandlerTestCase): ...@@ -212,11 +210,10 @@ class SubmissionTest(XBlockHandlerTestCase):
course_id='test_course', course_id='test_course',
anonymous_student_id='test_student', anonymous_student_id='test_student',
) )
download_url = api.get_download_url("test_student/test_course/" + xblock.scope_ids.usage_id) download_url = api.get_download_url("test_student/test_course/" + xblock.scope_ids.usage_id)
resp = self.request(xblock, 'download_url', json.dumps(dict()), response_format='json') resp = self.request(xblock, 'download_url', json.dumps(dict()), response_format='json')
self.assertTrue(resp['success']) self.assertTrue(resp['success'])
self.assertEqual(download_url, resp['url']) self.assertTrue(resp['url'].startswith(download_url))
resp = self.request(xblock, 'remove_all_uploaded_files', json.dumps(dict()), response_format='json') resp = self.request(xblock, 'remove_all_uploaded_files', json.dumps(dict()), response_format='json')
self.assertTrue(resp['success']) self.assertTrue(resp['success'])
......
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