Commit e3cf7703 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'rd-fix-wrong-license-error-message' into 'master'

Fix validation error message when historical data is empty

Closes #5268

See merge request gitlab-org/gitlab-ee!4961
parents 46b29428 5c11c0a4
...@@ -341,6 +341,10 @@ class License < ActiveRecord::Base ...@@ -341,6 +341,10 @@ class License < ActiveRecord::Base
HistoricalData.during(from..to).maximum(:active_user_count) || 0 HistoricalData.during(from..to).maximum(:active_user_count) || 0
end end
def empty_historical_max?
historical_max == 0
end
def check_users_limit def check_users_limit
return unless restricted_user_count return unless restricted_user_count
...@@ -350,8 +354,9 @@ class License < ActiveRecord::Base ...@@ -350,8 +354,9 @@ class License < ActiveRecord::Base
return if restricted_user_count >= historical_max return if restricted_user_count >= historical_max
end end
overage = historical_max - restricted_user_count user_count = empty_historical_max? ? current_active_users_count : historical_max
add_limit_error(user_count: historical_max, restricted_user_count: restricted_user_count, overage: overage)
add_limit_error(current_period: empty_historical_max?, user_count: user_count)
end end
def check_trueup def check_trueup
...@@ -359,7 +364,6 @@ class License < ActiveRecord::Base ...@@ -359,7 +364,6 @@ class License < ActiveRecord::Base
trueup_from = Date.parse(restrictions[:trueup_from]) rescue (starts_at - 1.year) trueup_from = Date.parse(restrictions[:trueup_from]) rescue (starts_at - 1.year)
trueup_to = Date.parse(restrictions[:trueup_to]) rescue starts_at trueup_to = Date.parse(restrictions[:trueup_to]) rescue starts_at
max_historical = historical_max(trueup_from, trueup_to) max_historical = historical_max(trueup_from, trueup_to)
overage = current_active_users_count - restricted_user_count
expected_trueup_qty = if previous_user_count expected_trueup_qty = if previous_user_count
max_historical - previous_user_count max_historical - previous_user_count
else else
...@@ -368,7 +372,7 @@ class License < ActiveRecord::Base ...@@ -368,7 +372,7 @@ class License < ActiveRecord::Base
if trueup_qty >= expected_trueup_qty if trueup_qty >= expected_trueup_qty
if restricted_user_count < current_active_users_count if restricted_user_count < current_active_users_count
add_limit_error(trueup: true, user_count: current_active_users_count, restricted_user_count: restricted_user_count, overage: overage) add_limit_error(user_count: current_active_users_count)
end end
else else
message = "You have applied a True-up for #{trueup_qty} #{"user".pluralize(trueup_qty)} " message = "You have applied a True-up for #{trueup_qty} #{"user".pluralize(trueup_qty)} "
...@@ -379,8 +383,10 @@ class License < ActiveRecord::Base ...@@ -379,8 +383,10 @@ class License < ActiveRecord::Base
end end
end end
def add_limit_error(trueup: false, user_count:, restricted_user_count:, overage:) def add_limit_error(current_period: true, user_count:)
message = trueup ? "This GitLab installation currently has " : "During the year before this license started, this GitLab installation had " overage = user_count - restricted_user_count
message = current_period ? "This GitLab installation currently has " : "During the year before this license started, this GitLab installation had "
message << "#{number_with_delimiter(user_count)} active #{"user".pluralize(user_count)}, " message << "#{number_with_delimiter(user_count)} active #{"user".pluralize(user_count)}, "
message << "exceeding this license's limit of #{number_with_delimiter(restricted_user_count)} by " message << "exceeding this license's limit of #{number_with_delimiter(restricted_user_count)} by "
message << "#{number_with_delimiter(overage)} #{"user".pluralize(overage)}. " message << "#{number_with_delimiter(overage)} #{"user".pluralize(overage)}. "
......
---
title: Fix validation error message when historical data is empty
merge_request: 4961
author:
type: fixed
...@@ -34,6 +34,34 @@ describe License do ...@@ -34,6 +34,34 @@ describe License do
end end
end end
context 'without historical data' do
before do
create_list(:user, 2)
gl_license.restrictions = {
previous_user_count: 1,
active_user_count: User.active.count - 1
}
allow(license).to receive(:historical_max).and_return(0)
end
context 'with previous_user_count and active users above of license limit' do
it 'is invalid' do
expect(license).to be_invalid
end
it 'shows the proper error message' do
license.valid?
error_msg = "This GitLab installation currently has 2 active users, exceeding this license's limit of 1 by 1 user. "
error_msg << "Please upload a license for at least 2 users or contact sales at renewals@gitlab.com"
expect(license.errors[:base].first).to eq(error_msg)
end
end
end
context "when the active user count restriction is exceeded" do context "when the active user count restriction is exceeded" do
before do before do
gl_license.restrictions = { active_user_count: active_user_count - 1 } gl_license.restrictions = { active_user_count: active_user_count - 1 }
......
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