Skip to content

Commit

Permalink
Additional test cases for Files#can_download_as_zip (#4019)
Browse files Browse the repository at this point in the history
* Implement tests for directory size limit

* Make minor stylistic adjustments

* Add test case for unauthorized directory

* Stub file size calculation

* Rename file_size to dir_size

* Add tests for timeout and calculation error

* Remove size-within-limit test
  • Loading branch information
ahmed-mgd authored Dec 30, 2024
1 parent d06da8b commit 70208d4
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion apps/dashboard/test/models/files_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,42 @@ class FilesTest < ActiveSupport::TestCase
error_msg = 'some failure message'
Open3.stubs(:capture3).returns(["blarg \n 28d", error_msg, exit_failure])
result = PosixFile.new(dir).can_download_as_zip?
error = I18n.t('dashboard.files_directory_size_unknown', exit_code: '1', error: error_msg)

assert_equal([false, I18n.t('dashboard.files_directory_size_unknown', exit_code: '1', error: error_msg)], result)
assert_equal [false, error], result
end
end

test "can_download_as_zip handles unauthorized directory" do
Dir.mktmpdir do |dir|
FileUtils.chmod(0400, dir) # Read-only permission
result = PosixFile.new(dir).can_download_as_zip?
error = I18n.t('dashboard.files_directory_download_unauthorized')

assert_equal [false, error], result
end
end

test "can_download_as_zip handles directory size calculation timeout" do
Dir.mktmpdir do |dir|
Open3.stubs(:capture3).returns(["", "Timeout", exit_failure(124)])
result = PosixFile.new(dir).can_download_as_zip?
error = I18n.t('dashboard.files_directory_size_calculation_timeout')

assert_equal [false, error], result
end
end

test "can_download_as_zip handles directory size calculation error" do
Dir.mktmpdir do |dir|
Open3.stubs(:capture3).returns(["", "", exit_success])
result = PosixFile.new(dir).can_download_as_zip?
error = I18n.t('dashboard.files_directory_size_calculation_error')

assert_equal [false, error], result
end
end

test "can_download_as_zip handles files sizes of 0" do
Dir.mktmpdir do |dir|
Open3.stubs(:capture3).returns(["0 /dev
Expand All @@ -53,6 +84,21 @@ class FilesTest < ActiveSupport::TestCase
end
end

test "can_download_as_zip handles directory size exceeding limit" do
download_directory_size_limit = Configuration.file_download_dir_max
Dir.mktmpdir do |dir|
dir_size = download_directory_size_limit + 1
PosixFile.any_instance.stubs(:calculate_directory_size)
.returns(download_directory_size_limit + 1)
Open3.stubs(:capture3).returns(["#{dir_size} #{dir}
\n #{dir_size} total", "", exit_success])
result = PosixFile.new(dir).can_download_as_zip?
error = I18n.t('dashboard.files_directory_too_large', download_directory_size_limit: download_directory_size_limit)

assert_equal([false, error], result)
end
end

test "Ensuring PosixFile.username(uid) returns string" do
assert_equal "9999999", PosixFile.username(9999999)
end
Expand Down

0 comments on commit 70208d4

Please sign in to comment.