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

visibility check for relations api

ulferts1b7d66bea7fe
diff --git a/lib/api/v3/relations/relations_api.rb b/lib/api/v3/relations/relations_api.rb
index cf40459e507..ee1ac75a3b2 100644
--- a/lib/api/v3/relations/relations_api.rb
+++ b/lib/api/v3/relations/relations_api.rb
@@ -50,19 +50,17 @@ module API
end
route_param :id, type: Integer, desc: 'Relation ID' do
- get do
- representer.new(
- Relation.find_by_id!(params[:id]),
- current_user: current_user,
- embed_links: true
- )
+ after_validation do
+ @relation = Relation.visible.find(params[:id])
end
+ get &::API::V3::Utilities::Endpoints::Show.new(model: Relation).mount
+
patch do
rep = parse_representer.new Relation.new, current_user: current_user
relation = rep.from_json request.body.read
attributes = filter_attributes relation
- service = ::Relations::UpdateService.new relation: Relation.find_by_id!(params[:id]),
+ service = ::Relations::UpdateService.new relation: @relation,
user: current_user
call = service.call attributes: attributes,
send_notifications: (params[:notify] != 'false')
diff --git a/spec/requests/api/v3/relations/relations_api_spec.rb b/spec/requests/api/v3/relations/relations_api_spec.rb
index b572c5575b4..e134e61abec 100644
--- a/spec/requests/api/v3/relations/relations_api_spec.rb
+++ b/spec/requests/api/v3/relations/relations_api_spec.rb
@@ -32,6 +32,7 @@ describe 'API v3 Relation resource', type: :request, content_type: :json do
include API::V3::Utilities::PathHelper
let(:user) { FactoryBot.create :admin }
+ let(:current_user) { user }
let!(:from) { FactoryBot.create :work_package }
let!(:to) { FactoryBot.create :work_package }
@@ -65,7 +66,7 @@ describe 'API v3 Relation resource', type: :request, content_type: :json do
end
before do
- login_as user
+ login_as current_user
end
describe "creating a relation" do
@@ -446,4 +447,58 @@ describe 'API v3 Relation resource', type: :request, content_type: :json do
.at_path('_embedded/elements/0/id')
end
end
+
+ describe 'GET /api/v3/relations/:id' do
+ let(:path) do
+ api_v3_paths.relation(relation.id)
+ end
+
+ let(:role) { FactoryBot.create(:role, permissions: [:view_work_packages]) }
+
+ let(:current_user) do
+ FactoryBot.create(:user).tap do |user|
+ FactoryBot.create(:member,
+ project: to.project,
+ user: user,
+ roles: [role])
+ FactoryBot.create(:member,
+ project: from.project,
+ user: user,
+ roles: [role])
+ end
+ end
+
+ before do
+ get path
+ end
+
+ context 'for a relation with visible work packages' do
+ it 'returns 200' do
+ expect(last_response.status).to eql 200
+ end
+
+ it 'returns the relation' do
+ expect(last_response.body)
+ .to be_json_eql API::V3::Relations::RelationRepresenter.new(relation, current_user: current_user, embed_links: true).to_json
+ end
+ end
+
+ context 'for a relation with an invisible work package' do
+ let(:invisible_relation) do
+ invisible_wp = FactoryBot.create(:work_package)
+
+ FactoryBot.create :relation,
+ from: from,
+ to: invisible_wp
+ end
+
+ let(:path) do
+ api_v3_paths.relation(invisible_relation.id)
+ end
+
+ it 'returns 404 NOT FOUND' do
+ expect(last_response.status).to eql 404
+ end
+ end
+ end
end
diff --git a/spec/requests/api/v3/relations_resource_spec.rb b/spec/requests/api/v3/relations_resource_spec.rb
index 2582d51a4a2..57c6416b4db 100644
--- a/spec/requests/api/v3/relations_resource_spec.rb
+++ b/spec/requests/api/v3/relations_resource_spec.rb
@@ -36,21 +36,21 @@ describe 'API v3 Relation resource', type: :request do
let(:project) { FactoryBot.create(:project_with_types) }
let(:current_user) do
FactoryBot.create(:user,
- member_in_project: project,
- member_through_role: role)
+ member_in_project: project,
+ member_through_role: role)
end
let(:permissions) { [] }
let(:role) { FactoryBot.create(:role, permissions: permissions) }
let(:work_package) do
FactoryBot.create(:work_package,
- project: project,
- type: project.types.first)
+ project: project,
+ type: project.types.first)
end
let(:visible_work_package) do
FactoryBot.create(:work_package,
- project: project,
- type: project.types.first)
+ project: project,
+ type: project.types.first)
end
let(:invisible_work_package) do
# will be inside another project
@@ -58,13 +58,13 @@ describe 'API v3 Relation resource', type: :request do
end
let(:visible_relation) do
FactoryBot.create(:relation,
- from: work_package,
- to: visible_work_package)
+ from: work_package,
+ to: visible_work_package)
end
let(:invisible_relation) do
FactoryBot.create(:relation,
- from: work_package,
- to: invisible_work_package)
+ from: work_package,
+ to: invisible_work_package)
end
before do