From e92c87f7500d0543cb95ab007046b9c91ba1a763 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Fri, 29 Jan 2016 14:28:51 -0300 Subject: testing filesize validation, on attachments Issue #550 --- web-ui/app/js/mail_view/ui/attachment_list.js | 77 +++++++++++----------- .../test/spec/mail_view/ui/attachment_list.spec.js | 23 +++++++ 2 files changed, 63 insertions(+), 37 deletions(-) (limited to 'web-ui') diff --git a/web-ui/app/js/mail_view/ui/attachment_list.js b/web-ui/app/js/mail_view/ui/attachment_list.js index eb8ae12a..9a5fd3a9 100644 --- a/web-ui/app/js/mail_view/ui/attachment_list.js +++ b/web-ui/app/js/mail_view/ui/attachment_list.js @@ -61,46 +61,49 @@ define( return templates.compose.attachmentItem(attachmentData); }; + this.checkAttachmentSize = function(e, data) { + var self = this; + var uploadError = self.select('uploadError'); + if (uploadError) { + uploadError.remove(); + } + + var uploadErrors = []; + + var showUploadFailed = function () { + var html = $(templates.compose.uploadAttachmentFailed()); + html.insertAfter(self.select('attachmentListItem')); + + self.on(self.select('closeIcon'), 'click', dismissUploadFailed); + self.on(self.select('dismissButton'), 'click', dismissUploadFailed); + self.on(self.select('uploadFileButton'), 'click', uploadAnotherFile); + }; + + var dismissUploadFailed = function (event) { + event.preventDefault(); + self.select('uploadError').remove(); + }; + + var uploadAnotherFile = function (event) { + event.preventDefault(); + self.startUpload(); + }; + + var ONE_MEGABYTE = 1000000; + if (data.originalFiles[0].size > ONE_MEGABYTE) { + uploadErrors.push('Filesize is too big'); + } + if (uploadErrors.length > 0) { + showUploadFailed(); + } else { + data.submit(); + } + }; + this.addJqueryFileUploadConfig = function() { var self = this; this.select('inputFileUpload').fileupload({ - add: function(e, data) { - var uploadError = self.select('uploadError'); - if (uploadError) { - uploadError.remove(); - } - - var uploadErrors = []; - - this.showUploadFailed = function () { - var html = $(templates.compose.uploadAttachmentFailed()); - html.insertAfter(self.$node.find(self.attr.attachmentListItem)); - - self.on(self.select('closeIcon'), 'click', this.dismissUploadFailed); - self.on(self.select('dismissButton'), 'click', this.dismissUploadFailed); - self.on(self.select('uploadFileButton'), 'click', this.uploadAnotherFile); - }; - - this.dismissUploadFailed = function (event) { - event.preventDefault(); - self.select('uploadError').remove(); - }; - - this.uploadAnotherFile = function (event) { - event.preventDefault(); - self.startUpload(); - }; - - var ONE_MEGABYTE = 1000000; - if (data.originalFiles[0].size > ONE_MEGABYTE) { - uploadErrors.push('Filesize is too big'); - } - if (uploadErrors.length > 0) { - this.showUploadFailed(); - } else { - data.submit(); - } - }, + add: function(e, data) { self.checkAttachmentSize(e, data); }, url: self.attr.attachmentBaseUrl, dataType: 'json', done: function (e, response) { diff --git a/web-ui/test/spec/mail_view/ui/attachment_list.spec.js b/web-ui/test/spec/mail_view/ui/attachment_list.spec.js index 2308c227..9f14031e 100644 --- a/web-ui/test/spec/mail_view/ui/attachment_list.spec.js +++ b/web-ui/test/spec/mail_view/ui/attachment_list.spec.js @@ -39,6 +39,29 @@ describeMixin('mail_view/ui/attachment_list', function () { expect(this.component.select('attachmentListItem').html()).toContain('(4.39 Kb'); }); + it('should not upload files larger than 1MB', function () { + var submitFile = 'file not submitted', submitted = 'file submitted'; + var mockSubmit = function (){ submitFile = submitted; }; + var largeAttachment = {originalFiles: [{size: 4500000}], submit: mockSubmit}; + spyOn(largeAttachment, 'submit'); + var dummyEvent = 'whatever, not used'; + + this.component.checkAttachmentSize(dummyEvent, largeAttachment); + + expect(largeAttachment.submit).not.toHaveBeenCalled(); + }); + + it('should upload files smaller than 1MB', function () { + var submitFile = 'file not submitted', submitted = 'file submitted'; + var mockSubmit = function (){ submitFile = submitted; }; + var largeAttachment = {originalFiles: [{size: 450}], submit: mockSubmit}; + var dummyEvent = 'whatever, not used'; + + this.component.checkAttachmentSize(dummyEvent, largeAttachment); + + expect(submitFile).toEqual(submitted); + }); + xit('should start uploading attachments', function () { var stubAttachment = {ident: 'faked', name: 'haha.txt', size: 4500}; var mockAjax = spyOn($, 'ajax').and.callFake(function (params) {params.success(stubAttachment);}); -- cgit v1.2.3