Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Bug/52098 misleading error message when ifc upload file size exceeds `Setting.attachment_max_size` (#15280)

Kabiru Mwenja51f684f6ff00
diff --git a/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb b/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
index a5e4b3cbe03..bf4cb5ddf25 100644
--- a/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
+++ b/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
@@ -34,6 +34,9 @@ module Bim
def set_attributes(params)
model.project = params[:project] if params.key?(:project)
set_ifc_attachment(params.delete(:ifc_attachment))
+ # Do not proceed to build the IfcModel if the attachment contract is not valid
+ # Let the errors be displayed back to the user
+ return model if model.errors.any?
super
@@ -47,16 +50,11 @@ module Bim
end
def validate_and_result
- super.tap do |call|
- # map errors on attachments to better display them
- if call.errors[:attachments].any?
- model.ifc_attachment.errors.details.each do |_, errors|
- errors.each do |error|
- call.errors.add(:attachments, error[:error], **error.except(:error))
- end
- end
- end
- end
+ # Do not proceed to build the IfcModel if the attachment contract is not valid
+ # Let the errors be displayed back to the user before validating the IFC contract
+ return ServiceResult.failure(result: model, errors: model.errors) if model.errors.any?
+
+ super
end
def set_title
@@ -74,11 +72,20 @@ module Bim
model.attachments << ifc_attachment
else
- ::Attachments::BuildService
- .bypass_whitelist(user:)
- .call(file: ifc_attachment, container: model, filename: ifc_attachment.original_filename, description: "ifc")
+ build_ifc_attachment(ifc_attachment)
end
end
+
+ def build_ifc_attachment(ifc_attachment)
+ ::Attachments::BuildService
+ .bypass_whitelist(user:)
+ .call(file: ifc_attachment, container: model, filename: ifc_attachment.original_filename, description: "ifc")
+ .on_failure do |build_attachment_result|
+ build_attachment_result.errors.each do |error|
+ model.errors.add(:attachments, error.type, **error.detail.except(:error))
+ end
+ end
+ end
end
end
end
diff --git a/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb b/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb
index 07ce9d6ff40..5444797e4da 100644
--- a/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb
+++ b/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb
@@ -35,7 +35,7 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model do
let(:other_user) { build_stubbed(:user) }
let(:contract_class) do
- contract = double("contract_class")
+ contract = double("contract_class") # rubocop:disable RSpec/VerifiedDoubles
allow(contract)
.to receive(:new)
@@ -44,13 +44,9 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model do
contract
end
- let(:contract_instance) do
- double("contract_instance", validate: contract_valid, errors: contract_errors)
- end
+ let(:contract_instance) { double("contract_instance", validate: contract_valid, errors: contract_errors) } # rubocop:disable RSpec/VerifiedDoubles
let(:contract_valid) { true }
- let(:contract_errors) do
- double("contract_errors")
- end
+ let(:contract_errors) { double("contract_errors") } # rubocop:disable RSpec/VerifiedDoubles
let(:model_valid) { true }
let(:instance) do
described_class.new(user:,
@@ -80,7 +76,7 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model do
.to receive(:valid?)
.and_return(model_valid)
- expect(contract_instance)
+ allow(contract_instance)
.to receive(:validate)
.and_return(contract_valid)
end
@@ -88,7 +84,8 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model do
subject { instance.call(call_attributes) }
it "is successful" do
- expect(subject.success?).to be_truthy
+ expect(subject).to be_a_success
+ expect(contract_instance).to have_received(:validate)
end
it "sets the attributes" do
@@ -99,10 +96,9 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model do
end
it "does not persist the model" do
- expect(model)
- .not_to receive(:save)
-
+ allow(model).to receive(:save)
subject
+ expect(model).not_to have_received(:save)
end
context "for a new record" do
@@ -118,7 +114,7 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model do
end
it "is successful" do
- expect(subject.success?).to be_truthy
+ expect(subject).to be_a_success
end
it "sets the title to the attachment`s filename" do
@@ -137,6 +133,30 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model do
end
end
+ context "when the attachment is too large", with_settings: { attachment_max_size: 1 } do
+ let(:model) { Bim::IfcModels::IfcModel.new(project:) }
+ let(:model_valid) { false }
+
+ let(:call_attributes) do
+ {
+ ifc_attachment: ifc_file
+ }
+ end
+
+ before do
+ allow(ifc_file).to receive(:size).and_return(2.kilobytes)
+ end
+
+ it "returns a service result failure with the file size error message" do
+ expect(subject).to be_a_failure
+ expect(subject.errors[:attachments]).to eq(["is too large (maximum size is 1024 Bytes)."])
+
+ aggregate_failures "skips the ifc model contract" do
+ expect(contract_instance).not_to have_received(:validate)
+ end
+ end
+ end
+
context "for an existing model" do
context "with an ifc_attachment" do
let(:call_attributes) do
@@ -146,7 +166,7 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model do
end
it "is successful" do
- expect(subject.success?).to be_truthy
+ expect(subject).to be_a_success
end
it "does not alter the title" do