Commit 4e54a7e5 authored by Job van der Voort's avatar Job van der Voort

Merge branch 'validate-user-count-before-issue-date' into 'master'

Validate maximum active user count up until license issue date.

Step 1 of #298

cc @sytse @job

See merge request !402
parents 064170d3 bf29e33d
...@@ -219,7 +219,7 @@ GEM ...@@ -219,7 +219,7 @@ GEM
diff-lcs (~> 1.1) diff-lcs (~> 1.1)
mime-types (~> 1.15) mime-types (~> 1.15)
posix-spawn (~> 0.3) posix-spawn (~> 0.3)
gitlab-license (0.0.2) gitlab-license (0.0.3)
gitlab-linguist (3.0.1) gitlab-linguist (3.0.1)
charlock_holmes (~> 0.6.6) charlock_holmes (~> 0.6.6)
escape_utils (~> 0.2.4) escape_utils (~> 0.2.4)
......
...@@ -3,6 +3,8 @@ class HistoricalData < ActiveRecord::Base ...@@ -3,6 +3,8 @@ class HistoricalData < ActiveRecord::Base
# HistoricalData.during((Date.today - 1.year)..Date.today).average(:active_user_count) # HistoricalData.during((Date.today - 1.year)..Date.today).average(:active_user_count)
scope :during, ->(range) { where(date: range) } scope :during, ->(range) { where(date: range) }
# HistoricalData.up_until(Date.today - 1.month).average(:active_user_count)
scope :up_until, ->(date) { where("date <= :date", date: date) }
class << self class << self
def track! def track!
......
...@@ -96,27 +96,23 @@ class License < ActiveRecord::Base ...@@ -96,27 +96,23 @@ class License < ActiveRecord::Base
return unless self.license? && self.restricted?(:active_user_count) return unless self.license? && self.restricted?(:active_user_count)
restricted_user_count = self.restrictions[:active_user_count] restricted_user_count = self.restrictions[:active_user_count]
active_user_count = User.active.count
historical_active_user_count = HistoricalData.maximum(:active_user_count) || 0
max_active_user_count = [active_user_count, historical_active_user_count].max date_range = (self.starts_at - 1.year)..self.starts_at
active_user_count = HistoricalData.during(date_range).maximum(:active_user_count) || 0
return unless active_user_count
return if max_active_user_count < restricted_user_count return if active_user_count < restricted_user_count
overage = max_active_user_count - restricted_user_count overage = active_user_count - restricted_user_count
message = "" message = ""
message << message << "During the year before this license started, this GitLab installation had "
if historical_active_user_count > active_user_count message << "#{number_with_delimiter active_user_count} active #{"user".pluralize(active_user_count)}, "
"At one point, this GitLab installation had "
else
"This GitLab installation has "
end
message << "#{number_with_delimiter max_active_user_count} active #{"user".pluralize(max_active_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)}. "
message << "Please upload a license for at least " message << "Please upload a license for at least "
message << "#{number_with_delimiter max_active_user_count} #{"user".pluralize(max_active_user_count)}." message << "#{number_with_delimiter active_user_count} #{"user".pluralize(active_user_count)}."
self.errors.add(:base, message) self.errors.add(:base, message)
end end
......
...@@ -20,12 +20,12 @@ ...@@ -20,12 +20,12 @@
.panel-heading .panel-heading
Details Details
%ul.well-list %ul.well-list
%li
%span.light Issued:
%strong= time_ago_with_tooltip @license.issued_at
%li %li
%span.light Uploaded: %span.light Uploaded:
%strong= time_ago_with_tooltip @license.created_at %strong= time_ago_with_tooltip @license.created_at
%li
%span.light Started:
%strong= time_ago_with_tooltip @license.starts_at
%li %li
%span.light %span.light
- if @license.expired? - if @license.expired?
...@@ -65,7 +65,8 @@ ...@@ -65,7 +65,8 @@
%strong %strong
Exceeds license limit Exceeds license limit
- historical = HistoricalData.maximum(:active_user_count) - date_range = (Date.today - 1.year)..Date.today
- historical = HistoricalData.during(date_range).maximum(:active_user_count)
- if historical - if historical
%li %li
%span.light Maximum active users: %span.light Maximum active users:
...@@ -106,8 +107,8 @@ ...@@ -106,8 +107,8 @@
%tr %tr
- @license.licensee.keys.each do |label| - @license.licensee.keys.each do |label|
%th= label %th= label
%th Issued at
%th Uploaded at %th Uploaded at
%th Started at
%th Expired at %th Expired at
%th Active users %th Active users
%tbody %tbody
...@@ -117,10 +118,10 @@ ...@@ -117,10 +118,10 @@
%td= license.licensee[label] %td= license.licensee[label]
%td %td
%span %span
= license.issued_at = license.created_at
%td %td
%span %span
= license.created_at = license.starts_at
%td %td
%span %span
= license.expires_at || "Never" = license.expires_at || "Never"
......
...@@ -211,7 +211,7 @@ FactoryGirl.define do ...@@ -211,7 +211,7 @@ FactoryGirl.define do
end end
factory :gitlab_license, class: "Gitlab::License" do factory :gitlab_license, class: "Gitlab::License" do
issued_at { Date.today - 1.month } starts_at { Date.today - 1.month }
licensee do licensee do
{ "Name" => Faker::Name.name } { "Name" => Faker::Name.name }
end end
......
...@@ -8,13 +8,19 @@ describe HistoricalData do ...@@ -8,13 +8,19 @@ describe HistoricalData do
end end
describe ".during" do describe ".during" do
it "returns the historical data during the given period" do it "returns the historical data during the specified period" do
expect(HistoricalData.during(Date.new(2014, 1, 1)..Date.new(2014, 12, 31)).average(:active_user_count)).to eq(650) expect(HistoricalData.during(Date.new(2014, 1, 1)..Date.new(2014, 12, 31)).average(:active_user_count)).to eq(650)
end end
end end
describe ".up_until" do
it "returns the historical data up until the specified date" do
expect(HistoricalData.up_until(Date.new(2014, 6, 1)).average(:active_user_count)).to eq(350)
end
end
describe ".at" do describe ".at" do
it "returns the historical data at the given date" do it "returns the historical data at the specified date" do
expect(HistoricalData.at(Date.new(2014, 8, 1)).active_user_count).to eq(800) expect(HistoricalData.at(Date.new(2014, 8, 1)).active_user_count).to eq(800)
end end
end end
......
...@@ -23,8 +23,10 @@ describe License do ...@@ -23,8 +23,10 @@ describe License do
end end
end end
describe "Active user count" do describe "Historical active user count" do
let(:active_user_count) { User.active.count } let(:active_user_count) { User.active.count + 10 }
let(:date) { License.current.starts_at }
let!(:historical_data) { HistoricalData.create!(date: date, active_user_count: active_user_count) }
context "when there is no active user count restriction" do context "when there is no active user count restriction" do
it "is valid" do it "is valid" do
...@@ -37,39 +39,34 @@ describe License do ...@@ -37,39 +39,34 @@ describe License do
gl_license.restrictions = { active_user_count: active_user_count - 1 } gl_license.restrictions = { active_user_count: active_user_count - 1 }
end end
it "is invalid" do context "when the license started" do
expect(license).to_not be_valid it "is invalid" do
expect(license).to_not be_valid
end
end end
end
context "when the active user count restriction is not exceeded" do context "after the license started" do
before do let(:date) { Date.today }
gl_license.restrictions = { active_user_count: active_user_count + 1 }
end
it "is valid" do it "is valid" do
expect(license).to be_valid expect(license).to be_valid
end
end end
end
end
describe "Historical active user count" do context "in the year before the license started" do
let(:active_user_count) { User.active.count + 10 } let(:date) { License.current.starts_at - 6.months }
let!(:historical_data) { HistoricalData.create!(date: License.current.issued_at, active_user_count: active_user_count) }
context "when there is no active user count restriction" do it "is invalid" do
it "is valid" do expect(license).to_not be_valid
expect(license).to be_valid end
end end
end
context "when the active user count restriction is exceeded" do context "earlier than a year before the license started" do
before do let(:date) { License.current.starts_at - 2.years }
gl_license.restrictions = { active_user_count: active_user_count - 1 }
end
it "is invalid" do it "is valid" do
expect(license).to_not be_valid expect(license).to be_valid
end
end end
end end
......
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