Commit 7f48e84d authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Fix logic for displaying gold trial callout

This makes it the same with previous behavior where a user that
is a Maintainer / Developer / Reporter of a paid plan should still
see the Gold trial banner.
parent 1a9fda7a
......@@ -120,7 +120,7 @@ module EE
end
def has_no_trial_or_paid_plan?(user)
return false if user.has_paid_namespace?
return false if user.owns_paid_namespace?
!user.any_namespace_with_trial?
end
......
......@@ -217,14 +217,14 @@ module EE
def any_namespace_with_trial?
::Namespace
.from("(#{namespace_union(:trial_ends_on)}) #{::Namespace.table_name}")
.from("(#{namespace_union_for_owned(:trial_ends_on)}) #{::Namespace.table_name}")
.where('trial_ends_on > ?', Time.now.utc)
.any?
end
def any_namespace_without_trial?
::Namespace
.from("(#{namespace_union(:trial_ends_on)}) #{::Namespace.table_name}")
.from("(#{namespace_union_for_owned(:trial_ends_on)}) #{::Namespace.table_name}")
.where(trial_ends_on: nil)
.any?
end
......@@ -233,7 +233,15 @@ module EE
::Namespace
.from("(#{namespace_union_for_reporter_developer_maintainer_owned}) #{::Namespace.table_name}")
.include_gitlab_subscription
.where(gitlab_subscriptions: { hosted_plan_id: Plan.where(name: Plan::PAID_HOSTED_PLANS) })
.where(gitlab_subscriptions: { hosted_plan: Plan.where(name: Plan::PAID_HOSTED_PLANS) })
.any?
end
def owns_paid_namespace?
::Namespace
.from("(#{namespace_union_for_owned}) #{::Namespace.table_name}")
.include_gitlab_subscription
.where(gitlab_subscriptions: { hosted_plan: Plan.where(name: Plan::PAID_HOSTED_PLANS) })
.any?
end
......@@ -335,7 +343,7 @@ module EE
private
def namespace_union(select = :id)
def namespace_union_for_owned(select = :id)
::Gitlab::SQL::Union.new([
::Namespace.select(select).where(type: nil, owner: self),
owned_groups.select(select).where(parent_id: nil)
......
......@@ -1030,13 +1030,14 @@ describe User do
end
end
describe '#has_paid_namespace?' do
context 'paid namespaces' do
let_it_be(:user) { create(:user) }
let_it_be(:gold_group) { create(:group_with_plan, plan: :gold_plan) }
let_it_be(:bronze_group) { create(:group_with_plan, plan: :bronze_plan) }
let_it_be(:free_group) { create(:group_with_plan, plan: :free_plan) }
let_it_be(:group_without_plan) { create(:group) }
describe '#has_paid_namespace?' do
context 'when the user has Reporter or higher on at least one paid group' do
it 'returns true' do
gold_group.add_reporter(user)
......@@ -1064,4 +1065,34 @@ describe User do
end
end
end
describe '#owns_paid_namespace?' do
context 'when the user is an owner of at least one paid group' do
it 'returns true' do
gold_group.add_owner(user)
bronze_group.add_owner(user)
expect(user.owns_paid_namespace?).to eq(true)
end
end
context 'when the user is only a Maintainer on paid groups' do
it 'returns false' do
gold_group.add_maintainer(user)
bronze_group.add_maintainer(user)
free_group.add_owner(user)
expect(user.owns_paid_namespace?).to eq(false)
end
end
context 'when the user is not a member of any groups with plans' do
it 'returns false' do
group_without_plan.add_owner(user)
expect(user.owns_paid_namespace?).to eq(false)
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