Fix/add missing types link to project representer (#6128)

* add missing types link to project representer

* fix rubocop violations


[ci skip]
pull/6132/head
ulferts 7 years ago committed by Oliver Günther
parent 99ff408c02
commit 58991c2b40
  1. 21
      lib/api/v3/projects/project_representer.rb
  2. 46
      spec/lib/api/v3/projects/project_representer_spec.rb

@ -37,17 +37,21 @@ module API
self_link
link :createWorkPackage do
next unless current_user_allowed_to(:add_work_packages, context: represented)
{
href: api_v3_paths.create_project_work_package_form(represented.id),
method: :post
} if current_user_allowed_to(:add_work_packages, context: represented)
}
end
link :createWorkPackageImmediate do
next unless current_user_allowed_to(:add_work_packages, context: represented)
{
href: api_v3_paths.work_packages_by_project(represented.id),
method: :post
} if current_user_allowed_to(:add_work_packages, context: represented)
}
end
link 'categories' do
@ -58,6 +62,13 @@ module API
{ href: api_v3_paths.versions_by_project(represented.id) }
end
link 'types' do
next unless current_user_allowed_to(:view_work_packages, context: represented) ||
current_user_allowed_to(:manage_types, context: represented)
{ href: api_v3_paths.types_by_project(represented.id) }
end
property :id, render_nil: true
property :identifier, render_nil: true
@ -67,13 +78,13 @@ module API
property :created_on,
as: 'createdAt',
exec_context: :decorator,
getter: -> (*) { datetime_formatter.format_datetime(represented.created_on) }
getter: ->(*) { datetime_formatter.format_datetime(represented.created_on) }
property :updated_on,
as: 'updatedAt',
exec_context: :decorator,
getter: -> (*) { datetime_formatter.format_datetime(represented.updated_on) }
getter: ->(*) { datetime_formatter.format_datetime(represented.updated_on) }
property :type, getter: -> (*) { project_type.try(:name) }, render_nil: true
property :type, getter: ->(*) { project_type.try(:name) }, render_nil: true
def _type
'Project'

@ -31,14 +31,26 @@ require 'spec_helper'
describe ::API::V3::Projects::ProjectRepresenter do
include ::API::V3::Utilities::PathHelper
let(:project) { FactoryGirl.create(:project) }
let(:project) { FactoryGirl.build_stubbed(:project) }
let(:representer) { described_class.new(project, current_user: user) }
let(:user) do
FactoryGirl.build(:user, member_in_project: project, member_through_role: role)
FactoryGirl.build_stubbed(:user)
end
let(:role) { FactoryGirl.create(:role, permissions: permissions) }
let(:permissions) { [:add_work_packages] }
before do
allow(user)
.to receive(:allowed_to?)
.and_return(false)
permissions.each do |permission|
allow(user)
.to receive(:allowed_to?)
.with(permission, project)
.and_return(true)
end
end
context 'generation' do
subject(:generated) { representer.to_json }
@ -108,6 +120,34 @@ describe ::API::V3::Projects::ProjectRepresenter do
.at_path('_links/versions/href')
end
end
describe 'types' do
context 'for a user having the view_work_packages permission' do
let(:permissions) { [:view_work_packages] }
it 'links to the types active in the project' do
is_expected.to be_json_eql(api_v3_paths.types_by_project(project.id).to_json)
.at_path('_links/types/href')
end
end
context 'for a user having the manage_types permission' do
let(:permissions) { [:manage_types] }
it 'links to the types active in the project' do
is_expected.to be_json_eql(api_v3_paths.types_by_project(project.id).to_json)
.at_path('_links/types/href')
end
end
context 'for a user not having the necessary permissions' do
let(:permission) { [] }
it 'has no types link' do
is_expected.to_not have_json_path('_links/types/href')
end
end
end
end
end

Loading…
Cancel
Save