0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-23 18:00:29 +01:00
discourse-custom-wizard/spec/components/custom_wizard/subscription_spec.rb

150 Zeilen
4,8 KiB
Ruby

2022-03-25 17:08:24 +01:00
# frozen_string_literal: true
describe CustomWizard::Subscription do
let(:guests_permitted) { get_wizard_fixture("wizard/guests_permitted") }
let!(:business_product_id) { SecureRandom.hex(8) }
let!(:standard_product_id) { SecureRandom.hex(8) }
let!(:community_product_id) { SecureRandom.hex(8) }
2024-10-16 13:52:03 +02:00
let!(:product_slugs) do
{
"#{business_product_id}" => "business",
"#{standard_product_id}" => "standard",
2024-10-16 13:52:03 +02:00
"#{community_product_id}" => "community",
}
2024-10-16 13:52:03 +02:00
end
context "with subscription client gem mocked out" do
2022-03-25 17:08:24 +01:00
context "without a subscription" do
2024-10-16 13:52:03 +02:00
before { DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(nil) }
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
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
result.supplier = SubscriptionClientSupplier.new(products: product_slugs)
result.resource = SubscriptionClientResource.new
2024-10-16 13:52:03 +02:00
result.subscriptions =
product_ids.map do |product_id|
::SubscriptionClientSubscription.new(product_id: product_id)
end
2023-05-04 17:25:10 +02:00
result.products = product_slugs
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]) }
2024-10-16 13:52:03 +02:00
let!(:multiple_subscription_result) do
get_subscription_result([community_product_id, business_product_id])
end
it "handles mapped values" do
2023-06-13 20:02:20 +02:00
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(standard_subscription_result)
2024-10-16 13:52:03 +02:00
expect(described_class.includes?(:wizard, :permitted, guests_permitted["permitted"])).to eq(
true,
)
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(
community_subscription_result,
)
expect(described_class.includes?(:wizard, :permitted, guests_permitted["permitted"])).to eq(
false,
)
end
context "with a standard subscription" do
before do
2024-10-16 13:52:03 +02:00
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(
standard_subscription_result,
)
end
2022-03-25 17:08:24 +01:00
it "detects standard type" do
expect(described_class.type).to eq(:standard)
end
2022-03-25 17:08:24 +01:00
it "standard features are included" do
2024-10-16 13:52:03 +02:00
expect(described_class.includes?(:wizard, :type, "send_message")).to eq(true)
end
2022-03-25 17:08:24 +01:00
it "business features are not included" do
2024-10-16 13:52:03 +02:00
expect(described_class.includes?(:action, :type, "create_category")).to eq(false)
end
2022-03-25 17:08:24 +01:00
end
context "with a business subscription" do
before do
2024-10-16 13:52:03 +02:00
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(
business_subscription_result,
)
end
2022-03-25 17:08:24 +01:00
it "detects business type" do
expect(described_class.type).to eq(:business)
end
2022-03-25 17:08:24 +01:00
it "business features are included" do
2024-10-16 13:52:03 +02:00
expect(described_class.includes?(:action, :type, "create_category")).to eq(true)
end
end
context "with a community subscription" do
before do
2024-10-16 13:52:03 +02:00
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(
community_subscription_result,
)
end
it "detects community type" do
expect(described_class.type).to eq(:community)
end
it "community features are included" do
2024-10-16 13:52:03 +02:00
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
2024-10-16 13:52:03 +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
2024-10-16 13:52:03 +02:00
before { ENV["CUSTOM_WIZARD_PRODUCT_SLUG"] = "standard" }
2023-06-29 10:26:39 +02:00
2024-10-16 13:52:03 +02:00
after { ENV["CUSTOM_WIZARD_PRODUCT_SLUG"] = nil }
2023-06-29 10:26:39 +02:00
it "enables the relevant subscription" do
expect(described_class.type).to eq(:standard)
end
context "with a subscription" do
2024-10-16 13:52:03 +02:00
before { enable_subscription("business") }
2023-06-29 10:26:39 +02:00
it "respects the subscription" do
expect(described_class.type).to eq(:business)
end
end
end
2022-03-25 17:08:24 +01:00
end