diff --git a/app/assets/javascripts/attachments.js b/app/assets/javascripts/attachments.js index 1650386b8..9e6441a54 100644 --- a/app/assets/javascripts/attachments.js +++ b/app/assets/javascripts/attachments.js @@ -350,7 +350,10 @@ function copyImageFromClipboard(e) { // get input file in the closest form var inputEl = $(this).closest("form").find('input:file.filedrop'); handleFileDropEvent.target = e.target; - addFile(inputEl, new File([file], filename, { type: file.type }), true); + if (addFile(inputEl, new File([file], filename, { type: file.type }), true) == null) { + window.alert(inputEl.data('max-number-of-files-message')); + break; + } } } } diff --git a/test/system/issues_test.rb b/test/system/issues_test.rb index ee7e4def9..4cd02bc14 100644 --- a/test/system/issues_test.rb +++ b/test/system/issues_test.rb @@ -174,6 +174,30 @@ class IssuesSystemTest < ApplicationSystemTestCase assert_equal 'Some description', issue.attachments.first.description end + def test_pasting_image_beyond_max_attachments_at_once_should_show_error + set_tmp_attachments_directory + log_user('jsmith', 'jsmith') + + visit '/projects/ecookbook/issues/new' + paste_image = lambda do + page.execute_script(<<~JS) + // 1x1 1-bit grayscale PNG image + const png = Uint8Array.from(atob('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVR42mNgAAAAAgAB5Sfe/AAAAABJRU5ErkJggg=='), c => c.charCodeAt(0)); + const clipboardData = new DataTransfer(); + clipboardData.items.add(new File([png], 'image.png', { type: 'image/png' })); + const event = new Event('paste', { bubbles: true, cancelable: true }); + Object.defineProperty(event, 'clipboardData', { value: clipboardData }); + document.getElementById('issue_description').dispatchEvent(event); + JS + end + 10.times { paste_image.call } + assert_selector '.attachments_fields > span', :count => 10 + accept_alert(/maximum number of files that can be attached simultaneously \(10\)/) do + paste_image.call + end + assert_selector '.attachments_fields > span', :count => 10 + end + def test_create_issue_with_new_target_version log_user('jsmith', 'jsmith')