Remove hack to allow IO files in Active Storage

We were using this hack in order to allow `File.new` attachments in
tests files. However, we can use the `fixture_file_upload` helper
instead.

Just like it happened with `file_fixture`, this helper method doesn't
work in fixtures, so in this case we're using `Rack::Test::UploadedFile`
instead.
This commit is contained in:
Javi Martín
2021-09-18 17:37:54 +02:00
parent 4f232c3a25
commit 8eea6f585a
17 changed files with 43 additions and 48 deletions

View File

@@ -6,9 +6,7 @@ module HasAttachment
has_one_attached attribute has_one_attached attribute
define_method :"#{attribute}=" do |file| define_method :"#{attribute}=" do |file|
if file.is_a?(IO) if file.nil?
send(attribute).attach(io: file, filename: File.basename(file.path))
elsif file.nil?
send(attribute).detach send(attribute).detach
else else
send(attribute).attach(file) send(attribute).attach(file)

View File

@@ -15,14 +15,12 @@ end
def add_image_to(imageable) def add_image_to(imageable)
# imageable should respond to #title & #author # imageable should respond to #title & #author
File.open(INVESTMENT_IMAGE_FILES.sample) do |file|
imageable.image = Image.create!({ imageable.image = Image.create!({
imageable: imageable, imageable: imageable,
title: imageable.title, title: imageable.title,
attachment: file, attachment: Rack::Test::UploadedFile.new(INVESTMENT_IMAGE_FILES.sample),
user: imageable.author user: imageable.author
}) })
end
imageable.save! imageable.save!
end end

View File

@@ -12,14 +12,12 @@ end
def add_image_to(imageable) def add_image_to(imageable)
# imageable should respond to #title & #author # imageable should respond to #title & #author
File.open(IMAGE_FILES.sample) do |file|
imageable.image = Image.create!({ imageable.image = Image.create!({
imageable: imageable, imageable: imageable,
title: imageable.title, title: imageable.title,
attachment: file, attachment: Rack::Test::UploadedFile.new(IMAGE_FILES.sample),
user: imageable.author user: imageable.author
}) })
end
imageable.save! imageable.save!
end end

View File

@@ -1,7 +1,7 @@
section "Creating header and cards for the homepage" do section "Creating header and cards for the homepage" do
def create_image_attachment(type) def create_image_attachment(type)
{ {
attachment: File.new(Rails.root.join("db/dev_seeds/images/#{type}_background.jpg")), attachment: Rack::Test::UploadedFile.new("db/dev_seeds/images/#{type}_background.jpg"),
title: "#{type}_background.jpg", title: "#{type}_background.jpg",
user: User.first user: User.first
} }

View File

@@ -58,7 +58,7 @@ FactoryBot.define do
end end
factory :site_customization_image, class: "SiteCustomization::Image" do factory :site_customization_image, class: "SiteCustomization::Image" do
image { File.new("spec/fixtures/files/logo_header.png") } image { Rack::Test::UploadedFile.new("spec/fixtures/files/logo_header.png") }
name { "logo_header" } name { "logo_header" }
end end

View File

@@ -1,6 +1,6 @@
FactoryBot.define do FactoryBot.define do
factory :image do factory :image do
attachment { File.new("spec/fixtures/files/clippy.jpg") } attachment { Rack::Test::UploadedFile.new("spec/fixtures/files/clippy.jpg") }
title { "Lorem ipsum dolor sit amet" } title { "Lorem ipsum dolor sit amet" }
association :user, factory: :user association :user, factory: :user
@@ -16,7 +16,7 @@ FactoryBot.define do
factory :document do factory :document do
sequence(:title) { |n| "Document title #{n}" } sequence(:title) { |n| "Document title #{n}" }
association :user, factory: :user association :user, factory: :user
attachment { File.new("spec/fixtures/files/empty.pdf") } attachment { Rack::Test::UploadedFile.new("spec/fixtures/files/empty.pdf") }
trait :proposal_document do trait :proposal_document do
association :documentable, factory: :proposal association :documentable, factory: :proposal
@@ -47,11 +47,11 @@ FactoryBot.define do
trait :documents do trait :documents do
resource_relation { "documents" } resource_relation { "documents" }
attachment { File.new("spec/fixtures/files/empty.pdf") } attachment { Rack::Test::UploadedFile.new("spec/fixtures/files/empty.pdf") }
end end
trait :image do trait :image do
resource_relation { "image" } resource_relation { "image" }
attachment { File.new("spec/fixtures/files/clippy.jpg") } attachment { Rack::Test::UploadedFile.new("spec/fixtures/files/clippy.jpg") }
end end
initialize_with { new(attributes) } initialize_with { new(attributes) }
end end

