Commit f45a4154 authored by Doug Stull's avatar Doug Stull Committed by Alper Akgun

Consider namespaces with no plans as eligible for capping as free

- namespaces don't always get a subscription unless asked for.
parent e09a291d
......@@ -301,15 +301,18 @@ module EE
end
end
def on_existing_free_subscription?
def has_free_or_no_subscription?
# this is a side-effect free version of checking if a namespace
# is on a free plan - see https://gitlab.com/gitlab-org/gitlab/-/merge_requests/80839#note_851566461
strong_memoize(:on_existing_free_subscription) do
::Plan
.joins(:hosted_subscriptions)
.where(name: ::Plan::FREE)
.where(gitlab_subscriptions: { namespace_id: id })
.exists?
# is on a free plan or has no plan - see https://gitlab.com/gitlab-org/gitlab/-/merge_requests/80839#note_851566461
strong_memoize(:has_free_or_no_subscription) do
subscription = root_ancestor.gitlab_subscription
# there is a chance that subscriptions do not have a plan https://gitlab.com/gitlab-org/gitlab/-/merge_requests/81432#note_858514873
if subscription&.plan_name
subscription.plan_name == ::Plan::FREE
else
true
end
end
end
......@@ -472,7 +475,7 @@ module EE
def apply_free_user_cap?
return false unless ::Gitlab.com?
return false unless root_ancestor.on_existing_free_subscription?
return false unless has_free_or_no_subscription?
::Feature.enabled?(:free_user_cap, root_ancestor, default_enabled: :yaml)
end
......
......@@ -2255,7 +2255,7 @@ RSpec.describe Namespace do
context 'when no plan exists' do
let_it_be(:namespace) { create(:group) }
it { is_expected.to be false }
it { is_expected.to be true }
end
end
end
......@@ -2282,23 +2282,70 @@ RSpec.describe Namespace do
end
end
describe '#on_existing_free_subscription?', :saas do
describe '#has_free_or_no_subscription?', :saas do
it 'returns true with a free plan' do
namespace = create(:group_with_plan, plan: :free_plan)
expect(namespace.on_existing_free_subscription?).to be(true)
expect(namespace.has_free_or_no_subscription?).to be(true)
end
it 'returns false when the plan is not free' do
namespace = create(:group_with_plan, plan: :ultimate_plan)
expect(namespace.on_existing_free_subscription?).to be(false)
expect(namespace.has_free_or_no_subscription?).to be(false)
end
it 'returns true when there is no plan' do
namespace = create(:namespace)
expect(namespace.has_free_or_no_subscription?).to be(true)
end
it 'returns false when there is no plan' do
it 'returns true when there is a subscription with no plan' do
namespace = create(:namespace)
create(:gitlab_subscription, hosted_plan: nil, namespace: namespace)
expect(namespace.has_free_or_no_subscription?).to be(true)
end
context 'when it is a subgroup' do
let(:subgroup) { create(:group, parent: namespace) }
context 'with a free plan' do
let(:namespace) { create(:group_with_plan, plan: :free_plan) }
it 'returns true' do
expect(subgroup.has_free_or_no_subscription?).to be(true)
end
end
context 'with a plan that is not free' do
let(:namespace) { create(:group_with_plan, plan: :ultimate_plan) }
expect(namespace.on_existing_free_subscription?).to be(false)
it 'returns false' do
expect(subgroup.has_free_or_no_subscription?).to be(false)
end
end
context 'when there is no plan' do
let(:namespace) { create(:group) }
it 'returns true' do
expect(subgroup.has_free_or_no_subscription?).to be(true)
end
end
context 'when there is a subscription with no plan' do
let(:namespace) { create(:group) }
before do
create(:gitlab_subscription, hosted_plan: nil, namespace: namespace)
end
it 'returns true' do
expect(subgroup.has_free_or_no_subscription?).to be(true)
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