From 97cf6b0014bc7fe6e7d42ba98f6f2c75338b475f Mon Sep 17 00:00:00 2001 From: ahmed-mgd Date: Tue, 3 Dec 2024 19:00:16 -0500 Subject: [PATCH 1/7] Implement tests for directory size limit --- apps/dashboard/test/models/files_test.rb | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/apps/dashboard/test/models/files_test.rb b/apps/dashboard/test/models/files_test.rb index b8b799fffb..dbb97a52b4 100644 --- a/apps/dashboard/test/models/files_test.rb +++ b/apps/dashboard/test/models/files_test.rb @@ -53,6 +53,35 @@ class FilesTest < ActiveSupport::TestCase end end + test "can_download_as_zip handles directory size within limit" do + Dir.mktmpdir do |dir| + file_size = 4096 + file_path = File.join(dir, 'foo.txt') + File.open(file_path, 'w') do |f| + f.write('x' * file_size) + end + Open3.stubs(:capture3).returns(["#{file_size} #{file_path} #{file_size} total", "", exit_success]) + + assert_equal [true, nil], PosixFile.new(dir).can_download_as_zip? + 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| + file_size = download_directory_size_limit + 1 + file_path = File.join(dir, 'foo.txt') + File.open(file_path, 'w') do |f| + f.write('x' * file_size) + end + + Open3.stubs(:capture3).returns(["#{file_size} #{file_path} #{file_size} total", "", exit_success]) + result = PosixFile.new(dir).can_download_as_zip? + + assert_equal([false, I18n.t('dashboard.files_directory_too_large', download_directory_size_limit: download_directory_size_limit)], result) + end + end + test "Ensuring PosixFile.username(uid) returns string" do assert_equal "9999999", PosixFile.username(9999999) end From b75b08b32515ec626df693160b43ca3775bcd315 Mon Sep 17 00:00:00 2001 From: ahmed-mgd Date: Mon, 16 Dec 2024 13:39:54 -0500 Subject: [PATCH 2/7] Make minor stylistic adjustments --- apps/dashboard/test/models/files_test.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/dashboard/test/models/files_test.rb b/apps/dashboard/test/models/files_test.rb index dbb97a52b4..2df70aa45a 100644 --- a/apps/dashboard/test/models/files_test.rb +++ b/apps/dashboard/test/models/files_test.rb @@ -39,8 +39,9 @@ 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 @@ -60,7 +61,7 @@ class FilesTest < ActiveSupport::TestCase File.open(file_path, 'w') do |f| f.write('x' * file_size) end - Open3.stubs(:capture3).returns(["#{file_size} #{file_path} #{file_size} total", "", exit_success]) + Open3.stubs(:capture3).returns(["#{file_size} #{file_path} \n #{file_size} total", "", exit_success]) assert_equal [true, nil], PosixFile.new(dir).can_download_as_zip? end @@ -75,10 +76,11 @@ class FilesTest < ActiveSupport::TestCase f.write('x' * file_size) end - Open3.stubs(:capture3).returns(["#{file_size} #{file_path} #{file_size} total", "", exit_success]) + Open3.stubs(:capture3).returns(["#{file_size} #{file_path} \n #{file_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, I18n.t('dashboard.files_directory_too_large', download_directory_size_limit: download_directory_size_limit)], result) + assert_equal([false, error], result) end end From 452b027b757d918a9dc2cbe568400d75c530eef4 Mon Sep 17 00:00:00 2001 From: ahmed-mgd Date: Mon, 16 Dec 2024 14:07:35 -0500 Subject: [PATCH 3/7] Add test case for unauthorized directory --- apps/dashboard/test/models/files_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/apps/dashboard/test/models/files_test.rb b/apps/dashboard/test/models/files_test.rb index 2df70aa45a..b6972f5d8a 100644 --- a/apps/dashboard/test/models/files_test.rb +++ b/apps/dashboard/test/models/files_test.rb @@ -45,6 +45,16 @@ class FilesTest < ActiveSupport::TestCase end end + test "can_download_as_zip handles unauthorized directory" do + Dir.mktmpdir do |dir| + FileUtils.chmod(0400, dir) # No execute 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 files sizes of 0" do Dir.mktmpdir do |dir| Open3.stubs(:capture3).returns(["0 /dev From e5c7e6ad950179292511e62dc2f7e0773ae7f7ab Mon Sep 17 00:00:00 2001 From: ahmed-mgd Date: Mon, 16 Dec 2024 17:35:20 -0500 Subject: [PATCH 4/7] Stub file size calculation --- apps/dashboard/test/models/files_test.rb | 28 +++++++++++------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/apps/dashboard/test/models/files_test.rb b/apps/dashboard/test/models/files_test.rb index b6972f5d8a..29ff442276 100644 --- a/apps/dashboard/test/models/files_test.rb +++ b/apps/dashboard/test/models/files_test.rb @@ -41,17 +41,17 @@ class FilesTest < ActiveSupport::TestCase 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, error], 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) # No execute permission + 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) + assert_equal [false, error], result end end @@ -65,13 +65,13 @@ class FilesTest < ActiveSupport::TestCase end test "can_download_as_zip handles directory size within limit" do + download_directory_size_limit = Configuration.file_download_dir_max Dir.mktmpdir do |dir| - file_size = 4096 - file_path = File.join(dir, 'foo.txt') - File.open(file_path, 'w') do |f| - f.write('x' * file_size) - end - Open3.stubs(:capture3).returns(["#{file_size} #{file_path} \n #{file_size} total", "", exit_success]) + file_size = download_directory_size_limit + PosixFile.any_instance.stubs(:calculate_directory_size) + .returns(download_directory_size_limit) + Open3.stubs(:capture3).returns(["#{file_size} #{dir} + \n #{file_size} total", "", exit_success]) assert_equal [true, nil], PosixFile.new(dir).can_download_as_zip? end @@ -81,12 +81,10 @@ class FilesTest < ActiveSupport::TestCase download_directory_size_limit = Configuration.file_download_dir_max Dir.mktmpdir do |dir| file_size = download_directory_size_limit + 1 - file_path = File.join(dir, 'foo.txt') - File.open(file_path, 'w') do |f| - f.write('x' * file_size) - end - - Open3.stubs(:capture3).returns(["#{file_size} #{file_path} \n #{file_size} total", "", exit_success]) + PosixFile.any_instance.stubs(:calculate_directory_size) + .returns(download_directory_size_limit + 1) + Open3.stubs(:capture3).returns(["#{file_size} #{dir} + \n #{file_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) From cbbb2cf3d11f13f9e834a9c978cd831e88e505a0 Mon Sep 17 00:00:00 2001 From: ahmed-mgd Date: Mon, 16 Dec 2024 17:39:45 -0500 Subject: [PATCH 5/7] Rename file_size to dir_size --- apps/dashboard/test/models/files_test.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/dashboard/test/models/files_test.rb b/apps/dashboard/test/models/files_test.rb index 29ff442276..b38dca8027 100644 --- a/apps/dashboard/test/models/files_test.rb +++ b/apps/dashboard/test/models/files_test.rb @@ -67,11 +67,11 @@ class FilesTest < ActiveSupport::TestCase test "can_download_as_zip handles directory size within limit" do download_directory_size_limit = Configuration.file_download_dir_max Dir.mktmpdir do |dir| - file_size = download_directory_size_limit + dir_size = download_directory_size_limit PosixFile.any_instance.stubs(:calculate_directory_size) .returns(download_directory_size_limit) - Open3.stubs(:capture3).returns(["#{file_size} #{dir} - \n #{file_size} total", "", exit_success]) + Open3.stubs(:capture3).returns(["#{dir_size} #{dir} + \n #{dir_size} total", "", exit_success]) assert_equal [true, nil], PosixFile.new(dir).can_download_as_zip? end @@ -80,11 +80,11 @@ class FilesTest < ActiveSupport::TestCase test "can_download_as_zip handles directory size exceeding limit" do download_directory_size_limit = Configuration.file_download_dir_max Dir.mktmpdir do |dir| - file_size = download_directory_size_limit + 1 + dir_size = download_directory_size_limit + 1 PosixFile.any_instance.stubs(:calculate_directory_size) .returns(download_directory_size_limit + 1) - Open3.stubs(:capture3).returns(["#{file_size} #{dir} - \n #{file_size} total", "", exit_success]) + 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) From d9183b508c6db10aef158c04051012815e49c42b Mon Sep 17 00:00:00 2001 From: ahmed-mgd Date: Tue, 17 Dec 2024 14:14:45 -0500 Subject: [PATCH 6/7] Add tests for timeout and calculation error --- apps/dashboard/test/models/files_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/apps/dashboard/test/models/files_test.rb b/apps/dashboard/test/models/files_test.rb index b38dca8027..ff44e52cdb 100644 --- a/apps/dashboard/test/models/files_test.rb +++ b/apps/dashboard/test/models/files_test.rb @@ -55,6 +55,26 @@ class FilesTest < ActiveSupport::TestCase 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 From 82763b9cf97a95697c57d824edd2fc2822b2fdbc Mon Sep 17 00:00:00 2001 From: ahmed-mgd Date: Tue, 17 Dec 2024 14:15:35 -0500 Subject: [PATCH 7/7] Remove size-within-limit test --- apps/dashboard/test/models/files_test.rb | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/apps/dashboard/test/models/files_test.rb b/apps/dashboard/test/models/files_test.rb index ff44e52cdb..5c667135a8 100644 --- a/apps/dashboard/test/models/files_test.rb +++ b/apps/dashboard/test/models/files_test.rb @@ -84,19 +84,6 @@ class FilesTest < ActiveSupport::TestCase end end - test "can_download_as_zip handles directory size within limit" do - download_directory_size_limit = Configuration.file_download_dir_max - Dir.mktmpdir do |dir| - dir_size = download_directory_size_limit - PosixFile.any_instance.stubs(:calculate_directory_size) - .returns(download_directory_size_limit) - Open3.stubs(:capture3).returns(["#{dir_size} #{dir} - \n #{dir_size} total", "", exit_success]) - - assert_equal [true, nil], PosixFile.new(dir).can_download_as_zip? - 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|