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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
add parent reference to project api
ulferts8431880de09d
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rbindex e36c95b9952..f1be98829ac 100644--- a/app/contracts/projects/base_contract.rb+++ b/app/contracts/projects/base_contract.rb@@ -42,7 +42,7 @@ module Projectsvalidate_status_includedendattribute :parent do- validate_parent_visible+ validate_parent_assignableendattribute_alias :is_public, :public@@ -54,7 +54,9 @@ module Projectsenddef assignable_parents- Project.visible+ Project+ .allowed_to(user, :add_subprojects)+ .where.not(id: model.self_and_descendants)enddef assignable_statuses@@ -85,8 +87,12 @@ module Projectsendend- def validate_parent_visible- errors.add(:parent, :does_not_exist) if model.parent && model.parent_id_changed? && !model.parent.visible?+ def validate_parent_assignable+ if model.parent &&+ model.parent_id_changed? &&+ !assignable_parents.where(id: parent.id).exists?+ errors.add(:parent, :does_not_exist)+ endenddef validate_user_allowed_to_managediff --git a/app/controllers/project_settings_controller.rb b/app/controllers/project_settings_controller.rbindex 232d6aa8bb7..6295545822b 100644--- a/app/controllers/project_settings_controller.rb+++ b/app/controllers/project_settings_controller.rb@@ -44,7 +44,7 @@ class ProjectSettingsController < ApplicationControllerenddef settings_custom_fields- @issue_custom_fields = WorkPackageCustomField.order("#{CustomField.table_name}.position")+ @wp_custom_fields = WorkPackageCustomField.order("#{CustomField.table_name}.position")enddef settings_repositorydiff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rbindex c535f692341..0fad12bf73a 100644--- a/app/controllers/projects_controller.rb+++ b/app/controllers/projects_controller.rb@@ -69,6 +69,7 @@ class ProjectsController < ApplicationControllerdef newassign_default_create_variables+ @project = Project.newrender layout: 'no_menu'end@@ -268,9 +269,9 @@ class ProjectsController < ApplicationControllerenddef assign_default_create_variables- @issue_custom_fields = WorkPackageCustomField.order("#{CustomField.table_name}.position")+ @wp_custom_fields = WorkPackageCustomField.order("#{CustomField.table_name}.position")@types = ::Type.all- @project = Project.new+@project.parent = Project.find(params[:parent_id]) if params[:parent_id]@project.attributes = permitted_params.project if params[:project].present?enddiff --git a/app/views/project_settings/_custom_fields.html.erb b/app/views/project_settings/_custom_fields.html.erbindex 10b9ea7dd39..3c3b6701790 100644--- a/app/views/project_settings/_custom_fields.html.erb+++ b/app/views/project_settings/_custom_fields.html.erb@@ -40,7 +40,7 @@ See docs/COPYRIGHT.rdoc for more details.</fieldset><% end %>-<% if @issue_custom_fields.any? %>+<% if @wp_custom_fields.any? %><%= labelled_tabular_form_for @project,url: { controller: '/projects', action: 'custom_fields', id: @project },method: :put,@@ -50,7 +50,7 @@ See docs/COPYRIGHT.rdoc for more details.locals: {project: @project,form: form,- work_package_custom_fields: @issue_custom_fields+ work_package_custom_fields: @wp_custom_fields} %><p><%= form.button l(:button_save), class: 'button -highlight -with-icon icon-checkmark' %></p>diff --git a/app/views/projects/_form.html.erb b/app/views/projects/_form.html.erbindex 4295e0ec663..262e4cf980a 100644--- a/app/views/projects/_form.html.erb+++ b/app/views/projects/_form.html.erb@@ -66,7 +66,7 @@ See docs/COPYRIGHT.rdoc for more details.locals: {form: f,project: project,- work_package_custom_fields: @issue_custom_fields+ work_package_custom_fields: @wp_custom_fields} %></section><% end %>diff --git a/docs/api/apiv3/endpoints/projects.apib b/docs/api/apiv3/endpoints/projects.apibindex b89ee4cb674..9c00f13df4e 100644--- a/docs/api/apiv3/endpoints/projects.apib+++ b/docs/api/apiv3/endpoints/projects.apib@@ -17,6 +17,7 @@| versions | Versions available in this project | Collection | not null | READ | **Permission**: view work packages or manage versions || memberships | Memberships in the project | Collection | not null | READ | **Permission**: view members || workPackages | Work Packages of this project | Collection | not null | READ | |+| parent | Parent project of the project | Project | | READ/WRITE | **Permission** edit projectDepending on custom fields defined for projects, additional links might exist.@@ -24,12 +25,12 @@ Depending on custom fields defined for projects, additional links might exist.| Property | Description | Type | Constraints | Supported operations || :---------: | ------------- | ---- | ----------- | -------------------- |-| id | Projects's id | Integer | x > 0 | READ |-| identifier | | String | | READ |-| name | | String | | READ |-| status | The project's status | String | 'active' or 'archived' | READ |-| public | Indicates whether the project is accessible for everybody | Boolean | | READ |-| description | | Formattable | | READ |+| id | Projects's id | Integer | x > 0 | READ/WRITE |+| identifier | | String | | READ/WRITE |+| name | | String | | READ/WRITE |+| status | The project's status | String | 'active' or 'archived' | READ/WRITE |+| public | Indicates whether the project is accessible for everybody | Boolean | | READ/WRITE |+| description | | Formattable | | READ/WRITE || createdAt | Time of creation | DateTime | | READ || updatedAt | Time of the most recent change to the project | DateTime | | READ |@@ -74,6 +75,9 @@ Depending on custom fields defined for projects, additional properties might exi"href": "/api/v3/users/315","method": "A user"},+ "projects": {+ "href": "/api/v3/projects/123"+ }},"id": 1,"identifier": "project_identifier",@@ -204,7 +208,7 @@ Updates the given project by applying the attributes provided in the body.}}-## Projects [/api/v3/projects{?filters}]+## Projects [/api/v3/projects{?filters,sortBy}]+ Model+ Body@@ -240,6 +244,9 @@ Updates the given project by applying the attributes provided in the body.},"versions": {"href": "/api/v3/projects/6/versions"+ },+ "projects": {+ "href": "/api/v3/projects/123"}},"id": 6,@@ -276,6 +283,9 @@ Updates the given project by applying the attributes provided in the body.},"versions": {"href": "/api/v3/projects/14/versions"+ },+ "projects": {+ "href": null}},"id": 14,@@ -314,6 +324,15 @@ Returns a collection of projects. The collection can be filtered via query param+ type_id: based on the types active in a project.+ id: based on projects' id.There might also be additional filters based on the custom fields that have been configured.+ + sortBy (optional, string, `[["id", "asc"]]`) ... JSON specifying sort criteria.+ Currently supported orders are:+ + id+ + name+ + created_on+ + is_public+ + latest_activity_at+ + required_disk_space+ There might also be additional orders based on the custom fields that have been configured.+ Response 200 (application/hal+json)@@ -337,6 +356,9 @@ You can use the form and schema to be retrieve the valid attribute values and by"_links": {"customField12": {"href": "/api/v3/users/5"+ },+ "parent": {+ "href": "/api/v3/projects/6"}}}@@ -446,6 +468,15 @@ You can use the form and schema to be retrieve the valid attribute values and by"visibility": "default","_links": {}},+ "parent": {+ "type": "Project",+ "name": "Subproject of",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "visibility": "default",+ "_links": {}+ },"createdAt": {"type": "DateTime","name": "Created on",@@ -567,6 +598,9 @@ For more details and all possible responses see the general specification of [Fo"customField31": {"href": null,"title": null+ },+ "parent": {+ "href": null}}},@@ -627,6 +661,19 @@ For more details and all possible responses see the general specification of [Fo},"_links": {}},+ "parent": {+ "type": "Project",+ "name": "Subproject of",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "visibility": "default",+ "_links": {+ "allowedValues": {+ "href": "/api/v3/projects/available_parent_projects"+ }+ }+ },"createdAt": {"type": "DateTime","name": "Created on",@@ -861,6 +908,9 @@ For more details and all possible responses see the general specification of [Fo"customField31": {"href": null,"title": null+ },+ "parent": {+ "href": "/api/v3/projects/45"}}},@@ -921,6 +971,19 @@ For more details and all possible responses see the general specification of [Fo},"_links": {}},+ "parent": {+ "type": "Project",+ "name": "Subproject of",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "visibility": "default",+ "_links": {+ "allowedValues": {+ "href": "/api/v3/projects/available_parent_projects?of=123"+ }+ }+ },"createdAt": {"type": "DateTime","name": "Created on",@@ -1102,6 +1165,140 @@ For more details and all possible responses see the general specification of [Fo"message": "You are not authorized to access this resource."}+## Available parent project candidates [/api/v3/projects/available_parent_projects{?filters,of,sortBy}]+++ Model+ + Body++ {+ "_links": {+ "self": {+ "href": "/api/v3/projects/available_parent_projects?of=123"+ }+ },+ "_type": "Collection",+ "total": 2,+ "count": 2,+ "_embedded": {+ "elements": [+ {+ "_type": "Project",+ "_links": {+ "self": {+ "href": "/api/v3/projects/6",+ "title": "A project"+ },+ "createWorkPackage": {+ "href": "/api/v3/projects/6/work_packages/form",+ "method": "post"+ },+ "createWorkPackageImmediate": {+ "href": "/api/v3/projects/6/work_packages",+ "method": "post"+ },+ "categories": {+ "href": "/api/v3/projects/6/categories"+ },+ "versions": {+ "href": "/api/v3/projects/6/versions"+ }+ },+ "id": 6,+ "identifier": "a_project",+ "name": "A project",+ "status": "active",+ "public": false,+ "description": {+ "format": "markdown",+ "raw": "Lorem **ipsum** dolor sit amet",+ "html": "<p>Lorem <strong>ipsum</strong> dolor sit amet</p>"+ },+ "createdAt": "2015-07-06T13:28:14+00:00",+ "updatedAt": "2015-10-01T09:55:02+00:00",+ "type": "Customer Project"+ },+ {+ "_type": "Project",+ "_links": {+ "self": {+ "href": "/api/v3/projects/14",+ "title": "Another project"+ },+ "createWorkPackage": {+ "href": "/api/v3/projects/14/work_packages/form",+ "method": "post"+ },+ "createWorkPackageImmediate": {+ "href": "/api/v3/projects/14/work_packages",+ "method": "post"+ },+ "categories": {+ "href": "/api/v3/projects/14/categories"+ },+ "versions": {+ "href": "/api/v3/projects/14/versions"+ }+ },+ "id": 14,+ "identifier": "another_project",+ "name": "Another project",+ "status": "archived",+ "public": true,+ "description": {+ "format": "markdown",+ "raw": "",+ "html": ""+ },+ "createdAt": "2016-02-29T12:50:20+00:00",+ "updatedAt": "2016-02-29T12:50:20+00:00",+ "type": null+ }]+ }+ }+ }++## List available parent project candidates [GET]++Lists projects which can become parent to another project. Only sound candidates are returned.+For instance a project cannot become parent of itself or it's children.++To specify the project for which a parent is queried for, the `of` parameter can be provided. If no `of`+parameter is provided, a new project is assumed. Then, the check for the hierarchy is omitted as a new project cannot be+part of a hierarchy yet.++Candidates can be filtered. Most commonly one will want to filter by name or identifier.+You can do this through the `filters` parameter which works just like the work package index.++For instance to find all parent candidates with "rollout" in their name:++```+?filters=[{"name_and_identifier":{"operator":"~","values":["rollout"]}}]+```+++ Parameters+ + filters (optional, string, `[{ "ancestor": { "operator": "=", "values": ['1'] }" }]`) ... JSON specifying filter conditions.+ + of (optional, number or identifier, `123` ... The id or identifier of the project the parent candidate is determined for+ + sortBy (optional, string, `[["id", "asc"]]`) ... JSON specifying sort criteria.+ Accepts the same format as returned by the [queries](#queries) endpoint and allows all the filters and sortBy supported by the project list end point.+++ Response 200 (application/hal+json)++ [Available parent project candidates][]+++ Response 403 (application/hal+json)++ Returned if the client does not have sufficient permissions.++ **Required permission:** edit project in a project or the global add project permission++ + Body++ {+ "_type": "Error",+ "errorIdentifier": "urn:openproject-org:api:v3:errors:MissingPermission",+ "message": "You are not authorized to access this resource."+ }+## Projects by version [/api/v3/versions/{id}/projects]+ Modeldiff --git a/lib/api/v3/projects/available_parents_api.rb b/lib/api/v3/projects/available_parents_api.rbnew file mode 100644index 00000000000..52decb42258--- /dev/null+++ b/lib/api/v3/projects/available_parents_api.rb@@ -0,0 +1,63 @@+#-- 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 'api/v3/users/user_collection_representer'++module API+ module V3+ module Projects+ class AvailableParentsAPI < ::API::OpenProjectAPI+ resource :available_parent_projects do+ after_validation do+ authorize_any(%i[add_project edit_project], global: true)+ end++ get &::API::V3::Utilities::Endpoints::Index.new(model: Project,+ scope: -> do+ project = if params[:of]+ Project.find(params[:of])+ else+ Project.new+ end++ contract_class = if project.new_record?+ ::Projects::CreateContract+ else+ ::Projects::UpdateContract+ end++ contract = contract_class.new(project, current_user)++ contract.assignable_parents.includes(:enabled_modules)+ end)+ .mount+ end+ end+ end+ end+enddiff --git a/lib/api/v3/projects/project_representer.rb b/lib/api/v3/projects/project_representer.rbindex 60742ac59ae..d6b757c6deb 100644--- a/lib/api/v3/projects/project_representer.rb+++ b/lib/api/v3/projects/project_representer.rb@@ -34,6 +34,7 @@ module APImodule V3module Projectsclass ProjectRepresenter < ::API::Decorators::Single+ include API::Decorators::LinkedResourceinclude API::Decorators::DatePropertyinclude ::API::Caching::CachedRepresenterinclude API::Decorators::FormattableProperty@@ -93,6 +94,24 @@ module API{ href: api_v3_paths.types_by_project(represented.id) }end+ associated_resource :parent,+ v3_path: :project,+ representer: ::API::V3::Projects::ProjectRepresenter,+ uncacheable_link: true,+ link: ->(*) {+ if represented.parent&.visible?+ {+ href: api_v3_paths.project(represented.parent.id),+ title: represented.parent.name+ }+ else+ {+ href: nil,+ title: nil+ }+ end+ }+property :idproperty :identifierproperty :namediff --git a/lib/api/v3/projects/projects_api.rb b/lib/api/v3/projects/projects_api.rbindex 9bf992f706f..e3518f10049 100644--- a/lib/api/v3/projects/projects_api.rb+++ b/lib/api/v3/projects/projects_api.rb@@ -43,6 +43,8 @@ module APImount ::API::V3::Projects::Schemas::ProjectSchemaAPImount ::API::V3::Projects::CreateFormAPI+ mount API::V3::Projects::AvailableParentsAPI+params dorequires :id, desc: 'Project id'enddiff --git a/lib/api/v3/projects/schemas/project_schema_representer.rb b/lib/api/v3/projects/schemas/project_schema_representer.rbindex bc8657fb62c..a2e47cc1f52 100644--- a/lib/api/v3/projects/schemas/project_schema_representer.rb+++ b/lib/api/v3/projects/schemas/project_schema_representer.rb@@ -65,6 +65,19 @@ module APIschema_with_allowed_string_collection :status,type: 'String'+ schema_with_allowed_link :parent,+ type: 'Project',+ required: false,+ href_callback: ->(*) {+ query_props = if represented.model.new_record?+ ''+ else+ "?of=#{represented.model.id}"+ end++ api_v3_paths.projects_available_parents + query_props+ }+schema :created_at,type: 'DateTime',visibility: falsediff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rbindex 38008569fbf..9db82753535 100644--- a/lib/api/v3/utilities/path_helper.rb+++ b/lib/api/v3/utilities/path_helper.rb@@ -210,6 +210,11 @@ module APIendresources :project++ def self.projects_available_parents+ "#{projects}/available_parent_projects"+ end+resources :querydef self.query_defaultdiff --git a/spec/contracts/projects/shared_contract_examples.rb b/spec/contracts/projects/shared_contract_examples.rbindex 2ac89dc3ab4..c51f34e1029 100644--- a/spec/contracts/projects/shared_contract_examples.rb+++ b/spec/contracts/projects/shared_contract_examples.rb@@ -44,13 +44,40 @@ shared_examples_for 'project contract' dolet(:project_status) { Project::STATUS_ACTIVE }let(:project_public) { true }let(:project_parent) do- FactoryBot.build_stubbed(:project).tap do |p|- allow(p)- .to receive(:visible?)- .and_return(parent_visible)+ FactoryBot.build_stubbed(:project)+ end+ let(:parent_assignable) { true }+ let!(:assignable_parents) do+ assignable_parents_scope = double('assignable parents scope')+ assignable_parents = double('assignable parents')++ allow(Project)+ .to receive(:allowed_to)+ .with(current_user, :add_subprojects)+ .and_return assignable_parents_scope++ allow(assignable_parents_scope)+ .to receive(:where)+ .and_return(assignable_parents_scope)++ allow(assignable_parents_scope)+ .to receive(:not)+ .with(id: project.self_and_descendants)+ .and_return(assignable_parents)++ if project_parent+ allow(assignable_parents)+ .to receive(:where)+ .with(id: project_parent.id)+ .and_return(assignable_parents_scope)++ allow(assignable_parents_scope)+ .to receive(:exists?)+ .and_return(parent_assignable)end++ assignable_parentsend- let(:parent_visible) { true }def expect_valid(valid, symbols = {})expect(contract.validate).to eq(valid)@@ -96,8 +123,8 @@ shared_examples_for 'project contract' doit_behaves_like 'is valid'end- context 'if the parent is invisible to the user' do- let(:parent_visible) { false }+ context 'if the parent is not in the set of assignable_parents' do+ let(:parent_assignable) { false }it 'is invalid' doexpect_valid(false, parent: %i(does_not_exist))@@ -122,16 +149,13 @@ shared_examples_for 'project contract' dodescribe 'assignable_values' docontext 'for project' do- let(:visible_projects) { double('visible projects') }before do- allow(Project)- .to receive(:visible)- .and_return(visible_projects)+ assignable_parentsend- it 'is the list of visible projects' do- expect(subject.assignable_values(:parent, current_user))- .to eql visible_projects+ it 'returns all projects the user has the add_subprojects permissions for' do+ expect(contract.assignable_parents)+ .to eql assignable_parentsendenddiff --git a/spec/lib/api/v3/projects/project_representer_spec.rb b/spec/lib/api/v3/projects/project_representer_spec.rbindex 14c444394cb..8a070aaa5f2 100644--- a/spec/lib/api/v3/projects/project_representer_spec.rb+++ b/spec/lib/api/v3/projects/project_representer_spec.rb@@ -33,6 +33,7 @@ describe ::API::V3::Projects::ProjectRepresenter dolet(:project) doFactoryBot.build_stubbed(:project,+ parent: parent_project,description: 'some description').tap do |p|allow(p).to receive(:available_custom_fields)@@ -48,6 +49,7 @@ describe ::API::V3::Projects::ProjectRepresenter do.and_return(version_custom_value)endend+ let(:parent_project) { FactoryBot.build_stubbed(:project) }let(:representer) { described_class.create(project, current_user: user) }let(:user) do@@ -149,10 +151,11 @@ describe ::API::V3::Projects::ProjectRepresenter dodescribe '_links' doit { is_expected.to have_json_type(Object).at_path('_links') }- it 'should link to self' do++ it 'links to self' doexpect(subject).to have_json_path('_links/self/href')end- it 'should have a title for link to self' do+ it 'has a title for link to self' doexpect(subject).to have_json_path('_links/self/title')end@@ -183,6 +186,31 @@ describe ::API::V3::Projects::ProjectRepresenter doendend+ describe 'parent' do+ before do+ allow(parent_project)+ .to receive(:visible?)+ .and_return(visible)+ end+ let(:visible) { true }++ it_behaves_like 'has a titled link' do+ let(:link) { 'parent' }+ let(:href) { api_v3_paths.project(parent_project.id) }+ let(:title) { parent_project.name }+ end++ context 'if lacking the permissions to see the parent' do+ let(:visible) { false }++ it_behaves_like 'has a titled link' do+ let(:link) { 'parent' }+ let(:href) { nil }+ let(:title) { nil }+ end+ end+ end+describe 'categories' doit 'has the correct link to its categories' dois_expected.to be_json_eql(api_v3_paths.categories_by_project(project.id).to_json)diff --git a/spec/lib/api/v3/projects/schemas/project_schema_representer_spec.rb b/spec/lib/api/v3/projects/schemas/project_schema_representer_spec.rbindex 5e6e877068c..f1e4da59ad3 100644--- a/spec/lib/api/v3/projects/schemas/project_schema_representer_spec.rb+++ b/spec/lib/api/v3/projects/schemas/project_schema_representer_spec.rb@@ -36,16 +36,18 @@ describe ::API::V3::Projects::Schemas::ProjectSchemaRepresenter dolet(:self_link) { '/a/self/link' }let(:embedded) { true }let(:new_record) { true }+ let(:model_id) { 1 }let(:custom_field) doFactoryBot.build_stubbed(:int_project_custom_field)endlet(:allowed_status) { ['some status'] }let(:contract) docontract = double('contract')+ model = double('project')allow(contract).to receive(:writable?) do |attribute|- writable = %w(name identifier description is_public status)+ writable = %w(name identifier description is_public status parent)writable.include?(attribute.to_s)end@@ -59,6 +61,18 @@ describe ::API::V3::Projects::Schemas::ProjectSchemaRepresenter do.with(:status, current_user).and_return(allowed_status)+ allow(contract)+ .to receive(:model)+ .and_return(model)++ allow(model)+ .to receive(:new_record?)+ .and_return(new_record)++ allow(model)+ .to receive(:id)+ .and_return(model_id)+contractendlet(:representer) do@@ -213,6 +227,62 @@ describe ::API::V3::Projects::Schemas::ProjectSchemaRepresenter doendend+ describe 'parent' do+ let(:path) { 'parent' }++ context 'if having a new record' do+ it_behaves_like 'has basic schema properties' do+ let(:type) { 'Project' }+ let(:name) { Project.human_attribute_name('parent') }+ let(:required) { false }+ let(:writable) { true }+ end++ context 'if embedding' do+ let(:embedded) { true }++ it_behaves_like 'links to allowed values via collection link' do+ let(:href) do+ api_v3_paths.projects_available_parents+ end+ end+ end++ context 'if not embedding' do+ let(:embedded) { false }++ it_behaves_like 'does not link to allowed values'+ end+ end++ context 'if having a persisted record' do+ let(:new_record) { false }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'Project' }+ let(:name) { Project.human_attribute_name('parent') }+ let(:required) { false }+ let(:writable) { true }+ end++ context 'if embedding' do+ let(:embedded) { true }++ it_behaves_like 'links to allowed values via collection link' do+ let(:href) do+ api_v3_paths.projects_available_parents + "?of=#{model_id}"+ end+ end+ end++ context 'if not embedding' do+ let(:embedded) { false }++ it_behaves_like 'does not link to allowed values'+ end+ end+ end+context '_links' dodescribe 'self link' doit_behaves_like 'has an untitled link' dodiff --git a/spec/lib/api/v3/utilities/path_helper_spec.rb b/spec/lib/api/v3/utilities/path_helper_spec.rbindex dc51ebaea54..1302168d59d 100644--- a/spec/lib/api/v3/utilities/path_helper_spec.rb+++ b/spec/lib/api/v3/utilities/path_helper_spec.rb@@ -239,6 +239,12 @@ describe ::API::V3::Utilities::PathHelper dodescribe 'projects paths' doit_behaves_like 'resource', :project++ describe '#projects_available_parents' do+ subject { helper.projects_available_parents }++ it_behaves_like 'api v3 path', '/projects/available_parent_projects'+ endenddescribe 'query paths' dodiff --git a/spec/requests/api/v3/project_resource_spec.rb b/spec/requests/api/v3/project_resource_spec.rbindex 85120124aa5..205a1d58767 100644--- a/spec/requests/api/v3/project_resource_spec.rb+++ b/spec/requests/api/v3/project_resource_spec.rb@@ -58,6 +58,19 @@ describe 'API v3 Project resource', type: :request, content_type: :json dodescribe '#get /projects/:id' dolet(:get_path) { api_v3_paths.project project.id }+ let!(:parent_project) do+ FactoryBot.create(:project, is_public: false).tap do |p|+ project.parent = p+ project.save!+ end+ end+ let!(:parent_memberships) do+ FactoryBot.create(:member,+ user: current_user,+ project: parent_project,+ roles: [FactoryBot.create(:role, permissions: [])])+ end+subject(:response) doget get_path@@ -74,6 +87,12 @@ describe 'API v3 Project resource', type: :request, content_type: :json doexpect(subject.body).to be_json_eql(project.identifier.to_json).at_path('identifier')end+ it 'links to the parent project' do+ expect(subject.body)+ .to be_json_eql(api_v3_paths.project(parent_project.id).to_json)+ .at_path('_links/parent/href')+ end+it 'includes custom fields' docustom_value@@ -107,6 +126,17 @@ describe 'API v3 Project resource', type: :request, content_type: :json dolet(:type) { 'Project' }endend++ context 'not being allowed to see the parent project' do+ let!(:parent_memberships) do+ end++ it 'has no path to the parent' do+ expect(subject.body)+ .to be_json_eql(nil.to_json)+ .at_path('_links/parent/href')+ end+ endendcontext 'not logged in user' dodiff --git a/spec/requests/api/v3/projects/available_parents_resource_spec.rb b/spec/requests/api/v3/projects/available_parents_resource_spec.rbnew file mode 100644index 00000000000..281b17b0f8a--- /dev/null+++ b/spec/requests/api/v3/projects/available_parents_resource_spec.rb@@ -0,0 +1,158 @@+#-- 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'+require 'rack/test'++describe 'API v3 Project available parents resource', type: :request, content_type: :json do+ include Rack::Test::Methods+ include API::V3::Utilities::PathHelper++ let!(:current_user) do+ FactoryBot.create(:user, member_in_project: project, member_with_permissions: permissions).tap do |u|+ u.global_roles << FactoryBot.create(:global_role, permissions: global_permissions)+ end+ end+ let!(:project_with_add_suproject_permission) do+ FactoryBot.create(:project).tap do |p|+ FactoryBot.create(:member,+ user: current_user,+ project: p,+ roles: [FactoryBot.create(:role, permissions: [:add_subprojects])])+ end+ end+ let!(:child_project_with_add_suproject_permission) do+ FactoryBot.create(:project, parent: project).tap do |p|+ FactoryBot.create(:member,+ user: current_user,+ project: p,+ roles: [FactoryBot.create(:role, permissions: [:add_subprojects])])+ end+ end+ let!(:project_without_add_suproject_permission) do+ FactoryBot.create(:project).tap do |p|+ FactoryBot.create(:member,+ user: current_user,+ project: p,+ roles: [FactoryBot.create(:role, permissions: [])])+ end+ end+ let!(:project) do+ FactoryBot.create(:project, is_public: false)+ end+ let(:permissions) { %i[edit_project add_subprojects] }+ let(:global_permissions) { %i[add_project] }+ let(:path) { api_v3_paths.path_for(:projects_available_parents, sort_by: [%i[id asc]]) }++ describe 'GET /api/v3/projects/available_parent_projects' do+ before do+ login_as current_user+ end++ subject(:response) do+ get path++ last_response+ end++ context 'without a project candidate' do+ it 'returns 200 OK' do+ expect(subject.status)+ .to eql 200+ end++ it 'returns projects for which the user has the add_subprojects permission' do+ expect(subject.body)+ .to have_json_size(3)+ .at_path('_embedded/elements')++ expect(subject.body)+ .to be_json_eql(project.id.to_json)+ .at_path('_embedded/elements/0/id')++ expect(subject.body)+ .to be_json_eql(project_with_add_suproject_permission.id.to_json)+ .at_path('_embedded/elements/1/id')++ expect(subject.body)+ .to be_json_eql(child_project_with_add_suproject_permission.id.to_json)+ .at_path('_embedded/elements/2/id')+ end+ end++ context 'with a project candidate' do+ let(:path) { api_v3_paths.projects_available_parents + "?of=#{project.id}" }++ it 'returns 200 OK' do+ expect(subject.status)+ .to eql 200+ end++ it 'returns projects for which the user has the add_subprojects permission but' ++ ' excludes the queried for project and it`s descendants' do+ expect(subject.body)+ .to have_json_size(1)+ .at_path('_embedded/elements')++ expect(subject.body)+ .to be_json_eql(project_with_add_suproject_permission.id.to_json)+ .at_path('_embedded/elements/0/id')+ end+ end++ context 'if lacking edit and add permission' do+ let(:permissions) { %i[add_subprojects] }+ let(:global_permissions) { %i[] }++ it 'returns 403' do+ expect(subject.status)+ .to eql 403+ end+ end++ context 'if having only edit permission' do+ let(:permissions) { %i[edit_project add_subprojects] }+ let(:global_permissions) { %i[] }++ it 'returns 200' do+ expect(subject.status)+ .to eql 200+ end+ end++ context 'if having only add permission' do+ let(:permissions) { %i[add_subprojects] }+ let(:global_permissions) { %i[add_project] }++ it 'returns 200' do+ expect(subject.status)+ .to eql 200+ end+ end+ end+enddiff --git a/spec/requests/api/v3/projects/update_form_resource_spec.rb b/spec/requests/api/v3/projects/update_form_resource_spec.rbindex 6026c95ea20..d2427f7173a 100644--- a/spec/requests/api/v3/projects/update_form_resource_spec.rb+++ b/spec/requests/api/v3/projects/update_form_resource_spec.rb@@ -50,7 +50,19 @@ describe ::API::V3::Projects::UpdateFormAPI, content_type: :json dolet(:list_custom_field) doFactoryBot.create(:list_project_custom_field)end+ let(:viable_parent_project) do+ FactoryBot.create(:project).tap do |p|+ FactoryBot.create(:member,+ project: p,+ principal: current_user,+ roles: [parent_project_role])+ end+ end+ let(:parent_project_role) do+ FactoryBot.create(:role, permissions: parent_project_permissions)+ endlet(:permissions) { [:edit_project] }+ let(:parent_project_permissions) { [:add_subprojects] }let(:path) { api_v3_paths.project_form(project.id) }let(:params) do{@@ -121,6 +133,32 @@ describe ::API::V3::Projects::UpdateFormAPI, content_type: :json doendend+ context 'with a viable parent project' do+ context 'with a correct parameter' do+ let(:params) do+ {+ "_links": {+ "parent": {+ "href": api_v3_paths.project(viable_parent_project.id)+ }+ }+ }+ end++ it 'sets the project in the payload' do+ expect(subject.body)+ .to be_json_eql(api_v3_paths.project(viable_parent_project.id).to_json)+ .at_path('_embedded/payload/_links/parent/href')+ end++ it 'links to the allowed parents in the schema' do+ expect(subject.body)+ .to be_json_eql((api_v3_paths.projects_available_parents + "?of=#{project.id}").to_json)+ .at_path('_embedded/schema/parent/_links/allowedValues/href')+ end+ end+ end+context 'with valid parameters' dolet(:params) do{@@ -191,5 +229,13 @@ describe ::API::V3::Projects::UpdateFormAPI, content_type: :json doexpect(response.status).to eq(403)endend++ context 'with a non existing id' do+ let(:path) { api_v3_paths.project_form(1) }++ it 'returns 404 Not found' do+ expect(response.status).to eq(404)+ end+ endendend