2022-03-25 17:08:24 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
describe CustomWizard::Subscription do
|
2023-02-08 13:32:24 +01:00
|
|
|
let(:guests_permitted) { get_wizard_fixture("wizard/guests_permitted") }
|
2023-02-23 19:24:11 +01:00
|
|
|
let!(:business_product_id) { SecureRandom.hex(8) }
|
|
|
|
let!(:standard_product_id) { SecureRandom.hex(8) }
|
|
|
|
let!(:community_product_id) { SecureRandom.hex(8) }
|
|
|
|
let!(:product_slugs) {
|
|
|
|
{
|
|
|
|
"#{business_product_id}" => "business",
|
|
|
|
"#{standard_product_id}" => "standard",
|
|
|
|
"#{community_product_id}" => "community"
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 13:32:24 +01:00
|
|
|
|
2022-03-25 17:08:24 +01:00
|
|
|
after do
|
|
|
|
undefine_client_classes
|
|
|
|
end
|
|
|
|
|
|
|
|
it "detects the subscription client" do
|
2023-02-24 14:17:00 +01:00
|
|
|
undefine_client_classes
|
2022-03-25 17:08:24 +01:00
|
|
|
expect(described_class.client_installed?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without a subscription client" do
|
|
|
|
it "is not subscribed" do
|
2023-10-03 19:54:15 +02:00
|
|
|
expect(described_class.subscribed?).to eq(true)
|
2022-03-25 17:08:24 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has none type" do
|
|
|
|
subscription = described_class.new
|
|
|
|
expect(subscription.type).to eq(:none)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "non subscriber features are included" do
|
|
|
|
expect(described_class.includes?(:wizard, :after_signup, true)).to eq(true)
|
|
|
|
end
|
|
|
|
|
2023-02-08 13:32:24 +01:00
|
|
|
it "subscriber features are not included" do
|
2022-03-25 17:08:24 +01:00
|
|
|
expect(described_class.includes?(:wizard, :permitted, {})).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with subscription client" do
|
|
|
|
before do
|
|
|
|
define_client_classes
|
|
|
|
end
|
|
|
|
|
|
|
|
it "detects the subscription client" do
|
|
|
|
expect(described_class.client_installed?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without a subscription" do
|
2023-02-23 19:24:11 +01:00
|
|
|
before do
|
2023-06-13 20:02:20 +02:00
|
|
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(nil)
|
2023-02-23 19:24:11 +01:00
|
|
|
end
|
|
|
|
|
2022-03-25 17:08:24 +01:00
|
|
|
it "has none type" do
|
|
|
|
expect(described_class.type).to eq(:none)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "non subscriber features are included" do
|
|
|
|
expect(described_class.includes?(:wizard, :after_signup, true)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "subscriber features are not included" do
|
|
|
|
expect(described_class.includes?(:wizard, :permitted, {})).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
context "with subscriptions" do
|
2023-05-19 10:29:54 +02:00
|
|
|
def get_subscription_result(product_ids)
|
2023-06-13 20:02:20 +02:00
|
|
|
result = DiscourseSubscriptionClient::Subscriptions::Result.new
|
2023-02-23 19:24:11 +01:00
|
|
|
result.supplier = SubscriptionClientSupplier.new(product_slugs)
|
|
|
|
result.resource = SubscriptionClientResource.new
|
2023-05-19 10:29:54 +02:00
|
|
|
result.subscriptions = product_ids.map { |product_id| SubscriptionClientSubscription.new(product_id) }
|
2023-05-04 17:25:10 +02:00
|
|
|
result.products = product_slugs
|
2023-02-23 19:24:11 +01:00
|
|
|
result
|
|
|
|
end
|
2023-05-19 10:29:54 +02:00
|
|
|
let!(:business_subscription_result) { get_subscription_result([business_product_id]) }
|
|
|
|
let!(:standard_subscription_result) { get_subscription_result([standard_product_id]) }
|
|
|
|
let!(:community_subscription_result) { get_subscription_result([community_product_id]) }
|
|
|
|
let!(:multiple_subscription_result) { get_subscription_result([community_product_id, business_product_id]) }
|
2023-02-23 19:24:11 +01:00
|
|
|
|
2023-02-08 13:32:24 +01:00
|
|
|
it "handles mapped values" do
|
2023-06-13 20:02:20 +02:00
|
|
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(standard_subscription_result)
|
2023-02-08 13:32:24 +01:00
|
|
|
expect(described_class.includes?(:wizard, :permitted, guests_permitted["permitted"])).to eq(true)
|
|
|
|
|
2023-06-13 20:02:20 +02:00
|
|
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(community_subscription_result)
|
2023-02-08 13:32:24 +01:00
|
|
|
expect(described_class.includes?(:wizard, :permitted, guests_permitted["permitted"])).to eq(false)
|
|
|
|
end
|
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
context "with a standard subscription" do
|
|
|
|
before do
|
2023-06-13 20:02:20 +02:00
|
|
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(standard_subscription_result)
|
2023-02-23 19:24:11 +01:00
|
|
|
end
|
2022-03-25 17:08:24 +01:00
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
it "detects standard type" do
|
|
|
|
expect(described_class.type).to eq(:standard)
|
|
|
|
end
|
2022-03-25 17:08:24 +01:00
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
it "standard features are included" do
|
|
|
|
expect(described_class.includes?(:wizard, :type, 'send_message')).to eq(true)
|
|
|
|
end
|
2022-03-25 17:08:24 +01:00
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
it "business features are not included" do
|
|
|
|
expect(described_class.includes?(:action, :type, 'create_category')).to eq(false)
|
|
|
|
end
|
2022-03-25 17:08:24 +01:00
|
|
|
end
|
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
context "with a business subscription" do
|
|
|
|
before do
|
2023-06-13 20:02:20 +02:00
|
|
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(business_subscription_result)
|
2023-02-23 19:24:11 +01:00
|
|
|
end
|
2022-03-25 17:08:24 +01:00
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
it "detects business type" do
|
|
|
|
expect(described_class.type).to eq(:business)
|
|
|
|
end
|
2022-03-25 17:08:24 +01:00
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
it "business features are included" do
|
|
|
|
expect(described_class.includes?(:action, :type, 'create_category')).to eq(true)
|
|
|
|
end
|
2022-09-23 15:52:05 +02:00
|
|
|
end
|
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
context "with a community subscription" do
|
|
|
|
before do
|
2023-06-13 20:02:20 +02:00
|
|
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(community_subscription_result)
|
2023-02-23 19:24:11 +01:00
|
|
|
end
|
2022-09-23 15:52:05 +02:00
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
it "detects community type" do
|
|
|
|
expect(described_class.type).to eq(:community)
|
|
|
|
end
|
2022-09-23 15:52:05 +02:00
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
it "community features are included" do
|
|
|
|
expect(described_class.includes?(:action, :type, 'create_category')).to eq(true)
|
|
|
|
end
|
2022-03-25 17:08:24 +01:00
|
|
|
end
|
2023-05-19 10:29:54 +02:00
|
|
|
|
|
|
|
context "with multiple subscriptions" do
|
|
|
|
before do
|
2023-06-13 20:02:20 +02:00
|
|
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(multiple_subscription_result)
|
2023-05-19 10:29:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "detects correct type in hierarchy" do
|
|
|
|
expect(described_class.type).to eq(:business)
|
|
|
|
end
|
|
|
|
end
|
2022-03-25 17:08:24 +01:00
|
|
|
end
|
|
|
|
end
|
2023-06-29 10:26:39 +02:00
|
|
|
|
|
|
|
context "with environment variable" do
|
|
|
|
before do
|
|
|
|
ENV["CUSTOM_WIZARD_PRODUCT_SLUG"] = "standard"
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
ENV["CUSTOM_WIZARD_PRODUCT_SLUG"] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "enables the relevant subscription" do
|
|
|
|
expect(described_class.type).to eq(:standard)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a subscription" do
|
|
|
|
before do
|
|
|
|
enable_subscription("business")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "respects the subscription" do
|
|
|
|
expect(described_class.type).to eq(:business)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-03-25 17:08:24 +01:00
|
|
|
end
|