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

add put end point for projects

ulferts9a11e768cc73
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rb
index 26e3a9e16ba..a2b5018c513 100644
--- a/app/contracts/projects/base_contract.rb
+++ b/app/contracts/projects/base_contract.rb
@@ -92,7 +92,9 @@ module Projects
end
def validate_user_allowed_to_manage
- errors.add :base, :error_unauthorized unless user.allowed_to?(manage_permission, model)
+ with_unchanged_id do
+ errors.add :base, :error_unauthorized unless user.allowed_to?(manage_permission, model)
+ end
end
def validate_status_code_included
@@ -102,5 +104,14 @@ module Projects
def manage_permission
raise NotImplementedError
end
+
+ def with_unchanged_id
+ project_id = model.id
+ model.id = model.id_was
+
+ yield
+ ensure
+ model.id = project_id
+ end
end
end
diff --git a/app/services/api/parse_resource_params_service.rb b/app/services/api/parse_resource_params_service.rb
new file mode 100644
index 00000000000..12086156ead
--- /dev/null
+++ b/app/services/api/parse_resource_params_service.rb
@@ -0,0 +1,80 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 doc/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ class ParseResourceParamsService
+ attr_accessor :model,
+ :representer,
+ :current_user
+
+ def initialize(user, model: nil, representer: nil)
+ self.current_user = user
+ self.model = model
+
+ self.representer = if !representer && model
+ deduce_representer(model)
+ elsif representer
+ representer
+ else
+ raise 'Representer not defined'
+ end
+ end
+
+ def call(request_body)
+ parsed = if request_body
+ parse_attributes(request_body)
+ else
+ {}
+ end
+
+ ServiceResult.new(success: true,
+ result: parsed)
+ end
+
+ private
+
+ def deduce_representer(model)
+ raise NotImplementedError
+ end
+
+ def parsing_representer
+ representer
+ .new(struct, current_user: current_user)
+ end
+
+ def parse_attributes(request_body)
+ parsing_representer
+ .from_hash(request_body)
+ .to_h
+ end
+
+ def struct
+ OpenStruct.new
+ end
+ end
+end
diff --git a/app/services/api/v3/parse_resource_params_service.rb b/app/services/api/v3/parse_resource_params_service.rb
index 41b2de1a9b5..f9ea957d17d 100644
--- a/app/services/api/v3/parse_resource_params_service.rb
+++ b/app/services/api/v3/parse_resource_params_service.rb
@@ -28,42 +28,20 @@
module API
module V3
- class ParseResourceParamsService
- attr_accessor :model,
- :representer,
- :current_user
-
- def initialize(user, model: nil, representer: nil)
- self.current_user = user
- self.model = model
+ class ParseResourceParamsService < ::API::ParseResourceParamsService
+ private
- self.representer = if !representer && model
- "API::V3::#{model.to_s.pluralize}::#{model}Representer".constantize
- elsif representer
- representer
- else
- raise 'Representer not defined'
- end
+ def deduce_representer(model)
+ "API::V3::#{model.to_s.pluralize}::#{model}Representer".constantize
end
- def call(request_body)
- parsed = if request_body
- parse_attributes(request_body)
- else
- {}
- end
-
- ServiceResult.new(success: true,
- result: parsed)
+ def parsing_representer
+ representer
+ .create(struct, current_user: current_user)
end
- private
-
def parse_attributes(request_body)
- representer
- .create(struct, current_user: current_user)
- .from_hash(request_body)
- .to_h
+ super
.except(:available_custom_fields)
end
@@ -71,7 +49,7 @@ module API
if model&.respond_to?(:available_custom_fields)
OpenStruct.new available_custom_fields: model.available_custom_fields(model.new)
else
- OpenStruct.new
+ super
end
end
end
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
index 57da1c3459e..0a489a0d785 100644
--- a/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
@@ -57,6 +57,23 @@ module Bcf::API::V2_1
Bcf::API::V2_1::Projects::SingleRepresenter
.new(@project)
end
+
+ put do
+ parse_call = Bcf::API::V2_1::ParseResourceParamsService
+ .new(current_user, model: Project)
+ .call(request_body)
+
+ update_call = ::Projects::UpdateService
+ .new(user: current_user, model: @project)
+ .call(parse_call.result)
+
+ if update_call.success?
+ Bcf::API::V2_1::Projects::SingleRepresenter
+ .new(update_call.result)
+ else
+ fail ::API::Errors::ErrorBase.create_and_merge_errors(update_call.errors)
+ end
+ end
end
end
end
diff --git a/modules/bcf/app/services/bcf/api/v2_1/parse_resource_params_service.rb b/modules/bcf/app/services/bcf/api/v2_1/parse_resource_params_service.rb
new file mode 100644
index 00000000000..07d6d4ccae4
--- /dev/null
+++ b/modules/bcf/app/services/bcf/api/v2_1/parse_resource_params_service.rb
@@ -0,0 +1,46 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 doc/COPYRIGHT.rdoc for more details.
+#++
+
+module Bcf
+ module API
+ module V2_1
+ class ParseResourceParamsService < ::API::ParseResourceParamsService
+ private
+
+ def deduce_representer(model)
+ "Bcf::API::V2_1::#{model.to_s.pluralize}::SingleRepresenter".constantize
+ end
+
+ def parsing_representer
+ representer
+ .new(struct)
+ end
+ end
+ end
+ end
+end
diff --git a/modules/bcf/spec/requests/api/bcf/v2_1/projects/projects_api_spec.rb b/modules/bcf/spec/requests/api/bcf/v2_1/projects/projects_api_spec.rb
index b638d086c3a..5f48ef01104 100644
--- a/modules/bcf/spec/requests/api/bcf/v2_1/projects/projects_api_spec.rb
+++ b/modules/bcf/spec/requests/api/bcf/v2_1/projects/projects_api_spec.rb
@@ -29,13 +29,20 @@
require 'spec_helper'
require 'rack/test'
-describe 'BCF 2.1 projects resource', type: :request do
+require_relative '../shared_responses'
+
+describe 'BCF 2.1 projects resource', type: :request, content_type: :json do
include Rack::Test::Methods
- let(:member_user) do
+ let(:view_only_user) do
FactoryBot.create(:user,
member_in_project: project)
end
+ let(:edit_user) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: [:edit_project])
+ end
let(:non_member_user) do
FactoryBot.create(:user)
end
@@ -43,79 +50,90 @@ describe 'BCF 2.1 projects resource', type: :request do
let(:project) { FactoryBot.create(:project, enabled_module_names: [:bcf]) }
subject(:response) { last_response }
- shared_examples_for 'successful get request' do
- it 'responds 200 OK' do
- expect(subject.status)
- .to eql 200
- end
-
- it 'returns the project' do
- expect(subject.body)
- .to be_json_eql(expected_body.to_json)
- end
+ describe 'GET /api/bcf/2.1/projects/:project_id' do
+ let(:path) { "/api/bcf/2.1/projects/#{project.id}" }
+ let(:current_user) { view_only_user }
- it 'is has a json content type header' do
- expect(subject.headers['Content-Type'])
- .to eql 'application/json; charset=utf-8'
+ before do
+ login_as(current_user)
+ get path
end
- end
- shared_examples_for 'not found' do
- it 'responds 404 NOT FOUND' do
- expect(subject.status)
- .to eql 404
+ it_behaves_like 'bcf api successful response' do
+ let(:expected_body) do
+ {
+ project_id: project.id,
+ name: project.name
+ }
+ end
end
- it 'states a NOT FOUND message' do
- expected = {
- message: 'The requested resource could not be found.'
- }
-
- expect(subject.body)
- .to be_json_eql(expected.to_json)
- end
+ context 'lacking permissions' do
+ let(:current_user) { non_member_user }
- it 'is has a json content type header' do
- expect(subject.headers['Content-Type'])
- .to eql 'application/json; charset=utf-8'
+ it_behaves_like 'bcf api not found response'
end
end
- describe 'GET /api/bcf/2.1/projects/:project_id' do
+ describe 'PUT /api/bcf/2.1/projects/:project_id' do
let(:path) { "/api/bcf/2.1/projects/#{project.id}" }
- let(:current_user) { member_user }
+ let(:current_user) { edit_user }
+
+ let(:params) do
+ {
+ name: 'new project name'
+ }
+ end
before do
login_as(current_user)
- get path
+ put path, params.to_json
end
- it_behaves_like 'successful get request' do
+ it_behaves_like 'bcf api successful response' do
let(:expected_body) do
{
project_id: project.id,
- name: project.name
+ name: 'new project name'
}
end
end
- context 'lacking permissions' do
+ context 'lacking view permissions' do
let(:current_user) { non_member_user }
- it_behaves_like 'not found'
+ it_behaves_like 'bcf api not found response'
+ end
+
+ context 'lacking edit permissions' do
+ let(:current_user) { view_only_user }
+
+ it_behaves_like 'bcf api not allowed response'
+ end
+
+ context 'attempting to alter the id' do
+ let(:params) do
+ {
+ project_id: 0
+ }
+ end
+
+ it_behaves_like 'bcf api unprocessable response' do
+ let(:message) { 'You must not write a read-only attribute.' }
+ end
end
end
describe 'GET /api/bcf/2.1/projects' do
let(:path) { "/api/bcf/2.1/projects" }
- let(:current_user) { member_user }
+ let(:current_user) { view_only_user }
let!(:invisible_project) { FactoryBot.create(:project, enabled_module_names: [:bcf]) }
let!(:non_bcf_project) do
FactoryBot.create(:project, enabled_module_names: [:work_packages]).tap do |p|
FactoryBot.create(:member,
project: p,
- user: member_user,
- roles: member_user.members.first.roles)
+ user: view_only_user,
+ roles: view_only_user.members.first.roles)
end
end
@@ -124,7 +142,7 @@ describe 'BCF 2.1 projects resource', type: :request do
get path
end
- it_behaves_like 'successful get request' do
+ it_behaves_like 'bcf api successful response' do
let(:expected_body) do
[
{
diff --git a/modules/bcf/spec/requests/api/bcf/v2_1/shared_responses.rb b/modules/bcf/spec/requests/api/bcf/v2_1/shared_responses.rb
new file mode 100644
index 00000000000..bd671ab8234
--- /dev/null
+++ b/modules/bcf/spec/requests/api/bcf/v2_1/shared_responses.rb
@@ -0,0 +1,108 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2019 the OpenProject Foundation (OPF)
+#
+# 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-2017 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.
+#++
+
+shared_examples_for 'bcf api successful response' do
+ it 'responds 200 OK' do
+ expect(subject.status)
+ .to eql 200
+ end
+
+ it 'returns the project' do
+ expect(subject.body)
+ .to be_json_eql(expected_body.to_json)
+ end
+
+ it 'is has a json content type header' do
+ expect(subject.headers['Content-Type'])
+ .to eql 'application/json; charset=utf-8'
+ end
+end
+
+shared_examples_for 'bcf api not found response' do
+ it 'responds 404 NOT FOUND' do
+ expect(subject.status)
+ .to eql 404
+ end
+
+ it 'states a NOT FOUND message' do
+ expected = {
+ message: 'The requested resource could not be found.'
+ }
+
+ expect(subject.body)
+ .to be_json_eql(expected.to_json)
+ end
+
+ it 'is has a json content type header' do
+ expect(subject.headers['Content-Type'])
+ .to eql 'application/json; charset=utf-8'
+ end
+end
+
+shared_examples_for 'bcf api not allowed response' do
+ it 'responds 403 NOT ALLOWED' do
+ expect(subject.status)
+ .to eql 403
+ end
+
+ it 'states a NOT ALLOWED message' do
+ expected = {
+ message: 'You are not authorized to access this resource.'
+ }
+
+ expect(subject.body)
+ .to be_json_eql(expected.to_json)
+ end
+
+ it 'is has a json content type header' do
+ expect(subject.headers['Content-Type'])
+ .to eql 'application/json; charset=utf-8'
+ end
+end
+
+shared_examples_for 'bcf api unprocessable response' do
+ it 'responds 403 NOT ALLOWED' do
+ expect(subject.status)
+ .to eql 422
+ end
+
+ it 'states a reason message' do
+ expected = {
+ message: message
+ }
+
+ expect(subject.body)
+ .to be_json_eql(expected.to_json)
+ end
+
+ it 'is has a json content type header' do
+ expect(subject.headers['Content-Type'])
+ .to eql 'application/json; charset=utf-8'
+ end
+end
+