View File

@@ -35,7 +35,7 @@ describe "active storage tasks" do
end end
it "does not modify old ckeditor attachments" do it "does not modify old ckeditor attachments" do
image = Ckeditor::Picture.create!(data: Rack::Test::UploadedFile.new(file_fixture("clippy.png"))) image = Ckeditor::Picture.create!(data: fixture_file_upload("clippy.png"))
expect(image.storage_data.attachment.name).to eq "storage_data" expect(image.storage_data.attachment.name).to eq "storage_data"

View File

@@ -10,10 +10,10 @@ describe DirectUpload do
end end
it "is not valid for different kind of combinations with invalid atttachment content types" do it "is not valid for different kind of combinations with invalid atttachment content types" do
expect(build(:direct_upload, :proposal, :documents, attachment: File.new(file_fixture("clippy.png")))).not_to be_valid expect(build(:direct_upload, :proposal, :documents, attachment: fixture_file_upload("clippy.png"))).not_to be_valid
expect(build(:direct_upload, :proposal, :image, attachment: File.new(file_fixture("empty.pdf")))).not_to be_valid expect(build(:direct_upload, :proposal, :image, attachment: fixture_file_upload("empty.pdf"))).not_to be_valid
expect(build(:direct_upload, :budget_investment, :documents, attachment: File.new(file_fixture("clippy.png")))).not_to be_valid expect(build(:direct_upload, :budget_investment, :documents, attachment: fixture_file_upload("clippy.png"))).not_to be_valid
expect(build(:direct_upload, :budget_investment, :image, attachment: File.new(file_fixture("empty.pdf")))).not_to be_valid expect(build(:direct_upload, :budget_investment, :image, attachment: fixture_file_upload("empty.pdf"))).not_to be_valid
end end
it "is not valid without resource_type" do it "is not valid without resource_type" do

View File

@@ -5,7 +5,7 @@ describe Document do
it_behaves_like "document validations", "proposal_document" it_behaves_like "document validations", "proposal_document"
it "stores attachments with Active Storage" do it "stores attachments with Active Storage" do
document = create(:document, attachment: File.new(file_fixture("clippy.pdf"))) document = create(:document, attachment: fixture_file_upload("clippy.pdf"))
expect(document.attachment).to be_attached expect(document.attachment).to be_attached
expect(document.attachment.filename).to eq "clippy.pdf" expect(document.attachment.filename).to eq "clippy.pdf"

View File

@@ -5,7 +5,7 @@ describe Image do
it_behaves_like "image validations", "proposal_image" it_behaves_like "image validations", "proposal_image"
it "stores attachments with Active Storage" do it "stores attachments with Active Storage" do
image = create(:image, attachment: File.new(file_fixture("clippy.jpg"))) image = create(:image, attachment: fixture_file_upload("clippy.jpg"))
expect(image.attachment).to be_attached expect(image.attachment).to be_attached
expect(image.attachment.filename).to eq "clippy.jpg" expect(image.attachment.filename).to eq "clippy.jpg"

View File

