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

tighten ifc model contracts

ulferts2db1571e5935
diff --git a/modules/ifc_models/app/contracts/ifc_models/base_contract.rb b/modules/ifc_models/app/contracts/ifc_models/base_contract.rb
index 339c47e551a..c15176edbba 100644
--- a/modules/ifc_models/app/contracts/ifc_models/base_contract.rb
+++ b/modules/ifc_models/app/contracts/ifc_models/base_contract.rb
@@ -35,8 +35,6 @@ module IFCModels
attribute :title
attribute :is_default
attribute :project
- attribute :uploader
- attribute :is_default
def self.model
::IFCModels::IFCModel
@@ -45,6 +43,8 @@ module IFCModels
def validate
user_allowed_to_manage
user_is_uploader
+ ifc_attachment_existent
+ uploader_is_ifc_attachment_author
super
end
@@ -56,9 +56,17 @@ module IFCModels
end
def user_is_uploader
- unless model.uploader == user
- errors.add :base, :error_unauthorized
+ if model.uploader_id_changed? && 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
+
+ def uploader_is_ifc_attachment_author
+ errors.add :uploader_id, :invalid if model.ifc_attachment && model.uploader != model.ifc_attachment.author
+ end
end
end
diff --git a/modules/ifc_models/app/models/ifc_models/ifc_model.rb b/modules/ifc_models/app/models/ifc_models/ifc_model.rb
index 8131d00d70a..3191d96e0a0 100644
--- a/modules/ifc_models/app/models/ifc_models/ifc_model.rb
+++ b/modules/ifc_models/app/models/ifc_models/ifc_model.rb
@@ -5,6 +5,9 @@ module IFCModels
belongs_to :project
belongs_to :uploader, class_name: 'User', foreign_key: 'uploader_id'
+ validates :title, presence: true
+ validates :project, presence: true
+
scope :defaults, -> { where(is_default: true) }
%i(ifc xkt metadata).each do |name|
diff --git a/modules/ifc_models/config/locales/en.yml b/modules/ifc_models/config/locales/en.yml
index 06249b70433..4b00fd40d5e 100644
--- a/modules/ifc_models/config/locales/en.yml
+++ b/modules/ifc_models/config/locales/en.yml
@@ -12,7 +12,6 @@ en:
check_1: 'Check that you have uploaded at least one IFC model.'
check_2: 'Check that at least one IFC model is set to "Default".'
no_results: "No IFC models have been uploaded in this project."
- could_not_save_file': 'Could not save the file.'
processing_state:
label: 'Processing?'
in_progress: 'In progress'
@@ -27,3 +26,17 @@ en:
extraction:
available:
ifc_convert: "IFC conversion pipeline available"
+
+ activerecord:
+ attributes:
+ ifc_models/ifc_model:
+ ifc_attachment: "IFC file"
+ is_default: "Default model"
+ attachments: "IFC file"
+
+ errors:
+ models:
+ ifc_models/ifc_model:
+ attributes:
+ base:
+ ifc_attachment_missing: "No ifc file attached"
diff --git a/modules/ifc_models/spec/contracts/ifc_models/create_contract_spec.rb b/modules/ifc_models/spec/contracts/ifc_models/create_contract_spec.rb
new file mode 100644
index 00000000000..15840b9fdaf
--- /dev/null
+++ b/modules/ifc_models/spec/contracts/ifc_models/create_contract_spec.rb
@@ -0,0 +1,48 @@
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+require_relative './shared_contract_examples'
+
+describe IFCModels::CreateContract do
+ it_behaves_like 'ifc model contract' do
+ let(:ifc_model) do
+ ::IFCModels::IFCModel.new(project: model_project,
+ title: model_title,
+ uploader: model_user)
+ end
+ let(:permissions) { %i(manage_ifc_models) }
+ let(:other_user) { FactoryBot.build_stubbed(:user) }
+ let(:changed_by_system) { %w(uploader_id) }
+
+ subject(:contract) do
+ described_class.new(ifc_model, current_user, options: { changed_by_system: changed_by_system })
+ end
+ end
+end
diff --git a/modules/ifc_models/spec/contracts/ifc_models/shared_contract_examples.rb b/modules/ifc_models/spec/contracts/ifc_models/shared_contract_examples.rb
new file mode 100644
index 00000000000..cbcc33c4678
--- /dev/null
+++ b/modules/ifc_models/spec/contracts/ifc_models/shared_contract_examples.rb
@@ -0,0 +1,124 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+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(:model_project) { FactoryBot.build_stubbed(:project) }
+ let(:ifc_attachment) { FactoryBot.build_stubbed(:attachment, author: model_user) }
+ let(:model_user) { current_user }
+ let(:model_title) { 'some title' }
+
+ before do
+ allow(ifc_model)
+ .to receive(:ifc_attachment)
+ .and_return(ifc_attachment)
+ end
+
+ def expect_valid(valid, symbols = {})
+ expect(contract.validate).to eq(valid)
+
+ symbols.each do |key, arr|
+ expect(contract.errors.symbols_for(key)).to match_array arr
+ end
+ end
+
+ shared_examples 'is valid' do
+ it 'is valid' do
+ expect_valid(true)
+ end
+ end
+
+ it_behaves_like 'is valid'
+
+ context 'if the title is nil' do
+ let(:model_title) { nil }
+
+ it 'is invalid' do
+ expect_valid(false, title: %i(blank))
+ end
+ end
+
+ context 'if the title is blank' do
+ let(:model_title) { '' }
+
+ it 'is invalid' do
+ expect_valid(false, title: %i(blank))
+ end
+ end
+
+ context 'if the project is nil' do
+ let(:model_project) { nil }
+
+ it 'is invalid' do
+ expect_valid(false, project: %i(blank))
+ end
+ end
+
+ context 'if there is no ifc attachment' do
+ let(:ifc_attachment) { nil }
+
+ it 'is invalid' do
+ expect_valid(false, base: %i(ifc_attachment_missing))
+ end
+ end
+
+ context 'if user is not allowed to manage ifc models' do
+ let(:permissions) { [] }
+
+ it 'is invalid' do
+ expect_valid(false, base: %i(error_unauthorized))
+ end
+ end
+
+ context 'if user of attachment and uploader are different' do
+ let(:ifc_attachment) { FactoryBot.build_stubbed(:attachment, author: other_user) }
+
+ it 'is invalid' do
+ expect_valid(false, uploader_id: %i(invalid))
+ end
+ end
+end
diff --git a/modules/ifc_models/spec/contracts/ifc_models/update_contract_spec.rb b/modules/ifc_models/spec/contracts/ifc_models/update_contract_spec.rb
new file mode 100644
index 00000000000..8aef3ea50f1
--- /dev/null
+++ b/modules/ifc_models/spec/contracts/ifc_models/update_contract_spec.rb
@@ -0,0 +1,71 @@
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+require_relative './shared_contract_examples'
+
+describe IFCModels::UpdateContract do
+ it_behaves_like 'ifc model contract' do
+ let(:ifc_model) do
+ FactoryBot.build_stubbed(:ifc_model,
+ uploader: model_user,
+ title: model_title,
+ project: model_project).tap do |model|
+ model.uploader = uploader_user
+ end
+ end
+ subject(:contract) { described_class.new(ifc_model, current_user, options: { changed_by_system: %w(uploader_id) }) }
+ let(:permissions) { %i(manage_ifc_models) }
+ let(:uploader_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) }
+
+ it_behaves_like 'is valid'
+
+ context 'if the uploader is different from the current_user' do
+ let(:current_user) { model_user }
+
+ it 'is invalid' do
+ expect_valid(false, uploader_id: %i(invalid))
+ end
+ end
+ end
+
+ context 'if the uploader does not change and the current user is different from the uploader' do
+ let(:current_user) { other_user }
+ let(:model_user) { FactoryBot.build_stubbed(:user) }
+
+ it_behaves_like 'is valid'
+ end
+ end
+end