From 5806625cdde5f02e240c61b0542f22c389017862 Mon Sep 17 00:00:00 2001 From: Aaron Contreras Date: Wed, 28 Feb 2024 12:11:54 -0500 Subject: [PATCH] Ensure remaining_hours isn't writable on "status-based" mode --- .../schema/work_package_schema_representer.rb | 6 ++++-- .../work_packages/work_package_representer.rb | 2 ++ .../work_package_schema_representer_spec.rb | 11 +++++++++++ .../work_package_payload_representer_spec.rb | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb index 53b7aa5a1844..16832fc232f4 100644 --- a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb +++ b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb @@ -167,12 +167,14 @@ def initialize(schema, self_link:, **context) schema :remaining_time, name_source: :remaining_hours, type: 'Duration', - required: false + required: false, + writable: ->(*) { !WorkPackage.use_status_for_done_ratio? } schema :derived_remaining_time, name_source: :derived_remaining_hours, type: 'Duration', - required: false + required: false, + writable: false schema :spent_time, type: 'Duration', diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb index d4e23d11d875..face30dda149 100644 --- a/lib/api/v3/work_packages/work_package_representer.rb +++ b/lib/api/v3/work_packages/work_package_representer.rb @@ -409,6 +409,7 @@ def self_v3_path(*) datetime_formatter.format_duration_from_hours(represented.remaining_hours, allow_nil: true) end, + writable: ->(*) { !WorkPackage.use_status_for_done_ratio? }, render_nil: true property :derived_remaining_time, @@ -417,6 +418,7 @@ def self_v3_path(*) datetime_formatter.format_duration_from_hours(represented.derived_remaining_hours, allow_nil: true) end, + writable: ->(*) { !WorkPackage.use_status_for_done_ratio? }, render_nil: true property :duration, diff --git a/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb index cc573c5e4cdb..6210b75d02a0 100644 --- a/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb +++ b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb @@ -611,6 +611,17 @@ let(:required) { false } let(:writable) { true } end + + context 'when on the "status-based" % complete mode', + with_settings: { work_package_done_ratio: 'status' } do + it_behaves_like 'has basic schema properties' do + let(:path) { 'remainingTime' } + let(:type) { 'Duration' } + let(:name) { I18n.t('activerecord.attributes.work_package.remaining_hours') } + let(:required) { false } + let(:writable) { false } + end + end end describe 'derivedRemainingTime' do diff --git a/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb index ed71c552992f..4a7e2827a05b 100644 --- a/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb +++ b/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb @@ -126,6 +126,25 @@ end end + describe 'remaining hours' do + it { is_expected.to have_json_path('remainingTime') } + + it do + expect(subject).to be_json_eql(work_package.estimated_hours.to_json) + .at_path('estimatedTime') + end + + context 'when not set' do + it { is_expected.to have_json_type(NilClass).at_path('estimatedTime') } + end + + context 'when set' do + let(:work_package) { build(:work_package, estimated_hours: 7, remaining_hours: 5) } + + it { is_expected.to have_json_type(String).at_path('estimatedTime') } + end + end + describe 'startDate' do before do allow(work_package)