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

ulferts0f0d79381ffa
diff --git a/docs/api/apiv3/endpoints/projects.apib b/docs/api/apiv3/endpoints/projects.apib
index e9fc2c924f8..b89ee4cb674 100644
--- a/docs/api/apiv3/endpoints/projects.apib
+++ b/docs/api/apiv3/endpoints/projects.apib
@@ -117,6 +117,93 @@ Depending on custom fields defined for projects, additional properties might exi
"message": "The specified project does not exist."
}
+## Update Project [PATCH]
+
+Updates the given project by applying the attributes provided in the body.
+
++ Parameters
+ + id (required, integer, `1`) ... Project id
+
++ Request Update project
+
+ + Body
+
+ {
+ "name": "A new project name",
+ "_links": {
+ "customField35": {
+ "href": "/api/v3/users/3"
+ }
+ }
+ }
+
++ Response 200
+
+ [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:** edit project for the project to be altered
+
+ + Body
+
+ {
+ "_type": "Error",
+ "errorIdentifier": "urn:openproject-org:api:v3:errors:MissingPermission",
+ "message": "You are not authorized to access this resource."
+ }
+
++ Response 404 (application/hal+json)
+
+ Returned if the project does not exist or the client does not have sufficient permissions
+ to see it.
+
+ **Required permission:** view project
+
+ *Note: A client without sufficient permissions shall not be able to test for the existence of
+ a version. That's why a 404 is returned here, even if a 403 might be more appropriate.*
+
+ + Body
+
+ {
+ "_type": "Error",
+ "errorIdentifier": "urn:openproject-org:api:v3:errors:NotFound",
+ "message": "The requested resource could not be found."
+ }
+
++ 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 [/api/v3/projects{?filters}]
+ Model
diff --git a/lib/api/v3/projects/projects_api.rb b/lib/api/v3/projects/projects_api.rb
index b22a5b5ab0b..9bf992f706f 100644
--- a/lib/api/v3/projects/projects_api.rb
+++ b/lib/api/v3/projects/projects_api.rb
@@ -56,6 +56,7 @@ module API
end
get &::API::V3::Utilities::Endpoints::Show.new(model: Project).mount
+ patch &::API::V3::Utilities::Endpoints::Update.new(model: Project).mount
mount ::API::V3::Projects::UpdateFormAPI
diff --git a/spec/requests/api/v3/project_resource_spec.rb b/spec/requests/api/v3/project_resource_spec.rb
index 476d3c806cb..85120124aa5 100644
--- a/spec/requests/api/v3/project_resource_spec.rb
+++ b/spec/requests/api/v3/project_resource_spec.rb
@@ -309,4 +309,112 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
end
end
end
+
+ describe '#patch /projects/:id' do
+ let(:current_user) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: permissions)
+ end
+ let(:permissions) { [:edit_project] }
+ let(:path) { api_v3_paths.project(project.id) }
+ let(:body) do
+ {
+ identifier: 'new_project_identifier',
+ name: 'Project name'
+ }
+ end
+
+ before do
+ login_as current_user
+
+ patch path, body.to_json
+ end
+
+ it 'responds with 200 OK' do
+ expect(last_response.status).to eq(200)
+ end
+
+ it 'alters the project' do
+ project.reload
+
+ expect(project.name)
+ .to eql(body[:name])
+
+ expect(project.identifier)
+ .to eql(body[:identifier])
+ end
+
+ it 'returns the updated project' do
+ expect(last_response.body)
+ .to be_json_eql('Project'.to_json)
+ .at_path('_type')
+ expect(last_response.body)
+ .to be_json_eql(body[:name].to_json)
+ .at_path('name')
+ end
+
+ context 'with a custom field' do
+ let(:body) do
+ {
+ "customField#{custom_field.id}": {
+ "raw": "CF text"
+ }
+ }
+ end
+
+ it 'responds with 200 OK' do
+ expect(last_response.status).to eq(200)
+ end
+
+ it 'sets the cf value' do
+ expect(project.reload.send("custom_field_#{custom_field.id}"))
+ .to eql("CF text")
+ 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 'does not change the project' do
+ attributes_before = project.attributes
+
+ expect(project.reload.name)
+ .to eql(attributes_before['name'])
+ end
+ end
+
+ context 'with faulty params' do
+ let(:body) do
+ {
+ name: nil
+ }
+ end
+
+ it 'responds with 422' do
+ expect(last_response.status).to eq(422)
+ end
+
+ it 'does not change the project' do
+ attributes_before = project.attributes
+
+ expect(project.reload.name)
+ .to eql(attributes_before['name'])
+ 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