also use new link helper in version representer

pull/2516/head
Jan Sandbrink 10 years ago
parent 0570457822
commit 51081271af
  1. 12
      lib/api/v3/versions/version_representer.rb
  2. 26
      spec/lib/api/v3/support/links.rb
  3. 53
      spec/lib/api/v3/versions/version_representer_spec.rb
  4. 13
      spec/lib/api/v3/work_packages/work_package_representer_spec.rb

@ -38,16 +38,14 @@ module API
link :self do link :self do
{ {
href: api_v3_paths.version(represented.id), href: api_v3_paths.version(represented.id),
title: "#{represented.name}" title: represented.name
} }
end end
link :definingProject do linked_property :definingProject,
{ path: :project,
href: api_v3_paths.project(represented.project.id), backing_field: :project,
title: represented.project.name visible_condition: -> (*) { represented.project.visible?(current_user) }
} if represented.project.visible?(current_user)
end
link :availableInProjects do link :availableInProjects do
{ {

@ -48,3 +48,29 @@ shared_examples_for 'action link' do
it { expect(subject).to have_json_path("_links/#{action}/href") } it { expect(subject).to have_json_path("_links/#{action}/href") }
end end
end end
shared_examples_for 'has a titled link' do
it { is_expected.to be_json_eql(href.to_json).at_path("_links/#{link}/href") }
it { is_expected.to be_json_eql(title.to_json).at_path("_links/#{link}/title") }
end
shared_examples_for 'has an untitled link' do
it { is_expected.to be_json_eql(href.to_json).at_path("_links/#{link}/href") }
it { is_expected.to_not have_json_path("_links/#{link}/title") }
end
shared_examples_for 'has an empty link' do
it { is_expected.to be_json_eql(nil.to_json).at_path("_links/#{link}/href") }
it 'has no embedded resource' do
is_expected.to_not have_json_path("_embedded/#{link}")
end
end
shared_examples_for 'has no link' do
it { is_expected.to_not have_json_path("_links/#{link}") }
it 'has no embedded resource' do
is_expected.to_not have_json_path("_embedded/#{link}")
end
end

@ -41,40 +41,47 @@ describe ::API::V3::Versions::VersionRepresenter do
it { should include_json('Version'.to_json).at_path('_type') } it { should include_json('Version'.to_json).at_path('_type') }
context 'links' do describe 'links' do
it { should have_json_type(Object).at_path('_links') } it { should have_json_type(Object).at_path('_links') }
it 'to self' do describe 'to self' do
path = api_v3_paths.version(version.id) it_behaves_like 'has a titled link' do
let(:link) { 'self' }
expect(subject).to be_json_eql(path.to_json).at_path('_links/self/href') let(:href) { api_v3_paths.version(version.id) }
let(:title) { version.name }
end
end end
context 'to the defining project' do describe 'to the defining project' do
let(:path) { api_v3_paths.project(version.project.id) } context 'if the user has the permission to see the project' do
before do
it 'exists if the user has the permission to see the project' do allow(version.project).to receive(:visible?).with(user).and_return(true)
allow(version.project).to receive(:visible?).with(user).and_return(true) end
subject = representer.to_json it_behaves_like 'has a titled link' do
let(:link) { 'definingProject' }
expect(subject).to be_json_eql(path.to_json).at_path('_links/definingProject/href') let(:href) { api_v3_paths.project(version.project.id) }
let(:title) { version.project.name }
end
end end
it 'does not exist if the user lacks the permission to see the project' do context 'if the user lacks the permission to see the project' do
allow(version.project).to receive(:visible?).with(user).and_return(false) before do
allow(version.project).to receive(:visible?).with(user).and_return(false)
subject = representer.to_json end
expect(subject).to_not have_json_path('_links/definingProject/href') it_behaves_like 'has no link' do
let(:link) { 'definingProject' }
end
end end
end end
it 'to available projects' do describe 'to available projects' do
path = api_v3_paths.versions_projects(version.id) it_behaves_like 'has an untitled link' do
let(:link) { 'availableInProjects' }
expect(subject).to be_json_eql(path.to_json).at_path('_links/availableInProjects/href') let(:href) { api_v3_paths.versions_projects(version.id) }
end
end end
end end

@ -233,19 +233,6 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
end end
describe '_links' do describe '_links' do
shared_examples_for 'has a titled link' do
it { is_expected.to be_json_eql(href.to_json).at_path("_links/#{link}/href") }
it { is_expected.to be_json_eql(title.to_json).at_path("_links/#{link}/title") }
end
shared_examples_for 'has an empty link' do
it { is_expected.to be_json_eql(nil.to_json).at_path("_links/#{link}/href") }
it 'has no embedded resource' do
is_expected.to_not have_json_path("_embedded/#{link}")
end
end
it { is_expected.to have_json_type(Object).at_path('_links') } it { is_expected.to have_json_type(Object).at_path('_links') }
it 'should link to self' do it 'should link to self' do

Loading…
Cancel
Save