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
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 :check_users_limit, if: :new_record?, unless: :validate_with_trueup?
validate :check_trueup, unless: :persisted?, if: :validate_with_trueup?
......@@ -14,6 +32,10 @@ class License < ActiveRecord::Base
scope :previous, -> { order(created_at: :desc).offset(1) }
class << self
def features_for_plan(plan)
FEATURES_BY_PLAN.fetch(plan, []).reduce({}, :merge)
end
def current
if RequestStore.active?
RequestStore.fetch(:current_license) { load_license }
......@@ -83,8 +105,14 @@ class License < ActiveRecord::Base
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
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
def add_on?(code)
......
---
title: Feature availability check using feature list AND license addons
merge_request:
author:
......@@ -211,6 +211,17 @@ describe License do
allow(described_class).to receive(:last).and_return(license)
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
context "when there is no license" do
let!(:license) { nil }
......@@ -317,6 +328,15 @@ describe License do
expect(license.add_ons['custom-domain']).to eq(2)
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
describe '#add_on?' do
......@@ -339,8 +359,8 @@ describe License do
end
end
def build_license_with_add_ons(add_ons)
gl_license = build(:gitlab_license, restrictions: { add_ons: add_ons })
def build_license_with_add_ons(add_ons, plan: nil)
gl_license = build(:gitlab_license, restrictions: { add_ons: add_ons, plan: plan })
build(:license, data: gl_license.export)
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