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

add specs for relation contract

ulferts4c2d9f9b2452
diff --git a/app/contracts/relations/base_contract.rb b/app/contracts/relations/base_contract.rb
index 5500763ddfe..976fec321c5 100644
--- a/app/contracts/relations/base_contract.rb
+++ b/app/contracts/relations/base_contract.rb
@@ -26,8 +26,6 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-require 'model_contract'
-
module Relations
class BaseContract < ::ModelContract
attribute :relation_type
@@ -65,7 +63,7 @@ module Relations
def validate_nodes_relatable
if (model.from_id_changed? || model.to_id_changed?) &&
- WorkPackage.relatable(model.from, model.relation_type, ignore_relation: model).where(id: model.to).empty?
+ WorkPackage.relatable(model.from, model.relation_type, ignored_relation: model).where(id: model.to).empty?
errors.add :base, I18n.t(:'activerecord.errors.messages.circular_dependency')
end
end
diff --git a/app/contracts/relations/update_contract.rb b/app/contracts/relations/update_contract.rb
index 37ab87112a7..cda517a52fe 100644
--- a/app/contracts/relations/update_contract.rb
+++ b/app/contracts/relations/update_contract.rb
@@ -26,8 +26,6 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-require 'relations/base_contract'
-
module Relations
class UpdateContract < BaseContract
validate :from_immutable
diff --git a/app/models/work_packages/scopes/directly_related.rb b/app/models/work_packages/scopes/directly_related.rb
index 6a5447a9e38..5082cd06dcd 100644
--- a/app/models/work_packages/scopes/directly_related.rb
+++ b/app/models/work_packages/scopes/directly_related.rb
@@ -31,8 +31,8 @@ module WorkPackages::Scopes
extend ActiveSupport::Concern
class_methods do
- def directly_related(work_package, ignore_relation: nil)
- relations_without_ignored = ignore_relation ? Relation.where.not(id: ignore_relation.id) : Relation.all
+ def directly_related(work_package, ignored_relation: nil)
+ relations_without_ignored = ignored_relation ? Relation.where.not(id: ignored_relation.id) : Relation.all
where(id: relations_without_ignored.where(from_id: work_package).select(:to_id))
.or(where(id: relations_without_ignored.where(to_id: work_package).select(:from_id)))
diff --git a/app/models/work_packages/scopes/relatable.rb b/app/models/work_packages/scopes/relatable.rb
index 58d21f76908..0d5a4cdcec0 100644
--- a/app/models/work_packages/scopes/relatable.rb
+++ b/app/models/work_packages/scopes/relatable.rb
@@ -173,8 +173,8 @@ module WorkPackages::Scopes
# work packages are relatable. This can be helpful in case an existing relation is updated
# especially if the the direction is switched. Only a single relation can be provided and
# that one has to either be from or to the work package queried for.
- def relatable(work_package, relation_type, ignore_relation: nil)
- relatable_ensure_single_relation(ignore_relation, work_package)
+ def relatable(work_package, relation_type, ignored_relation: nil)
+ relatable_ensure_single_relation(ignored_relation, work_package)
return all if work_package.new_record?
@@ -184,11 +184,11 @@ module WorkPackages::Scopes
when Relation::TYPE_CHILD
not_having_potential_tree_relation_child(work_package)
else
- where.not(id: directly_related(work_package, ignore_relation:))
+ where.not(id: directly_related(work_package, ignored_relation:))
end
scope = scope
- .not_having_transitive_relation(work_package, relation_type, ignore_relation:)
+ .not_having_transitive_relation(work_package, relation_type, ignored_relation:)
.where.not(id: work_package.id)
if Setting.cross_project_work_package_relations
@@ -198,7 +198,7 @@ module WorkPackages::Scopes
end
end
- def not_having_transitive_relation(work_package, relation_type, ignore_relation:)
+ def not_having_transitive_relation(work_package, relation_type, ignored_relation:)
if relation_type == Relation::TYPE_RELATES
# Bypassing the recursive query in this case as only children and parent needs to be excluded.
# Using this more complicated statement since
@@ -211,7 +211,7 @@ module WorkPackages::Scopes
sql = <<~SQL.squish
WITH
RECURSIVE
- #{non_relatable_paths_sql(work_package, relation_type, ignore_relation:)}
+ #{non_relatable_paths_sql(work_package, relation_type, ignored_relation:)}
SELECT id
FROM related
@@ -250,7 +250,7 @@ module WorkPackages::Scopes
.where.not(id: where(parent_id: work_package.id).select(:id))
end
- def non_relatable_paths_sql(work_package, relation_type, ignore_relation: nil)
+ def non_relatable_paths_sql(work_package, relation_type, ignored_relation: nil)
<<~SQL.squish
related (id,
from_hierarchy,
@@ -275,7 +275,7 @@ module WorkPackages::Scopes
FROM
related
JOIN LATERAL (
- #{joined_existing_connections(relation_type, ignore_relation:)}
+ #{joined_existing_connections(relation_type, ignored_relation:)}
) relations ON 1 = 1
)
SQL
@@ -315,7 +315,7 @@ module WorkPackages::Scopes
id: work_package.id
end
- def joined_existing_connections(relation_type, ignore_relation:)
+ def joined_existing_connections(relation_type, ignored_relation:)
unions = [existing_hierarchy_lateral(with_descendants: relation_type != Relation::TYPE_CHILD)]
case relation_type
@@ -323,14 +323,14 @@ module WorkPackages::Scopes
unions << existing_relation_of_type_lateral(Relation::TYPE_FOLLOWS, limit_direction: true)
unions << existing_relation_of_type_lateral(Relation::TYPE_PRECEDES, limit_direction: true)
else
- unions << existing_relation_of_type_lateral(relation_type, ignore_relation:)
+ unions << existing_relation_of_type_lateral(relation_type, ignored_relation:)
end
unions.join(' UNION ')
end
# rubocop:disable Metrics/PerceivedComplexity
- def existing_relation_of_type_lateral(relation_type, ignore_relation: nil, limit_direction: false)
+ def existing_relation_of_type_lateral(relation_type, ignored_relation: nil, limit_direction: false)
canonical_type = Relation.canonical_type(relation_type)
is_canonical = canonical_type == relation_type
@@ -363,7 +363,7 @@ module WorkPackages::Scopes
WHERE (relations.#{direction2} = related.id AND relations.relation_type = :relation_type)
AND NOT related.from_#{direction2}
#{direction_limit ? "AND NOT #{direction_limit}" : ''}
- #{ignore_relation&.id ? " AND NOT relations.id = #{ignore_relation.id}" : ''}
+ #{ignored_relation&.id ? " AND NOT relations.id = #{ignored_relation.id}" : ''}
SQL
::OpenProject::SqlSanitization
diff --git a/spec/contracts/relations/create_contract_spec.rb b/spec/contracts/relations/create_contract_spec.rb
index 6ccb9e0a949..20ce9f84cd3 100644
--- a/spec/contracts/relations/create_contract_spec.rb
+++ b/spec/contracts/relations/create_contract_spec.rb
@@ -27,51 +27,17 @@
#++
require 'spec_helper'
+require_relative 'shared_contract_examples'
RSpec.describe Relations::CreateContract do
- let(:from) { build_stubbed(:work_package) }
- let(:to) { build_stubbed(:work_package) }
- let(:user) { build_stubbed(:admin) }
-
- let(:relation) do
- Relation.new from:, to:, relation_type: "follows", delay: 42
- end
-
- subject(:contract) { described_class.new relation, user }
-
- before do
- allow(WorkPackage)
- .to receive_message_chain(:visible, :exists?)
- .and_return(true)
- end
-
- describe 'to' do
- context 'not visible' do
- before do
- allow(WorkPackage)
- .to receive_message_chain(:visible, :exists?)
- .with(to.id)
- .and_return(false)
- end
-
- it 'is invalid' do
- expect(subject).not_to be_valid
- end
+ it_behaves_like 'relation contract' do
+ let(:relation) do
+ Relation.new from: relation_from,
+ to: relation_to,
+ relation_type:,
+ delay: relation_delay
end
- end
- describe 'from' do
- context 'not visible' do
- before do
- allow(WorkPackage)
- .to receive_message_chain(:visible, :exists?)
- .with(from.id)
- .and_return(false)
- end
-
- it 'is invalid' do
- expect(subject).not_to be_valid
- end
- end
+ subject(:contract) { described_class.new relation, current_user }
end
end
diff --git a/spec/contracts/relations/shared_contract_examples.rb b/spec/contracts/relations/shared_contract_examples.rb
new file mode 100644
index 00000000000..7bab94abe28
--- /dev/null
+++ b/spec/contracts/relations/shared_contract_examples.rb
@@ -0,0 +1,144 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2023 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-2013 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 COPYRIGHT and LICENSE files for more details.
+# ++
+
+require 'contracts/shared/model_contract_shared_context'
+
+RSpec.shared_examples_for 'relation contract' do
+ include_context 'ModelContract shared context'
+
+ let(:current_user) { build_stubbed(:user) }
+ let(:relation_from) do
+ build_stubbed(:work_package, subject: 'From WP') do |wp|
+ allow(wp_visible_scope)
+ .to receive(:exists?)
+ .with(wp.id)
+ .and_return(relation_from_visible)
+ end
+ end
+ let(:relation_to) do
+ build_stubbed(:work_package, subject: 'To WP') do |wp|
+ allow(wp_visible_scope)
+ .to receive(:exists?)
+ .with(wp.id)
+ .and_return(relation_to_visible)
+ end
+ end
+ let(:relation_type) do
+ Relation::TYPE_RELATES
+ end
+ let(:relation_delay) { 42 }
+ let!(:wp_visible_scope) do
+ instance_double(ActiveRecord::Relation).tap do |relation|
+ allow(WorkPackage)
+ .to receive(:visible)
+ .with(current_user)
+ .and_return(relation)
+ end
+ end
+ let!(:relatable_scope) do
+ scope = instance_double(ActiveRecord::Relation)
+
+ allow(WorkPackage)
+ .to receive(:relatable)
+ .with(canonical_relation_from, canonical_relation_type, ignored_relation: relation)
+ .and_return(scope)
+
+ allow(scope)
+ .to receive(:where)
+ .with(id: canonical_relation_to)
+ .and_return(scope)
+
+ allow(scope)
+ .to receive(:empty?)
+ .and_return(!relatable)
+
+ scope
+ end
+
+ # The relation might get reversed by the code under test
+ let(:canonical_relation_from) { Relation::TYPES.dig(relation_type, :reverse).present? ? relation_to : relation_from }
+ let(:canonical_relation_to) { Relation::TYPES.dig(relation_type, :reverse).present? ? relation_from : relation_to }
+ let(:canonical_relation_type) do
+ Relation::TYPES.dig(relation_type, :reverse).present? ? Relation::TYPES[relation_type][:reverse] : relation_type
+ end
+
+ let(:relation_from_visible) { true }
+ let(:relation_to_visible) { true }
+ let(:relatable) { true }
+ let(:permissions) { [:manage_work_package_relations] }
+
+ before do
+ mock_permissions_for(current_user) do |mock|
+ mock.allow_in_project *permissions, project: canonical_relation_from.project
+ end
+ end
+
+ describe 'validation' do
+ it_behaves_like 'contract is valid'
+
+ context 'when lacking the necessary permission' do
+ let(:permissions) { [] }
+
+ it_behaves_like 'contract is invalid', base: :error_unauthorized
+ end
+
+ context 'when the work package for from is not visible' do
+ let(:relation_from_visible) { false }
+
+ it_behaves_like 'contract is invalid', from: :error_not_found
+ end
+
+ context 'when the work package for to is not visible' do
+ let(:relation_to_visible) { false }
+
+ it_behaves_like 'contract is invalid', to: :error_not_found
+ end
+
+ Relation::TYPES.keys.select { Relation::TYPES[_1][:reverse].nil? }.each do |available_type|
+ context "when having the type '#{available_type}'" do
+ let(:relation_type) { available_type }
+
+ it_behaves_like 'contract is valid'
+ end
+ end
+
+ Relation::TYPES.keys.select { Relation::TYPES[_1][:reverse].present? }.each do |available_type|
+ context "when having the type '#{available_type}'" do
+ let(:relation_type) { available_type }
+
+ it_behaves_like 'contract is valid'
+ end
+ end
+
+ context 'when the work package type is unknown' do
+ let(:relation_type) { 'some_bogus' }
+
+ it_behaves_like 'contract is invalid', relation_type: :inclusion
+ end
+ end
+end
diff --git a/spec/contracts/relations/update_contract_spec.rb b/spec/contracts/relations/update_contract_spec.rb
new file mode 100644
index 00000000000..2f5f64e7a28
--- /dev/null
+++ b/spec/contracts/relations/update_contract_spec.rb
@@ -0,0 +1,98 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2023 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-2013 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 COPYRIGHT and LICENSE files for more details.
+# ++
+
+require 'spec_helper'
+require_relative 'shared_contract_examples'
+
+RSpec.describe Relations::UpdateContract do
+ it_behaves_like 'relation contract' do
+ let(:relation) do
+ build_stubbed(:relation,
+ from: relation_from,
+ to: relation_to,
+ relation_type:,
+ delay: relation_delay)
+ end
+
+ let(:contract) { described_class.new(relation, current_user) }
+ end
+
+ describe 'valid?' do
+ create_shared_association_defaults_for_work_package_factory
+ let(:current_user) { build_stubbed(:admin) }
+ let(:contract) { described_class.new(relation, current_user) }
+
+ context 'when an isolated relation is reversed' do
+ let(:relation) do
+ create(:relation, relation_type: Relation::TYPE_BLOCKS)
+ end
+
+ before do
+ relation.relation_type = Relation::TYPE_BLOCKED
+ end
+
+ it 'is valid' do
+ expect(contract).to be_valid
+ end
+ end
+
+ context 'when a relation that cannot be reversed is reversed' do
+ let(:relation_from_wp) { create(:work_package) }
+ let(:relation_to_wp) { create(:work_package) }
+ let(:intermediary_wp) { create(:work_package) }
+
+ let!(:to_intermediary_wp) do
+ create(:relation,
+ from: relation_from_wp,
+ to: intermediary_wp,
+ relation_type: Relation::TYPE_FOLLOWS)
+ end
+ let!(:from_intermediary_wp) do
+ create(:relation,
+ from: intermediary_wp,
+ to: relation_to_wp,
+ relation_type: Relation::TYPE_FOLLOWS)
+ end
+
+ let(:relation) do
+ create(:relation,
+ from: relation_from_wp,
+ to: relation_to_wp,
+ relation_type: Relation::TYPE_FOLLOWS)
+ end
+
+ before do
+ relation.relation_type = Relation::TYPE_PRECEDES
+ end
+
+ it 'is invalid' do
+ expect(contract).not_to be_valid
+ end
+ end
+ end
+end
diff --git a/spec/models/work_packages/scopes/directly_related_spec.rb b/spec/models/work_packages/scopes/directly_related_spec.rb
index 433e205bcb5..6237128ce7f 100644
--- a/spec/models/work_packages/scopes/directly_related_spec.rb
+++ b/spec/models/work_packages/scopes/directly_related_spec.rb
@@ -66,7 +66,7 @@ RSpec.describe WorkPackages::Scopes::DirectlyRelated, '.directly_related scope'
let!(:existing_relations) { [relation_to, transitive_relation_to, relation_from, transitive_relation_from] }
- subject(:directly_related) { WorkPackage.directly_related(origin, ignore_relation: ignored_relations) }
+ subject(:directly_related) { WorkPackage.directly_related(origin, ignored_relation: ignored_relations) }
it 'is an AR scope' do
expect(directly_related)
diff --git a/spec/models/work_packages/scopes/for_scheduling_spec.rb b/spec/models/work_packages/scopes/for_scheduling_spec.rb
index a697245b43c..3ba089dd817 100644
--- a/spec/models/work_packages/scopes/for_scheduling_spec.rb
+++ b/spec/models/work_packages/scopes/for_scheduling_spec.rb
@@ -29,6 +29,8 @@
require 'spec_helper'
RSpec.describe WorkPackages::Scopes::ForScheduling, 'allowed scope' do
+ create_shared_association_defaults_for_work_package_factory
+
let(:project) { create(:project) }
let(:origin) { create(:work_package, project:) }
let(:predecessor) do
diff --git a/spec/models/work_packages/scopes/relatable_spec.rb b/spec/models/work_packages/scopes/relatable_spec.rb
index d22d65ac399..849b47c932e 100644
--- a/spec/models/work_packages/scopes/relatable_spec.rb
+++ b/spec/models/work_packages/scopes/relatable_spec.rb
@@ -75,7 +75,7 @@ RSpec.describe WorkPackages::Scopes::Relatable, '.relatable scope' do
let(:relation_type) { Relation::TYPE_FOLLOWS }
let(:ignored_relation) { nil }
- subject(:relatable) { WorkPackage.relatable(origin, relation_type, ignore_relation: ignored_relation) }
+ subject(:relatable) { WorkPackage.relatable(origin, relation_type, ignored_relation:) }
it 'is an AR scope' do
expect(relatable)