Skip to content

Commit

Permalink
Updating the readme to allow for existing files for prevenance (#1771)
Browse files Browse the repository at this point in the history
Allows for back and forth movement in the wizard and the correct update of the provenance log
  • Loading branch information
carolyncole authored Apr 17, 2024
1 parent 9ed17eb commit f34dde5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
11 changes: 4 additions & 7 deletions app/models/readme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def attach(readme_file_param)
if key
@file_names = [readme_file_param.original_filename]
@s3_readme_idx = 0
log_change(key)
log_changes
nil
else
"An error uploading your README was encountered. Please try again."
Expand Down Expand Up @@ -45,7 +45,6 @@ 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

Expand All @@ -56,10 +55,8 @@ def upload_readme(readme_file_param)
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('"') }])
work.track_change(:added, key)
work.log_file_changes(current_user.id)
def log_changes
work.s3_query_service.client_s3_files(reload: true)
work.reload_snapshots(user_id: current_user.id)
end
end
10 changes: 8 additions & 2 deletions spec/models/readme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
tempfile: File.new(Rails.root.join("spec", "fixtures", "files", "orcid.csv"))
})
end
let(:new_readme) { FactoryBot.build :s3_file, filename: "bucket/key/README.csv" }

it "attaches the file and renames to to README" do
allow(fake_s3_service).to receive(:client_s3_files).and_return([new_readme])
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\"}]")
expect(work.activities.first.message).to eq("[{\"action\":\"added\",\"filename\":\"bucket/key/README.csv\",\"checksum\":\"abc123\"}]")
end

context "when no uploaded file is sent" do
Expand Down Expand Up @@ -72,14 +74,18 @@

context "there is an existing readme that should be replaced" do
let(:s3_files) { [FactoryBot.build(:s3_file, work:), FactoryBot.build(:s3_readme, work:)] }
let(:new_readme) { FactoryBot.build :s3_file, filename: "bucket/key/README.csv" }

it "returns no error message" do
allow(fake_s3_service).to receive(:client_s3_files).and_return(s3_files, s3_files, s3_files, [s3_files.first, new_readme])
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")
work.reload_snapshots
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\"}]")
expect(work.activities.last.message).to eq("[{\"action\":\"removed\",\"filename\":\"README.txt\",\"checksum\":\"abc123\"}," \
"{\"action\":\"added\",\"filename\":\"bucket/key/README.csv\",\"checksum\":\"abc123\"}]")
end
end
end
Expand Down

0 comments on commit f34dde5

Please sign in to comment.