Skip to content
代码片段 群组 项目
提交 cd36fe96 编辑于 作者: Heinrich Lee Yu's avatar Heinrich Lee Yu
浏览文件

Merge branch 'md-rest-api-time-tracking-endpoint-validation' into 'master'

Add non negative validation to update time estimate rest api endpoint

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/126620



Merged-by: default avatarHeinrich Lee Yu <heinrich@gitlab.com>
Approved-by: default avatarMarco Zille <marco.zille@gmail.com>
Approved-by: default avatarGregorius Marco <gmarco@gitlab.com>
Approved-by: default avatarHeinrich Lee Yu <heinrich@gitlab.com>
Reviewed-by: default avatarMarco Zille <marco.zille@gmail.com>
Reviewed-by: default avatarGregorius Marco <gmarco@gitlab.com>
Co-authored-by: default avatarMissy Davies <ms.melissadavies@gmail.com>
No related branches found
No related tags found
无相关合并请求
...@@ -55,10 +55,11 @@ def update_service ...@@ -55,10 +55,11 @@ def update_service
issuable_key = "#{issuable_name}_iid".to_sym issuable_key = "#{issuable_name}_iid".to_sym
desc "Set a time estimate for a #{issuable_name}" do desc "Set a time estimate for a #{issuable_name}" do
detail " Sets an estimated time of work for this #{issuable_name}." detail "Sets an estimated time of work for this #{issuable_name}."
success Entities::IssuableTimeStats success Entities::IssuableTimeStats
failure [ failure [
{ code: 401, message: 'Unauthorized' }, { code: 401, message: 'Unauthorized' },
{ code: 400, message: 'Bad request' },
{ code: 404, message: 'Not found' } { code: 404, message: 'Not found' }
] ]
tags [issuable_collection_name] tags [issuable_collection_name]
...@@ -70,8 +71,14 @@ def update_service ...@@ -70,8 +71,14 @@ def update_service
post ":id/#{issuable_collection_name}/:#{issuable_key}/time_estimate" do post ":id/#{issuable_collection_name}/:#{issuable_key}/time_estimate" do
authorize! admin_issuable_key, load_issuable authorize! admin_issuable_key, load_issuable
status :ok time_estimate = Gitlab::TimeTrackingFormatter.parse(params.delete(:duration), keep_zero: true)
update_issuable(time_estimate: Gitlab::TimeTrackingFormatter.parse(params.delete(:duration)))
if time_estimate && time_estimate >= 0
status :ok
update_issuable(time_estimate: time_estimate)
else
bad_request!(reason: 'Time estimate must have a valid format and be greater than or equal to zero.')
end
end end
desc "Reset the time estimate for a project #{issuable_name}" do desc "Reset the time estimate for a project #{issuable_name}" do
......
...@@ -20,40 +20,49 @@ ...@@ -20,40 +20,49 @@
issuable_collection_name = issuable_name.pluralize issuable_collection_name = issuable_name.pluralize
describe "POST /projects/:id/#{issuable_collection_name}/:#{issuable_name}_id/time_estimate" do describe "POST /projects/:id/#{issuable_collection_name}/:#{issuable_name}_id/time_estimate" do
subject(:set_time_estimate) do
post(api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.iid}/time_estimate", user), params: { duration: duration })
end
let(:duration) { '2h' }
context 'with an unauthorized user' do context 'with an unauthorized user' do
subject { post(api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.iid}/time_estimate", non_member), params: { duration: '1w' }) } let(:user) { non_member }
it_behaves_like 'an unauthorized API user' it_behaves_like 'an unauthorized API user'
it_behaves_like 'API user with insufficient permissions' it_behaves_like 'API user with insufficient permissions'
end end
it "sets the time estimate for #{issuable_name}" do context 'with an authorized user' do
post api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.iid}/time_estimate", user), params: { duration: '1w' } it "sets the time estimate for #{issuable_name}" do
set_time_estimate
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['human_time_estimate']).to eq('1w') expect(json_response['time_estimate']).to eq(7200)
end
end end
describe 'updating the current estimate' do describe 'updating the current estimate' do
before do before do
post api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.iid}/time_estimate", user), params: { duration: '1w' } post(api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.iid}/time_estimate", user), params: { duration: '2h' })
end end
context 'when duration has a bad format' do using RSpec::Parameterized::TableSyntax
it 'does not modify the original estimate' do
post api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.iid}/time_estimate", user), params: { duration: 'foo' }
expect(response).to have_gitlab_http_status(:bad_request) where(:updated_duration, :expected_http_status, :expected_time_estimate) do
expect(issuable.reload.human_time_estimate).to eq('1w') 'foo' | :bad_request | 7200
end '-1' | :bad_request | 7200
'1h' | :ok | 3600
'0' | :ok | 0
end end
context 'with a valid duration' do with_them do
it 'updates the estimate' do let(:duration) { updated_duration }
post api("/projects/#{project.id}/#{issuable_collection_name}/#{issuable.iid}/time_estimate", user), params: { duration: '3w1h' } it 'returns expected HTTP status and time estimate' do
set_time_estimate
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(expected_http_status)
expect(issuable.reload.human_time_estimate).to eq('3w 1h') expect(issuable.reload.time_estimate).to eq(expected_time_estimate)
end end
end end
end end
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册