Commit 64de25f1 authored by Adam Hegyi's avatar Adam Hegyi

Date range validation for Cycle Analytics

This change adds date range validation (180 days) to cycle analytics
feature. The frontend validation is already implemented.
parent d4a9566e
---
title: Add date range validation for Cycle Analytics at the backend side
merge_request: 24254
author:
type: changed
...@@ -8,6 +8,8 @@ module Gitlab ...@@ -8,6 +8,8 @@ module Gitlab
include ActiveModel::Validations include ActiveModel::Validations
include ActiveModel::Attributes include ActiveModel::Attributes
MAX_RANGE_DAYS = 180.days.freeze
attr_writer :project_ids attr_writer :project_ids
attribute :created_after, :date attribute :created_after, :date
...@@ -17,6 +19,7 @@ module Gitlab ...@@ -17,6 +19,7 @@ module Gitlab
validates :created_before, presence: true validates :created_before, presence: true
validate :validate_created_before validate :validate_created_before
validate :validate_date_range
def project_ids def project_ids
Array(@project_ids) Array(@project_ids)
...@@ -29,6 +32,14 @@ module Gitlab ...@@ -29,6 +32,14 @@ module Gitlab
errors.add(:created_before, :invalid) if created_after > created_before errors.add(:created_before, :invalid) if created_after > created_before
end 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 end
end end
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::Analytics::CycleAnalytics::RequestParams do 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) } subject { described_class.new(params) }
...@@ -32,6 +32,17 @@ describe Gitlab::Analytics::CycleAnalytics::RequestParams do ...@@ -32,6 +32,17 @@ describe Gitlab::Analytics::CycleAnalytics::RequestParams do
expect(subject.errors.messages[:created_before]).not_to be_empty expect(subject.errors.messages[:created_before]).not_to be_empty
end end
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 end
it 'casts `created_after` to date' do it 'casts `created_after` to date' do
......
...@@ -79,7 +79,7 @@ end ...@@ -79,7 +79,7 @@ end
RSpec.shared_examples 'cycle analytics data endpoint examples' do RSpec.shared_examples 'cycle analytics data endpoint examples' do
before do before do
params[:created_after] = '2019-01-01' params[:created_after] = '2019-01-01'
params[:created_before] = '2020-01-01' params[:created_before] = '2019-04-01'
end end
context 'when valid parameters are given' do context 'when valid parameters are given' do
...@@ -153,4 +153,13 @@ RSpec.shared_examples 'cycle analytics data endpoint examples' do ...@@ -153,4 +153,13 @@ RSpec.shared_examples 'cycle analytics data endpoint examples' do
include_examples 'example for invalid parameter' include_examples 'example for invalid parameter'
end 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 end
...@@ -5903,6 +5903,9 @@ msgstr "" ...@@ -5903,6 +5903,9 @@ msgstr ""
msgid "CycleAnalytics|Tasks by type" msgid "CycleAnalytics|Tasks by type"
msgstr "" msgstr ""
msgid "CycleAnalytics|The given date range is larger than 180 days"
msgstr ""
msgid "CycleAnalytics|Total days to completion" msgid "CycleAnalytics|Total days to completion"
msgstr "" 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