From eaf3bd0bae230b4b44665705a5c1bda9378b17ba Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 17 May 2023 15:18:29 +1000 Subject: [PATCH] Update spec after new import file validation --- spec/requests/admin/product_import_spec.rb | 61 ++++++++++++++-------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/spec/requests/admin/product_import_spec.rb b/spec/requests/admin/product_import_spec.rb index 810c604627..3075457f40 100644 --- a/spec/requests/admin/product_import_spec.rb +++ b/spec/requests/admin/product_import_spec.rb @@ -12,15 +12,17 @@ describe "Product Import", type: :request do expect(response).to redirect_to %r|#/login$| end - it "rejects non-csv files" do + it "raises an error on non-csv files" do login_as_admin - post admin_product_import_process_async_path, params: { - filepath: "/etc/passwd", - }, as: :json + expect do + post admin_product_import_process_async_path, params: { + filepath: "/etc/passwd", + }, as: :json + end + .to raise_error "Invalid File Path" - expect(response).to have_http_status :ok - expect(response.body).to eq "undefined method `validate_all' for nil:NilClass" + # The user just sees a server error. end it "raises an error when csv file doesn't exist" do @@ -31,29 +33,46 @@ describe "Product Import", type: :request do filepath: "/file/does/not/exist.csv", }, as: :json end - # This would result in server error and we know the file doesn't exist. - .to raise_error( - Errno::ENOENT, - "No such file or directory @ rb_sysopen - /file/does/not/exist.csv" - ) + .to raise_error "Invalid File Path" + + # The user just sees a server error. end - it "tries to read any csv file" do + it "raises an error non unauthorized csv file" do login_as_admin # This could point to a secret file in the file system: existing_file = Rails.public_path.join('inventory_template.csv').to_s - post admin_product_import_process_async_path, params: { - filepath: existing_file, - start: 1, - end: 5, - }, as: :json + expect do + post admin_product_import_process_async_path, params: { + filepath: existing_file, + start: 1, + end: 5, + }, as: :json + end + .to raise_error "Invalid File Path" - # No error, the file exists: - expect(response).to have_http_status :ok - # But it doesn't contain product data: - expect(response.body).to eq '{"entries":"{}","reset_counts":{}}' + # The user just sees a server error. + end + + it "raises an error on valid but missing csv file" do + login_as_admin + + # This could point to a secret file in the file system: + directory = Dir.mktmpdir("product_import") + missing_valid_file = File.join(directory, "import.csv").to_s + + expect do + post admin_product_import_process_async_path, params: { + filepath: missing_valid_file, + start: 1, + end: 5, + }, as: :json + end + .to raise_error "Invalid File Path" + + # The user just sees a server error. end end end