@@ -17,7 +17,7 @@ describe LocalCensusRecords::Import do
context "When file extension" do context "When file extension" do
it "is wrong it should not be valid" do it "is wrong it should not be valid" do
file = Rack::Test::UploadedFile.new(file_fixture("clippy.gif")) file = fixture_file_upload("clippy.gif")
import = build(:local_census_records_import, file: file) import = build(:local_census_records_import, file: file)
expect(import).not_to be_valid expect(import).not_to be_valid
@@ -25,7 +25,7 @@ describe LocalCensusRecords::Import do
it "is csv it should be valid" do it "is csv it should be valid" do
path = base_files_path << "valid.csv" path = base_files_path << "valid.csv"
file = Rack::Test::UploadedFile.new(file_fixture(path)) file = fixture_file_upload(path)
import = build(:local_census_records_import, file: file) import = build(:local_census_records_import, file: file)
expect(import).to be_valid expect(import).to be_valid
@@ -35,7 +35,7 @@ describe LocalCensusRecords::Import do
context "When file headers" do context "When file headers" do
it "are all missing it should not be valid" do it "are all missing it should not be valid" do
path = base_files_path << "valid-without-headers.csv" path = base_files_path << "valid-without-headers.csv"
file = Rack::Test::UploadedFile.new(file_fixture(path)) file = fixture_file_upload(path)
import = build(:local_census_records_import, file: file) import = build(:local_census_records_import, file: file)
expect(import).not_to be_valid expect(import).not_to be_valid
@@ -64,7 +64,7 @@ describe LocalCensusRecords::Import do
it "Add invalid local census records to invalid_records array" do it "Add invalid local census records to invalid_records array" do
path = base_files_path << "invalid.csv" path = base_files_path << "invalid.csv"
file = Rack::Test::UploadedFile.new(file_fixture(path)) file = fixture_file_upload(path)
import.file = file import.file = file
import.save! import.save!

View File

@@ -3,7 +3,7 @@ require "rails_helper"
describe SiteCustomization::Image do describe SiteCustomization::Image do
it "stores images with Active Storage" do it "stores images with Active Storage" do
image = create(:site_customization_image, name: "map", image = create(:site_customization_image, name: "map",
image: File.new(file_fixture("custom_map.jpg"))) image: fixture_file_upload("custom_map.jpg"))
expect(image.image).to be_attached expect(image.image).to be_attached
expect(image.image.filename).to eq "custom_map.jpg" expect(image.image.filename).to eq "custom_map.jpg"
@@ -13,7 +13,7 @@ describe SiteCustomization::Image do
it "is valid with a 260x80 image" do it "is valid with a 260x80 image" do
image = build(:site_customization_image, image = build(:site_customization_image,
name: "logo_header", name: "logo_header",
image: File.new(file_fixture("logo_header-260x80.png"))) image: fixture_file_upload("logo_header-260x80.png"))
expect(image).to be_valid expect(image).to be_valid
end end
@@ -21,7 +21,7 @@ describe SiteCustomization::Image do
it "is valid with a 223x80 image" do it "is valid with a 223x80 image" do
image = build(:site_customization_image, image = build(:site_customization_image,
name: "logo_header", name: "logo_header",
image: File.new(file_fixture("logo_header.png"))) image: fixture_file_upload("logo_header.png"))
expect(image).to be_valid expect(image).to be_valid
end end
@@ -29,7 +29,7 @@ describe SiteCustomization::Image do
it "is not valid with a 400x80 image" do it "is not valid with a 400x80 image" do
image = build(:site_customization_image, image = build(:site_customization_image,
name: "logo_header", name: "logo_header",
image: File.new(file_fixture("logo_email_custom.png"))) image: fixture_file_upload("logo_email_custom.png"))
expect(image).not_to be_valid expect(image).not_to be_valid
end end

View File

