Skip to content

Commit

Permalink
3666 disable dl btn when non dlable is checked files (#4008)
Browse files Browse the repository at this point in the history
* Adds failing test for expected behavior

* Adds code to make test pass

* Re-enables download button when non-downloadable file is unchecked

* Only allows DL button to be re-enabled if all non-downloadable files are unchecked

* Small refactor
  • Loading branch information
HazelGrant authored Dec 13, 2024
1 parent dd9d8c0 commit d219198
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions apps/dashboard/app/javascript/files/data_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ class DataTable {
data: null,
orderable: false,
defaultContent: '<input type="checkbox">',
render: (data, type, row, meta) => {
return `<input type='checkbox' data-dl-url='${row.download_url}'>`;
}
},
{ data: 'type', render: (data, type, row, meta) => data == 'd' ? '<span title="directory" class="fa fa-folder" style="color: gold"><span class="sr-only"> dir</span></span>' : '<span title="file" class="fa fa-file" style="color: lightgrey"><span class="sr-only"> file</span></span>' }, // type
{ name: 'name', data: 'name', className: 'text-break', render: (data, type, row, meta) => `<a class="${row.type} name ${row.type == 'd' ? '' : 'view-file'}" href="${row.url}">${Handlebars.escapeExpression(data)}</a>` }, // name
Expand Down
8 changes: 8 additions & 0 deletions apps/dashboard/app/javascript/files/file_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ jQuery(function() {

}
});

$('#directory-contents tbody').on('click', 'tr td:first-child input[type=checkbox]', function (e) {
if (this.dataset['dlUrl'] == 'undefined' && this.checked) {
$("#download-btn").attr('disabled', true);
} else if ($("input[data-dl-url='undefined']:checked" ).length == 0) {
$("#download-btn").attr('disabled', false);
}
});

$('#directory-contents tbody').on('dblclick', 'tr td:not(:first-child)', function(){
// handle doubleclick
Expand Down
73 changes: 72 additions & 1 deletion apps/dashboard/test/system/files_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def setup

sleep 5 # give it enough time to download
assert(File.exist?(zip_file), "#{zip_file} was never downloaded!")

Dir.mktmpdir do |unzip_tmp_dir|
`cd #{unzip_tmp_dir}; unzip #{zip_file}`
assert(File.exist?("#{unzip_tmp_dir}/real_directory"))
Expand Down Expand Up @@ -656,6 +656,77 @@ def setup
assert_equal(expected_links, null_links)
end

test 'download button is disabled when non-downloadable item is checked' do
Dir.mktmpdir do |dir|
cant_read = 'cant_read.txt'
can_read = 'can_read.txt'

`touch #{dir}/#{can_read}`
`touch #{dir}/#{cant_read}`
`chmod 000 #{dir}/#{cant_read}`

visit files_url(dir)

can_read_row = find('tbody a', exact_text: can_read).ancestor('tr')
cant_read_row = find('tbody a', exact_text: cant_read).ancestor('tr')

can_read_row.find('input[type="checkbox"]').check

refute find("#download-btn").disabled?

cant_read_row.find('input[type="checkbox"]').check

assert find("#download-btn").disabled?
end
end

test 'download button is re-enabled when non-downloadable item is unchecked' do
Dir.mktmpdir do |dir|
cant_read = 'cant_read.txt'

`touch #{dir}/#{cant_read}`
`chmod 000 #{dir}/#{cant_read}`

visit files_url(dir)

cant_read_row = find('tbody a', exact_text: cant_read).ancestor('tr')
cant_read_row.find('input[type="checkbox"]').check
assert find("#download-btn").disabled?

cant_read_row.find('input[type="checkbox"]').uncheck
refute find("#download-btn").disabled?
end
end

test 'download button is NOT re-enabled until ALL non-downloadable files are unchecked' do
Dir.mktmpdir do |dir|
cant_read1 = 'cant_read1.txt'
cant_read2 = 'cant_read2.txt'

`touch #{dir}/#{cant_read1}`
`touch #{dir}/#{cant_read2}`
`chmod 000 #{dir}/#{cant_read1}`
`chmod 000 #{dir}/#{cant_read2}`

visit files_url(dir)

cant_read1_row = find('tbody a', exact_text: cant_read1).ancestor('tr')
cant_read2_row = find('tbody a', exact_text: cant_read2).ancestor('tr')

cant_read1_row.find('input[type="checkbox"]').check
assert find("#download-btn").disabled?

cant_read2_row.find('input[type="checkbox"]').check
assert find("#download-btn").disabled?

cant_read1_row.find('input[type="checkbox"]').uncheck
assert find("#download-btn").disabled?

cant_read2_row.find('input[type="checkbox"]').uncheck
refute find("#download-btn").disabled?
end
end

test 'allowlist errors flash' do
with_modified_env({ OOD_ALLOWLIST_PATH: Rails.root.to_s }) do
visit(files_url(Rails.root))
Expand Down

0 comments on commit d219198

Please sign in to comment.