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
Fix uploader being changed by system
Oliver Günther8a881cb20cde
diff --git a/modules/bim/app/contracts/bim/ifc_models/base_contract.rb b/modules/bim/app/contracts/bim/ifc_models/base_contract.rbindex ac7e0a55e9b..2dfbf4dabdc 100644--- a/modules/bim/app/contracts/bim/ifc_models/base_contract.rb+++ b/modules/bim/app/contracts/bim/ifc_models/base_contract.rb@@ -42,7 +42,6 @@ module Bimendvalidate :user_allowed_to_manage- validate :user_is_uploadervalidate :ifc_attachment_existentvalidate :ifc_attachment_is_ifcvalidate :uploader_is_ifc_attachment_author@@ -53,14 +52,6 @@ module Bimendend- def user_is_uploader- return unless changed_by_user.include?('uploader_id')-- if model.uploader != user- errors.add :uploader_id, :invalid- end- end-def ifc_attachment_existenterrors.add :base, :ifc_attachment_missing unless model.ifc_attachmentenddiff --git a/modules/bim/spec/contracts/ifc_models/shared_contract_examples.rb b/modules/bim/spec/contracts/ifc_models/shared_contract_examples.rbindex 82936c81848..d16b8d0adf1 100644--- a/modules/bim/spec/contracts/ifc_models/shared_contract_examples.rb+++ b/modules/bim/spec/contracts/ifc_models/shared_contract_examples.rb@@ -31,22 +31,8 @@require 'spec_helper'shared_examples_for 'ifc model contract' do- let(:current_user) do- FactoryBot.build_stubbed(:user) do |user|- allow(user)- .to receive(:allowed_to?) do |permission, permission_project|- permissions.include?(permission) && model_project == permission_project- end- end- end- let(:other_user) do- FactoryBot.build_stubbed(:user) do |user|- allow(user)- .to receive(:allowed_to?) do |permission, permission_project|- permissions.include?(permission) && model_project == permission_project- end- end- end+ let(:current_user) { FactoryBot.build_stubbed(:user) }+ let(:other_user) { FactoryBot.build_stubbed(:user) }let(:model_project) { FactoryBot.build_stubbed(:project) }let(:ifc_attachment) { FactoryBot.build_stubbed(:attachment, author: model_user) }let(:model_user) { current_user }@@ -56,6 +42,16 @@ shared_examples_for 'ifc model contract' doallow(ifc_model).to receive(:ifc_attachment).and_return(ifc_attachment)++ allow(other_user)+ .to receive(:allowed_to?) do |permission, permission_project|+ permissions.include?(permission) && model_project == permission_project+ end++ allow(current_user)+ .to receive(:allowed_to?) do |permission, permission_project|+ permissions.include?(permission) && model_project == permission_project+ endenddef expect_valid(valid, symbols = {})@@ -109,10 +105,10 @@ shared_examples_for 'ifc model contract' docontext 'if the new ifc file is no valid ifc file' dolet(:ifc_file) { FileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true }let(:ifc_attachment) do- User.execute_as current_user do- ifc_model.attach_files('first' => { 'file' => ifc_file, 'description' => 'ifc' })- ifc_model.attachments.last- end+ ::Attachments::BuildService+ .new(user: current_user)+ .call(file: ifc_file, filename: 'model.ifc')+ .resultendit 'is invalid' do@@ -125,10 +121,10 @@ shared_examples_for 'ifc model contract' doFileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true, content: "ISO-10303-21;"endlet(:ifc_attachment) do- User.execute_as current_user do- ifc_model.attach_files('first' => { 'file' => ifc_file, 'description' => 'ifc' })- ifc_model.attachments.last- end+ ::Attachments::BuildService+ .new(user: current_user)+ .call(file: ifc_file, filename: 'model.ifc')+ .resultendit_behaves_like 'is valid'diff --git a/modules/bim/spec/contracts/ifc_models/update_contract_spec.rb b/modules/bim/spec/contracts/ifc_models/update_contract_spec.rbindex afa42d0ceda..84d71c019b5 100644--- a/modules/bim/spec/contracts/ifc_models/update_contract_spec.rb+++ b/modules/bim/spec/contracts/ifc_models/update_contract_spec.rb@@ -42,13 +42,18 @@ describe Bim::IfcModels::UpdateContract doproject: model_project).tap do |model|model.extend(OpenProject::ChangedBySystem)- model.change_by_system do+ if changed_by_system+ changed_by_system do+ model.uploader = uploader_user+ end+ elsemodel.uploader = uploader_userendendendlet(:permissions) { %i(manage_ifc_models) }let(:uploader_user) { model_user }+ let(:changed_by_system) { false }context 'if the uploader changes' dolet(:model_user) { FactoryBot.build_stubbed(:user) }@@ -56,14 +61,20 @@ describe Bim::IfcModels::UpdateContract dolet(:current_user) { other_user }let(:ifc_attachment) { FactoryBot.build_stubbed(:attachment, author: other_user) }- it_behaves_like 'is valid'+ it 'is invalid as not writable' do+ expect_valid(false, uploader_id: %i(error_readonly))+ end+ end- context 'if the uploader is different from the current_user' do- let(:current_user) { model_user }+ context 'if the uploader changes' do+ let(:model_user) { FactoryBot.build_stubbed(:user) }+ let(:uploader_user) { other_user }+ let(:current_user) { other_user }+ let(:ifc_attachment) { FactoryBot.build_stubbed(:attachment, author: other_user) }+ let(:changed_by_system) { true }- it 'is invalid' do- expect_valid(false, uploader_id: %i(invalid))- end+ it 'is invalid as does not match' do+ expect_valid(false, uploader_id: %i(invalid))endend