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

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.rb
index 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 Bim
end
validate :user_allowed_to_manage
- validate :user_is_uploader
validate :ifc_attachment_existent
validate :ifc_attachment_is_ifc
validate :uploader_is_ifc_attachment_author
@@ -53,14 +52,6 @@ module Bim
end
end
- 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_existent
errors.add :base, :ifc_attachment_missing unless model.ifc_attachment
end
diff --git a/modules/bim/spec/contracts/ifc_models/shared_contract_examples.rb b/modules/bim/spec/contracts/ifc_models/shared_contract_examples.rb
index 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' do
allow(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
+ end
end
def expect_valid(valid, symbols = {})
@@ -109,10 +105,10 @@ shared_examples_for 'ifc model contract' do
context 'if the new ifc file is no valid ifc file' do
let(: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')
+ .result
end
it 'is invalid' do
@@ -125,10 +121,10 @@ shared_examples_for 'ifc model contract' do
FileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true, content: "ISO-10303-21;"
end
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')
+ .result
end
it_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.rb
index 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 do
project: 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
+ else
model.uploader = uploader_user
end
end
end
let(:permissions) { %i(manage_ifc_models) }
let(:uploader_user) { model_user }
+ let(:changed_by_system) { false }
context 'if the uploader changes' do
let(:model_user) { FactoryBot.build_stubbed(:user) }
@@ -56,14 +61,20 @@ describe Bim::IfcModels::UpdateContract do
let(: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))
end
end