Commit d80fc724 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Feature availability check using feature list AND license addons

parent 40dac302
class License < ActiveRecord::Base class License < ActiveRecord::Base
include ActionView::Helpers::NumberHelper include ActionView::Helpers::NumberHelper
EES_FEATURES = [
# ..
].freeze
EEP_FEATURES = [
*EES_FEATURES,
{ 'GitLab_DeployBoard' => 1 },
{ 'GitLab_FileLocks' => 1 },
{ 'GitLab_Geo' => 1 },
{ 'GitLab_Auditor_User' => 1 },
{ 'GitLab_ServiceDesk' => 1 },
].freeze
FEATURES_BY_PLAN = {
'starter' => EES_FEATURES,
'premium' => EEP_FEATURES,
}.freeze
validate :valid_license validate :valid_license
validate :check_users_limit, if: :new_record?, unless: :validate_with_trueup? validate :check_users_limit, if: :new_record?, unless: :validate_with_trueup?
validate :check_trueup, unless: :persisted?, if: :validate_with_trueup? validate :check_trueup, unless: :persisted?, if: :validate_with_trueup?
...@@ -14,6 +32,10 @@ class License < ActiveRecord::Base ...@@ -14,6 +32,10 @@ class License < ActiveRecord::Base
scope :previous, -> { order(created_at: :desc).offset(1) } scope :previous, -> { order(created_at: :desc).offset(1) }
class << self class << self
def features_for_plan(plan)
FEATURES_BY_PLAN.fetch(plan, []).reduce({}, :merge)
end
def current def current
if RequestStore.active? if RequestStore.active?
RequestStore.fetch(:current_license) { load_license } RequestStore.fetch(:current_license) { load_license }
...@@ -83,8 +105,14 @@ class License < ActiveRecord::Base ...@@ -83,8 +105,14 @@ class License < ActiveRecord::Base
end end
end end
# New licenses persists only the `plan` (premium, starter, ..). But, old licenses
# keep `add_ons`, therefore this method needs to be backward-compatible in that sense.
# See https://gitlab.com/gitlab-org/gitlab-ee/issues/2019
def add_ons def add_ons
restricted_attr(:add_ons, {}) explicit_add_ons = restricted_attr(:add_ons, {})
plan_features = self.class.features_for_plan(plan)
explicit_add_ons.merge(plan_features)
end end
def add_on?(code) def add_on?(code)
......
---
title: Feature availability check using feature list AND license addons
merge_request:
author:
...@@ -211,6 +211,17 @@ describe License do ...@@ -211,6 +211,17 @@ describe License do
allow(described_class).to receive(:last).and_return(license) allow(described_class).to receive(:last).and_return(license)
end end
describe '.features_for_plan' do
it 'returns features for given plan' do
expect(described_class.features_for_plan('premium'))
.to include({ 'GitLab_DeployBoard' => 1, 'GitLab_FileLocks' => 1 })
end
it 'returns empty Hash if no features for given plan' do
expect(described_class.features_for_plan('starter')).to eq({})
end
end
describe ".current" do describe ".current" do
context "when there is no license" do context "when there is no license" do
let!(:license) { nil } let!(:license) { nil }
...@@ -317,6 +328,15 @@ describe License do ...@@ -317,6 +328,15 @@ describe License do
expect(license.add_ons['custom-domain']).to eq(2) expect(license.add_ons['custom-domain']).to eq(2)
end end
end end
context 'with extra features mapped by plan' do
it 'returns all available add-ons and extra features' do
license = build_license_with_add_ons({ 'support' => 1 }, plan: 'premium')
eep_features = License::EEP_FEATURES.reduce({}, :merge).keys
expect(license.add_ons.keys).to include('support', *eep_features)
end
end
end end
describe '#add_on?' do describe '#add_on?' do
...@@ -339,8 +359,8 @@ describe License do ...@@ -339,8 +359,8 @@ describe License do
end end
end end
def build_license_with_add_ons(add_ons) def build_license_with_add_ons(add_ons, plan: nil)
gl_license = build(:gitlab_license, restrictions: { add_ons: add_ons }) gl_license = build(:gitlab_license, restrictions: { add_ons: add_ons, plan: plan })
build(:license, data: gl_license.export) build(:license, data: gl_license.export)
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