Files
grecia/spec/models/direct_upload_spec.rb
Javi Martín 930bb753c5 Use a rake task to delete cached attachments
Our previous system to delete cached attachments didn't work for
documents because the `custom_hash_data` is different for files created
from a file and files created from cached attachments.

When creating a document attachment, the name of the file is taken into
account to calculate the hash. Let's say the original file name is
"logo.pdf", and the generated hash is "123456". The cached attachment
will be "123456.pdf", so the generated hash using the cached attachment
will be something different, like "28af3". So the file that will be
removed will be "28af3.pdf", and not "123456.pdf", which will still be
present.

Furthermore, there are times where users choose a file and then they
close the browser or go to a different page. In those cases, we weren't
deleting the cached attachments either.

So we're adding a rake task to delete these files once a day. This way
we can simplify the logic we were using to destroy cached attachments.

Note there's related a bug in documents: when editing a record (for
example, a proposal), if the title of the document changes, its hash
changes, and so it will be impossible to generate a link to that
document. Changing the way this hash is generated is not an option
because it would break links to existing files. We'll try to fix it when
moving to Active Storage.
2021-07-30 17:30:11 +02:00

47 lines
2.1 KiB
Ruby

require "rails_helper"
describe DirectUpload do
context "configurations" do
it "is valid for different kind of combinations when attachment is valid" do
expect(build(:direct_upload, :proposal, :documents)).to be_valid
expect(build(:direct_upload, :proposal, :image)).to be_valid
expect(build(:direct_upload, :budget_investment, :documents)).to be_valid
expect(build(:direct_upload, :budget_investment, :image)).to be_valid
end
it "is not valid for different kind of combinations with invalid atttachment content types" do
expect(build(:direct_upload, :proposal, :documents, attachment: File.new("spec/fixtures/files/clippy.png"))).not_to be_valid
expect(build(:direct_upload, :proposal, :image, attachment: File.new("spec/fixtures/files/empty.pdf"))).not_to be_valid
expect(build(:direct_upload, :budget_investment, :documents, attachment: File.new("spec/fixtures/files/clippy.png"))).not_to be_valid
expect(build(:direct_upload, :budget_investment, :image, attachment: File.new("spec/fixtures/files/empty.pdf"))).not_to be_valid
end
it "is not valid without resource_type" do
expect(build(:direct_upload, :proposal, :documents, resource_type: nil)).not_to be_valid
end
it "is not valid without resource_relation" do
expect(build(:direct_upload, :proposal, :documents, resource_relation: nil)).not_to be_valid
end
it "is not valid without attachment" do
expect(build(:direct_upload, :proposal, :documents, attachment: nil)).not_to be_valid
end
it "is not valid without user" do
expect(build(:direct_upload, :proposal, :documents, user: nil)).not_to be_valid
end
end
context "save_attachment" do
it "saves uploaded file" do
proposal_document_direct_upload = build(:direct_upload, :proposal, :documents)
proposal_document_direct_upload.save_attachment
expect(File.exist?(proposal_document_direct_upload.relation.attachment.path)).to eq(true)
expect(proposal_document_direct_upload.relation.attachment.path).to include("cached_attachments")
end
end
end