Commit c6631e6b authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '36744-ca-daterange-validation' into 'master'

Date range validation for Cycle Analytics

Closes #36744

See merge request gitlab-org/gitlab!24254
parents 970b5be1 64de25f1
---
title: Add date range validation for Cycle Analytics at the backend side
merge_request: 24254
author:
type: changed
......@@ -8,6 +8,8 @@ module Gitlab
include ActiveModel::Validations
include ActiveModel::Attributes
MAX_RANGE_DAYS = 180.days.freeze
attr_writer :project_ids
attribute :created_after, :date
......@@ -17,6 +19,7 @@ module Gitlab
validates :created_before, presence: true
validate :validate_created_before
validate :validate_date_range
def project_ids
Array(@project_ids)
......@@ -29,6 +32,14 @@ module Gitlab
errors.add(:created_before, :invalid) if created_after > created_before
end
def validate_date_range
return if created_after.nil? || created_before.nil?
if (created_before - created_after).days > MAX_RANGE_DAYS
errors.add(:created_after, s_('CycleAnalytics|The given date range is larger than 180 days'))
end
end
end
end
end
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
describe Gitlab::Analytics::CycleAnalytics::RequestParams do
let(:params) { { created_after: '2018-01-01', created_before: '2019-01-01' } }
let(:params) { { created_after: '2019-01-01', created_before: '2019-03-01' } }
subject { described_class.new(params) }
......@@ -32,6 +32,17 @@ describe Gitlab::Analytics::CycleAnalytics::RequestParams do
expect(subject.errors.messages[:created_before]).not_to be_empty
end
end
context 'when the date range exceeds 180 days' do
before do
params[:created_before] = '2019-07-15'
end
it 'is invalid' do
expect(subject).not_to be_valid
expect(subject.errors.messages[:created_after]).to include(s_('CycleAnalytics|The given date range is larger than 180 days'))
end
end
end
it 'casts `created_after` to date' do
......
......@@ -79,7 +79,7 @@ end
RSpec.shared_examples 'cycle analytics data endpoint examples' do
before do
params[:created_after] = '2019-01-01'
params[:created_before] = '2020-01-01'
params[:created_before] = '2019-04-01'
end
context 'when valid parameters are given' do
......@@ -153,4 +153,13 @@ RSpec.shared_examples 'cycle analytics data endpoint examples' do
include_examples 'example for invalid parameter'
end
context 'when the date range exceeds 180 days' do
before do
params[:created_after] = '2019-01-01'
params[:created_before] = '2019-08-01'
end
include_examples 'example for invalid parameter'
end
end
......@@ -5915,6 +5915,9 @@ msgstr ""
msgid "CycleAnalytics|Tasks by type"
msgstr ""
msgid "CycleAnalytics|The given date range is larger than 180 days"
msgstr ""
msgid "CycleAnalytics|Total days to completion"
msgstr ""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment