From 5f29680186046666cbd78a8b60d11bfd28a66e61 Mon Sep 17 00:00:00 2001 From: Julian Herrero Date: Mon, 7 Aug 2023 13:35:41 +0200 Subject: [PATCH 1/2] Do not validate the attachment file size ... when the attachment has not changed --- app/models/concerns/attachable.rb | 2 +- spec/models/attachable_spec.rb | 44 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/attachable.rb b/app/models/concerns/attachable.rb index b2bcbf1b4..a08d0bddc 100644 --- a/app/models/concerns/attachable.rb +++ b/app/models/concerns/attachable.rb @@ -18,7 +18,7 @@ module Attachable }, file_size: { less_than_or_equal_to: ->(record) { record.max_file_size.megabytes }, - if: -> { association_class && attachment.attached? }, + if: -> { association_class && attachment.attached? && attachment.new_record? }, message: ->(record, *) do I18n.t("#{record.model_name.plural}.errors.messages.in_between", min: "0 Bytes", diff --git a/spec/models/attachable_spec.rb b/spec/models/attachable_spec.rb index 2cb13825b..3c3c48d14 100644 --- a/spec/models/attachable_spec.rb +++ b/spec/models/attachable_spec.rb @@ -14,4 +14,48 @@ describe Attachable do expect(build(:image).file_path).to include "storage/tenants/image-master/" end + + context "file size validation" do + it "is not applied when the image attachment has not changed" do + image = create(:image, :proposal_image) + + expect(image.valid?).to be(true) + + Setting["uploads.images.max_size"] = 0.1 + + expect(image.valid?).to be(true) + end + + it "is applied when the image attachment changes" do + image = create(:image, :proposal_image) + + expect(image.valid?).to be(true) + + Setting["uploads.images.max_size"] = 0.1 + image.attachment = Rack::Test::UploadedFile.new("spec/fixtures/files/clippy.png") + + expect(image.valid?).to be(false) + end + + it "is not applied when the document attachment has not changed" do + document = create(:document, :proposal_document) + + expect(document.valid?).to be(true) + + Setting["uploads.documents.max_size"] = 0.1 + + expect(document.valid?).to be(true) + end + + it "is applied when the document attachment changes" do + document = create(:document, :proposal_document) + + expect(document.valid?).to be(true) + + Setting["uploads.documents.max_size"] = 0.1 + document.attachment = Rack::Test::UploadedFile.new("spec/fixtures/files/clippy.pdf") + + expect(document.valid?).to be(false) + end + end end From 2ae3045a049d533ce206fecb29d98198ada310d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= <15726+Senen@users.noreply.github.com> Date: Mon, 9 Oct 2023 17:50:20 +0200 Subject: [PATCH 2/2] Do not validate the attachment file content type ... when the attachment has not changed --- app/models/concerns/attachable.rb | 2 +- spec/models/attachable_spec.rb | 44 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/attachable.rb b/app/models/concerns/attachable.rb index a08d0bddc..eb725eac1 100644 --- a/app/models/concerns/attachable.rb +++ b/app/models/concerns/attachable.rb @@ -9,7 +9,7 @@ module Attachable presence: true, file_content_type: { allow: ->(record) { record.accepted_content_types }, - if: -> { association_class && attachment.attached? }, + if: -> { association_class && attachment.attached? && attachment.new_record? }, message: ->(record, *) do I18n.t("#{record.model_name.plural}.errors.messages.wrong_content_type", content_type: record.attachment_content_type, diff --git a/spec/models/attachable_spec.rb b/spec/models/attachable_spec.rb index 3c3c48d14..12ba0e92e 100644 --- a/spec/models/attachable_spec.rb +++ b/spec/models/attachable_spec.rb @@ -58,4 +58,48 @@ describe Attachable do expect(document.valid?).to be(false) end end + + context "file content types validation" do + it "is not applied when the image attachment has not changed" do + image = create(:image, :proposal_image) + + expect(image.valid?).to be(true) + + Setting["uploads.images.content_types"] = "image/gif" + + expect(image.valid?).to be(true) + end + + it "is applied when the image attachment changes" do + image = create(:image, :proposal_image) + + expect(image.valid?).to be(true) + + Setting["uploads.images.content_types"] = "image/gif" + image.attachment = Rack::Test::UploadedFile.new("spec/fixtures/files/clippy.png") + + expect(image.valid?).to be(false) + end + + it "is not applied when the document attachment has not changed" do + document = create(:document, :proposal_document) + + expect(document.valid?).to be(true) + + Setting["uploads.documents.content_types"] = "text/csv" + + expect(document.valid?).to be(true) + end + + it "is applied when the document attachment changes" do + document = create(:document, :proposal_document) + + expect(document.valid?).to be(true) + + Setting["uploads.documents.content_types"] = "text/csv" + document.attachment = Rack::Test::UploadedFile.new("spec/fixtures/files/clippy.pdf") + + expect(document.valid?).to be(false) + end + end end