@@ -7,21 +7,21 @@ shared_examples "acts as imageable" do |imageable_factory|
describe "file extension" do describe "file extension" do
it "is not valid with '.png' extension" do it "is not valid with '.png' extension" do
image.attachment = File.new(file_fixture("clippy.png")) image.attachment = fixture_file_upload("clippy.png")
expect(image).not_to be_valid expect(image).not_to be_valid
expect(image.errors[:attachment].size).to eq(1) expect(image.errors[:attachment].size).to eq(1)
end end
it "is not valid with '.gif' extension" do it "is not valid with '.gif' extension" do
image.attachment = File.new(file_fixture("clippy.gif")) image.attachment = fixture_file_upload("clippy.gif")
expect(image).not_to be_valid expect(image).not_to be_valid
expect(image.errors[:attachment].size).to eq(1) expect(image.errors[:attachment].size).to eq(1)
end end
it "is valid with '.jpg' extension" do it "is valid with '.jpg' extension" do
image.attachment = File.new(file_fixture("clippy.jpg")) image.attachment = fixture_file_upload("clippy.jpg")
expect(image).to be_valid expect(image).to be_valid
end end
@@ -33,7 +33,7 @@ shared_examples "acts as imageable" do |imageable_factory|
end end
it "is not valid when image dimmensions are smaller than 475X475" do it "is not valid when image dimmensions are smaller than 475X475" do
image.attachment = File.new(file_fixture("logo_header.jpg")) image.attachment = fixture_file_upload("logo_header.jpg")
expect(image).not_to be_valid expect(image).not_to be_valid
end end

View File

@@ -22,7 +22,7 @@ shared_examples "document validations" do |documentable_factory|
it "is valid for all accepted content types" do it "is valid for all accepted content types" do
acceptedcontenttypes.each do |content_type| acceptedcontenttypes.each do |content_type|
extension = content_type.split("/").last extension = content_type.split("/").last
document.attachment = File.new(file_fixture("empty.#{extension}")) document.attachment = fixture_file_upload("empty.#{extension}")
expect(document).to be_valid expect(document).to be_valid
end end

View File

@@ -21,7 +21,7 @@ shared_examples "image validations" do |imageable_factory|
it "is valid for all accepted content types" do it "is valid for all accepted content types" do
acceptedcontenttypes.each do |content_type| acceptedcontenttypes.each do |content_type|
extension = content_type.split("/").last extension = content_type.split("/").last
image.attachment = File.new(file_fixture("clippy.#{extension}")) image.attachment = fixture_file_upload("clippy.#{extension}")
expect(image).to be_valid expect(image).to be_valid
end end
@@ -30,7 +30,7 @@ shared_examples "image validations" do |imageable_factory|
it "is not valid for png and gif image content types" do it "is not valid for png and gif image content types" do
["gif", "png"].each do |content_type| ["gif", "png"].each do |content_type|
extension = content_type.split("/").last extension = content_type.split("/").last
image.attachment = File.new(file_fixture("clippy.#{extension}")) image.attachment = fixture_file_upload("clippy.#{extension}")
expect(image).not_to be_valid expect(image).not_to be_valid
end end

View File

@@ -9,6 +9,7 @@ Dir["./spec/shared/**/*.rb"].sort.each { |f| require f }
RSpec.configure do |config| RSpec.configure do |config|
config.use_transactional_fixtures = true config.use_transactional_fixtures = true
config.fixture_path = "spec/fixtures/files"
config.filter_run_when_matching :focus config.filter_run_when_matching :focus
config.include RequestSpecHelper, type: :request config.include RequestSpecHelper, type: :request

View File

@@ -63,7 +63,7 @@ describe "Admin custom images", :admin do
scenario "Custom apple touch icon is replaced on front views" do scenario "Custom apple touch icon is replaced on front views" do
create(:site_customization_image, create(:site_customization_image,
name: "apple-touch-icon-200", name: "apple-touch-icon-200",
image: File.new(file_fixture("apple-touch-icon-custom-200.png"))) image: fixture_file_upload("apple-touch-icon-custom-200.png"))
visit root_path visit root_path