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
{
href: api_v3_paths.version(represented.id),
title: "#{represented.name}"
title: represented.name
}
end
link :definingProject do
{
href: api_v3_paths.project(represented.project.id),
title: represented.project.name
} if represented.project.visible?(current_user)
end
linked_property :definingProject,
path: :project,
backing_field: :project,
visible_condition: -> (*) { represented.project.visible?(current_user) }
link :availableInProjects do
{

@ -48,3 +48,29 @@ shared_examples_for 'action link' do
it { expect(subject).to have_json_path("_links/#{action}/href") }
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') }
context 'links' do
describe 'links' do
it { should have_json_type(Object).at_path('_links') }
it 'to self' do
path = api_v3_paths.version(version.id)
expect(subject).to be_json_eql(path.to_json).at_path('_links/self/href')
describe 'to self' do
it_behaves_like 'has a titled link' do
let(:link) { 'self' }
let(:href) { api_v3_paths.version(version.id) }
let(:title) { version.name }
end
end
context 'to the defining project' do
let(:path) { api_v3_paths.project(version.project.id) }
it 'exists if the user has the permission to see the project' do
allow(version.project).to receive(:visible?).with(user).and_return(true)
subject = representer.to_json
expect(subject).to be_json_eql(path.to_json).at_path('_links/definingProject/href')
describe 'to the defining project' do
context 'if the user has the permission to see the project' do
before do
allow(version.project).to receive(:visible?).with(user).and_return(true)
end
it_behaves_like 'has a titled link' do
let(:link) { 'definingProject' }
let(:href) { api_v3_paths.project(version.project.id) }
let(:title) { version.project.name }
end
end
it 'does not exist if the user lacks the permission to see the project' do
allow(version.project).to receive(:visible?).with(user).and_return(false)
subject = representer.to_json
context 'if the user lacks the permission to see the project' do
before do
allow(version.project).to receive(:visible?).with(user).and_return(false)
end
expect(subject).to_not have_json_path('_links/definingProject/href')
it_behaves_like 'has no link' do
let(:link) { 'definingProject' }
end
end
end
it 'to available projects' do
path = api_v3_paths.versions_projects(version.id)
expect(subject).to be_json_eql(path.to_json).at_path('_links/availableInProjects/href')
describe 'to available projects' do
it_behaves_like 'has an untitled link' do
let(:link) { 'availableInProjects' }
let(:href) { api_v3_paths.versions_projects(version.id) }
end
end
end

@ -233,19 +233,6 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
end
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 'should link to self' do

Loading…
Cancel
Save