Commit ea5eedfc authored by Diana Zubova's avatar Diana Zubova Committed by Doug Stull

Extend namespace gitlab_subscription API

Added exclude_guests field

Changelog: changed
EE: true
parent 2ba1442e
......@@ -621,7 +621,7 @@ Example response:
"name":"premium",
"trial":false,
"auto_renew":null,
"upgradable":false
"upgradable":false,
},
"usage": {
"seats_in_subscription":10,
......@@ -672,7 +672,7 @@ Example response:
"name":"premium",
"trial":false,
"auto_renew":null,
"upgradable":false
"upgradable":false,
},
"usage": {
"seats_in_subscription":80,
......@@ -711,7 +711,8 @@ Example response:
"name":"premium",
"trial":false,
"auto_renew":null,
"upgradable":false
"upgradable":false,
"exclude_guests":false,
},
"usage": {
"seats_in_subscription":80,
......
......@@ -357,9 +357,7 @@ module EE
# converting the array of user_ids to a Set which will have unique user_ids.
override :billed_user_ids
def billed_user_ids(requested_hosted_plan = nil)
exclude_guests = ([actual_plan_name, requested_hosted_plan] & [::Plan::GOLD, ::Plan::ULTIMATE, ::Plan::ULTIMATE_TRIAL]).any?
exclude_guests ? billed_user_ids_excluding_guests : billed_user_ids_including_guests
exclude_guests?(requested_hosted_plan) ? billed_user_ids_excluding_guests : billed_user_ids_including_guests
end
override :supports_events?
......@@ -367,6 +365,11 @@ module EE
feature_available?(:epics)
end
override :exclude_guests?
def exclude_guests?(requested_hosted_plan = nil)
([actual_plan_name, requested_hosted_plan] & [::Plan::GOLD, ::Plan::ULTIMATE, ::Plan::ULTIMATE_TRIAL]).any?
end
def marked_for_deletion?
marked_for_deletion_on.present? &&
feature_available?(:adjourned_deletion_for_projects_and_groups)
......
......@@ -472,6 +472,10 @@ module EE
end
end
def exclude_guests?
false
end
private
def free_user_cap
......
......@@ -23,6 +23,7 @@ class GitlabSubscription < ApplicationRecord
validates :namespace_id, uniqueness: true, presence: true
delegate :name, :title, to: :hosted_plan, prefix: :plan, allow_nil: true
delegate :exclude_guests?, to: :namespace
scope :with_hosted_plan, -> (plan_name) do
joins(:hosted_plan).where(trial: false, 'plans.name' => plan_name)
......
......@@ -10,6 +10,7 @@ module EE
expose :trial
expose :auto_renew
expose :upgradable?, as: :upgradable
expose :exclude_guests?, as: :exclude_guests
end
expose :usage do
......
......@@ -1320,6 +1320,33 @@ RSpec.describe Group do
end
end
describe '#exclude_guests?', :saas do
using RSpec::Parameterized::TableSyntax
let_it_be(:group, refind: true) { create(:group) }
where(:actual_plan_name, :requested_plan_name, :result) do
:free | nil | false
:premium | nil | false
:ultimate | nil | true
:ultimate_trial | nil | true
:gold | nil | true
:free | 'premium' | false
:free | 'ultimate' | true
:premium | 'ultimate' | true
:ultimate | 'ultimate' | true
end
with_them do
let!(:subscription) { build(:gitlab_subscription, actual_plan_name, namespace: group) }
it 'returns the expected result' do
expect(group.exclude_guests?(requested_plan_name)).to eq(result)
end
end
end
describe '#users_count' do
subject { group.users_count }
......
......@@ -1752,6 +1752,14 @@ RSpec.describe Namespace do
end
end
describe '#exclude_guests?' do
let(:namespace) { build(:namespace) }
it 'returns false' do
expect(namespace.exclude_guests?).to eq(false)
end
end
def create_project(repository_size:, lfs_objects_size:, repository_size_limit:)
create(:project, namespace: namespace, repository_size_limit: repository_size_limit).tap do |project|
create(:project_statistics, project: project, repository_size: repository_size, lfs_objects_size: lfs_objects_size)
......
......@@ -9,6 +9,8 @@ RSpec.describe GitlabSubscription, :saas do
let_it_be(plan) { create(plan) } # rubocop:disable Rails/SaveBang
end
it { is_expected.to delegate_method(:exclude_guests?).to(:namespace) }
describe 'default values', :freeze_time do
it 'defaults start_date to the current date' do
expect(subject.start_date).to eq(Date.today)
......
......@@ -563,16 +563,32 @@ RSpec.describe API::Namespaces do
do_get(owner)
expect(json_response.keys).to match_array(%w[plan usage billing])
expect(json_response['plan'].keys).to match_array(%w[name code trial upgradable auto_renew])
expect(json_response['plan'].keys).to match_array(%w[name code trial upgradable exclude_guests auto_renew])
expect(json_response['plan']['name']).to eq('Premium')
expect(json_response['plan']['code']).to eq('premium')
expect(json_response['plan']['trial']).to eq(false)
expect(json_response['plan']['upgradable']).to eq(true)
expect(json_response['plan']['exclude_guests']).to eq(false)
expect(json_response['usage'].keys).to match_array(%w[seats_in_subscription seats_in_use max_seats_used seats_owed])
expect(json_response['billing'].keys).to match_array(%w[subscription_start_date subscription_end_date trial_ends_on])
end
end
context 'for groups inherits exclude_guests' do
let_it_be(:ultimate_namespace) { create(:group) }
let_it_be(:gitlab_subscription) { create(:gitlab_subscription, hosted_plan: ultimate_plan, namespace: ultimate_namespace) }
before do
ultimate_namespace.add_owner(owner)
end
it 'returns true for Ultimate-like plans' do
get api("/namespaces/#{ultimate_namespace.id}/gitlab_subscription", owner)
expect(json_response['plan']['exclude_guests']).to eq(true)
end
end
context 'when namespace is a project namespace' do
it 'returns a 404 error' do
get api("/namespaces/#{project_namespace.id}/gitlab_subscription", admin)
......
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