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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex 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 Bimdef 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 Bimenddef 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?++ superenddef set_title@@ -74,11 +72,20 @@ module Bimmodel.attachments << ifc_attachmentelse- ::Attachments::BuildService- .bypass_whitelist(user:)- .call(file: ifc_attachment, container: model, filename: ifc_attachment.original_filename, description: "ifc")+ build_ifc_attachment(ifc_attachment)endend++ 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+ endendendenddiff --git a/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb b/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rbindex 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 dolet(:other_user) { build_stubbed(:user) }let(:contract_class) do- contract = double("contract_class")+ contract = double("contract_class") # rubocop:disable RSpec/VerifiedDoublesallow(contract).to receive(:new)@@ -44,13 +44,9 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model docontractend- 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/VerifiedDoubleslet(:contract_valid) { true }- let(:contract_errors) do- double("contract_errors")- end+ let(:contract_errors) { double("contract_errors") } # rubocop:disable RSpec/VerifiedDoubleslet(:model_valid) { true }let(:instance) dodescribed_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 dosubject { 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)endit "sets the attributes" do@@ -99,10 +96,9 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model doendit "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)endcontext "for a new record" do@@ -118,7 +114,7 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model doendit "is successful" do- expect(subject.success?).to be_truthy+ expect(subject).to be_a_successendit "sets the title to the attachment`s filename" do@@ -137,6 +133,30 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model doendend+ 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" docontext "with an ifc_attachment" dolet(:call_attributes) do@@ -146,7 +166,7 @@ RSpec.describe Bim::IfcModels::SetAttributesService, type: :model doendit "is successful" do- expect(subject.success?).to be_truthy+ expect(subject).to be_a_successendit "does not alter the title" do