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
alter permission for custom fields in even more wp representers
ulferts7040d2bc532c
diff --git a/lib/api/v3/work_packages/create_project_form_representer.rb b/lib/api/v3/work_packages/create_project_form_representer.rbindex 5fb81dd5589..5f01c5ab359 100644--- a/lib/api/v3/work_packages/create_project_form_representer.rb+++ b/lib/api/v3/work_packages/create_project_form_representer.rb@@ -65,8 +65,7 @@ module APIendlink :customFields do- if current_user.try(:admin?) ||- current_user_allowed_to(:edit_project,+ if current_user_allowed_to(:select_custom_fields,context: represented.project){href: project_settings_custom_fields_path(represented.project.identifier),diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rbindex 7263f35c910..b580eb32b41 100644--- a/lib/api/v3/work_packages/work_package_representer.rb+++ b/lib/api/v3/work_packages/work_package_representer.rb@@ -143,7 +143,7 @@ module APIendlink :customFields,- cache_if: -> { current_user_allowed_to(:edit_project, context: represented.project) } do+ cache_if: -> { current_user_allowed_to(:select_custom_fields, context: represented.project) } donext if represented.project.nil?{diff --git a/spec/lib/api/v3/work_packages/create_form_representer_spec.rb b/spec/lib/api/v3/work_packages/create_form_representer_spec.rbindex b450b83ab30..acca0d880cc 100644--- a/spec/lib/api/v3/work_packages/create_form_representer_spec.rb+++ b/spec/lib/api/v3/work_packages/create_form_representer_spec.rb@@ -112,7 +112,7 @@ describe ::API::V3::WorkPackages::CreateFormRepresenter do.and_return(true)end- context 'valid work package' do+ context 'for a valid work package' doit 'links to the work package create api' doexpect(generated).to be_json_eql(api_v3_paths.work_packages.to_json)@@ -126,7 +126,7 @@ describe ::API::V3::WorkPackages::CreateFormRepresenter doendend- context 'invalid work package' do+ context 'for an invalid work package' dolet(:errors) { [::API::Errors::Validation.new(:subject, 'it is broken')] }it 'has no link' do@@ -134,7 +134,7 @@ describe ::API::V3::WorkPackages::CreateFormRepresenter doendend- context 'user with insufficient permissions' do+ context 'for a user with insufficient permissions' dobefore doallow(current_user).to receive(:allowed_to?)@@ -178,7 +178,7 @@ describe ::API::V3::WorkPackages::CreateFormRepresenter docontext 'without the permission to select custom fields' doit 'has no link to set the custom fields for that project' do- expect(generated).to_not have_json_path('_links/customFields')+ expect(generated).not_to have_json_path('_links/customFields')endendend@@ -188,53 +188,50 @@ describe ::API::V3::WorkPackages::CreateFormRepresenter doallow(current_user).to receive(:allowed_to?).and_return(true)end- context "as admin" do+ context 'for an admin and with type' do+ let(:type) { FactoryBot.build_stubbed(:type) }let(:current_user) { FactoryBot.build_stubbed(:admin) }+ let(:work_package) do+ FactoryBot.build(:work_package,+ id: 42,+ created_at: DateTime.now,+ updated_at: DateTime.now,+ type: type)+ end++ before do+ allow(current_user).to receive(:allowed_to?)+ .with(:edit_project, work_package.project)+ .and_return(false)+ end++ it 'has a link to configure the form' do+ expected = {+ href: "/types/#{type.id}/edit?tab=form_configuration",+ type: "text/html",+ title: "Configure form"+ }- context 'with type' do- let(:type) { FactoryBot.build_stubbed(:type) }- let(:work_package) do- FactoryBot.build(:work_package,- id: 42,- created_at: DateTime.now,- updated_at: DateTime.now,- type: type)- end-- before do- allow(current_user).to receive(:allowed_to?)- .with(:edit_project, work_package.project)- .and_return(false)- end-- it 'has a link to configure the form' do- expected = {- href: "/types/#{type.id}/edit?tab=form_configuration",- type: "text/html",- title: "Configure form"- }-- expect(generated)- .to be_json_eql(expected.to_json)- .at_path('_links/configureForm')- end+ expect(generated)+ .to be_json_eql(expected.to_json)+ .at_path('_links/configureForm')end+ end- context 'without type' do- before do- allow(work_package).to receive(:type).and_return(nil)- allow(work_package).to receive(:type_id).and_return(nil)- end+ context 'for an admin and without type' do+ before do+ allow(work_package).to receive(:type).and_return(nil)+ allow(work_package).to receive(:type_id).and_return(nil)+ end- it 'has no link to configure the form' do- expect(generated).to_not have_json_path('_links/configureForm')- end+ it 'has no link to configure the form' do+ expect(generated).not_to have_json_path('_links/configureForm')endend- context 'not being admin' do+ context 'for a nonadmin' doit 'has no link to configure the form' do- expect(generated).to_not have_json_path('_links/configureForm')+ expect(generated).not_to have_json_path('_links/configureForm')endendenddiff --git a/spec/lib/api/v3/work_packages/create_project_form_representer_spec.rb b/spec/lib/api/v3/work_packages/create_project_form_representer_spec.rbindex 254f64fab34..84ba7ba54e8 100644--- a/spec/lib/api/v3/work_packages/create_project_form_representer_spec.rb+++ b/spec/lib/api/v3/work_packages/create_project_form_representer_spec.rb@@ -41,155 +41,146 @@ describe ::API::V3::WorkPackages::CreateProjectFormRepresenter doFactoryBot.build_stubbed(:stubbed_work_package,type: type)end- include_context 'user with stubbed permissions'let(:representer) dodescribed_class.new(work_package, current_user: user, errors: errors)end- context 'generation' do- subject(:generated) { representer.to_json }+ include_context 'user with stubbed permissions'++ subject(:generated) { representer.to_json }++ describe '_links' do+ it do+ expect(generated).to be_json_eql(+ api_v3_paths.create_project_work_package_form(work_package.project_id).to_json+ )+ .at_path('_links/self/href')+ end- describe '_links' do+ it do+ expect(generated).to be_json_eql(:post.to_json).at_path('_links/self/method')+ end++ describe 'validate' doit do- is_expected.to be_json_eql(+ expect(generated).to be_json_eql(api_v3_paths.create_project_work_package_form(work_package.project_id).to_json)- .at_path('_links/self/href')+ .at_path('_links/validate/href')endit do- is_expected.to be_json_eql(:post.to_json).at_path('_links/self/method')+ expect(generated).to be_json_eql(:post.to_json).at_path('_links/validate/method')+ end+ end++ describe 'preview markup' do+ it do+ expect(generated).to be_json_eql(+ api_v3_paths.render_markup(+ link: api_v3_paths.project(work_package.project_id)+ ).to_json+ )+ .at_path('_links/previewMarkup/href')+ end++ it do+ expect(generated).to be_json_eql(:post.to_json).at_path('_links/previewMarkup/method')+ end++ it 'contains link to work package' do+ expected_preview_link =+ api_v3_paths.render_markup(link: "/api/v3/projects/#{work_package.project_id}")+ expect(subject).to be_json_eql(expected_preview_link.to_json)+ .at_path('_links/previewMarkup/href')end+ end- describe 'validate' do+ describe 'commit' do+ context 'with a valid work package' doit do- is_expected.to be_json_eql(- api_v3_paths.create_project_work_package_form(work_package.project_id).to_json+ expect(generated).to be_json_eql(+ api_v3_paths.work_packages_by_project(work_package.project_id).to_json)- .at_path('_links/validate/href')+ .at_path('_links/commit/href')endit do- is_expected.to be_json_eql(:post.to_json).at_path('_links/validate/method')+ expect(generated).to be_json_eql(:post.to_json).at_path('_links/commit/method')endend- describe 'preview markup' do- it do- is_expected.to be_json_eql(- api_v3_paths.render_markup(- link: api_v3_paths.project(work_package.project_id)- ).to_json- )- .at_path('_links/previewMarkup/href')- end+ context 'with an invalid work package' do+ let(:errors) { [::API::Errors::Validation.new(:subject, 'it is broken')] }it do- is_expected.to be_json_eql(:post.to_json).at_path('_links/previewMarkup/method')+ expect(generated).not_to have_json_path('_links/commit/href')end+ end- it 'contains link to work package' do- expected_preview_link =- api_v3_paths.render_markup(link: "/api/v3/projects/#{work_package.project_id}")- expect(subject).to be_json_eql(expected_preview_link.to_json)- .at_path('_links/previewMarkup/href')+ context 'with the user having insufficient permissions' do+ let(:permissions) { [] }++ it do+ expect(generated).not_to have_json_path('_links/commit/href')endend+ end- describe 'commit' do- context 'valid work package' do- it do- is_expected.to be_json_eql(- api_v3_paths.work_packages_by_project(work_package.project_id).to_json- )- .at_path('_links/commit/href')- end-- it do- is_expected.to be_json_eql(:post.to_json).at_path('_links/commit/method')- end- end+ describe 'customFields' do+ context 'with the permission to select custom fields' do+ let(:permissions) { [:select_custom_fields] }- context 'invalid work package' do- let(:errors) { [::API::Errors::Validation.new(:subject, 'it is broken')] }+ it 'has a link to set the custom fields for that project' do+ expected = {+ href: project_settings_custom_fields_path(work_package.project),+ type: "text/html",+ title: "Custom fields"+ }- it do- is_expected.not_to have_json_path('_links/commit/href')- end+ expect(generated)+ .to be_json_eql(expected.to_json)+ .at_path('_links/customFields')end+ end- context 'user with insufficient permissions' do- let(:permissions) { [] }-- it do- is_expected.not_to have_json_path('_links/commit/href')- end+ context 'without the permission to select custom fields' do+ it 'has no link to set the custom fields for that project' do+ expect(generated).not_to have_json_path('_links/customFields')endend+ end- describe 'customFields' do- shared_examples_for 'links to project custom field admin' do- it 'has a link to set the custom fields for that project' do- expected = {- href: "/projects/#{work_package.project.identifier}/settings/custom_fields",- type: "text/html",- title: "Custom fields"- }-- is_expected.to be_json_eql(expected.to_json).at_path('_links/customFields')- end- end+ describe 'configureForm' do+ context "for an admin and with a type" do+ include_context 'user with stubbed permissions', admin: true- context 'with admin privileges' do- include_context 'user with stubbed permissions', admin: true+ it 'has a link to configure the form' do+ expected = {+ href: "/types/#{type.id}/edit?tab=form_configuration",+ type: "text/html",+ title: "Configure form"+ }- it_behaves_like 'links to project custom field admin'+ expect(generated)+ .to be_json_eql(expected.to_json)+ .at_path('_links/configureForm')end+ end- context 'without project admin priviliges' do- it 'has no link to set the custom fields for that project' do- is_expected.to_not have_json_path('_links/customFields')- end- end+ context 'for an admin and without type' do+ let(:type) { nil }- context 'with project admin privileges' do- let(:permissions) { [:edit_project] }+ include_context 'user with stubbed permissions', admin: true- it_behaves_like 'links to project custom field admin'+ it 'has no link to configure the form' do+ expect(generated).not_to have_json_path('_links/configureForm')endend- describe 'configureForm' do- context "as admin" do- include_context 'user with stubbed permissions', admin: true-- context 'with type' do- it 'has a link to configure the form' do- expected = {- href: "/types/#{type.id}/edit?tab=form_configuration",- type: "text/html",- title: "Configure form"- }-- is_expected- .to be_json_eql(expected.to_json)- .at_path('_links/configureForm')- end- end-- context 'without type' do- let(:type) { nil }-- it 'has no link to configure the form' do- is_expected.to_not have_json_path('_links/configureForm')- end- end- end-- context 'not being admin' do- it 'has no link to configure the form' do- is_expected.to_not have_json_path('_links/configureForm')- end+ context 'for a non admin' do+ it 'has no link to configure the form' do+ expect(generated).not_to have_json_path('_links/configureForm')endendenddiff --git a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rbindex b035a424be5..f392688152c 100644--- a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb+++ b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb@@ -397,7 +397,7 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter docontext 'when passing groups' dolet(:groups) do- group = { 'custom': 'object' }+ group = { 'custom' => 'object' }allow(group).to receive(:has_sums?).and_return false[group]end@@ -409,7 +409,7 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter docontext 'when passing groups with sums' dolet(:groups) do- group = { 'sums': {} }+ group = { 'sums' => {} }allow(group).to receive(:has_sums?).and_return true[group]end@@ -431,9 +431,9 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter dolet(:total_sums) { OpenStruct.new(estimated_hours: 1) }it 'renders the groups object as json' do- expected = { 'estimatedTime': 'PT1H',- 'remainingTime': nil,- 'storyPoints': nil }+ expected = { 'estimatedTime' => 'PT1H',+ 'remainingTime' => nil,+ 'storyPoints' => nil }expect(collection).to be_json_eql(expected.to_json).at_path('totalSums')enddiff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rbindex 694c61f85d1..c5c6529d139 100644--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb@@ -1042,7 +1042,7 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter dodescribe 'customFields' doit_behaves_like 'action link' dolet(:action) { 'customFields' }- let(:permission) { :edit_project }+ let(:permission) { :select_custom_fields }let(:href) { project_settings_custom_fields_path(work_package.project.identifier) }endenddiff --git a/spec/models/attribute_help_text/work_package_spec.rb b/spec/models/attribute_help_text/work_package_spec.rbindex 4602a1ef321..fb1256bd728 100644--- a/spec/models/attribute_help_text/work_package_spec.rb+++ b/spec/models/attribute_help_text/work_package_spec.rb@@ -96,7 +96,7 @@ describe AttributeHelpText::WorkPackage, type: :model doendend- context 'user having the `select_custom_fields` permission' do+ context 'with a user having the `select_custom_fields` permission' dolet(:permissions) { [:select_custom_fields] }it 'returns the help text for the static and cf attribute' do