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/deleted user representation (#9025)

ulfertse1fbbf1fd49d
diff --git a/lib/api/v3/memberships/membership_representer.rb b/lib/api/v3/memberships/membership_representer.rb
index 6552704e7f1..08280cb1784 100644
--- a/lib/api/v3/memberships/membership_representer.rb
+++ b/lib/api/v3/memberships/membership_representer.rb
@@ -66,9 +66,12 @@ module API
associated_resource :project
associated_resource :principal,
- getter: ::API::V3::Principals::AssociatedSubclassLambda.getter(:principal),
- setter: ::API::V3::Principals::AssociatedSubclassLambda.setter(:user),
- link: ::API::V3::Principals::AssociatedSubclassLambda.link(:principal, getter: 'user_id')
+ getter: ::API::V3::Principals::PrincipalRepresenterFactory
+ .create_getter_lambda(:principal),
+ setter: ::API::V3::Principals::PrincipalRepresenterFactory
+ .create_setter_lambda(:user),
+ link: ::API::V3::Principals::PrincipalRepresenterFactory
+ .create_link_lambda(:principal, getter: 'user_id')
associated_resources :roles,
getter: ->(*) do
diff --git a/lib/api/v3/principals/associated_subclass_lambda.rb b/lib/api/v3/principals/associated_subclass_lambda.rb
deleted file mode 100644
index bd1b5882b9c..00000000000
--- a/lib/api/v3/principals/associated_subclass_lambda.rb
+++ /dev/null
@@ -1,90 +0,0 @@
-#-- encoding: UTF-8
-
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
-#++
-
-module API
- module V3
- module Principals
- module AssociatedSubclassLambda
- include ::API::Decorators::LinkedResource
-
- def self.link(name, getter: "#{name}_id")
- ->(*) {
- instance = represented.send(name)
-
- v3_path = case instance
- when User
- :user
- when Group
- :group
- when PlaceholderUser
- :placeholder_user
- when NilClass
- # Fall back to user for unknown principal
- # since we do not have a principal route.
- :user
- else
- raise "undefined subclass for #{instance}"
- end
-
- instance_exec(&self.class.associated_resource_default_link(name,
- v3_path: v3_path,
- skip_link: -> { false },
- title_attribute: :name,
- getter: getter))
- }
- end
-
- def self.getter(name)
- ->(*) {
- next unless embed_links
-
- instance = represented.send(name)
- next if instance.nil?
-
- ::API::V3::Principals::PrincipalRepresenterFactory
- .create(represented.send(name), current_user: current_user)
- }
- end
-
- def self.setter(name, property_name: name, namespaces: %i(groups users placeholder_users))
- ->(fragment:, **) {
- ::API::Decorators::LinkObject
- .new(represented,
- property_name: property_name,
- namespace: namespaces,
- getter: :"#{name}_id",
- setter: :"#{name}_id=")
- .from_hash(fragment)
- }
- end
- end
- end
- end
-end
diff --git a/lib/api/v3/principals/principal_representer_factory.rb b/lib/api/v3/principals/principal_representer_factory.rb
index 01f6b20cae1..26a99738ccf 100644
--- a/lib/api/v3/principals/principal_representer_factory.rb
+++ b/lib/api/v3/principals/principal_representer_factory.rb
@@ -35,23 +35,76 @@ module API
##
# Create the appropriate subclass representer
# for each principal entity
- def self.create(model, *args)
+ def self.create(model, **args)
representer_class(model)
- .create(model, *args)
+ .create(model, **args)
end
def self.representer_class(model)
- case model.type
- when 'User'
+ case model
+ when User
::API::V3::Users::UserRepresenter
- when 'Group'
+ when Group
::API::V3::Groups::GroupRepresenter
- when 'PlaceholderUser'
+ when PlaceholderUser
::API::V3::PlaceholderUsers::PlaceholderUserRepresenter
else
raise ArgumentError, "Missing concrete principal representer for #{model}"
end
end
+
+ def self.create_link_lambda(name, getter: "#{name}_id")
+ ->(*) {
+ v3_path = API::V3::Principals::PrincipalRepresenterFactory.v3_path(represented.send(name))
+
+ instance_exec(&self.class.associated_resource_default_link(name,
+ v3_path: v3_path,
+ skip_link: -> { false },
+ title_attribute: :name,
+ getter: getter))
+ }
+ end
+
+ def self.create_setter_lambda(name, property_name: name, namespaces: %i(groups users placeholder_users))
+ ->(fragment:, **) {
+ ::API::Decorators::LinkObject
+ .new(represented,
+ property_name: property_name,
+ namespace: namespaces,
+ getter: :"#{name}_id",
+ setter: :"#{name}_id=")
+ .from_hash(fragment)
+ }
+ end
+
+ def self.create_getter_lambda(name)
+ ->(*) {
+ next unless embed_links
+
+ instance = represented.send(name)
+ next if instance.nil?
+
+ ::API::V3::Principals::PrincipalRepresenterFactory
+ .create(represented.send(name), current_user: current_user)
+ }
+ end
+
+ def self.v3_path(instance)
+ case instance
+ when User
+ :user
+ when Group
+ :group
+ when PlaceholderUser
+ :placeholder_user
+ when NilClass
+ # Fall back to user for unknown principal
+ # since we do not have a principal route.
+ :user
+ else
+ raise "undefined subclass for #{instance}"
+ end
+ end
end
end
end
diff --git a/lib/api/v3/utilities/custom_field_injector/link_value_getter.rb b/lib/api/v3/utilities/custom_field_injector/link_value_getter.rb
index 5ebd4c1400f..94233604d12 100644
--- a/lib/api/v3/utilities/custom_field_injector/link_value_getter.rb
+++ b/lib/api/v3/utilities/custom_field_injector/link_value_getter.rb
@@ -93,7 +93,7 @@ module API
end
def derive_principal_path_method(custom_value)
- custom_value.typed_value.model_name.singular
+ API::V3::Principals::PrincipalRepresenterFactory.v3_path(custom_value.typed_value)
end
def link_value_title(custom_value)
diff --git a/lib/api/v3/work_packages/principal_setter.rb b/lib/api/v3/work_packages/principal_setter.rb
index 9f12aa88b8e..5856eb1afe6 100644
--- a/lib/api/v3/work_packages/principal_setter.rb
+++ b/lib/api/v3/work_packages/principal_setter.rb
@@ -34,10 +34,10 @@ module API
class PrincipalSetter
def self.lambda(name, property_name = name)
->(args) {
- lambda = ::API::V3::Principals::AssociatedSubclassLambda
- .setter(name,
- property_name: property_name,
- namespaces: %i(groups users placeholder_users))
+ lambda = ::API::V3::Principals::PrincipalRepresenterFactory
+ .create_setter_lambda(name,
+ property_name: property_name,
+ namespaces: %i(groups users placeholder_users))
instance_exec(**args, &lambda)
}
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index c825b2336a5..b396864eea0 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -432,14 +432,18 @@ module API
representer: ::API::V3::Users::UserRepresenter
associated_resource :responsible,
- getter: ::API::V3::Principals::AssociatedSubclassLambda.getter(:responsible),
+ getter: ::API::V3::Principals::PrincipalRepresenterFactory
+ .create_getter_lambda(:responsible),
setter: PrincipalSetter.lambda(:responsible),
- link: ::API::V3::Principals::AssociatedSubclassLambda.link(:responsible)
+ link: ::API::V3::Principals::PrincipalRepresenterFactory
+ .create_link_lambda(:responsible)
associated_resource :assignee,
- getter: ::API::V3::Principals::AssociatedSubclassLambda.getter(:assigned_to),
+ getter: ::API::V3::Principals::PrincipalRepresenterFactory
+ .create_getter_lambda(:assigned_to),
setter: PrincipalSetter.lambda(:assigned_to, :assignee),
- link: ::API::V3::Principals::AssociatedSubclassLambda.link(:assigned_to)
+ link: ::API::V3::Principals::PrincipalRepresenterFactory
+ .create_link_lambda(:assigned_to)
associated_resource :version,
v3_path: :version,
diff --git a/spec/lib/api/v3/principals/principal_representer_factory_spec.rb b/spec/lib/api/v3/principals/principal_representer_factory_spec.rb
index 3b81f667dec..a8f4e3de637 100644
--- a/spec/lib/api/v3/principals/principal_representer_factory_spec.rb
+++ b/spec/lib/api/v3/principals/principal_representer_factory_spec.rb
@@ -28,14 +28,27 @@
require 'spec_helper'
+class TestRepresenter < ::API::Decorators::Single
+ include ::API::Decorators::LinkedResource
+end
+
describe ::API::V3::Principals::PrincipalRepresenterFactory do
let(:current_user) { FactoryBot.build_stubbed :user }
+ let(:represented) do
+ OpenStruct.new(association_id: 5, association: principal)
+ end
+ let(:principal) { nil }
+ let(:user) { FactoryBot.build_stubbed :user }
+ let(:group) { FactoryBot.build_stubbed :group }
+ let(:placeholder) { FactoryBot.build_stubbed :placeholder_user }
+ let(:deleted) { FactoryBot.build_stubbed :deleted_user }
+
describe '.create' do
subject { described_class.create principal, current_user: current_user }
context 'with a user' do
- let(:principal) { FactoryBot.build_stubbed :user }
+ let(:principal) { user }
it 'returns a user representer' do
expect(subject).to be_a ::API::V3::Users::UserRepresenter
@@ -43,7 +56,7 @@ describe ::API::V3::Principals::PrincipalRepresenterFactory do
end
context 'with a group' do
- let(:principal) { FactoryBot.build_stubbed :group }
+ let(:principal) { group }
it 'returns a group representer' do
expect(subject).to be_a ::API::V3::Groups::GroupRepresenter
@@ -51,11 +64,174 @@ describe ::API::V3::Principals::PrincipalRepresenterFactory do
end
context 'with a placeholder user' do
- let(:principal) { FactoryBot.build_stubbed :placeholder_user }
+ let(:principal) { placeholder }
it 'returns a user representer' do
expect(subject).to be_a ::API::V3::PlaceholderUsers::PlaceholderUserRepresenter
end
end
+
+ context 'with a deleted user' do
+ let(:principal) { deleted }
+
+ it 'returns a user representer' do
+ expect(subject).to be_a ::API::V3::Users::UserRepresenter
+ end
+ end
+ end
+
+ describe '.create_link_lambda' do
+ subject(:link) do
+ TestRepresenter
+ .new(represented, current_user: nil)
+ .instance_exec(&described_class.create_link_lambda('association'))
+ end
+
+ context 'with a user' do
+ let(:principal) { user }
+
+ it 'renders a link to the user' do
+ expect(link)
+ .to eql({ "href" => "/api/v3/users/5", "title" => principal.name })
+ end
+ end
+
+ context 'with a group' do
+ let(:principal) { group }
+
+ it 'renders a link to the group' do
+ expect(link)
+ .to eql({ "href" => "/api/v3/groups/5", "title" => principal.name })
+ end
+ end
+
+ context 'with a placeholder user' do
+ let(:principal) { placeholder }
+
+ it 'renders a link to the placeholder' do
+ expect(link)
+ .to eql({ "href" => "/api/v3/placeholder_users/5", "title" => principal.name })
+ end
+ end
+
+ context 'with a deleted user' do
+ let(:principal) { deleted }
+
+ it 'renders a link to the user (which is deleted)' do
+ expect(link)
+ .to eql({ "href" => "/api/v3/users/5", "title" => principal.name })
+ end
+ end
+ end
+
+ describe '.create_getter_lambda' do
+ subject(:getter) do
+ TestRepresenter
+ .new(represented, current_user: nil, embed_links: embed_links)
+ .instance_exec(&described_class.create_getter_lambda('association'))
+ end
+
+ let(:embed_links) { true }
+
+ context 'with a user' do
+ let(:principal) { user }
+
+ it 'returns a user representer' do
+ expect(getter)
+ .to be_a ::API::V3::Users::UserRepresenter
+ end
+ end
+
+ context 'with a group' do
+ let(:principal) { group }
+
+ it 'renders a group representer' do
+ expect(getter)
+ .to be_a ::API::V3::Groups::GroupRepresenter
+ end
+ end
+
+ context 'with a placeholder user' do
+ let(:principal) { placeholder }
+
+ it 'renders a placeholder representer' do
+ expect(getter)
+ .to be_a ::API::V3::PlaceholderUsers::PlaceholderUserRepresenter
+ end
+ end
+
+ context 'with a deleted user' do
+ let(:principal) { deleted }
+
+ it 'renders a user representer' do
+ expect(getter)
+ .to be_a ::API::V3::Users::UserRepresenter
+ end
+ end
+
+ context 'with nil' do
+ let(:principal) { nil }
+
+ it 'return nil' do
+ expect(getter)
+ .to be_nil
+ end
+ end
+
+ context 'without embedding links' do
+ let(:principal) { user }
+ let(:embed_links) { false }
+
+ it 'returns a user representer' do
+ expect(getter)
+ .to be_nil
+ end
+ end
+ end
+
+ describe '.create_setter_lambda' do
+ subject(:setter) do
+ TestRepresenter
+ .new(represented, current_user: nil)
+ .instance_exec(fragment: { "href" => link }, &described_class.create_setter_lambda('association'))
+
+ represented.association_id
+ end
+
+ context 'with a user link' do
+ let(:link) { "/api/v3/users/90" }
+
+ it 'sets the association' do
+ expect(setter)
+ .to eql("90")
+ end
+ end
+
+ context 'with a group link' do
+ let(:link) { "/api/v3/groups/90" }
+
+ it 'sets the association' do
+ expect(setter)
+ .to eql("90")
+ end
+ end
+
+ context 'with a placeholder user link' do
+ let(:link) { "/api/v3/placeholder_users/90" }
+
+ it 'sets the association' do
+ expect(setter)
+ .to eql("90")
+ end
+ end
+
+ context 'with an invalid link' do
+ let(:link) { "/api/v3/schum/90" }
+
+ it 'raises an exception' do
+ expect { setter }
+ .to raise_error(API::Errors::InvalidResourceLink)
+ end
+ end
end
end
diff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
index dbcf3981378..22af529272d 100644
--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
@@ -456,6 +456,26 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
end
end
+ context 'is placeholder user' do
+ let(:assignee) { FactoryBot.build_stubbed(:placeholder_user) }
+
+ it_behaves_like 'has a titled link' do
+ let(:link) { 'assignee' }
+ let(:href) { "/api/v3/placeholder_users/#{work_package.assigned_to.id}" }
+ let(:title) { work_package.assigned_to.name }
+ end
+ end
+
+ context 'is deleted user' do
+ let(:assignee) { FactoryBot.build_stubbed(:deleted_user) }
+
+ it_behaves_like 'has a titled link' do
+ let(:link) { 'assignee' }
+ let(:href) { "/api/v3/users/#{work_package.assigned_to.id}" }
+ let(:title) { work_package.assigned_to.name }
+ end
+ end
+
context 'is not set' do
it_behaves_like 'has an empty link' do
let(:link) { 'assignee' }
@@ -484,6 +504,26 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
end
end
+ context 'is placeholder user' do
+ let(:responsible) { FactoryBot.build_stubbed(:placeholder_user) }
+
+ it_behaves_like 'has a titled link' do
+ let(:link) { 'responsible' }
+ let(:href) { "/api/v3/placeholder_users/#{work_package.responsible.id}" }
+ let(:title) { work_package.responsible.name }
+ end
+ end
+
+ context 'is deleted user' do
+ let(:responsible) { FactoryBot.build_stubbed(:deleted_user) }
+
+ it_behaves_like 'has a titled link' do
+ let(:link) { 'responsible' }
+ let(:href) { "/api/v3/users/#{work_package.responsible.id}" }
+ let(:title) { work_package.responsible.name }
+ end
+ end
+
context 'is not set' do
it_behaves_like 'has an empty link' do
let(:link) { 'responsible' }