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
introduce wp_member update contract&service
ulfertsc6f6954ec352
diff --git a/app/contracts/work_package_members/base_contract.rb b/app/contracts/work_package_members/base_contract.rbindex 9b88db121eb..5a610372c12 100644--- a/app/contracts/work_package_members/base_contract.rb+++ b/app/contracts/work_package_members/base_contract.rb@@ -36,8 +36,10 @@ module WorkPackageMembersvalidate :user_allowed_to_managevalidate :role_grantablevalidate :single_role- validate :principal_assignablevalidate :project_set+ validate :entity_set++ attribute_alias(:user_id, :principal)private@@ -58,14 +60,6 @@ module WorkPackageMemberserrors.add(:roles, :ungrantable) unless active_roles.all? { _1.is_a?(WorkPackageRole) }end- def principal_assignable- return if principal.nil?-- if principal.builtin? || principal.locked?- errors.add(:principal, :unassignable)- end- end-def project_seterrors.add(:project, :blank) if project.nil?end@@ -73,5 +67,9 @@ module WorkPackageMembersdef active_rolesmodel.member_roles.reject(&:marked_for_destruction?).map(&:role)end++ def entity_set+ errors.add(:entity, :blank) if entity_id.nil?+ endendenddiff --git a/app/contracts/work_package_members/create_contract.rb b/app/contracts/work_package_members/create_contract.rbindex 8827de669ed..c21ce60ef1a 100644--- a/app/contracts/work_package_members/create_contract.rb+++ b/app/contracts/work_package_members/create_contract.rb@@ -32,5 +32,17 @@ module WorkPackageMembersattribute :entity_idattribute :entity_typeattribute :user_id++ private++ validate :principal_assignable++ def principal_assignable+ return if principal.nil?++ if principal.builtin? || principal.locked?+ errors.add(:principal, :unassignable)+ end+ endendenddiff --git a/app/contracts/work_package_members/update_contract.rb b/app/contracts/work_package_members/update_contract.rbnew file mode 100644index 00000000000..785ec1030ff--- /dev/null+++ b/app/contracts/work_package_members/update_contract.rb@@ -0,0 +1,36 @@+# -- 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.+# ++++module WorkPackageMembers+ class UpdateContract < BaseContract+ attribute :principal,+ writable: false+ attribute :entity_id,+ writable: false+ end+enddiff --git a/app/services/work_package_members/update_service.rb b/app/services/work_package_members/update_service.rbnew file mode 100644index 00000000000..709ac51adb8--- /dev/null+++ b/app/services/work_package_members/update_service.rb@@ -0,0 +1,30 @@+# -- 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.+# ++++class WorkPackageMembers::UpdateService < BaseServices::Update+enddiff --git a/spec/contracts/work_package_members/create_contract_spec.rb b/spec/contracts/work_package_members/create_contract_spec.rbindex fbb48b27022..23fb27fa676 100644--- a/spec/contracts/work_package_members/create_contract_spec.rb+++ b/spec/contracts/work_package_members/create_contract_spec.rb@@ -47,5 +47,54 @@ RSpec.describe WorkPackageMembers::CreateContract doendlet(:contract) { described_class.new(member, current_user) }++ describe 'validation' do+ context 'if the principal is nil' do+ let(:member_principal) { nil }++ it_behaves_like 'contract is invalid', principal: :blank+ end++ context 'if the principal is a locked user' do+ let(:member_principal) { build_stubbed(:locked_user) }++ it_behaves_like 'contract is invalid', principal: :unassignable+ end++ context 'if the principal is a builtin user' do+ let(:member_principal) { build_stubbed(:anonymous) }++ it_behaves_like 'contract is invalid', principal: :unassignable+ end++ context 'if the project is nil' do+ let(:member_project) { nil }++ it_behaves_like 'contract is invalid', project: :blank+ end++ context 'if the project is not set by the system' do+ before do+ member.project = build_stubbed(:project)+ end++ it_behaves_like 'contract is invalid', project_id: :error_readonly+ end++ context 'if the entity is nil' do+ let(:member_entity) { nil }+ let(:member_project) { build_stubbed(:project) }++ it_behaves_like 'contract is invalid', entity: :blank+ end++ context 'if the entity is something else' do+ before do+ member.entity_type = 'Meeting'+ end++ it_behaves_like 'contract is invalid', entity_type: :inclusion+ end+ endendenddiff --git a/spec/contracts/work_package_members/shared_contract_examples.rb b/spec/contracts/work_package_members/shared_contract_examples.rbindex 45fd235a326..230b55dfa95 100644--- a/spec/contracts/work_package_members/shared_contract_examples.rb+++ b/spec/contracts/work_package_members/shared_contract_examples.rb@@ -59,24 +59,6 @@ RSpec.shared_examples_for 'work package member contract' dodescribe 'validation' doit_behaves_like 'contract is valid'- context 'if the principal is nil' do- let(:member_principal) { nil }-- it_behaves_like 'contract is invalid', principal: :blank- end-- context 'if the principal is a builtin user' do- let(:member_principal) { build_stubbed(:anonymous) }-- it_behaves_like 'contract is invalid', principal: :unassignable- end-- context 'if the principal is a locked user' do- let(:member_principal) { build_stubbed(:locked_user) }-- it_behaves_like 'contract is invalid', principal: :unassignable- end-context 'if the roles are nil' dolet(:member_roles) { [] }@@ -106,19 +88,5 @@ RSpec.shared_examples_for 'work package member contract' doit_behaves_like 'contract is invalid', base: :error_unauthorizedend-- context 'if the project is nil' do- let(:member_project) { nil }-- it_behaves_like 'contract is invalid', project: :blank- end-- context 'if the project is not set by the system' do- before do- member.project = build_stubbed(:project)- end-- it_behaves_like 'contract is invalid', project_id: :error_readonly- endendenddiff --git a/spec/contracts/work_package_members/update_contract_spec.rb b/spec/contracts/work_package_members/update_contract_spec.rbnew file mode 100644index 00000000000..bbfcc95a9e2--- /dev/null+++ b/spec/contracts/work_package_members/update_contract_spec.rb@@ -0,0 +1,78 @@+# -- 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 WorkPackageMembers::UpdateContract do+ include_context 'ModelContract shared context'++ it_behaves_like 'work package member contract' do+ let(:member) do+ build_stubbed(:work_package_member,+ project: member_project,+ roles: member_roles,+ principal: member_principal,+ entity: member_entity)+ end++ let(:contract) { described_class.new(member, current_user) }++ describe 'validation' do+ context 'if the principal is changed' do+ before do+ member.principal = build_stubbed(:user)+ end++ it_behaves_like 'contract is invalid', principal: :error_readonly+ end++ context 'if the project is changed' do+ before do+ member.project = build_stubbed(:project)+ end++ it_behaves_like 'contract is invalid', project_id: :error_readonly+ end++ context 'if the entity is changed' do+ before do+ member.entity = build_stubbed(:work_package)+ end++ it_behaves_like 'contract is invalid', entity_id: :error_readonly+ end++ context 'if the principal is a locked user' do+ let(:member_principal) { build_stubbed(:locked_user) }++ it_behaves_like 'contract is valid'+ end+ end+ end+enddiff --git a/spec/features/work_packages/share_spec.rb b/spec/features/work_packages/share_spec.rbindex b074fb5376a..35a81394525 100644--- a/spec/features/work_packages/share_spec.rb+++ b/spec/features/work_packages/share_spec.rb@@ -109,6 +109,11 @@ RSpec.describe 'Work package sharing',share_modal.expect_not_shared_with(edit_user)share_modal.expect_shared_count_of(4)++ # Adding a user multiple times will lead to the user's role being updated.+ share_modal.invite_user(not_shared_yet_with_user, 'Edit')++ share_modal.expect_shared_with(not_shared_yet_with_user, 'Edit')endenddiff --git a/spec/services/work_package_members/update_service_spec.rb b/spec/services/work_package_members/update_service_spec.rbnew file mode 100644index 00000000000..f419efe7f50--- /dev/null+++ b/spec/services/work_package_members/update_service_spec.rb@@ -0,0 +1,42 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 'services/base_services/behaves_like_update_service'++RSpec.describe WorkPackageMembers::UpdateService do+ let(:role) { build_stubbed(:view_work_package_role) }+ let(:model_instance) { build_stubbed(:work_package_member) }++ it_behaves_like 'BaseServices update service' do+ let(:model_class) { Member }+ let(:call_attributes) { { roles: [role] } }+ end+end