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
adapt permission check in work package representers
ulfertsedfc30365bdb
diff --git a/lib/api/v3/work_packages/create_form_representer.rb b/lib/api/v3/work_packages/create_form_representer.rbindex 6f15dc74f89..749f3c74a68 100644--- a/lib/api/v3/work_packages/create_form_representer.rb+++ b/lib/api/v3/work_packages/create_form_representer.rb@@ -67,10 +67,9 @@ module APIendlink :customFields do- if represented.project && (- current_user.try(:admin?) ||- current_user_allowed_to(:edit_project,- context: represented.project))+ if represented.project &&+ current_user_allowed_to(:select_custom_fields,+ context: represented.project){href: project_settings_custom_fields_path(represented.project.identifier),type: 'text/html',diff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rbindex e3832fa112d..5782f347a04 100644--- a/lib/api/v3/work_packages/work_package_collection_representer.rb+++ b/lib/api/v3/work_packages/work_package_collection_representer.rb@@ -113,7 +113,7 @@ module APIlink :customFields doif project.present? &&- (current_user.try(:admin?) || current_user_allowed_to(:edit_project, context: project))+ current_user_allowed_to(:select_custom_fields, context: project){href: project_settings_custom_fields_path(project.identifier),type: 'text/html',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 50032bed6a7..b450b83ab30 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@@ -49,137 +49,157 @@ describe ::API::V3::WorkPackages::CreateFormRepresenter dodescribed_class.new(work_package, current_user: current_user, errors: errors)end- context 'generation' do- subject(:generated) { representer.to_json }+ subject(:generated) { representer.to_json }- describe '_links' do+ describe '_links' do+ it 'links to the create form api' do+ expect(generated)+ .to be_json_eql(api_v3_paths.create_work_package_form.to_json)+ .at_path('_links/self/href')+ end++ it 'is a post' do+ expect(generated)+ .to be_json_eql(:post.to_json)+ .at_path('_links/self/method')+ end++ describe 'validate' doit 'links to the create form api' do- is_expected+ expect(generated).to be_json_eql(api_v3_paths.create_work_package_form.to_json)- .at_path('_links/self/href')+ .at_path('_links/validate/href')endit 'is a post' do- is_expected+ expect(generated).to be_json_eql(:post.to_json)- .at_path('_links/self/method')+ .at_path('_links/validate/method')end+ end- describe 'validate' do- it 'links to the create form api' do- is_expected- .to be_json_eql(api_v3_paths.create_work_package_form.to_json)- .at_path('_links/validate/href')- end+ describe 'preview markup' do+ it 'links to the markup api' do+ path = api_v3_paths.render_markup(link: api_v3_paths.project(work_package.project_id))+ expect(generated)+ .to be_json_eql(path.to_json)+ .at_path('_links/previewMarkup/href')+ end- it 'is a post' do- is_expected- .to be_json_eql(:post.to_json)- .at_path('_links/validate/method')- end+ it 'is a post' 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 'preview markup' do- it 'links to the markup api' do- path = api_v3_paths.render_markup(link: api_v3_paths.project(work_package.project_id))- is_expected- .to be_json_eql(path.to_json)- .at_path('_links/previewMarkup/href')+ describe 'commit' do+ before do+ allow(current_user)+ .to receive(:allowed_to?)+ .and_return(false)+ allow(current_user)+ .to receive(:allowed_to?)+ .with(:edit_work_packages, project)+ .and_return(true)+ end++ context 'valid work package' do+ it 'links to the work package create api' do+ expect(generated)+ .to be_json_eql(api_v3_paths.work_packages.to_json)+ .at_path('_links/commit/href')endit 'is a post' do- is_expected+ expect(generated).to be_json_eql(:post.to_json)- .at_path('_links/previewMarkup/method')+ .at_path('_links/commit/method')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 'invalid work package' do+ let(:errors) { [::API::Errors::Validation.new(:subject, 'it is broken')] }++ it 'has no link' do+ expect(generated).not_to have_json_path('_links/commit/href')endend- describe 'commit' do+ context 'user with insufficient permissions' dobefore doallow(current_user).to receive(:allowed_to?)- .and_return(false)- allow(current_user)- .to receive(:allowed_to?)- .with(:edit_work_packages, project)- .and_return(true)+ .with(:edit_work_packages, project)+ .and_return(false)end- context 'valid work package' do- it 'links to the work package create api' do- is_expected- .to be_json_eql(api_v3_paths.work_packages.to_json)- .at_path('_links/commit/href')- end-- it 'is a post' do- is_expected- .to be_json_eql(:post.to_json)- .at_path('_links/commit/method')- end+ it 'has no link' do+ expect(generated).not_to have_json_path('_links/commit/href')end+ end+ end- context 'invalid work package' do- let(:errors) { [::API::Errors::Validation.new(:subject, 'it is broken')] }+ describe 'customFields' do+ before do+ allow(current_user)+ .to receive(:allowed_to?)+ .and_return(false)+ end- it 'has no link' do- is_expected.not_to have_json_path('_links/commit/href')- end+ context 'with the permission to select custom fields' do+ before do+ allow(current_user)+ .to receive(:allowed_to?)+ .with(:select_custom_fields, work_package.project)+ .and_return(true)end- context 'user with insufficient permissions' do- before do- allow(current_user)- .to receive(:allowed_to?)- .with(:edit_work_packages, project)- .and_return(false)- end+ 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 'has no link' 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')endend- describe 'customFields' do- before do- allow(current_user).to receive(:allowed_to?).and_return(true)- end-- context 'with project admin priviliges' 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+ context 'without the permission to select custom fields' do+ it 'has no link to set the custom fields for that project' do+ expect(generated).to_not have_json_path('_links/customFields')end+ end+ end- context 'without project admin priviliges' do- before do- allow(current_user).to receive(:allowed_to?)- .with(:edit_project, work_package.project)- .and_return(false)- end+ describe 'configureForm' do+ before do+ allow(current_user).to receive(:allowed_to?).and_return(true)+ end- it 'has no link to set the custom fields for that project' do- is_expected.to_not have_json_path('_links/customFields')+ context "as admin" do+ let(:current_user) { FactoryBot.build_stubbed(:admin) }++ 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- end-- context 'with project and general admin priviliges' do- let(:current_user) { FactoryBot.build_stubbed(:admin) }before doallow(current_user).to receive(:allowed_to?)@@ -187,73 +207,34 @@ describe ::API::V3::WorkPackages::CreateFormRepresenter do.and_return(false)end- it 'has a link to set the custom fields for that project' do+ it 'has a link to configure the form' doexpected = {- href: "/projects/#{work_package.project.identifier}/settings/custom_fields",+ href: "/types/#{type.id}/edit?tab=form_configuration",type: "text/html",- title: "Custom fields"+ title: "Configure form"}- is_expected+ expect(generated).to be_json_eql(expected.to_json)- .at_path('_links/customFields')+ .at_path('_links/configureForm')endend- end-- describe 'configureForm' do- before do- allow(current_user).to receive(:allowed_to?).and_return(true)- end- context "as admin" do- let(:current_user) { FactoryBot.build_stubbed(:admin) }-- 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"- }-- is_expected- .to be_json_eql(expected.to_json)- .at_path('_links/configureForm')- 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 '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- is_expected.to_not have_json_path('_links/configureForm')- end+ it 'has no link to configure the form' do+ expect(generated).to_not have_json_path('_links/configureForm')endend+ 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 'not being admin' do+ it 'has no link to configure the form' do+ expect(generated).to_not 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 ed5ab9f0851..b035a424be5 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@@ -60,6 +60,7 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter doembed_schemas: embed_schemas)end+ let(:collection_inner_type) { 'WorkPackage' }before doallow(user)@@ -69,402 +70,392 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter doFactoryBot.create_list(:work_package, total)end- context 'generation' do- subject(:collection) { representer.to_json }- let(:collection_inner_type) { 'WorkPackage' }-- context '_links' do- context 'representations' do- context 'when the user has the export_work_packages permission' do- let(:query) { { foo: 'bar' } }-- it 'has a collection of export formats' do- expected_query = query.merge(pageSize: 30, offset: 1)- expected = JSON.parse([- {- href: work_packages_path({ format: 'pdf' }.merge(expected_query)),- type: 'application/pdf',- identifier: 'pdf',- title: I18n.t('export.format.pdf')- },- {- href: work_packages_path({ format: 'pdf', show_descriptions: true }.merge(expected_query)),- identifier: 'pdf-with-descriptions',- type: 'application/pdf',- title: I18n.t('export.format.pdf_with_descriptions')- },- {- href: work_packages_path({ format: 'csv' }.merge(expected_query)),- type: 'text/csv',- identifier: 'csv',- title: I18n.t('export.format.csv')- },- {- href: work_packages_path({ format: 'atom' }.merge(expected_query)),- identifier: 'atom',- type: 'application/atom+xml',- title: I18n.t('export.format.atom')- }- ].to_json)-- actual = JSON.parse(subject).dig('_links', 'representations')-- # As plugins might extend the representation, we only- # check for a subset- expect(actual)- .to include(*expected)- end-- context 'with project scope' do- let(:project) { FactoryBot.build_stubbed(:project) }-- it 'has a project scoped collection of export formats if inside a project' do- expected_query = query.merge(pageSize: 30, offset: 1)- expected = JSON.parse([- {- href: project_work_packages_path(project, { format: 'pdf' }.merge(expected_query)),- type: 'application/pdf',- identifier: 'pdf',- title: I18n.t('export.format.pdf')- },- {- href: project_work_packages_path(project, { format: 'pdf', show_descriptions: true }.merge(expected_query)),- type: 'application/pdf',- identifier: 'pdf-with-descriptions',- title: I18n.t('export.format.pdf_with_descriptions')- },- {- href: project_work_packages_path(project, { format: 'csv' }.merge(expected_query)),- identifier: 'csv',- type: 'text/csv',- title: I18n.t('export.format.csv')- },- {- href: project_work_packages_path(project, { format: 'atom' }.merge(expected_query)),- identifier: 'atom',- type: 'application/atom+xml',- title: I18n.t('export.format.atom')- }- ].to_json)-- actual = JSON.parse(subject).dig('_links', 'representations')-- # As plugins might extend the representation, we only- # check for a subset- expect(actual)- .to include(*expected)- end- end+ subject(:collection) { representer.to_json }++ describe '_links' do+ describe 'representations' do+ context 'when outside of a project and the user has the export_work_packages permission' do+ let(:query) { { foo: 'bar' } }++ let(:expected) do+ expected_query = query.merge(pageSize: 30, offset: 1)+ JSON.parse([+ {+ href: work_packages_path({ format: 'pdf' }.merge(expected_query)),+ type: 'application/pdf',+ identifier: 'pdf',+ title: I18n.t('export.format.pdf')+ },+ {+ href: work_packages_path({ format: 'pdf', show_descriptions: true }.merge(expected_query)),+ identifier: 'pdf-with-descriptions',+ type: 'application/pdf',+ title: I18n.t('export.format.pdf_with_descriptions')+ },+ {+ href: work_packages_path({ format: 'csv' }.merge(expected_query)),+ type: 'text/csv',+ identifier: 'csv',+ title: I18n.t('export.format.csv')+ },+ {+ href: work_packages_path({ format: 'atom' }.merge(expected_query)),+ identifier: 'atom',+ type: 'application/atom+xml',+ title: I18n.t('export.format.atom')+ }+ ].to_json)end- context 'when the user lacks the export_work_packages permission' do- before do- allow(user)- .to receive(:allowed_to?)- .with(:export_work_packages, project, global: project.nil?)- .and_return(false)- end-- it 'has no export links' do- is_expected- .to_not have_json_path('_links/representations')- end+ it 'has a collection of export formats' do+ actual = JSON.parse(subject).dig('_links', 'representations')++ # As plugins might extend the representation, we only+ # check for a subset+ expect(actual)+ .to include(*expected)endend- end- it 'does not render groups' do- is_expected.not_to have_json_path('groups')- end+ context 'when inside of a project and the user has the export_work_packages permission' do+ let(:project) { FactoryBot.build_stubbed(:project) }++ let(:expected) do+ expected_query = query.merge(pageSize: 30, offset: 1)+ JSON.parse([+ {+ href: project_work_packages_path(project, { format: 'pdf' }.merge(expected_query)),+ type: 'application/pdf',+ identifier: 'pdf',+ title: I18n.t('export.format.pdf')+ },+ {+ href: project_work_packages_path(project, { format: 'pdf', show_descriptions: true }.merge(expected_query)),+ type: 'application/pdf',+ identifier: 'pdf-with-descriptions',+ title: I18n.t('export.format.pdf_with_descriptions')+ },+ {+ href: project_work_packages_path(project, { format: 'csv' }.merge(expected_query)),+ identifier: 'csv',+ type: 'text/csv',+ title: I18n.t('export.format.csv')+ },+ {+ href: project_work_packages_path(project, { format: 'atom' }.merge(expected_query)),+ identifier: 'atom',+ type: 'application/atom+xml',+ title: I18n.t('export.format.atom')+ }+ ].to_json)+ end- it 'does not render sums' do- is_expected.not_to have_json_path('totalSums')- end+ it 'has a project scoped collection of export formats if inside a project' do+ actual = JSON.parse(subject).dig('_links', 'representations')- it 'has a schemas link' do- ids = work_packages.map do |wp|- [wp.project_id, wp.type_id]+ # As plugins might extend the representation, we only+ # check for a subset+ expect(actual)+ .to include(*expected)+ endend- path = api_v3_paths.work_package_schemas *ids-- is_expected- .to be_json_eql(path.to_json)- .at_path('_links/schemas/href')- end-- describe 'ancestors' do- before do- expect(WorkPackage).to receive(:aggregate_ancestors).and_call_original- end+ context 'when the user lacks the export_work_packages permission' do+ before do+ allow(user)+ .to receive(:allowed_to?)+ .with(:export_work_packages, project, global: project.nil?)+ .and_return(false)+ end- it 'are being eager loaded' do- representer.represented.each do |wp|- expect(wp.work_package_ancestors).to be_kind_of(Array)- expect(wp.ancestors).to eq(wp.work_package_ancestors)+ it 'has no export links' do+ expect(collection)+ .not_to have_json_path('_links/representations')endendend- context 'when the user has the edit_work_package permission in any project' do- before do- allow(user)- .to receive(:allowed_to?)- .and_return(false)+ describe 'customFields' do+ let(:project) { FactoryBot.build_stubbed(:project) }+ before doallow(user).to receive(:allowed_to?)- .with(:edit_work_packages, nil, global: true)- .and_return(allowed)+ .and_return(false)end- context 'when allowed' do- let(:allowed) { true }+ context 'with the permission to select custom fields' do+ before do+ allow(user)+ .to receive(:allowed_to?)+ .with(:select_custom_fields, project)+ .and_return(true)+ end++ it 'has a link to set the custom fields for that project' do+ expected = {+ href: project_settings_custom_fields_path(project),+ type: "text/html",+ title: "Custom fields"+ }- it 'has a link to templated edit work_package' do- is_expected- .to be_json_eql(api_v3_paths.work_package_form('{work_package_id}').to_json)- .at_path('_links/editWorkPackage/href')+ expect(collection)+ .to be_json_eql(expected.to_json)+ .at_path('_links/customFields')endend- context 'when allowed' do- let(:allowed) { false }+ context 'without the permission to select custom fields' do+ it 'has no link to set the custom fields for that project' do+ expect(collection).not_to have_json_path('_links/customFields')+ end+ end++ context 'when not in a project' do+ let(:project) { nil }- it 'has no link to templated edit work_package' do- is_expected.not_to have_json_path('_links/editWorkPackage')+ it 'has no link to set the custom fields for that project' do+ expect(collection).not_to have_json_path('_links/customFields')endendend+ end- context 'when the user has the add_work_package permission in any project' do- before do- allow(user)- .to receive(:allowed_to?)- .and_return(false)+ it 'does not render groups' do+ expect(collection).not_to have_json_path('groups')+ end- allow(user)- .to receive(:allowed_to?)- .with(:add_work_packages, nil, global: true)- .and_return(true)- end+ it 'does not render sums' do+ expect(collection).not_to have_json_path('totalSums')+ end- it 'has a link to create work_packages' do- is_expected- .to be_json_eql(api_v3_paths.create_work_package_form.to_json)- .at_path('_links/createWorkPackage/href')- end+ it 'has a schemas link' do+ ids = work_packages.map do |wp|+ [wp.project_id, wp.type_id]+ end- it 'declares to use POST to create work_packages' do- is_expected- .to be_json_eql(:post.to_json)- .at_path('_links/createWorkPackage/method')- end+ path = api_v3_paths.work_package_schemas *ids- it 'has a link to create work_packages immediately' do- is_expected- .to be_json_eql(api_v3_paths.work_packages.to_json)- .at_path('_links/createWorkPackageImmediate/href')- end+ expect(collection)+ .to be_json_eql(path.to_json)+ .at_path('_links/schemas/href')+ end- it 'declares to use POST to create work_packages immediately' do- is_expected- .to be_json_eql(:post.to_json)- .at_path('_links/createWorkPackageImmediate/method')+ describe 'ancestors' do+ it 'are being eager loaded' do+ representer.represented.each do |wp|+ expect(wp.work_package_ancestors).to be_kind_of(Array)+ expect(wp.ancestors).to eq(wp.work_package_ancestors)end+ end+ end- context 'in project context' do- let(:project) { FactoryBot.build_stubbed :project }-- it 'has no link to create work_packages' do- is_expected- .to_not have_json_path('_links/createWorkPackage')- end+ context 'when the user has the edit_work_package permission in any project' do+ before do+ allow(user)+ .to receive(:allowed_to?)+ .and_return(false)- it 'has no link to create work_packages immediately' do- is_expected- .to_not have_json_path('_links/createWorkPackageImmediate')- end- end+ allow(user)+ .to receive(:allowed_to?)+ .with(:edit_work_packages, nil, global: true)+ .and_return(allowed)end- context 'when the user lacks the add_work_package permission' do- before do- allow(user)- .to receive(:allowed_to?)- .and_return(false)- end+ context 'when allowed' do+ let(:allowed) { true }- it 'has no link to create work_packages' do- is_expected- .to_not have_json_path('_links/createWorkPackage')+ it 'has a link to templated edit work_package' do+ expect(collection)+ .to be_json_eql(api_v3_paths.work_package_form('{work_package_id}').to_json)+ .at_path('_links/editWorkPackage/href')end+ end- it 'has no link to create work_packages immediately' do- is_expected- .to_not have_json_path('_links/createWorkPackageImmediate')+ context 'when not allowed' do+ let(:allowed) { false }++ it 'has no link to templated edit work_package' do+ expect(collection).not_to have_json_path('_links/editWorkPackage')endend+ end- context 'limited page size' do- let(:page_size_parameter) { 2 }+ context 'when the user has the add_work_package permission in any project' do+ before do+ allow(user)+ .to receive(:allowed_to?)+ .and_return(false)- context 'on the first page' do- it_behaves_like 'offset-paginated APIv3 collection' do- let(:page) { 1 }- let(:page_size) { page_size_parameter }- let(:actual_count) { page_size_parameter }- let(:collection_type) { 'WorkPackageCollection' }+ allow(user)+ .to receive(:allowed_to?)+ .with(:add_work_packages, nil, global: true)+ .and_return(true)+ end- it_behaves_like 'links to next page by offset'- end+ it 'has a link to create work_packages' do+ expect(collection)+ .to be_json_eql(api_v3_paths.create_work_package_form.to_json)+ .at_path('_links/createWorkPackage/href')+ end- it_behaves_like 'has no link' do- let(:link) { 'previousByOffset' }- end- end+ it 'declares to use POST to create work_packages' do+ expect(collection)+ .to be_json_eql(:post.to_json)+ .at_path('_links/createWorkPackage/method')+ end- context 'on the last page' do- let(:page_parameter) { 3 }+ it 'has a link to create work_packages immediately' do+ expect(collection)+ .to be_json_eql(api_v3_paths.work_packages.to_json)+ .at_path('_links/createWorkPackageImmediate/href')+ end- it_behaves_like 'offset-paginated APIv3 collection' do- let(:page) { 3 }- let(:page_size) { page_size_parameter }- let(:actual_count) { 1 }- let(:collection_type) { 'WorkPackageCollection' }+ it 'declares to use POST to create work_packages immediately' do+ expect(collection)+ .to be_json_eql(:post.to_json)+ .at_path('_links/createWorkPackageImmediate/method')+ end- it_behaves_like 'links to previous page by offset'- end+ context 'when in project context' do+ let(:project) { FactoryBot.build_stubbed :project }- it_behaves_like 'has no link' do- let(:link) { 'nextByOffset' }- end+ it 'has no link to create work_packages' do+ expect(collection)+ .not_to have_json_path('_links/createWorkPackage')end- end- context 'passing a query hash' do- let(:query) { { a: 'b', b: 'c' } }-- it_behaves_like 'has an untitled link' do- let(:link) { 'self' }- let(:href) { '/api/v3/example?a=b&b=c&offset=1&pageSize=30' }+ it 'has no link to create work_packages immediately' do+ expect(collection)+ .not_to have_json_path('_links/createWorkPackageImmediate')endend+ end- context 'passing groups' do- let(:groups) do- group = { 'custom': 'object' }- allow(group).to receive(:has_sums?).and_return false- [group]- end+ context 'when the user lacks the add_work_package permission' do+ before do+ allow(user)+ .to receive(:allowed_to?)+ .and_return(false)+ end- it 'renders the groups object as json' do- is_expected.to be_json_eql(groups.to_json).at_path('groups')- end+ it 'has no link to create work_packages' do+ expect(collection)+ .not_to have_json_path('_links/createWorkPackage')end- context 'passing groups with sums' do- let(:groups) do- group = { 'sums': {} }- allow(group).to receive(:has_sums?).and_return true- [group]- end+ it 'has no link to create work_packages immediately' do+ expect(collection)+ .not_to have_json_path('_links/createWorkPackageImmediate')+ end+ end- it 'renders the groups object as json' do- is_expected.to be_json_eql(groups.to_json).at_path('groups')- end+ context 'with a limited page size' do+ let(:page_size_parameter) { 2 }++ context 'when on the first page' do+ it_behaves_like 'offset-paginated APIv3 collection' do+ let(:page) { 1 }+ let(:page_size) { page_size_parameter }+ let(:actual_count) { page_size_parameter }+ let(:collection_type) { 'WorkPackageCollection' }- it 'has a link to the sums schema' do- expected = {- href: api_v3_paths.work_package_sums_schema- }+ it_behaves_like 'links to next page by offset'+ end- is_expected.to be_json_eql(expected.to_json).at_path('_links/sumsSchema')+ it_behaves_like 'has no link' do+ let(:link) { 'previousByOffset' }endend- context 'passing sums' do- let(:total_sums) { OpenStruct.new(estimated_hours: 1) }+ context 'when on the last page' do+ let(:page_parameter) { 3 }- it 'renders the groups object as json' do- expected = { 'estimatedTime': 'PT1H',- 'remainingTime': nil,- 'storyPoints': nil }- is_expected.to be_json_eql(expected.to_json).at_path('totalSums')- end+ it_behaves_like 'offset-paginated APIv3 collection' do+ let(:page) { 3 }+ let(:page_size) { page_size_parameter }+ let(:actual_count) { 1 }+ let(:collection_type) { 'WorkPackageCollection' }- it 'has a link to the sums schema' do- expected = {- href: api_v3_paths.work_package_sums_schema- }+ it_behaves_like 'links to previous page by offset'+ end- is_expected.to be_json_eql(expected.to_json).at_path('_links/sumsSchema')+ it_behaves_like 'has no link' do+ let(:link) { 'nextByOffset' }endend+ end- context 'passing schemas' do- let(:embed_schemas) { true }+ context 'when passing a query hash' do+ let(:query) { { a: 'b', b: 'c' } }- it 'embeds a schema collection' do- expected_path = api_v3_paths.work_package_schema(work_packages[0].project.id,- work_packages[0].type.id)+ it_behaves_like 'has an untitled link' do+ let(:link) { 'self' }+ let(:href) { '/api/v3/example?a=b&b=c&offset=1&pageSize=30' }+ end+ end- is_expected- .to be_json_eql(expected_path.to_json)- .at_path('_embedded/schemas/_embedded/elements/0/_links/self/href')- end+ context 'when passing groups' do+ let(:groups) do+ group = { 'custom': 'object' }+ allow(group).to receive(:has_sums?).and_return false+ [group]end- context 'with project admin priviliges' do- # In this spec a user responds to `allowed_to` with true per default.- let(:project) { FactoryBot.build_stubbed(:project) }+ it 'renders the groups object as json' do+ expect(collection).to be_json_eql(groups.to_json).at_path('groups')+ end+ end- it 'has a link to set the custom fields for that project' do- expected = {- href: "/projects/#{project.identifier}/settings/custom_fields",- type: "text/html",- title: "Custom fields"- }+ context 'when passing groups with sums' do+ let(:groups) do+ group = { 'sums': {} }+ allow(group).to receive(:has_sums?).and_return true+ [group]+ end- is_expected- .to be_json_eql(expected.to_json)- .at_path('_links/customFields')- end+ it 'renders the groups object as json' do+ expect(collection).to be_json_eql(groups.to_json).at_path('groups')end- context 'without project admin priviliges' do- # In this spec a user responds to `allowed_to` with true per default.- let(:project) { FactoryBot.build_stubbed(:project) }+ it 'has a link to the sums schema' do+ expected = {+ href: api_v3_paths.work_package_sums_schema+ }- before do- allow(user).to receive(:allowed_to?).with(:edit_project, project).and_return(false)- end+ expect(collection).to be_json_eql(expected.to_json).at_path('_links/sumsSchema')+ end+ end- it 'has no link to set the custom fields for that project' do- is_expected.to_not have_json_path('_links/customFields')- end+ context 'when passing sums' do+ let(:total_sums) { OpenStruct.new(estimated_hours: 1) }++ it 'renders the groups object as json' do+ expected = { 'estimatedTime': 'PT1H',+ 'remainingTime': nil,+ 'storyPoints': nil }+ expect(collection).to be_json_eql(expected.to_json).at_path('totalSums')end- context 'with project and general admin priviliges' do- # In this spec a user responds to `allowed_to` with true per default.- let(:project) { FactoryBot.build_stubbed(:project) }- let(:user) { FactoryBot.build_stubbed(:admin) }+ it 'has a link to the sums schema' do+ expected = {+ href: api_v3_paths.work_package_sums_schema+ }- before do- allow(user).to receive(:allowed_to?).with(:edit_project, project).and_return(false)- end+ expect(collection).to be_json_eql(expected.to_json).at_path('_links/sumsSchema')+ end+ end- it 'has a link to set the custom fields for that project' do- expected = {- href: "/projects/#{project.identifier}/settings/custom_fields",- type: "text/html",- title: "Custom fields"- }+ context 'when passing schemas' do+ let(:embed_schemas) { true }- is_expected- .to be_json_eql(expected.to_json)- .at_path('_links/customFields')- end+ it 'embeds a schema collection' do+ expected_path = api_v3_paths.work_package_schema(work_packages[0].project.id,+ work_packages[0].type.id)++ expect(collection)+ .to be_json_eql(expected_path.to_json)+ .at_path('_embedded/schemas/_embedded/elements/0/_links/self/href')endendend