Skip to content
代码片段 群组 项目
未验证 提交 e7da102b 编辑于 作者: Adam Hegyi's avatar Adam Hegyi 提交者: GitLab
浏览文件

Merge branch '468050-refactor-finders-for-addons' into 'master'

Generalize the add on finder for namespaces

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/160115



Merged-by: default avatarAdam Hegyi <ahegyi@gitlab.com>
Approved-by: default avatarMohamed Hamda <mhamda@gitlab.com>
Approved-by: default avatarLee Tickett <ltickett@gitlab.com>
Approved-by: default avatarHarsimar Sandhu <hsandhu@gitlab.com>
Approved-by: default avatarAdam Hegyi <ahegyi@gitlab.com>
Reviewed-by: default avatarDoug Stull <dstull@gitlab.com>
Reviewed-by: default avatarSimon Tomlinson <stomlinson@gitlab.com>
Reviewed-by: default avatarMohamed Hamda <mhamda@gitlab.com>
Co-authored-by: default avatarDoug Stull <dstull@gitlab.com>
No related branches found
No related tags found
无相关合并请求
# frozen_string_literal: true
module GitlabSubscriptions
module DuoPro
class NamespaceAddOnPurchasesFinder
def initialize(namespace, trial: false, only_active: true)
@namespace = namespace
@trial = trial
@only_active = only_active
end
def execute
# There will only be one, but we want to return a collection here and then consume it outside of this
items = GitlabSubscriptions::AddOnPurchase.for_gitlab_duo_pro.by_namespace(namespace)
items = by_active(items)
by_trial(items)
end
private
attr_reader :namespace, :trial, :only_active
def by_trial(items)
return items unless trial
items.trial
end
def by_active(items)
return items unless only_active
items.active
end
end
end
end
# frozen_string_literal: true
module GitlabSubscriptions
class NamespaceAddOnPurchasesFinder
def initialize(namespace, add_on:, trial: false, only_active: true)
@namespace = namespace
@trial = trial
@only_active = only_active
@add_on = add_on
end
def execute
# There will only be one, but we want to return a collection here and then consume it outside of this
items = GitlabSubscriptions::AddOnPurchase.by_namespace(namespace)
items = by_add_on(items)
items = by_active(items)
by_trial(items)
end
private
attr_reader :namespace, :trial, :only_active, :add_on
def by_add_on(items)
case add_on
when :duo_pro
items.for_gitlab_duo_pro
when :duo_enterprise
items.for_duo_enterprise
else
raise ArgumentError, "Unknown add_on: #{add_on}"
end
end
def by_trial(items)
return items unless trial
items.trial
end
def by_active(items)
return items unless only_active
items.active
end
end
end
...@@ -3,19 +3,21 @@ ...@@ -3,19 +3,21 @@
module GitlabSubscriptions module GitlabSubscriptions
module DuoPro module DuoPro
def self.add_on_purchase_for_namespace(namespace) def self.add_on_purchase_for_namespace(namespace)
GitlabSubscriptions::DuoPro::NamespaceAddOnPurchasesFinder.new(namespace).execute.first GitlabSubscriptions::NamespaceAddOnPurchasesFinder.new(namespace, add_on: :duo_pro).execute.first
end end
def self.any_add_on_purchase_for_namespace(namespace) def self.any_add_on_purchase_for_namespace(namespace)
GitlabSubscriptions::DuoPro::NamespaceAddOnPurchasesFinder.new(namespace, only_active: false).execute.first GitlabSubscriptions::NamespaceAddOnPurchasesFinder
.new(namespace, add_on: :duo_pro, only_active: false).execute.first
end end
def self.no_add_on_purchase_for_namespace?(namespace) def self.no_add_on_purchase_for_namespace?(namespace)
GitlabSubscriptions::DuoPro::NamespaceAddOnPurchasesFinder.new(namespace, only_active: false).execute.none? GitlabSubscriptions::NamespaceAddOnPurchasesFinder
.new(namespace, add_on: :duo_pro, only_active: false).execute.none?
end end
def self.no_active_add_on_purchase_for_namespace?(namespace) def self.no_active_add_on_purchase_for_namespace?(namespace)
GitlabSubscriptions::DuoPro::NamespaceAddOnPurchasesFinder.new(namespace).execute.none? GitlabSubscriptions::NamespaceAddOnPurchasesFinder.new(namespace, add_on: :duo_pro).execute.none?
end end
end end
end end
...@@ -24,8 +24,8 @@ def self.show_duo_pro_discover?(namespace, user) ...@@ -24,8 +24,8 @@ def self.show_duo_pro_discover?(namespace, user)
end end
def self.add_on_purchase_for_namespace(namespace) def self.add_on_purchase_for_namespace(namespace)
GitlabSubscriptions::DuoPro::NamespaceAddOnPurchasesFinder GitlabSubscriptions::NamespaceAddOnPurchasesFinder
.new(namespace, trial: true, only_active: false) .new(namespace, add_on: :duo_pro, trial: true, only_active: false)
.execute .execute
.first .first
end end
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe GitlabSubscriptions::DuoPro::NamespaceAddOnPurchasesFinder, feature_category: :plan_provisioning do RSpec.describe GitlabSubscriptions::NamespaceAddOnPurchasesFinder, feature_category: :plan_provisioning do
describe '#execute' do describe '#execute' do
let_it_be(:namespace) { create(:group) } let_it_be(:namespace) { create(:group) }
subject(:execute) { described_class.new(namespace).execute } subject(:execute) { described_class.new(namespace, add_on: :duo_pro).execute }
context 'when add_on is not available' do context 'when add_on is not available' do
it { is_expected.to be_empty } it { is_expected.to be_empty }
...@@ -21,24 +21,24 @@ ...@@ -21,24 +21,24 @@
create(:gitlab_subscription_add_on_purchase, :active, add_on: add_on, namespace: namespace) create(:gitlab_subscription_add_on_purchase, :active, add_on: add_on, namespace: namespace)
end end
context 'with default values of non trial and active' do context 'with duo_pro default values of non trial and active' do
it { is_expected.to match_array([add_on_purchase]) } it { is_expected.to match_array([add_on_purchase]) }
end end
context 'when a namespace_id is provided' do context 'when a namespace_id is provided' do
subject(:execute) { described_class.new(namespace.id).execute } subject(:execute) { described_class.new(namespace.id, add_on: :duo_pro).execute }
it { is_expected.to match_array([add_on_purchase]) } it { is_expected.to match_array([add_on_purchase]) }
end end
context 'when filtering by trial' do context 'when filtering by trial' do
subject(:execute) { described_class.new(namespace, trial: true).execute } subject(:execute) { described_class.new(namespace, add_on: :duo_pro, trial: true).execute }
it { is_expected.to be_empty } it { is_expected.to be_empty }
end end
context 'when filtering by any active state' do context 'when filtering by any active state' do
subject(:execute) { described_class.new(namespace, only_active: false).execute } subject(:execute) { described_class.new(namespace, add_on: :duo_pro, only_active: false).execute }
it { is_expected.to match_array([add_on_purchase]) } it { is_expected.to match_array([add_on_purchase]) }
end end
...@@ -54,17 +54,27 @@ ...@@ -54,17 +54,27 @@
end end
context 'when filtering by trial' do context 'when filtering by trial' do
subject(:execute) { described_class.new(namespace, trial: true).execute } subject(:execute) { described_class.new(namespace, add_on: :duo_pro, trial: true).execute }
it { is_expected.to be_empty } it { is_expected.to be_empty }
end end
context 'when filtering by any active state' do context 'when filtering by any active state' do
subject(:execute) { described_class.new(namespace, only_active: false).execute } subject(:execute) { described_class.new(namespace, add_on: :duo_pro, only_active: false).execute }
it { is_expected.to match_array([add_on_purchase]) } it { is_expected.to match_array([add_on_purchase]) }
end end
end end
context 'with duo_enterprise add_on' do
let_it_be(:add_on_purchase) do
create(:gitlab_subscription_add_on_purchase, :active, :duo_enterprise, namespace: namespace)
end
subject(:execute) { described_class.new(namespace, add_on: :duo_enterprise).execute }
it { is_expected.to match_array([add_on_purchase]) }
end
end end
context 'with trial add_on_purchase' do context 'with trial add_on_purchase' do
...@@ -78,25 +88,25 @@ ...@@ -78,25 +88,25 @@
end end
context 'when a namespace_id is provided' do context 'when a namespace_id is provided' do
subject(:execute) { described_class.new(namespace.id).execute } subject(:execute) { described_class.new(namespace.id, add_on: :duo_pro).execute }
it { is_expected.to match_array([add_on_purchase]) } it { is_expected.to match_array([add_on_purchase]) }
end end
context 'when filtering by trial' do context 'when filtering by trial' do
subject(:execute) { described_class.new(namespace, trial: true).execute } subject(:execute) { described_class.new(namespace, add_on: :duo_pro, trial: true).execute }
it { is_expected.to match_array([add_on_purchase]) } it { is_expected.to match_array([add_on_purchase]) }
context 'when filtering by any active state' do context 'when filtering by any active state' do
subject(:execute) { described_class.new(namespace, only_active: false).execute } subject(:execute) { described_class.new(namespace, add_on: :duo_pro, only_active: false).execute }
it { is_expected.to match_array([add_on_purchase]) } it { is_expected.to match_array([add_on_purchase]) }
end end
end end
context 'when filtering by any active state' do context 'when filtering by any active state' do
subject(:execute) { described_class.new(namespace, only_active: false).execute } subject(:execute) { described_class.new(namespace, add_on: :duo_pro, only_active: false).execute }
it { is_expected.to match_array([add_on_purchase]) } it { is_expected.to match_array([add_on_purchase]) }
end end
...@@ -112,24 +122,34 @@ ...@@ -112,24 +122,34 @@
end end
context 'when filtering by trial' do context 'when filtering by trial' do
subject(:execute) { described_class.new(namespace, trial: true).execute } subject(:execute) { described_class.new(namespace, add_on: :duo_pro, trial: true).execute }
it { is_expected.to be_empty } it { is_expected.to be_empty }
context 'when filtering by any active state' do context 'when filtering by any active state' do
subject(:execute) { described_class.new(namespace, trial: true, only_active: false).execute } subject(:execute) do
described_class.new(namespace, add_on: :duo_pro, trial: true, only_active: false).execute
end
it { is_expected.to match_array([add_on_purchase]) } it { is_expected.to match_array([add_on_purchase]) }
end end
end end
context 'when filtering by any active state' do context 'when filtering by any active state' do
subject(:execute) { described_class.new(namespace, only_active: false).execute } subject(:execute) { described_class.new(namespace, add_on: :duo_pro, only_active: false).execute }
it { is_expected.to match_array([add_on_purchase]) } it { is_expected.to match_array([add_on_purchase]) }
end end
end end
end end
end end
context 'with invalid add_on' do
subject(:execute) { described_class.new(namespace, add_on: :invalid_add_on).execute }
it 'raises an error' do
expect { execute }.to raise_error(ArgumentError)
end
end
end end
end end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册