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 projects create end point to api

ulferts821c624d4a00
diff --git a/app/contracts/projects/create_contract.rb b/app/contracts/projects/create_contract.rb
index dcec34ebc84..d671bdf0338 100644
--- a/app/contracts/projects/create_contract.rb
+++ b/app/contracts/projects/create_contract.rb
@@ -30,8 +30,8 @@ module Projects
class CreateContract < BaseContract
private
- def manage_permission
- :add_project
+ def validate_user_allowed_to_manage
+ errors.add :base, :error_unauthorized unless user.allowed_to_globally?(:add_project)
end
end
end
diff --git a/app/services/projects/create_service.rb b/app/services/projects/create_service.rb
new file mode 100644
index 00000000000..aa2ce74a14d
--- /dev/null
+++ b/app/services/projects/create_service.rb
@@ -0,0 +1,34 @@
+#-- encoding: UTF-8
+
+#-- 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 doc/COPYRIGHT.rdoc for more details.
+#++
+
+module Projects
+ class CreateService < ::BaseServices::Create
+ end
+end
diff --git a/docs/api/apiv3/endpoints/members.apib b/docs/api/apiv3/endpoints/members.apib
index e1b2d4fa99e..4fe4572cd48 100644
--- a/docs/api/apiv3/endpoints/members.apib
+++ b/docs/api/apiv3/endpoints/members.apib
@@ -337,18 +337,18 @@ You can use the form and schema to be retrieve the valid attribute values and by
+ Body
{
- project: {
- href: "/api/v3/projects/1"
+ "project": {
+ "href": "/api/v3/projects/1"
},
- principal: {
- href: "/api/v3/users/5"
+ "principal": {
+ "href": "/api/v3/users/5"
},
- roles: [
+ "roles": [
{
- href: "/api/v3/roles/5"
+ "href": "/api/v3/roles/5"
},
{
- href: "/api/v3/roles/8"
+ "href": "/api/v3/roles/8"
}
]
}
diff --git a/docs/api/apiv3/endpoints/projects.apib b/docs/api/apiv3/endpoints/projects.apib
index 67dbc2a9d4c..2917b04b1e2 100644
--- a/docs/api/apiv3/endpoints/projects.apib
+++ b/docs/api/apiv3/endpoints/projects.apib
@@ -233,6 +233,76 @@ Returns a collection of projects. The collection can be filtered via query param
[Projects][]
+## Create project [POST]
+
+Creates a new project, applying the attributes provided in the body.
+
+You can use the form and schema to be retrieve the valid attribute values and by that be guided towards successful creation.
+
++ Request Create project
+
+ + Body
+
+ {
+ "identifier": "new_project_identifier",
+ "name": "New project name",
+ "customField35": "Text custom field value"
+ "_links": {
+ "customField12": {
+ "href": "/api/v3/users/5"
+ }
+ }
+ }
+
++ Response 201
+
+ [Project][]
+
++ Response 400 (application/hal+json)
+
+ Occurs when the client did not send a valid JSON object in the request body.
+
+ + Body
+
+ {
+ "_type": "Error",
+ "errorIdentifier": "urn:openproject-org:api:v3:errors:InvalidRequestBody",
+ "message": "The request body was not a single JSON object."
+ }
+
++ Response 403 (application/hal+json)
+
+ Returned if the client does not have sufficient permissions.
+
+ **Required permission:** add project which is a global permission
+
+ + Body
+
+ {
+ "_type": "Error",
+ "errorIdentifier": "urn:openproject-org:api:v3:errors:MissingPermission",
+ "message": "You are not authorized to access this resource."
+ }
+
++ Response 422 (application/hal+json)
+
+ Returned if:
+
+ * a constraint for a property was violated (`PropertyConstraintViolation`)
+
+ + Body
+
+ {
+ "_type": "Error",
+ "errorIdentifier": "urn:openproject-org:api:v3:errors:PropertyConstraintViolation",
+ "message": "Name can't be blank.",
+ "_embedded": {
+ "details": {
+ "attribute": "name"
+ }
+ }
+ }
+
## Projects schema [/api/v3/projects/schemas]
+ Model
diff --git a/lib/api/v3/projects/project_payload_representer.rb b/lib/api/v3/projects/project_payload_representer.rb
new file mode 100644
index 00000000000..0e626b8d686
--- /dev/null
+++ b/lib/api/v3/projects/project_payload_representer.rb
@@ -0,0 +1,39 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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.
+#++
+
+module API
+ module V3
+ module Projects
+ class ProjectPayloadRepresenter < ProjectRepresenter
+ include ::API::Utilities::PayloadRepresenter
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/projects/projects_api.rb b/lib/api/v3/projects/projects_api.rb
index 73882aa975e..bc14a9fe822 100644
--- a/lib/api/v3/projects/projects_api.rb
+++ b/lib/api/v3/projects/projects_api.rb
@@ -37,6 +37,9 @@ module API
scope: -> { Project.visible(User.current).includes(:enabled_modules) })
.mount
+ post &::API::V3::Utilities::Endpoints::Create.new(model: Project)
+ .mount
+
mount ::API::V3::Projects::Schemas::ProjectSchemaAPI
params do
diff --git a/spec/contracts/projects/create_contract_spec.rb b/spec/contracts/projects/create_contract_spec.rb
index 172e444f50f..4d2cc242bc9 100644
--- a/spec/contracts/projects/create_contract_spec.rb
+++ b/spec/contracts/projects/create_contract_spec.rb
@@ -40,6 +40,12 @@ describe Projects::CreateContract do
parent: project_parent)
end
let(:permissions) { [:add_project] }
+ let!(:allowed_to) do
+ allow(current_user)
+ .to receive(:allowed_to_globally?) do |permission|
+ permissions.include?(permission)
+ end
+ end
subject(:contract) { described_class.new(project, current_user) }
end
diff --git a/spec/contracts/projects/shared_contract_examples.rb b/spec/contracts/projects/shared_contract_examples.rb
index 826f0da0ce9..419373e1abc 100644
--- a/spec/contracts/projects/shared_contract_examples.rb
+++ b/spec/contracts/projects/shared_contract_examples.rb
@@ -30,11 +30,12 @@ require 'spec_helper'
shared_examples_for 'project contract' do
let(:current_user) do
- FactoryBot.build_stubbed(:user) do |user|
- allow(user)
- .to receive(:allowed_to?) do |permission, permission_project|
- permissions.include?(permission) && project == permission_project
- end
+ FactoryBot.build_stubbed(:user)
+ end
+ let!(:allowed_to) do
+ allow(current_user)
+ .to receive(:allowed_to?) do |permission, permission_project|
+ permissions.include?(permission) && project == permission_project
end
end
let(:project_name) { 'Project name' }
diff --git a/spec/requests/api/v3/project_resource_spec.rb b/spec/requests/api/v3/project_resource_spec.rb
index 41df8d2402c..476d3c806cb 100644
--- a/spec/requests/api/v3/project_resource_spec.rb
+++ b/spec/requests/api/v3/project_resource_spec.rb
@@ -29,7 +29,7 @@
require 'spec_helper'
require 'rack/test'
-describe 'API v3 Project resource' do
+describe 'API v3 Project resource', type: :request, content_type: :json do
include Rack::Test::Methods
include API::V3::Utilities::PathHelper
@@ -208,4 +208,105 @@ describe 'API v3 Project resource' do
end
end
end
+
+ describe '#post /projects' do
+ let(:current_user) do
+ FactoryBot.create(:user).tap do |u|
+ u.global_roles << global_role
+ end
+ end
+ let(:global_role) do
+ FactoryBot.create(:global_role, permissions: permissions)
+ end
+ let(:permissions) { [:add_project] }
+ let(:path) { api_v3_paths.projects }
+ let(:body) do
+ {
+ identifier: 'new_project_identifier',
+ name: 'Project name'
+ }.to_json
+ end
+
+ before do
+ login_as current_user
+
+ post path, body
+ end
+
+ it 'responds with 201 CREATED' do
+ expect(last_response.status).to eq(201)
+ end
+
+ it 'creates a project' do
+ expect(Project.count)
+ .to eql(1)
+ end
+
+ it 'returns the created project' do
+ expect(last_response.body)
+ .to be_json_eql('Project'.to_json)
+ .at_path('_type')
+ expect(last_response.body)
+ .to be_json_eql('Project name'.to_json)
+ .at_path('name')
+ end
+
+ context 'with a custom field' do
+ let(:body) do
+ {
+ identifier: 'new_project_identifier',
+ name: 'Project name',
+ "customField#{custom_field.id}": {
+ "raw": "CF text"
+ }
+ }.to_json
+ end
+
+ it 'sets the cf value' do
+ expect(last_response.body)
+ .to be_json_eql("CF text".to_json)
+ .at_path("customField#{custom_field.id}/raw")
+ end
+ end
+
+ context 'without permission to create projects' do
+ let(:permissions) { [] }
+
+ it 'responds with 403' do
+ expect(last_response.status).to eq(403)
+ end
+
+ it 'creates no project' do
+ expect(Project.count)
+ .to eql(0)
+ end
+ end
+
+ context 'with faulty params' do
+ let(:body) do
+ {
+ identifier: 'some_identifier'
+ }.to_json
+ end
+
+ it 'responds with 422' do
+ expect(last_response.status).to eq(422)
+ end
+
+ it 'creates no project' do
+ expect(Project.count)
+ .to eql(0)
+ end
+
+ it 'denotes the error' do
+ expect(last_response.body)
+ .to be_json_eql('Error'.to_json)
+ .at_path('_type')
+
+ expect(last_response.body)
+ .to be_json_eql("Name can't be blank.".to_json)
+ .at_path('message')
+ end
+ end
+ end
end
diff --git a/spec/services/projects/create_service_spec.rb b/spec/services/projects/create_service_spec.rb
new file mode 100644
index 00000000000..a85370c557f
--- /dev/null
+++ b/spec/services/projects/create_service_spec.rb
@@ -0,0 +1,148 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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.
+#++
+
+require 'spec_helper'
+
+describe Projects::CreateService, type: :model do
+ let(:user) { FactoryBot.build_stubbed(:user) }
+ let(:contract_class) do
+ double('contract_class', '<=': true)
+ end
+ let(:project_valid) { true }
+ let(:instance) do
+ described_class.new(user: user,
+ contract_class: contract_class)
+ end
+ let(:call_attributes) { { name: 'Some name', identifier: 'Some identifier' } }
+ let(:set_attributes_success) do
+ true
+ end
+ let(:set_attributes_errors) do
+ double('set_attributes_errors')
+ end
+ let(:set_attributes_result) do
+ ServiceResult.new result: created_project,
+ success: set_attributes_success,
+ errors: set_attributes_errors
+ end
+ let!(:created_project) do
+ project = FactoryBot.build_stubbed(:project)
+
+ allow(Project)
+ .to receive(:new)
+ .and_return(project)
+
+ allow(project)
+ .to receive(:save)
+ .and_return(project_valid)
+
+ project
+ end
+ let!(:set_attributes_service) do
+ service = double('set_attributes_service_instance')
+
+ allow(Projects::SetAttributesService)
+ .to receive(:new)
+ .with(user: user,
+ model: created_project,
+ contract_class: contract_class)
+ .and_return(service)
+
+ allow(service)
+ .to receive(:call)
+ .and_return(set_attributes_result)
+ end
+
+ describe 'call' do
+ subject { instance.call(call_attributes) }
+
+ it 'is successful' do
+ expect(subject.success?).to be_truthy
+ end
+
+ it 'returns the result of the SetAttributesService' do
+ expect(subject)
+ .to eql set_attributes_result
+ end
+
+ it 'persists the project' do
+ expect(created_project)
+ .to receive(:save)
+ .and_return(project_valid)
+
+ subject
+ end
+
+ it 'creates a project' do
+ expect(subject.result)
+ .to eql created_project
+ end
+
+ context 'when the SetAttributeService is unsuccessful' do
+ let(:set_attributes_success) { false }
+
+ it 'is unsuccessful' do
+ expect(subject.success?).to be_falsey
+ end
+
+ it 'returns the result of the SetAttributesService' do
+ expect(subject)
+ .to eql set_attributes_result
+ end
+
+ it 'does not persist the changes' do
+ expect(created_project)
+ .to_not receive(:save)
+
+ subject
+ end
+
+ it "exposes the contract's errors" do
+ subject
+
+ expect(subject.errors).to eql set_attributes_errors
+ end
+ end
+
+ context 'when the project is invalid' do
+ let(:project_valid) { false }
+
+ it 'is unsuccessful' do
+ expect(subject.success?).to be_falsey
+ end
+
+ it "exposes the project's errors" do
+ subject
+
+ expect(subject.errors).to eql created_project.errors
+ end
+ end
+ end
+end