From 70208d4672c5f54ede38a25534bd281e17f7c2cf Mon Sep 17 00:00:00 2001 From: Ahmed Abdelmageed <111192682+ahmed-mgd@users.noreply.github.com> Date: Mon, 30 Dec 2024 10:06:48 -0500 Subject: [PATCH] Additional test cases for Files#can_download_as_zip (#4019) * 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 --- apps/dashboard/test/models/files_test.rb | 48 +++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/apps/dashboard/test/models/files_test.rb b/apps/dashboard/test/models/files_test.rb index b8b799fffb..5c667135a8 100644 --- a/apps/dashboard/test/models/files_test.rb +++ b/apps/dashboard/test/models/files_test.rb @@ -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 @@ -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