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
bail on inexistent related resource
ulfertsbc682f6b014b
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rbindex 7b1c5fc9dfd..1f682d9e428 100644--- a/app/contracts/work_packages/base_contract.rb+++ b/app/contracts/work_packages/base_contract.rb@@ -103,7 +103,9 @@ module WorkPackagesmessage: :greater_than_or_equal_to_start_date,allow_blank: true },unless: Proc.new { |wp| wp.start_date.blank? }+validate :validate_enabled_type+ validate :validate_type_existsvalidate :validate_milestone_constraintvalidate :validate_parent_not_milestone@@ -112,13 +114,17 @@ module WorkPackagesvalidate :validate_parent_in_same_projectvalidate :validate_parent_not_subtask+ validate :validate_status_existsvalidate :validate_status_transitionvalidate :validate_active_priority+ validate :validate_priority_existsvalidate :validate_categoryvalidate :validate_estimated_hours+ validate :validate_assigned_to_exists+def initialize(work_package, user, options: {})super@@ -149,11 +155,19 @@ module WorkPackagesdef validate_enabled_type# Checks that the issue can not be added/moved to a disabled type- if model.project && (model.type_id_changed? || model.project_id_changed?)+ if model.project && !type_inexistent? && (model.type_id_changed? || model.project_id_changed?)errors.add :type_id, :inclusion unless model.project.types.include?(model.type)endend+ def validate_assigned_to_exists+ errors.add :assigned_to, :does_not_exist if model.assigned_to&.is_a?(User::InexistentUser)+ end++ def validate_type_exists+ errors.add :type, :does_not_exist if type_inexistent?+ end+def validate_milestone_constraintif model.is_milestone? && model.due_date && model.start_date && model.start_date != model.due_dateerrors.add :due_date, :not_start_date@@ -186,6 +200,10 @@ module WorkPackagesendend+ def validate_status_exists+ errors.add :status, :does_not_exist if model.status&.is_a?(Status::InexistentStatus)+ end+def validate_status_transitionif status_changed? && status_exists? && !(model.type_id_changed? || status_transition_exists?)errors.add :status_id, :status_transition_invalid@@ -198,6 +216,10 @@ module WorkPackagesendend+ def validate_priority_exists+ errors.add :priority, :does_not_exist if model.priority&.is_a?(Priority::InexistentPriority)+ end+def validate_categoryif inexistent_category?errors.add :category, :does_not_exist@@ -287,5 +309,9 @@ module WorkPackagesqueryendend++ def type_inexistent?+ model.type.is_a?(Type::InexistentType)+ endendenddiff --git a/app/models/priority/inexistent_priority.rb b/app/models/priority/inexistent_priority.rbnew file mode 100644index 00000000000..12a721167ac--- /dev/null+++ b/app/models/priority/inexistent_priority.rb@@ -0,0 +1,31 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# 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.+#++++class Priority::InexistentPriority < IssuePriority; enddiff --git a/app/models/status/inexistent_status.rb b/app/models/status/inexistent_status.rbnew file mode 100644index 00000000000..a7cb10fa7f0--- /dev/null+++ b/app/models/status/inexistent_status.rb@@ -0,0 +1,31 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# 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.+#++++class Status::InexistentStatus < Status; enddiff --git a/app/models/type/inexistent_type.rb b/app/models/type/inexistent_type.rbnew file mode 100644index 00000000000..2a0af5c8517--- /dev/null+++ b/app/models/type/inexistent_type.rb@@ -0,0 +1,32 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# 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.+#++++class Type::InexistentType < Type+enddiff --git a/app/models/user/inexistent_user.rb b/app/models/user/inexistent_user.rbnew file mode 100644index 00000000000..6fd662b8b58--- /dev/null+++ b/app/models/user/inexistent_user.rb@@ -0,0 +1,31 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# 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.+#++++class User::InexistentUser < User; enddiff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rbindex 2b9990cfa18..6a874320e7c 100644--- a/app/services/work_packages/set_attributes_service.rb+++ b/app/services/work_packages/set_attributes_service.rb@@ -177,7 +177,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributesenddef reassign_status(available_statuses)- return if available_statuses.include? work_package.status+ return if available_statuses.include?(work_package.status) || work_package.status.is_a?(Status::InexistentStatus)new_status = available_statuses.detect(&:is_default) || available_statuses.firstwork_package.status = new_status if new_status.present?diff --git a/modules/bcf/app/representers/bcf/api/v2_1/errors/error_mapper.rb b/modules/bcf/app/representers/bcf/api/v2_1/errors/error_mapper.rbindex c04ac968594..40be1524eb7 100644--- a/modules/bcf/app/representers/bcf/api/v2_1/errors/error_mapper.rb+++ b/modules/bcf/app/representers/bcf/api/v2_1/errors/error_mapper.rb@@ -53,6 +53,10 @@ module Bcf::API::V2_1::Errorsmapped_errorsend+ def self.i18n_scope+ :activerecord+ end+def self.error_key_mapper(key){ subject: :title }[key] || keyenddiff --git a/modules/bcf/app/services/bcf/issues/transform_attributes_service.rb b/modules/bcf/app/services/bcf/issues/transform_attributes_service.rbindex ec0b8646267..f5033766cd8 100644--- a/modules/bcf/app/services/bcf/issues/transform_attributes_service.rb+++ b/modules/bcf/app/services/bcf/issues/transform_attributes_service.rb@@ -52,7 +52,13 @@ module Bcf::Issuesenddef assignee(project, attributes)- find_user_in_project(project, attributes[:assignee])+ assignee = find_user_in_project(project, attributes[:assignee])++ return assignee if assignee.present?++ if attributes[:assignee]+ User::InexistentUser.new+ endend##@@ -67,16 +73,7 @@ module Bcf::Issuesreturn type if type.present?- import_options = attributes[:import_options]-- return unless import_options-- if import_options[:unknown_types_action] == 'default'- ::Type.default&.first- elsif import_options[:unknown_types_action] == 'chose' &&- import_options[:unknown_types_chose_ids].any?- ::Type.find_by(id: import_options[:unknown_types_chose_ids].first)- end+ missing_type(type_name, attributes[:import_options] || {})end##@@ -87,16 +84,7 @@ module Bcf::Issuesreturn status if status.present?- import_options = attributes[:import_options]-- return unless import_options-- if import_options[:unknown_statuses_action] == 'use_default'- ::Status.default- elsif import_options[:unknown_statuses_action] == 'chose' &&- import_options[:unknown_statuses_chose_ids].any?- ::Status.find_by(id: import_options[:unknown_statuses_chose_ids].first)- end+ missing_status(status_name, attributes[:import_options] || {})end##@@ -107,16 +95,7 @@ module Bcf::Issuesreturn priority if priority.present?- import_options = attributes[:import_options]-- return unless import_options-- if import_options[:unknown_priorities_action] == 'use_default'- # NOP The 'use_default' case gets already covered by OP.- elsif import_options[:unknown_priorities_action] == 'chose' &&- import_options[:unknown_priorities_chose_ids].any?- ::IssuePriority.find_by(id: import_options[:unknown_priorities_chose_ids].first)- end+ missing_priority(priority_name, attributes[:import_options] || {})end##@@ -142,5 +121,38 @@ module Bcf::Issuespriority: priority(attributes)}.compactend++ def missing_status(status_name, import_options)+ if import_options[:unknown_statuses_action] == 'use_default'+ ::Status.default+ elsif import_options[:unknown_statuses_action] == 'chose' &&+ import_options[:unknown_statuses_chose_ids].any?+ ::Status.find_by(id: import_options[:unknown_statuses_chose_ids].first)+ elsif status_name+ Status::InexistentStatus.new+ end+ end++ def missing_priority(priority_name, import_options)+ if import_options[:unknown_priorities_action] == 'use_default'+ # NOP The 'use_default' case gets already covered by OP.+ elsif import_options[:unknown_priorities_action] == 'chose' &&+ import_options[:unknown_priorities_chose_ids].any?+ ::IssuePriority.find_by(id: import_options[:unknown_priorities_chose_ids].first)+ elsif priority_name+ Priority::InexistentPriority.new+ end+ end++ def missing_type(type_name, import_options)+ if import_options[:unknown_types_action] == 'default'+ ::Type.default&.first+ elsif import_options[:unknown_types_action] == 'chose' &&+ import_options[:unknown_types_chose_ids].any?+ ::Type.find_by(id: import_options[:unknown_types_chose_ids].first)+ elsif type_name+ Type::InexistentType.new+ end+ endendenddiff --git a/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb b/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rbindex 10062a60c22..3dd074c63f8 100644--- a/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb+++ b/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb@@ -90,6 +90,7 @@ describe 'BCF 2.1 topics resource', type: :request, content_type: :json, with_ma"guid": bcf_issue.uuid,"index": bcf_issue.index,"labels": bcf_issue.labels,+ "priority": work_package.priority.name,"modified_author": current_user.mail,"modified_date": work_package.updated_at.iso8601,"reference_links": [@@ -138,6 +139,7 @@ describe 'BCF 2.1 topics resource', type: :request, content_type: :json, with_ma"guid": bcf_issue.uuid,"index": bcf_issue.index,"labels": bcf_issue.labels,+ "priority": work_package.priority.name,"modified_author": current_user.mail,"modified_date": work_package.updated_at.iso8601,"reference_links": [@@ -294,7 +296,67 @@ describe 'BCF 2.1 topics resource', type: :request, content_type: :json, with_mait_behaves_like 'bcf api unprocessable response' dolet(:message) do- "Title can't be blank"+ "Title can't be blank."+ end+ end+ end++ context 'with an inexistent status' do+ let(:params) do+ {+ title: 'Some title',+ topic_status: 'Some non existing status'+ }+ end++ it_behaves_like 'bcf api unprocessable response' do+ let(:message) do+ "Status does not exist."+ end+ end+ end++ context 'with an inexistent priority' do+ let(:params) do+ {+ title: 'Some title',+ priority: 'Some non existing priority'+ }+ end++ it_behaves_like 'bcf api unprocessable response' do+ let(:message) do+ "Priority does not exist."+ end+ end+ end++ context 'with an inexistent type' do+ let(:params) do+ {+ title: 'Some title',+ topic_type: 'Some non existing type'+ }+ end++ it_behaves_like 'bcf api unprocessable response' do+ let(:message) do+ "Type does not exist."+ end+ end+ end++ context 'with an inexistent assigned_to' do+ let(:params) do+ {+ title: 'Some title',+ assigned_to: 'Some non existing assignee'+ }+ end++ it_behaves_like 'bcf api unprocessable response' do+ let(:message) do+ "Assignee does not exist."endendenddiff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rbindex 8c5e010034c..a5c100f7e6b 100644--- a/spec/contracts/work_packages/base_contract_spec.rb+++ b/spec/contracts/work_packages/base_contract_spec.rb@@ -174,7 +174,7 @@ describe WorkPackages::BaseContract dobefore doallow(work_package).to receive(:status_id_change)- .and_return [1,2]+ .and_return [1, 2]endit 'is writable' do@@ -182,6 +182,19 @@ describe WorkPackages::BaseContract doendendend++ context 'is an inexistent status' do+ before do+ work_package.status = Status::InexistentStatus.new+ end++ it 'is invalid' do+ contract.validate++ expect(subject.errors.symbols_for(:status))+ .to match_array [:does_not_exist]+ end+ endenddescribe 'estimated hours' do@@ -521,6 +534,34 @@ describe WorkPackages::BaseContract doendendend++ context 'inexistent type' do+ before do+ work_package.type = Type::InexistentType.new++ contract.validate+ end++ it 'is invalid' do+ expect(contract.errors.symbols_for(:type))+ .to match_array [:does_not_exist]+ end+ end+ end++ context 'assigned_to' do+ context 'inexistent user' do+ before do+ work_package.assigned_to = User::InexistentUser.new++ contract.validate+ end++ it 'is invalid' do+ expect(contract.errors.symbols_for(:assigned_to))+ .to match_array [:does_not_exist]+ end+ endenddescribe 'category' do@@ -630,6 +671,19 @@ describe WorkPackages::BaseContract do.to be_emptyendend++ context 'inexistent priority' do+ before do+ work_package.priority = Priority::InexistentPriority.new++ contract.validate+ end++ it 'is invalid' do+ expect(contract.errors.symbols_for(:priority))+ .to match_array [:does_not_exist]+ end+ endenddescribe 'status' do