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

Bug/35510 editing project settings removes link to parent project for certain permission constellations (#9336)

ulferts2f00810e92bc
diff --git a/config/locales/en.yml b/config/locales/en.yml
index ccf0086b130..f5c9f2ae4d2 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -2787,6 +2787,9 @@ en:
total_progress: "Total progress"
+ undisclosed:
+ parent: Undisclosed - The selected parent is invisible because of lacking permissions.
+
user:
all: "all"
active: "active"
diff --git a/docs/api/apiv3/basic-objects.apib b/docs/api/apiv3/basic-objects.apib
index c6511254fa0..2b4b8794fc7 100644
--- a/docs/api/apiv3/basic-objects.apib
+++ b/docs/api/apiv3/basic-objects.apib
@@ -42,6 +42,15 @@ in its strings (e.g. `{ "href": "/api/v3/examples/{example_id}" }`).
Note: When writing links (e.g. during a `PATCH` operation) only changes to `href` are accepted.
Changes to all other properties will be **silently ignored**.
+For resources invisible to the client (e.g. because of missing permissions), a link will contain
+the uri `urn:openproject-org:api:v3:undisclosed` instead of a url. This indicates the existence of a value
+without revealing the actual value. An example for this is the parent project. A project resource which itself might be
+visible to the client can have a reference to a parent project invisible to the same client. Revealing the existence of
+a parent over hiding has the benefit of allowing the client to make an informed decision of whether to keep the existing reference
+or updating it. Sending ‘{ "href": "urn:openproject-org:api:v3:undisclosed" }` for a resource will have the effect of keeping the
+original value. This is especially beneficial if the client creates and updates resources based on the payload object provided
+as part of a form as is recommended when interacting with the API.
+
# Errors
In case of an error, the API will respond with an appropriate HTTP status code.
diff --git a/docs/api/apiv3/endpoints/projects.apib b/docs/api/apiv3/endpoints/projects.apib
index ec19eaedd68..b64596b78fd 100644
--- a/docs/api/apiv3/endpoints/projects.apib
+++ b/docs/api/apiv3/endpoints/projects.apib
@@ -29,6 +29,10 @@ As containers, they also control behaviour of the elements within them. One of t
Depending on custom fields defined for projects, additional links might exist.
+Note, that the parent link may contain the "undisclosed uri" `urn:openproject-org:api:v3:undisclosed` in case a
+parent project is defined but the client lacks permission to see it. See the
+[general introduction into links' properties](/api/basic-objects/#header-local-properties-1) for more information.
+
## Local Properties
| Property | Description | Type | Constraints | Supported operations |
diff --git a/frontend/src/app/core/services/forms/forms.service.ts b/frontend/src/app/core/services/forms/forms.service.ts
index 2d7d4ec3559..3cd79bcfc21 100644
--- a/frontend/src/app/core/services/forms/forms.service.ts
+++ b/frontend/src/app/core/services/forms/forms.service.ts
@@ -86,8 +86,8 @@ export class FormsService {
// Form.payload resources have a HalLinkSource interface while
// API resource options have a IAllowedValue interface
const resourceValue = Array.isArray(resource) ?
- resource.map(resourceElement => ({ href: resourceElement?.href || resourceElement?._links?.self?.href || null })) :
- { href: resource?.href || resource?._links?.self?.href || null };
+ resource.map(resourceElement => ({ href: resourceElement?.href || resourceElement?._links?.self?.href })) :
+ { href: resource?.href || resource?._links?.self?.href };
return {
...result,
diff --git a/frontend/src/app/core/services/forms/typings.d.ts b/frontend/src/app/core/services/forms/typings.d.ts
index b432aababfd..bfcf8051128 100644
--- a/frontend/src/app/core/services/forms/typings.d.ts
+++ b/frontend/src/app/core/services/forms/typings.d.ts
@@ -80,7 +80,7 @@ interface IOPApiCall {
}
interface IOPApiOption {
- href:string;
+ href:string|null;
title?:string;
}
@@ -99,8 +99,8 @@ interface IOPAllowedValue {
name:string;
[key:string]:unknown;
_links?:{
- self:HalSource | IOPApiOption;
- [key:string]:HalSource;
+ self:HalSourceLink | IOPApiOption;
+ [key:string]:HalSourceLink;
};
}
diff --git a/frontend/src/app/modules/common/dynamic-forms/services/dynamic-fields/dynamic-fields.service.ts b/frontend/src/app/modules/common/dynamic-forms/services/dynamic-fields/dynamic-fields.service.ts
index 2ddab0c0bf6..1041ea9c826 100644
--- a/frontend/src/app/modules/common/dynamic-forms/services/dynamic-fields/dynamic-fields.service.ts
+++ b/frontend/src/app/modules/common/dynamic-forms/services/dynamic-fields/dynamic-fields.service.ts
@@ -9,11 +9,12 @@ import { Observable, of } from "rxjs";
import { map } from "rxjs/operators";
import { HttpClient } from "@angular/common/http";
import { I18nService } from "core-app/modules/common/i18n/i18n.service";
+import { HalLink } from "core-app/modules/hal/hal-link/hal-link";
@Injectable()
export class DynamicFieldsService {
- readonly selectDefaultValue = {name:'-'};
+ readonly selectDefaultValue = { name: '-', _links: { self: { href: null } } };
readonly inputsCatalogue:IOPDynamicInputTypeSettings[] = [
{
config: {
@@ -145,7 +146,7 @@ export class DynamicFieldsService {
const elementValue = otherElements[key];
if (this.isValue(elementValue)) {
- model = {...model, [key]:elementValue}
+ model = { ...model, [key]: elementValue }
}
return model;
@@ -208,7 +209,7 @@ export class DynamicFieldsService {
result = {
...result,
- ...this.isValue(resourceModel) && {[resourceKey]: resourceModel},
+ ...this.isValue(resourceModel) && { [resourceKey]: resourceModel },
};
return result;
@@ -222,9 +223,9 @@ export class DynamicFieldsService {
return null;
}
const { templateOptions, ...fieldTypeConfig } = fieldTypeConfigSearch;
- const fieldOptions = this.getFieldOptions(fieldSchema);
const property = this.getFieldProperty(key);
- const payloadValue = property && formPayload[property];
+ const payloadValue = property && (formPayload[property] || formPayload['_links'] && formPayload['_links'][property]);
+ const fieldOptions = this.getFieldOptions(fieldSchema, payloadValue);
const formlyFieldConfig = {
...fieldTypeConfig,
key,
@@ -265,8 +266,8 @@ export class DynamicFieldsService {
className: field.name,
templateOptions: {
...inputConfig.templateOptions,
- ...this.isMultiSelectField(field) && {multiple: true},
- ...fieldType === 'User' && {showAddNewUserButton: true},
+ ...this.isMultiSelectField(field) && { multiple: true },
+ ...fieldType === 'User' && { showAddNewUserButton: true },
},
};
} else if (inputConfig.type === 'formattableInput') {
@@ -282,7 +283,7 @@ export class DynamicFieldsService {
return { ...inputConfig, ...configCustomizations };
}
- private getFieldOptions(field:IOPFieldSchemaWithKey):Observable<IOPAllowedValue[]>|undefined {
+ private getFieldOptions(field:IOPFieldSchemaWithKey, currentValue:HalLink|null):Observable<IOPAllowedValue[]>|undefined {
const allowedValues = field._embedded?.allowedValues || field._links?.allowedValues;
let options;
@@ -305,7 +306,10 @@ export class DynamicFieldsService {
);
}
- return options?.pipe(map(options => !field.required && !this.isMultiSelectField(field) ? [{name: '-'}, ...options] : options));
+ return options?.pipe(
+ map(options => this.prependCurrentValue(options, currentValue)),
+ map(options => this.prependDefaultValue(options, field))
+ );
}
// ng-select needs a 'name' in order to show the label
@@ -396,6 +400,30 @@ export class DynamicFieldsService {
}
}
+ // Invalid values, ones that are not in the list of allowedValues (Array or backend fetched) do occur, e.g.
+ // if constraints change or in case a value is undisclosed as for a project's parent.
+ private prependCurrentValue(options:IOPAllowedValue[], currentValue:HalLink|null):IOPAllowedValue[] {
+ if (!currentValue?.href || options.some(option => option?._links?.self?.href === currentValue.href)) {
+ return options;
+ } else {
+ return [
+ { name: currentValue.title, _links: { self: currentValue } },
+ ...options
+ ];
+ }
+ }
+
+ // So select properties that are not required always get a default ('-'/'none') option.
+ // This way, the user can more easily deselect a value.
+ // Multi seleccts do not have the same behaviour since the x next to each option is quite clear.
+ private prependDefaultValue(options:IOPAllowedValue[], field:IOPFieldSchemaWithKey):IOPAllowedValue[] {
+ if (field.required || this.isMultiSelectField(field)) {
+ return options;
+ } else {
+ return [this.selectDefaultValue, ...options];
+ }
+ }
+
private isMultiSelectField(field:IOPFieldSchemaWithKey) {
return field?.type?.startsWith('[]');
}
diff --git a/lib/api/decorators/link_object.rb b/lib/api/decorators/link_object.rb
index 9ee71751592..14018517894 100644
--- a/lib/api/decorators/link_object.rb
+++ b/lib/api/decorators/link_object.rb
@@ -66,12 +66,16 @@ module API
end
def href=(value)
- if value
- id = ::API::Utilities::ResourceLinkParser.parse_id value,
+ # Ignore linked resources that are hidden to the client
+ # See lib/api/v3.rb for more details.
+ return if value == API::V3::URN_UNDISCLOSED
+
+ id = if value
+ ::API::Utilities::ResourceLinkParser.parse_id value,
property: @property_name,
expected_version: '3',
expected_namespace: @namespace
- end
+ end
represented.send(@setter, id)
end
diff --git a/lib/api/decorators/linked_resource.rb b/lib/api/decorators/linked_resource.rb
index c74deb07ed0..5128d7ff30f 100644
--- a/lib/api/decorators/linked_resource.rb
+++ b/lib/api/decorators/linked_resource.rb
@@ -115,12 +115,19 @@ module API
embedded: false)
end
+ # Includes _link and _embedded elements into the HAL representer for
+ # resources that are connected to the current resource via a belongs_to association, e.g.
+ # WorkPackage -> belongs_to -> project.
+ #
+ # @param skip_render [optional, Proc] If the proc returns true, neither _link nor _embedded of the resource will be rendered.
+ # @param undisclosed [optional, true, false] If true, instead of not rendering the resource upon `skip_render`, an { "href": "urn:openproject-org:api:v3:undisclosed" } link will be rendered. This can be used e.g. when the parent of a project is invisible to the user and the existence, if not the actual parent, is to be communicated. The resource is still not embedded in this case.
def associated_resource(name,
as: nil,
representer: nil,
v3_path: name,
skip_render: ->(*) { false },
skip_link: skip_render,
+ undisclosed: false,
link_title_attribute: :name,
uncacheable_link: false,
getter: associated_resource_default_getter(name, representer),
@@ -128,6 +135,7 @@ module API
link: associated_resource_default_link(name,
v3_path: v3_path,
skip_link: skip_link,
+ undisclosed: undisclosed,
title_attribute: link_title_attribute))
resource((as || name),
@@ -172,17 +180,23 @@ module API
v3_path:,
skip_link:,
title_attribute:,
- getter: :"#{name}_id")
+ getter: :"#{name}_id",
+ undisclosed: false)
->(*) do
- next if instance_exec(&skip_link)
-
- ::API::Decorators::LinkObject
- .new(represented,
- path: v3_path,
- property_name: name,
- title_attribute: title_attribute,
- getter: getter)
- .to_hash
+ if undisclosed && instance_exec(&skip_link)
+ {
+ href: API::V3::URN_UNDISCLOSED,
+ title: I18n.t(:"undisclosed.#{name}")
+ }
+ elsif !instance_exec(&skip_link)
+ ::API::Decorators::LinkObject
+ .new(represented,
+ path: v3_path,
+ property_name: name,
+ title_attribute: title_attribute,
+ getter: getter)
+ .to_hash
+ end
end
end
diff --git a/lib/api/v3.rb b/lib/api/v3.rb
new file mode 100644
index 00000000000..0e05116caf1
--- /dev/null
+++ b/lib/api/v3.rb
@@ -0,0 +1,46 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 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
+ URN_PREFIX = 'urn:openproject-org:api:v3:'.freeze
+ URN_ERROR_PREFIX = "#{URN_PREFIX}errors:".freeze
+ # For resources invisible to the user, a resource (including a payload) will contain
+ # an "undisclosed uri" instead of a url. This indicates the existence of a value
+ # without revealing anything. An example for this is the parent project which might be
+ # invisible to a user.
+ # In case a "undisclosed uri" is provided as a link, the current value is not
+ # to be altered and thus it is treated as if the value where never provided in
+ # the first place. This allows a schema/_embedded/payload -> client -> POST/PUT
+ # request/response round trip where the user knows of the existence of the value without revealing
+ # the contents. The payload remains valid in this case and the client can distinguish between
+ # keeping the value and unsetting the linked resource to null.
+ URN_UNDISCLOSED = "#{URN_PREFIX}undisclosed".freeze
+ end
+end
+
diff --git a/lib/api/v3/errors/error_representer.rb b/lib/api/v3/errors/error_representer.rb
index 8b1b1ad9086..7c3cf0dff4d 100644
--- a/lib/api/v3/errors/error_representer.rb
+++ b/lib/api/v3/errors/error_representer.rb
@@ -38,8 +38,6 @@ module API
include Roar::JSON::HAL
include Roar::Hypermedia
- ERROR_PREFIX = 'urn:openproject-org:api:v3:errors:'.freeze
-
self.as_strategy = API::Utilities::CamelCasingStrategy.new
property :_type, exec_context: :decorator
@@ -58,7 +56,7 @@ module API
end
def error_identifier
- ERROR_PREFIX + represented.class.identifier
+ ::API::V3::URN_ERROR_PREFIX + represented.class.identifier
end
end
end
diff --git a/lib/api/v3/projects/project_representer.rb b/lib/api/v3/projects/project_representer.rb
index 68cc5d1c0c0..fb93abbc3ac 100644
--- a/lib/api/v3/projects/project_representer.rb
+++ b/lib/api/v3/projects/project_representer.rb
@@ -157,19 +157,8 @@ module API
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
- }
+ undisclosed: true,
+ skip_render: ->(*) { !represented.parent&.visible? }
property :id
property :identifier,
diff --git a/lib/api/v3/queries/sort_bys/sort_by_decorator.rb b/lib/api/v3/queries/sort_bys/sort_by_decorator.rb
index a54099b7e7a..0476e34faea 100644
--- a/lib/api/v3/queries/sort_bys/sort_by_decorator.rb
+++ b/lib/api/v3/queries/sort_bys/sort_by_decorator.rb
@@ -65,7 +65,7 @@ module API
end
def direction_uri
- "urn:openproject-org:api:v3:queries:directions:#{direction}"
+ "#{API::V3::URN_PREFIX}queries:directions:#{direction}"
end
def direction_l10n
diff --git a/spec/features/projects/create_spec.rb b/spec/features/projects/create_spec.rb
new file mode 100644
index 00000000000..6793df7deda
--- /dev/null
+++ b/spec/features/projects/create_spec.rb
@@ -0,0 +1,127 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 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', 'creation', type: :feature, js: true do
+ let(:name_field) { ::FormFields::InputFormField.new :name }
+
+ current_user { FactoryBot.create(:admin) }
+
+ shared_let(:project) { FactoryBot.create(:project, name: 'Foo project', identifier: 'foo-project') }
+
+ before do
+ visit projects_path
+ end
+
+ it 'can create a project' do
+ click_on 'New project'
+
+ name_field.set_value 'Foo bar'
+ click_button 'Save'
+
+ sleep 1
+
+ expect(page).to have_content 'Foo bar'
+ expect(page).to have_current_path /\/projects\/foo-bar\/?/
+ end
+
+ it 'does not create a project with an already existing identifier' do
+ click_on 'New project'
+
+ name_field.set_value 'Foo project'
+ click_on 'Save'
+
+ expect(page).to have_current_path /\/projects\/foo-project-1\/?/
+
+ project = Project.last
+ expect(project.identifier).to eq 'foo-project-1'
+ end
+
+ context 'with a multi-select custom field' do
+ let!(:list_custom_field) { FactoryBot.create(:list_project_custom_field, name: 'List CF', multi_value: true) }
+ let(:list_field) { ::FormFields::SelectFormField.new list_custom_field }
+
+ it 'can create a project' do
+ click_on 'New project'
+
+ name_field.set_value 'Foo bar'
+
+ find('.op-fieldset--toggle', text: 'ADVANCED SETTINGS').click
+
+ list_field.select_option 'A', 'B'
+
+ click_button 'Save'
+
+ expect(page).to have_current_path /\/projects\/foo-bar\/?/
+ expect(page).to have_content 'Foo bar'
+
+ project = Project.last
+ expect(project.name).to eq 'Foo bar'
+ cvs = project.custom_value_for(list_custom_field)
+ expect(cvs.count).to eq 2
+ expect(cvs.map(&:typed_value)).to contain_exactly 'A', 'B'
+ end
+ end
+
+ it 'hides the active field and the identifier' do
+ visit new_project_path
+
+ find('.op-fieldset--toggle', text: 'ADVANCED SETTINGS').click
+
+ expect(page).to have_no_content 'Active'
+ expect(page).to have_no_content 'Identifier'
+ end
+
+ context 'with optional and required custom fields' do
+ let!(:optional_custom_field) do
+ FactoryBot.create(:custom_field, name: 'Optional Foo',
+ type: ProjectCustomField,
+ is_for_all: true)
+ end
+ let!(:required_custom_field) do
+ FactoryBot.create(:custom_field, name: 'Required Foo',
+ type: ProjectCustomField,
+ is_for_all: true,
+ is_required: true)
+ end
+
+ it 'seperates optional and required custom fields for new' do
+ visit new_project_path
+
+ expect(page).to have_content 'Required Foo'
+
+ click_on 'Advanced settings'
+
+ within('.op-fieldset') do
+ expect(page).to have_text 'Optional Foo'
+ expect(page).to have_no_text 'Required Foo'
+ end
+ end
+ end
+end
diff --git a/spec/features/projects/destroy_spec.rb b/spec/features/projects/destroy_spec.rb
index 5baa7d164f3..23e0746c633 100644
--- a/spec/features/projects/destroy_spec.rb
+++ b/spec/features/projects/destroy_spec.rb
@@ -32,25 +32,34 @@ describe 'Projects#destroy',
type: :feature,
js: true do
let!(:project) { FactoryBot.create(:project, name: 'foo', identifier: 'foo') }
- let(:user) { FactoryBot.create(:admin) }
- let(:projects_page) { Pages::Projects::Destroy.new(project) }
+ let(:project_page) { Pages::Projects::Destroy.new(project) }
let(:danger_zone) { DangerZone.new(page) }
- before do
- login_as user
+ current_user { FactoryBot.create(:admin) }
+ before do
# Disable background worker
allow(Delayed::Worker)
.to receive(:delay_jobs)
.and_return(false)
- expect(project)
-
- projects_page.visit!
+ project_page.visit!
end
- it 'can destroy the project' do
+ it 'destroys the project' do
# Confirm the deletion
+ # Without confirmation, the button is disabled
+ expect(danger_zone)
+ .to be_disabled
+
+ # With wrong confirmation, the button is disabled
+ danger_zone.confirm_with("#{project.identifier}_wrong")
+
+ expect(danger_zone)
+ .to be_disabled
+
+ # With correct confirmation, the button is enabled
+ # and the project can be deleted
danger_zone.confirm_with(project.identifier)
expect(danger_zone).not_to be_disabled
danger_zone.danger_button.click
diff --git a/spec/features/projects/edit_settings_spec.rb b/spec/features/projects/edit_settings_spec.rb
new file mode 100644
index 00000000000..2d4baa086cd
--- /dev/null
+++ b/spec/features/projects/edit_settings_spec.rb
@@ -0,0 +1,210 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 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', 'editing settings', type: :feature, js: true do
+ let(:name_field) { ::FormFields::InputFormField.new :name }
+ let(:parent_field) { ::FormFields::SelectFormField.new :parent }
+ let(:permissions) { %i(edit_project) }
+
+ current_user do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: permissions)
+ end
+
+ shared_let(:project) do
+ FactoryBot.create(:project, name: 'Foo project', identifier: 'foo-project')
+ end
+
+ it 'hides the field whose functionality is presented otherwise' do
+ visit settings_generic_project_path(project.id)
+
+ expect(page).to have_no_text :all, 'Active'
+ expect(page).to have_no_text :all, 'Identifier'
+ end
+
+ describe 'identifier edit' do
+ it 'updates the project identifier' do
+ visit projects_path
+ click_on project.name
+ SeleniumHubWaiter.wait
+ click_on 'Project settings'
+ SeleniumHubWaiter.wait
+ click_on 'Change identifier'
+
+ expect(page).to have_content "CHANGE THE PROJECT'S IDENTIFIER"
+ expect(current_path).to eq '/projects/foo-project/identifier'
+
+ fill_in 'project[identifier]', with: 'foo-bar'
+ click_on 'Update'
+
+ expect(page).to have_content 'Successful update.'
+ expect(current_path).to match '/projects/foo-bar/settings/generic'
+ expect(Project.first.identifier).to eq 'foo-bar'
+ end
+
+ it 'displays error messages on invalid input' do
+ visit identifier_project_path(project)
+
+ fill_in 'project[identifier]', with: 'FOOO'
+ click_on 'Update'
+
+ expect(page).to have_content 'Identifier is invalid.'
+ expect(current_path).to eq '/projects/foo-project/identifier'
+ end
+ end
+
+ context 'with optional and required custom fields' do
+ let!(:optional_custom_field) do
+ FactoryBot.create(:custom_field, name: 'Optional Foo',
+ type: ProjectCustomField,
+ is_for_all: true)
+ end
+ let!(:required_custom_field) do
+ FactoryBot.create(:custom_field, name: 'Required Foo',
+ type: ProjectCustomField,
+ is_for_all: true,
+ is_required: true)
+ end
+
+ it 'shows optional and required custom fields for edit without a separation' do
+ project.custom_field_values.last.value = 'FOO'
+ project.save!
+
+ visit settings_generic_project_path(project.id)
+
+ expect(page).to have_text 'Optional Foo'
+ expect(page).to have_text 'Required Foo'
+ end
+ end
+
+ context 'with a length restricted custom field' do
+ let!(:required_custom_field) do
+ FactoryBot.create(:string_project_custom_field,
+ name: 'Foo',
+ type: ProjectCustomField,
+ min_length: 1,
+ max_length: 2,
+ is_for_all: true)
+ end
+ let(:foo_field) { ::FormFields::InputFormField.new required_custom_field }
+
+ it 'shows the errors of that field when saving (Regression #33766)' do
+ visit settings_generic_project_path(project.id)
+
+ expect(page).to have_content 'Foo'
+
+ # Enter something too long
+ foo_field.set_value '1234'
+
+ # It should cut of that remaining value
+ foo_field.expect_value '12'
+
+ click_button 'Save'
+
+ expect(page).to have_text 'Successful update.'
+ end
+ end
+
+ context 'with a multi-select custom field' do
+ include_context 'ng-select-autocomplete helpers'
+
+ let!(:list_custom_field) { FactoryBot.create(:list_project_custom_field, name: 'List CF', multi_value: true) }
+ let(:form_field) { ::FormFields::SelectFormField.new list_custom_field }
+
+ it 'can select multiple values' do
+ visit settings_generic_project_path(project.id)
+
+ form_field.select_option 'A', 'B'
+
+ click_on 'Save'
+
+ expect(page).to have_content 'Successful update.'
+
+ form_field.expect_selected 'A', 'B'
+
+ cvs = project.reload.custom_value_for(list_custom_field)
+ expect(cvs.count).to eq 2
+ expect(cvs.map(&:typed_value)).to contain_exactly 'A', 'B'
+ end
+ end
+
+ context 'with a date custom field' do
+ let!(:date_custom_field) { FactoryBot.create(:date_project_custom_field, name: 'Date') }
+ let(:form_field) { ::FormFields::InputFormField.new date_custom_field }
+
+ it 'can save and remove the date (Regression #37459)' do
+ visit settings_generic_project_path(project.id)
+
+ form_field.set_value '2021-05-26'
+ form_field.send_keys :escape
+
+ click_on 'Save'
+
+ expect(page).to have_content 'Successful update.'
+
+ form_field.expect_value '2021-05-26'
+
+ cv = project.reload.custom_value_for(date_custom_field)
+ expect(cv.typed_value).to eq '2021-05-26'.to_date
+ end
+ end
+
+ context 'with a user not allowed to see the parent project' do
+ include_context 'ng-select-autocomplete helpers'
+
+ let(:parent_project) { FactoryBot.create(:project) }
+ let(:parent_field) { ::FormFields::SelectFormField.new 'parent' }
+
+ before do
+ project.update_attribute(:parent, parent_project)
+ end
+
+ it 'can update the project without destroying the relation to the parent' do
+ visit settings_generic_project_path(project.id)
+
+ fill_in 'Name', with: 'New project name'
+
+ parent_field.expect_selected I18n.t(:'undisclosed.parent')
+
+ click_on 'Save'
+
+ expect(page).to have_content 'Successful update.'
+
+ project.reload
+
+ expect(project.name)
+ .to eql 'New project name'
+
+ expect(project.parent)
+ .to eql parent_project
+ end
+ end
+end
diff --git a/spec/features/projects/projects_spec.rb b/spec/features/projects/projects_spec.rb
deleted file mode 100644
index 03c727f1916..00000000000
--- a/spec/features/projects/projects_spec.rb
+++ /dev/null
@@ -1,339 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2021 the OpenProject GmbH
-#
-# 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-2013 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', type: :feature, js: true do
- let(:current_user) { FactoryBot.create(:admin) }
- let(:name_field) { ::FormFields::InputFormField.new :name }
- let(:parent_field) { ::FormFields::SelectFormField.new :parent }
-
- before do
- allow(User).to receive(:current).and_return current_user
- end
-
- describe 'creation' do
- shared_let(:project) { FactoryBot.create(:project, name: 'Foo project', identifier: 'foo-project') }
-
- before do
- visit projects_path
- end
-
- it 'can create a project' do
- click_on 'New project'
-
- name_field.set_value 'Foo bar'
- click_button 'Save'
-
- sleep 1
-
- expect(page).to have_content 'Foo bar'
- expect(page).to have_current_path /\/projects\/foo-bar\/?/
- end
-
- it 'can create a subproject' do
- click_on project.name
- SeleniumHubWaiter.wait
- click_on 'Project settings'
- SeleniumHubWaiter.wait
- click_on 'New subproject'
-
- name_field.set_value 'Foo child'
-
- sleep 1
-
- parent_field.expect_selected project.name
-
- click_button 'Save'
-
- sleep 1
-
- expect(page).to have_current_path /\/projects\/foo-child\/?/
-
- child = Project.last
- expect(child.identifier).to eq 'foo-child'
- expect(child.parent).to eq project
- end
-
- it 'does not create a project with an already existing identifier' do
- click_on 'New project'
-
- name_field.set_value 'Foo project'
- click_on 'Save'
-
- expect(page).to have_current_path /\/projects\/foo-project-1\/?/
-
- project = Project.last
- expect(project.identifier).to eq 'foo-project-1'
- end
-
- context 'with a multi-select custom field' do
- let!(:list_custom_field) { FactoryBot.create(:list_project_custom_field, name: 'List CF', multi_value: true) }
- let(:list_field) { ::FormFields::SelectFormField.new list_custom_field }
-
- it 'can create a project' do
- click_on 'New project'
-
- name_field.set_value 'Foo bar'
-
- find('.op-fieldset--toggle', text: 'ADVANCED SETTINGS').click
-
- list_field.select_option 'A', 'B'
-
- click_button 'Save'
-
- expect(page).to have_current_path /\/projects\/foo-bar\/?/
- expect(page).to have_content 'Foo bar'
-
- project = Project.last
- expect(project.name).to eq 'Foo bar'
- cvs = project.custom_value_for(list_custom_field)
- expect(cvs.count).to eq 2
- expect(cvs.map(&:typed_value)).to contain_exactly 'A', 'B'
- end
- end
- end
-
- describe 'project types' do
- let(:phase_type) { FactoryBot.create(:type, name: 'Phase', is_default: true) }
- let(:milestone_type) { FactoryBot.create(:type, name: 'Milestone', is_default: false) }
- let!(:project) { FactoryBot.create(:project, name: 'Foo project', types: [phase_type, milestone_type]) }
-
- it "have the correct types checked for the project's types" do
- visit projects_path
- click_on 'Foo project'
- click_on 'Project settings'
- click_on 'Work package types'
-
- field_checked = find_field('Phase', visible: false)['checked']
- expect(field_checked).to be_truthy
- field_checked = find_field('Milestone', visible: false)['checked']
- expect(field_checked).to be_truthy
- end
- end
-
- describe 'deletion' do
- let(:project) { FactoryBot.create(:project) }
- let(:projects_page) { Pages::Projects::Destroy.new(project) }
-
- before do
- projects_page.visit!
- end
-
- describe 'disable delete w/o confirm' do
- it { expect(page).to have_css('.danger-zone .button[disabled]') }
- end
-
- describe 'disable delete with wrong input' do
- let(:input) { find('.danger-zone input') }
- it do
- input.set 'Not the project name'
- expect(page).to have_css('.danger-zone .button[disabled]')
- end
- end
-
- describe 'enable delete with correct input' do
- let(:input) { find('.danger-zone input') }
- it do
- input.set project.name
- expect(page).to have_css('.danger-zone .button:not([disabled])')
- end
- end
- end
-
- describe 'identifier edit' do
- let!(:project) { FactoryBot.create(:project, identifier: 'foo') }
-
- it 'updates the project identifier' do
- visit projects_path
- click_on project.name
- SeleniumHubWaiter.wait
- click_on 'Project settings'
- SeleniumHubWaiter.wait
- click_on 'Change identifier'
-
- expect(page).to have_content "CHANGE THE PROJECT'S IDENTIFIER"
- expect(current_path).to eq '/projects/foo/identifier'
-
- fill_in 'project[identifier]', with: 'foo-bar'
- click_on 'Update'
-
- expect(page).to have_content 'Successful update.'
- expect(current_path).to match '/projects/foo-bar/settings/generic'
- expect(Project.first.identifier).to eq 'foo-bar'
- end
-
- it 'displays error messages on invalid input' do
- visit identifier_project_path(project)
-
- fill_in 'project[identifier]', with: 'FOOO'
- click_on 'Update'
-
- expect(page).to have_content 'Identifier is invalid.'
- expect(current_path).to eq '/projects/foo/identifier'
- end
- end
-
- describe 'form' do
- let(:project) { FactoryBot.build(:project, name: 'Foo project', identifier: 'foo-project') }
-
- context 'when creating' do
- it 'hides the active field and the identifier' do
- visit new_project_path
-
- find('.op-fieldset--toggle', text: 'ADVANCED SETTINGS').click
-
- expect(page).to have_no_content 'Active'
- expect(page).to have_no_content 'Identifier'
- end
- end
-
- context 'when editing' do
- it 'hides the active field' do
- project.save!
-
- visit settings_generic_project_path(project.id)
-
- expect(page).to have_no_text :all, 'Active'
- expect(page).to have_no_text :all, 'Identifier'
- end
- end
-
- context 'with optional and required custom fields' do
- let!(:optional_custom_field) do
- FactoryBot.create(:custom_field, name: 'Optional Foo',
- type: ProjectCustomField,
- is_for_all: true)
- end
- let!(:required_custom_field) do
- FactoryBot.create(:custom_field, name: 'Required Foo',
- type: ProjectCustomField,
- is_for_all: true,
- is_required: true)
- end
-
- it 'seperates optional and required custom fields for new' do
- visit new_project_path
-
- expect(page).to have_content 'Required Foo'
-
- click_on 'Advanced settings'
-
- within('.op-fieldset') do
- expect(page).to have_text 'Optional Foo'
- expect(page).to have_no_text 'Required Foo'
- end
- end
-
- it 'shows optional and required custom fields for edit without a separation' do
- project.custom_field_values.last.value = 'FOO'
- project.save!
-
- visit settings_generic_project_path(project.id)
-
- expect(page).to have_text 'Optional Foo'
- expect(page).to have_text 'Required Foo'
- end
- end
-
- context 'with a length restricted custom field' do
- let(:project) { FactoryBot.create(:project, name: 'Foo project', identifier: 'foo-project') }
- let!(:required_custom_field) do
- FactoryBot.create(:string_project_custom_field,
- name: 'Foo',
- type: ProjectCustomField,
- min_length: 1,
- max_length: 2,
- is_for_all: true)
- end
- let(:foo_field) { ::FormFields::InputFormField.new required_custom_field }
-
- it 'shows the errors of that field when saving (Regression #33766)' do
- visit settings_generic_project_path(project.id)
-
- expect(page).to have_content 'Foo'
-
- # Enter something too long
- foo_field.set_value '1234'
-
- # It should cut of that remaining value
- foo_field.expect_value '12'
-
- click_button 'Save'
-
- expect(page).to have_text 'Successful update.'
- end
- end
- end
-
- context 'with a multi-select custom field' do
- include_context 'ng-select-autocomplete helpers'
-
- let(:project) { FactoryBot.create(:project, name: 'Foo project', identifier: 'foo-project') }
- let!(:list_custom_field) { FactoryBot.create(:list_project_custom_field, name: 'List CF', multi_value: true) }
- let(:form_field) { ::FormFields::SelectFormField.new list_custom_field }
-
- it 'can create a project' do
- visit settings_generic_project_path(project.id)
-
- form_field.select_option 'A', 'B'
-
- click_on 'Save'
-
- expect(page).to have_content 'Successful update.'
-
- form_field.expect_selected 'A', 'B'
-
- cvs = project.reload.custom_value_for(list_custom_field)
- expect(cvs.count).to eq 2
- expect(cvs.map(&:typed_value)).to contain_exactly 'A', 'B'
- end
- end
-
- context 'with a date custom field' do
- let(:project) { FactoryBot.create(:project, name: 'Foo project', identifier: 'foo-project') }
- let!(:date_custom_field) { FactoryBot.create(:date_project_custom_field, name: 'Date') }
- let(:form_field) { ::FormFields::InputFormField.new date_custom_field }
-
- it 'can save and remove the date (Regression #37459)' do
- visit settings_generic_project_path(project.id)
-
- form_field.set_value '2021-05-26'
- form_field.send_keys :escape
-
- click_on 'Save'
-
- expect(page).to have_content 'Successful update.'
-
- form_field.expect_value '2021-05-26'
-
- cv = project.reload.custom_value_for(date_custom_field)
- expect(cv.typed_value).to eq '2021-05-26'.to_date
- end
- end
-end
diff --git a/spec/features/projects/work_package_type_mgmt_spec.rb b/spec/features/projects/work_package_type_mgmt_spec.rb
new file mode 100644
index 00000000000..675ab354917
--- /dev/null
+++ b/spec/features/projects/work_package_type_mgmt_spec.rb
@@ -0,0 +1,49 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 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', 'work package type mgmt', type: :feature, js: true do
+ current_user { FactoryBot.create(:admin) }
+
+ let(:phase_type) { FactoryBot.create(:type, name: 'Phase', is_default: true) }
+ let(:milestone_type) { FactoryBot.create(:type, name: 'Milestone', is_default: false) }
+ let!(:project) { FactoryBot.create(:project, name: 'Foo project', types: [phase_type, milestone_type]) }
+
+ it "have the correct types checked for the project's types" do
+ visit projects_path
+ click_on 'Foo project'
+ click_on 'Project settings'
+ click_on 'Work package types'
+
+ field_checked = find_field('Phase', visible: false)['checked']
+ expect(field_checked).to be_truthy
+ field_checked = find_field('Milestone', visible: false)['checked']
+ expect(field_checked).to be_truthy
+ end
+end
diff --git a/spec/lib/api/v3/projects/project_payload_representer_parsing_spec.rb b/spec/lib/api/v3/projects/project_payload_representer_parsing_spec.rb
index 00ac82694bd..95c7bfb4c07 100644
--- a/spec/lib/api/v3/projects/project_payload_representer_parsing_spec.rb
+++ b/spec/lib/api/v3/projects/project_payload_representer_parsing_spec.rb
@@ -105,7 +105,7 @@ describe ::API::V3::Projects::ProjectPayloadRepresenter, 'parsing' do
end
end
- context 'with null for a scope' do
+ context 'with null for a status' do
let(:hash) do
{
'_links' => {
@@ -135,4 +135,68 @@ describe ::API::V3::Projects::ProjectPayloadRepresenter, 'parsing' do
end
end
end
+
+ describe '_links' do
+ context 'with a parent link' do
+ context 'with the href being an url' do
+ let(:hash) do
+ {
+ '_links' => {
+ 'parent' => {
+ 'href' => api_v3_paths.project(5)
+ }
+ }
+ }
+ end
+
+ it 'sets the parent_id to the value' do
+ project = representer.from_hash(hash).to_h
+
+ expect(project[:parent_id])
+ .to eq "5"
+ end
+ end
+
+ context 'with the href being nil' do
+ let(:hash) do
+ {
+ '_links' => {
+ 'parent' => {
+ 'href' => nil
+ }
+ }
+ }
+ end
+
+ it 'sets the parent_id to nil' do
+ project = representer.from_hash(hash).to_h
+
+ expect(project)
+ .to have_key(:parent_id)
+
+ expect(project[:parent_id])
+ .to eq nil
+ end
+ end
+
+ context 'with the href being the hidden uri' do
+ let(:hash) do
+ {
+ '_links' => {
+ 'parent' => {
+ 'href' => API::V3::URN_UNDISCLOSED
+ }
+ }
+ }
+ end
+
+ it 'omits the parent information' do
+ project = representer.from_hash(hash).to_h
+
+ expect(project)
+ .not_to have_key(:parent_id)
+ end
+ end
+ end
+ end
end
diff --git a/spec/lib/api/v3/projects/project_representer_rendering_spec.rb b/spec/lib/api/v3/projects/project_representer_rendering_spec.rb
new file mode 100644
index 00000000000..08bcd692949
--- /dev/null
+++ b/spec/lib/api/v3/projects/project_representer_rendering_spec.rb
@@ -0,0 +1,510 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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-2013 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 ::API::V3::Projects::ProjectRepresenter, 'rendering' do
+ include ::API::V3::Utilities::PathHelper
+
+ subject(:generated) { representer.to_json }
+
+ let(:project) do
+ FactoryBot.build_stubbed(:project,
+ parent: parent_project,
+ description: 'some description',
+ status: status).tap do |p|
+ allow(p)
+ .to receive(:available_custom_fields)
+ .and_return([int_custom_field, version_custom_field])
+
+ allow(p)
+ .to receive(:"custom_field_#{int_custom_field.id}")
+ .and_return(int_custom_value.value)
+
+ allow(p)
+ .to receive(:custom_value_for)
+ .with(version_custom_field)
+ .and_return(version_custom_value)
+ end
+ end
+ let(:status) do
+ FactoryBot.build_stubbed(:project_status)
+ end
+ let(:parent_project) { FactoryBot.build_stubbed(:project) }
+ let(:representer) { described_class.create(project, current_user: user, embed_links: true) }
+
+ let(:user) do
+ FactoryBot.build_stubbed(:user).tap do |u|
+ allow(u)
+ .to receive(:allowed_to?) do |permission, context|
+ permissions.include?(permission) && context == project
+ end
+ end
+ end
+
+ let(:int_custom_field) { FactoryBot.build_stubbed(:int_project_custom_field, visible: false) }
+ let(:version_custom_field) { FactoryBot.build_stubbed(:version_project_custom_field, visible: true) }
+ let(:int_custom_value) do
+ CustomValue.new(custom_field: int_custom_field,
+ value: '1234',
+ customized: nil)
+ end
+ let(:version) { FactoryBot.build_stubbed(:version) }
+ let(:version_custom_value) do
+ CustomValue.new(custom_field: version_custom_field,
+ value: version.id,
+ customized: nil).tap do |cv|
+ allow(cv)
+ .to receive(:typed_value)
+ .and_return(version)
+ end
+ end
+
+ let(:permissions) { %i[add_work_packages view_members] }
+
+ it { is_expected.to include_json('Project'.to_json).at_path('_type') }
+
+ describe 'properties' do
+ it_behaves_like 'property', :_type do
+ let(:value) { 'Project' }
+ end
+
+ it_behaves_like 'property', :id do
+ let(:value) { project.id }
+ end
+
+ it_behaves_like 'property', :identifier do
+ let(:value) { project.identifier }
+ end
+
+ it_behaves_like 'property', :name do
+ let(:value) { project.name }
+ end
+
+ it_behaves_like 'property', :active do
+ let(:value) { project.active }
+ end
+
+ it_behaves_like 'property', :public do
+ let(:value) { project.public }
+ end
+
+ it_behaves_like 'formattable property', :description do
+ let(:value) { project.description }
+ end
+
+ context 'statusExplanation' do
+ it_behaves_like 'formattable property', 'statusExplanation' do
+ let(:value) { status.explanation }
+ end
+ end
+
+ it_behaves_like 'has UTC ISO 8601 date and time' do
+ let(:date) { project.created_at }
+ let(:json_path) { 'createdAt' }
+ end
+
+ it_behaves_like 'has UTC ISO 8601 date and time' do
+ let(:date) { project.updated_at }
+ let(:json_path) { 'updatedAt' }
+ end
+
+ context 'int custom field' do
+ context 'if the user is admin' do
+ before do
+ allow(user)
+ .to receive(:admin?)
+ .and_return(true)
+ end
+
+ it "has a property for the int custom field" do
+ is_expected
+ .to be_json_eql(int_custom_value.value.to_json)
+ .at_path("customField#{int_custom_field.id}")
+ end
+ end
+
+ context 'if the user is no admin' do
+ it "has no property for the int custom field" do
+ is_expected
+ .not_to have_json_path("customField#{int_custom_field.id}")
+ end
+ end
+ end
+ end
+
+ describe '_links' do
+ it { is_expected.to have_json_type(Object).at_path('_links') }
+
+ it 'links to self' do
+ expect(subject).to have_json_path('_links/self/href')
+ end
+ it 'has a title for link to self' do
+ expect(subject).to have_json_path('_links/self/title')
+ end
+
+ describe 'create work packages' do
+ context 'user allowed to create work packages' do
+ it 'has the correct path for a create form' do
+ is_expected
+ .to be_json_eql(api_v3_paths.create_project_work_package_form(project.id).to_json)
+ .at_path('_links/createWorkPackage/href')
+ end
+
+ it 'has the correct path to create a work package' do
+ is_expected.to be_json_eql(api_v3_paths.work_packages_by_project(project.id).to_json)
+ .at_path('_links/createWorkPackageImmediately/href')
+ end
+ end
+
+ context 'user not allowed to create work packages' do
+ let(:permissions) { [] }
+
+ it_behaves_like 'has no link' do
+ let(:link) { 'createWorkPackage' }
+ end
+
+ it_behaves_like 'has no link' do
+ let(:link) { 'createWorkPackageImmediately' }
+ end
+ end
+ end
+
+ 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) { API::V3::URN_UNDISCLOSED }
+ let(:title) { I18n.t(:'undisclosed.parent') }
+ end
+ end
+ end
+
+ context 'status' do
+ it_behaves_like 'has a titled link' do
+ let(:link) { 'status' }
+ let(:href) { api_v3_paths.project_status(project.status.code) }
+ let(:title) { I18n.t(:"activerecord.attributes.projects/status.codes.#{project.status.code}") }
+ end
+
+ context 'if the status is nil' do
+ let(:status) { nil }
+
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'status' }
+ let(:href) { nil }
+ end
+ end
+ end
+
+ describe 'categories' do
+ it 'has the correct link to its categories' do
+ is_expected.to be_json_eql(api_v3_paths.categories_by_project(project.id).to_json)
+ .at_path('_links/categories/href')
+ end
+ end
+
+ describe 'versions' do
+ context 'with only manage_versions permission' do
+ let(:permissions) { [:manage_versions] }
+
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'versions' }
+ let(:href) { api_v3_paths.versions_by_project(project.id) }
+ end
+ end
+
+ context 'with only view_work_packages permission' do
+ let(:permissions) { [:view_work_packages] }
+
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'versions' }
+ let(:href) { api_v3_paths.versions_by_project(project.id) }
+ end
+ end
+
+ context 'without both permissions' do
+ let(:permissions) { [:add_work_packages] }
+
+ it_behaves_like 'has no link' do
+ let(:link) { 'versions' }
+ end
+ end
+ end
+
+ describe 'types' do
+ context 'for a user having the view_work_packages permission' do
+ let(:permissions) { [:view_work_packages] }
+
+ it 'links to the types active in the project' do
+ is_expected.to be_json_eql(api_v3_paths.types_by_project(project.id).to_json)
+ .at_path('_links/types/href')
+ end
+
+ it 'links to the work packages in the project' do
+ is_expected.to be_json_eql(api_v3_paths.work_packages_by_project(project.id).to_json)
+ .at_path('_links/workPackages/href')
+ end
+ end
+
+ context 'for a user having the manage_types permission' do
+ let(:permissions) { [:manage_types] }
+
+ it 'links to the types active in the project' do
+ is_expected.to be_json_eql(api_v3_paths.types_by_project(project.id).to_json)
+ .at_path('_links/types/href')
+ end
+ end
+
+ context 'for a user not having the necessary permissions' do
+ let(:permission) { [] }
+
+ it 'has no types link' do
+ is_expected.to_not have_json_path('_links/types/href')
+ end
+
+ it 'has no work packages link' do
+ is_expected.to_not have_json_path('_links/workPackages/href')
+ end
+ end
+ end
+
+ describe 'memberships' do
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'memberships' }
+ let(:href) { api_v3_paths.path_for(:memberships, filters: [{ project: { operator: "=", values: [project.id.to_s] } }]) }
+ end
+
+ context 'without the view_members permission' do
+ let(:permissions) { [] }
+
+ it_behaves_like 'has no link' do
+ let(:link) { 'memberships' }
+ end
+ end
+ end
+
+ context 'link custom field' do
+ context 'if the user is admin and the field is invisible' do
+ before do
+ allow(user)
+ .to receive(:admin?)
+ .and_return(true)
+
+ version_custom_field.visible = false
+ end
+
+ it 'links custom fields' do
+ is_expected
+ .to be_json_eql(api_v3_paths.version(version.id).to_json)
+ .at_path("_links/customField#{version_custom_field.id}/href")
+ end
+ end
+
+ context 'if the user is no admin and the field is invisible' do
+ before do
+ version_custom_field.visible = false
+ end
+
+ it "has no property for the int custom field" do
+ is_expected
+ .not_to have_json_path("links/customField#{version_custom_field.id}")
+ end
+ end
+
+ context 'if the user is no admin and the field is visible' do
+ it 'links custom fields' do
+ is_expected
+ .to be_json_eql(api_v3_paths.version(version.id).to_json)
+ .at_path("_links/customField#{version_custom_field.id}/href")
+ end
+ end
+ end
+
+ describe 'update' do
+ context 'for a user having the edit_project permission' do
+ let(:permissions) { [:edit_project] }
+
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'update' }
+ let(:href) { api_v3_paths.project_form project.id }
+ end
+ end
+
+ context 'for a user lacking the edit_project permission' do
+ let(:permissions) { [] }
+
+ it_behaves_like 'has no link' do
+ let(:link) { 'update' }
+ end
+ end
+ end
+
+ describe 'updateImmediately' do
+ context 'for a user having the edit_project permission' do
+ let(:permissions) { [:edit_project] }
+
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'updateImmediately' }
+ let(:href) { api_v3_paths.project project.id }
+ end
+ end
+
+ context 'for a user lacking the edit_project permission' do
+ let(:permissions) { [] }
+
+ it_behaves_like 'has no link' do
+ let(:link) { 'updateImmediately' }
+ end
+ end
+ end
+
+ describe 'delete' do
+ context 'for a user being admin' do
+ before do
+ allow(user)
+ .to receive(:admin?)
+ .and_return(true)
+ end
+
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'delete' }
+ let(:href) { api_v3_paths.project project.id }
+ end
+ end
+
+ context 'for a non admin user' do
+ let(:permissions) { [] }
+
+ it_behaves_like 'has no link' do
+ let(:link) { 'delete' }
+ end
+ end
+ end
+ end
+
+ describe '_embedded' do
+ describe 'parent' do
+ let(:embedded_path) { '_embedded/parent' }
+
+ before do
+ allow(parent_project)
+ .to receive(:visible?)
+ .and_return(parent_visible)
+ end
+
+ context 'when the user is allowed to see the parent' do
+ let(:parent_visible) { true }
+
+ it 'has the parent embedded' do
+ expect(generated)
+ .to be_json_eql('Project'.to_json)
+ .at_path("#{embedded_path}/_type")
+
+ expect(generated)
+ .to be_json_eql(parent_project.name.to_json)
+ .at_path("#{embedded_path}/name")
+ end
+ end
+
+ context 'when the user is forbidden to see the parent' do
+ let(:parent_visible) { false }
+
+ it 'hides the parent' do
+ expect(generated)
+ .not_to have_json_path(embedded_path)
+ end
+ end
+ end
+ end
+
+ describe 'caching' do
+ it 'is based on the representer\'s cache_key' do
+ allow(OpenProject::Cache)
+ .to receive(:fetch)
+ .and_call_original
+
+ representer.to_json
+
+ expect(OpenProject::Cache)
+ .to have_received(:fetch)
+ .with(representer.json_cache_key)
+ end
+
+ describe '#json_cache_key' do
+ let!(:former_cache_key) { representer.json_cache_key }
+
+ it 'includes the name of the representer class' do
+ expect(representer.json_cache_key)
+ .to include('API', 'V3', 'Projects', 'ProjectRepresenter')
+ end
+
+ it 'changes when the locale changes' do
+ I18n.with_locale(:fr) do
+ expect(representer.json_cache_key)
+ .not_to eql former_cache_key
+ end
+ end
+
+ it 'changes when the project is updated' do
+ project.updated_at = Time.now + 20.seconds
+
+ expect(representer.json_cache_key)
+ .not_to eql former_cache_key
+ end
+
+ it 'changes when the project status is updated' do
+ project.status.updated_at = Time.now + 20.seconds
+
+ expect(representer.json_cache_key)
+ .not_to eql former_cache_key
+ end
+ end
+ end
+
+ describe '.checked_permissions' do
+ it 'lists add_work_packages' do
+ expect(described_class.checked_permissions).to match_array([:add_work_packages])
+ end
+ end
+end
diff --git a/spec/lib/api/v3/projects/project_representer_spec.rb b/spec/lib/api/v3/projects/project_representer_spec.rb
deleted file mode 100644
index 90ee5d106b8..00000000000
--- a/spec/lib/api/v3/projects/project_representer_spec.rb
+++ /dev/null
@@ -1,474 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2021 the OpenProject GmbH
-#
-# 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-2013 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 ::API::V3::Projects::ProjectRepresenter do
- include ::API::V3::Utilities::PathHelper
-
- let(:project) do
- FactoryBot.build_stubbed(:project,
- parent: parent_project,
- description: 'some description',
- status: status).tap do |p|
- allow(p)
- .to receive(:available_custom_fields)
- .and_return([int_custom_field, version_custom_field])
-
- allow(p)
- .to receive(:"custom_field_#{int_custom_field.id}")
- .and_return(int_custom_value.value)
-
- allow(p)
- .to receive(:custom_value_for)
- .with(version_custom_field)
- .and_return(version_custom_value)
- end
- end
- let(:status) do
- FactoryBot.build_stubbed(:project_status)
- end
- let(:parent_project) { FactoryBot.build_stubbed(:project) }
- let(:representer) { described_class.create(project, current_user: user) }
-
- let(:user) do
- FactoryBot.build_stubbed(:user).tap do |u|
- allow(u)
- .to receive(:allowed_to?) do |permission, context|
- permissions.include?(permission) && context == project
- end
- end
- end
-
- let(:int_custom_field) { FactoryBot.build_stubbed(:int_project_custom_field, visible: false) }
- let(:version_custom_field) { FactoryBot.build_stubbed(:version_project_custom_field, visible: true) }
- let(:int_custom_value) do
- CustomValue.new(custom_field: int_custom_field,
- value: '1234',
- customized: nil)
- end
- let(:version) { FactoryBot.build_stubbed(:version) }
- let(:version_custom_value) do
- CustomValue.new(custom_field: version_custom_field,
- value: version.id,
- customized: nil).tap do |cv|
- allow(cv)
- .to receive(:typed_value)
- .and_return(version)
- end
- end
-
- let(:permissions) { %i[add_work_packages view_members] }
-
- context 'generation' do
- subject(:generated) { representer.to_json }
-
- it { is_expected.to include_json('Project'.to_json).at_path('_type') }
-
- describe 'properties' do
- it_behaves_like 'property', :_type do
- let(:value) { 'Project' }
- end
-
- it_behaves_like 'property', :id do
- let(:value) { project.id }
- end
-
- it_behaves_like 'property', :identifier do
- let(:value) { project.identifier }
- end
-
- it_behaves_like 'property', :name do
- let(:value) { project.name }
- end
-
- it_behaves_like 'property', :active do
- let(:value) { project.active }
- end
-
- it_behaves_like 'property', :public do
- let(:value) { project.public }
- end
-
- it_behaves_like 'formattable property', :description do
- let(:value) { project.description }
- end
-
- context 'statusExplanation' do
- it_behaves_like 'formattable property', 'statusExplanation' do
- let(:value) { status.explanation }
- end
- end
-
- it_behaves_like 'has UTC ISO 8601 date and time' do
- let(:date) { project.created_at }
- let(:json_path) { 'createdAt' }
- end
-
- it_behaves_like 'has UTC ISO 8601 date and time' do
- let(:date) { project.updated_at }
- let(:json_path) { 'updatedAt' }
- end
-
- context 'int custom field' do
- context 'if the user is admin' do
- before do
- allow(user)
- .to receive(:admin?)
- .and_return(true)
- end
-
- it "has a property for the int custom field" do
- is_expected
- .to be_json_eql(int_custom_value.value.to_json)
- .at_path("customField#{int_custom_field.id}")
- end
- end
-
- context 'if the user is no admin' do
- it "has no property for the int custom field" do
- is_expected
- .not_to have_json_path("customField#{int_custom_field.id}")
- end
- end
- end
- end
-
- describe '_links' do
- it { is_expected.to have_json_type(Object).at_path('_links') }
-
- it 'links to self' do
- expect(subject).to have_json_path('_links/self/href')
- end
- it 'has a title for link to self' do
- expect(subject).to have_json_path('_links/self/title')
- end
-
- describe 'create work packages' do
- context 'user allowed to create work packages' do
- it 'has the correct path for a create form' do
- is_expected
- .to be_json_eql(api_v3_paths.create_project_work_package_form(project.id).to_json)
- .at_path('_links/createWorkPackage/href')
- end
-
- it 'has the correct path to create a work package' do
- is_expected.to be_json_eql(api_v3_paths.work_packages_by_project(project.id).to_json)
- .at_path('_links/createWorkPackageImmediately/href')
- end
- end
-
- context 'user not allowed to create work packages' do
- let(:permissions) { [] }
-
- it_behaves_like 'has no link' do
- let(:link) { 'createWorkPackage' }
- end
-
- it_behaves_like 'has no link' do
- let(:link) { 'createWorkPackageImmediately' }
- end
- end
- end
-
- 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
-
- context 'status' do
- it_behaves_like 'has a titled link' do
- let(:link) { 'status' }
- let(:href) { api_v3_paths.project_status(project.status.code) }
- let(:title) { I18n.t(:"activerecord.attributes.projects/status.codes.#{project.status.code}") }
- end
-
- context 'if the status is nil' do
- let(:status) { nil }
-
- it_behaves_like 'has an untitled link' do
- let(:link) { 'status' }
- let(:href) { nil }
- end
- end
- end
-
- describe 'categories' do
- it 'has the correct link to its categories' do
- is_expected.to be_json_eql(api_v3_paths.categories_by_project(project.id).to_json)
- .at_path('_links/categories/href')
- end
- end
-
- describe 'versions' do
- context 'with only manage_versions permission' do
- let(:permissions) { [:manage_versions] }
-
- it_behaves_like 'has an untitled link' do
- let(:link) { 'versions' }
- let(:href) { api_v3_paths.versions_by_project(project.id) }
- end
- end
-
- context 'with only view_work_packages permission' do
- let(:permissions) { [:view_work_packages] }
-
- it_behaves_like 'has an untitled link' do
- let(:link) { 'versions' }
- let(:href) { api_v3_paths.versions_by_project(project.id) }
- end
- end
-
- context 'without both permissions' do
- let(:permissions) { [:add_work_packages] }
-
- it_behaves_like 'has no link' do
- let(:link) { 'versions' }
- end
- end
- end
-
- describe 'types' do
- context 'for a user having the view_work_packages permission' do
- let(:permissions) { [:view_work_packages] }
-
- it 'links to the types active in the project' do
- is_expected.to be_json_eql(api_v3_paths.types_by_project(project.id).to_json)
- .at_path('_links/types/href')
- end
-
- it 'links to the work packages in the project' do
- is_expected.to be_json_eql(api_v3_paths.work_packages_by_project(project.id).to_json)
- .at_path('_links/workPackages/href')
- end
- end
-
- context 'for a user having the manage_types permission' do
- let(:permissions) { [:manage_types] }
-
- it 'links to the types active in the project' do
- is_expected.to be_json_eql(api_v3_paths.types_by_project(project.id).to_json)
- .at_path('_links/types/href')
- end
- end
-
- context 'for a user not having the necessary permissions' do
- let(:permission) { [] }
-
- it 'has no types link' do
- is_expected.to_not have_json_path('_links/types/href')
- end
-
- it 'has no work packages link' do
- is_expected.to_not have_json_path('_links/workPackages/href')
- end
- end
- end
-
- describe 'memberships' do
- it_behaves_like 'has an untitled link' do
- let(:link) { 'memberships' }
- let(:href) { api_v3_paths.path_for(:memberships, filters: [{ project: { operator: "=", values: [project.id.to_s] } }]) }
- end
-
- context 'without the view_members permission' do
- let(:permissions) { [] }
-
- it_behaves_like 'has no link' do
- let(:link) { 'memberships' }
- end
- end
- end
-
- context 'link custom field' do
- context 'if the user is admin and the field is invisible' do
- before do
- allow(user)
- .to receive(:admin?)
- .and_return(true)
-
- version_custom_field.visible = false
- end
-
- it 'links custom fields' do
- is_expected
- .to be_json_eql(api_v3_paths.version(version.id).to_json)
- .at_path("_links/customField#{version_custom_field.id}/href")
- end
- end
-
- context 'if the user is no admin and the field is invisible' do
- before do
- version_custom_field.visible = false
- end
-
- it "has no property for the int custom field" do
- is_expected
- .not_to have_json_path("links/customField#{version_custom_field.id}")
- end
- end
-
- context 'if the user is no admin and the field is visible' do
- it 'links custom fields' do
- is_expected
- .to be_json_eql(api_v3_paths.version(version.id).to_json)
- .at_path("_links/customField#{version_custom_field.id}/href")
- end
- end
- end
-
- describe 'update' do
- context 'for a user having the edit_project permission' do
- let(:permissions) { [:edit_project] }
-
- it_behaves_like 'has an untitled link' do
- let(:link) { 'update' }
- let(:href) { api_v3_paths.project_form project.id }
- end
- end
-
- context 'for a user lacking the edit_project permission' do
- let(:permissions) { [] }
-
- it_behaves_like 'has no link' do
- let(:link) { 'update' }
- end
- end
- end
-
- describe 'updateImmediately' do
- context 'for a user having the edit_project permission' do
- let(:permissions) { [:edit_project] }
-
- it_behaves_like 'has an untitled link' do
- let(:link) { 'updateImmediately' }
- let(:href) { api_v3_paths.project project.id }
- end
- end
-
- context 'for a user lacking the edit_project permission' do
- let(:permissions) { [] }
-
- it_behaves_like 'has no link' do
- let(:link) { 'updateImmediately' }
- end
- end
- end
-
- describe 'delete' do
- context 'for a user being admin' do
- before do
- allow(user)
- .to receive(:admin?)
- .and_return(true)
- end
-
- it_behaves_like 'has an untitled link' do
- let(:link) { 'delete' }
- let(:href) { api_v3_paths.project project.id }
- end
- end
-
- context 'for a non admin user' do
- let(:permissions) { [] }
-
- it_behaves_like 'has no link' do
- let(:link) { 'delete' }
- end
- end
- end
- end
-
- describe 'caching' do
- it 'is based on the representer\'s cache_key' do
- expect(OpenProject::Cache)
- .to receive(:fetch)
- .with(representer.json_cache_key)
- .and_call_original
-
- representer.to_json
- end
-
- describe '#json_cache_key' do
- let!(:former_cache_key) { representer.json_cache_key }
-
- it 'includes the name of the representer class' do
- expect(representer.json_cache_key)
- .to include('API', 'V3', 'Projects', 'ProjectRepresenter')
- end
-
- it 'changes when the locale changes' do
- I18n.with_locale(:fr) do
- expect(representer.json_cache_key)
- .not_to eql former_cache_key
- end
- end
-
- it 'changes when the project is updated' do
- project.updated_at = Time.now + 20.seconds
-
- expect(representer.json_cache_key)
- .not_to eql former_cache_key
- end
-
- it 'changes when the project status is updated' do
- project.status.updated_at = Time.now + 20.seconds
-
- expect(representer.json_cache_key)
- .not_to eql former_cache_key
- end
- end
- end
- end
-
- describe '.checked_permissions' do
- it 'lists add_work_packages' do
- expect(described_class.checked_permissions).to match_array([:add_work_packages])
- end
- end
-end
diff --git a/spec/requests/api/v3/project_resource_spec.rb b/spec/requests/api/v3/project_resource_spec.rb
index be772bd41ea..b145cf2e2d8 100644
--- a/spec/requests/api/v3/project_resource_spec.rb
+++ b/spec/requests/api/v3/project_resource_spec.rb
@@ -146,9 +146,9 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
let!(:parent_memberships) do
end
- it 'has no path to the parent' do
+ it 'shows the `undisclosed` uri' do
expect(subject.body)
- .to be_json_eql(nil.to_json)
+ .to be_json_eql(API::V3::URN_UNDISCLOSED.to_json)
.at_path('_links/parent/href')
end
end