Skip to content

Commit

Permalink
Making sure the provenance record is updated correctly for the readme…
Browse files Browse the repository at this point in the history
… step (#1765)
  • Loading branch information
carolyncole authored Apr 16, 2024
1 parent a4b6b3d commit 10333e7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
1 change: 0 additions & 1 deletion app/controllers/works_wizard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ def readme_uploaded
if readme_error.nil?
if params[:save_only] == "true"
@readme = readme.file_name
@work.reload_snapshots
render :readme_select
else
redirect_to work_attachment_select_url(@work)
Expand Down
16 changes: 11 additions & 5 deletions app/models/readme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ def attach(readme_file_param)
return nil if ActiveStorage::Blob.service.name == :local
remove_old_readme

extension = File.extname(readme_file_param.original_filename)
readme_name = "README#{extension}"
size = readme_file_param.size
key = work.s3_query_service.upload_file(io: readme_file_param.to_io, filename: readme_name, size:)
key = upload_readme(readme_file_param)
if key
@file_names = [readme_file_param.original_filename]
@s3_readme_idx = 0
log_change(key)
nil
else
Expand Down Expand Up @@ -46,10 +45,17 @@ def file_names

def remove_old_readme
return if blank?

work.track_change(:removed, work.pre_curation_uploads_fast[s3_readme_idx].key)
work.s3_query_service.delete_s3_object(work.pre_curation_uploads_fast[s3_readme_idx].key)
end

def upload_readme(readme_file_param)
extension = File.extname(readme_file_param.original_filename)
readme_name = "README#{extension}"
size = readme_file_param.size
work.s3_query_service.upload_file(io: readme_file_param.to_io, filename: readme_name, size:)
end

def log_change(key)
last_response = work.s3_query_service.last_response
UploadSnapshot.create(work:, files: [{ "filename" => key, "checksum" => last_response.etag.delete('"') }])
Expand Down
4 changes: 1 addition & 3 deletions spec/controllers/works_wizard_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
describe "#readme_uploaded" do
let(:attach_status) { nil }
let(:fake_readme) { instance_double Readme, attach: attach_status, "blank?": true, file_name: "abc123" }
let(:fake_s3_service) { stub_s3 }
let(:params) do
{
"_method" => "patch",
Expand Down Expand Up @@ -110,12 +109,11 @@
let(:save_only_params) { params.merge(save_only: true) }

it "stays on the readme select page" do
fake_s3_service # make sure the stubs are in place
post :readme_uploaded, params: save_only_params
expect(response.status).to be 200
expect(fake_readme).to have_received(:attach)
expect(response).to render_template(:readme_select)
expect(fake_s3_service).to have_received(:client_s3_files)
expect(assigns[:readme]).to eq("abc123")
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/models/readme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
end

it "attaches the file and renames to to README" do
allow(fake_s3_service).to receive(:upload_file).with(io: uploaded_file.to_io, filename: "README.csv", size: 287).and_return("bucket/key/README.csv")
expect { expect(readme.attach(uploaded_file)).to be_nil }.to change { UploadSnapshot.count }.by 1
expect(fake_s3_service).to have_received(:upload_file).with(io: uploaded_file.to_io, filename: "README.csv", size: 287)
expect(readme.file_name).to eq("orcid.csv")
expect(work.activities.first.message).to eq("[{\"action\":\"added\",\"filename\":\"bucket/key/README.csv\"}]")
end

context "when no uploaded file is sent" do
Expand Down Expand Up @@ -71,9 +74,12 @@
let(:s3_files) { [FactoryBot.build(:s3_file, work:), FactoryBot.build(:s3_readme, work:)] }

it "returns no error message" do
allow(fake_s3_service).to receive(:upload_file).with(io: uploaded_file.to_io, filename: "README.csv", size: 287).and_return("bucket/key/README.csv")
expect { expect(readme.attach(uploaded_file)).to be_nil }.to change { UploadSnapshot.count }.by 1
expect(fake_s3_service).to have_received(:upload_file).with(io: uploaded_file.to_io, filename: "README.csv", size: 287)
expect(fake_s3_service).to have_received(:delete_s3_object).with(s3_files.last.key)
expect(readme.file_name).to eq("orcid.csv")
expect(work.activities.first.message).to eq("[{\"action\":\"removed\",\"filename\":\"README.txt\"},{\"action\":\"added\",\"filename\":\"bucket/key/README.csv\"}]")
end
end
end
Expand Down

0 comments on commit 10333e7

Please sign in to comment.