repo
stringlengths
5
92
file_url
stringlengths
80
287
file_path
stringlengths
5
197
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 15:37:27
2026-01-04 17:58:21
truncated
bool
2 classes
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/edit/pwyw_spec.rb
spec/requests/products/edit/pwyw_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" describe("Product Edit pay what you want setting", type: :system, js: true) do include ProductEditPageHelpers let(:seller) { create(:named_seller) } let(:product) { create(:product_with_pdf_file, user: seller, size: 1024, custom_receipt: "Thanks!") } include_context "with switching account to user as admin for seller" it "displays the setting" do visit edit_link_path(product.unique_permalink) fill_in("Amount", with: "0") pwyw_toggle = find_field("Allow customers to pay what they want", disabled: true) expect(pwyw_toggle).to be_checked expect(page).to have_field("Minimum amount", disabled: true) expect(page).to have_field("Suggested amount") save_change wait_for_ajax expect(product.reload.customizable_price).to eq(true) expect(page).to have_field("Minimum amount", disabled: true) end it "tests that PWYW is still available" do visit edit_link_path(product.unique_permalink) fill_in "Amount", with: "0" pwyw_toggle = find_field("Allow customers to pay what they want", disabled: true) expect(pwyw_toggle).to be_checked fill_in "Suggested amount", with: "10" save_change wait_for_ajax in_preview do expect(page).to have_selector("[itemprop='price']", text: "$0+") end expect(product.reload.suggested_price_cents).to eq(10_00) expect(find_field("Suggested amount").value).to eq "10" end it "hides info alert and enables toggle when price is changed from $0 to a positive value" do visit edit_link_path(product.unique_permalink) fill_in "Amount", with: "0" expect(page).to have_content("Free products require a pay what they want price.") pwyw_toggle = find_field("Allow customers to pay what they want", disabled: true) expect(pwyw_toggle).to be_checked expect(pwyw_toggle).to be_disabled fill_in "Amount", with: "10" expect(page).not_to have_content("Free products require a pay what they want price.") pwyw_toggle = find_field("Allow customers to pay what they want") expect(pwyw_toggle).to be_checked expect(pwyw_toggle).not_to be_disabled end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/edit/options_spec.rb
spec/requests/products/edit/options_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" describe("ProductMoreOptionScenario", type: :system, js: true) do include ProductEditPageHelpers def visit_product_edit(link) visit("/products/#{link.unique_permalink}/edit") wait_for_ajax end def visit_product_edit_checkout_tab(link) visit("/products/#{link.unique_permalink}/edit#checkout") wait_for_ajax end def shipping_rows within :section, "Shipping destinations", match: :first do all("[aria-label='Shipping destination']") end end let(:seller) { create(:named_seller) } let(:product) { create(:product_with_pdf_file, user: seller) } include_context "with switching account to user as admin for seller" describe "variants & skus" do it "allows user to add and remove variants" do visit_product_edit(product) click_on "Add version" within version_rows.last do fill_in "Version name", with: "M" end expect do save_change product.reload product.variant_categories.reload end.to(change { product.variant_categories.alive.count }.by(1)) expect(product.variant_categories.alive.first.variants.count).to eq 1 within version_rows.last do within version_option_rows[0] do remove_version_option end end within_modal "Remove M?" do expect(page).to have_text("If you delete this version, its associated content will be removed as well. Your existing customers who purchased it will see the content from the current cheapest version as a fallback. If no version exists, they will see the product-level content.") click_on "Yes, remove" end expect do save_change product.reload end.to(change { product.variant_categories.alive.count }.to(0)) end it("accepts variants with % in their name") do visit_product_edit(product) click_on "Add version" within version_rows.last do fill_in "Version name", with: "99%" end save_change expect(product.reload.variant_categories.count).to eq 1 variant_category = product.reload.variant_categories.last expect(variant_category.variants.count).to eq 1 expect(variant_category.variants.last.name).to eq "99%" end end describe "shipping options" do before do product.is_physical = true product.require_shipping = true product.save! end it "does not show for non-physical products" do product.is_physical = false product.save! visit_product_edit(product) expect(page).to_not have_text("Shipping destinations") expect(page).to_not have_text("Add shipping destinations") expect(page).to_not have_text("Choose where you're able to ship your physical product to") end it "does not allow a save without any shipping options for a published product" do product.purchase_disabled_at = nil product.save! visit_product_edit(product) expect(page).to have_text("Add shipping destinations") expect(page).to have_text("Choose where you're able to ship your physical product to") save_change(expect_message: "The product needs to be shippable to at least one destination.") end it "allows shipping options to be saved" do visit_product_edit(product) expect(page).to have_text("Add shipping destinations") expect(page).to have_text("Choose where you're able to ship your physical product to") click_on("Add shipping destination") click_on("Add shipping destination") within shipping_rows[0] do page.select("United States", from: "Country") page.fill_in("Amount alone", with: "12") page.fill_in("Amount with others", with: "6") end within shipping_rows[1] do page.select("Germany", from: "Country") page.fill_in("Amount alone", with: "12") page.fill_in("Amount with others", with: "6") end save_change expect(page).to_not have_text("Add shipping destinations") expect(page).to_not have_text("Choose where you're able to ship your physical product to") product.reload expect(product.shipping_destinations.size).to eq(2) expect(product.shipping_destinations.alive.size).to eq(2) expect(product.shipping_destinations.first.country_code).to eq("US") expect(product.shipping_destinations.first.one_item_rate_cents).to eq(1200) expect(product.shipping_destinations.first.multiple_items_rate_cents).to eq(600) expect(product.shipping_destinations.second.country_code).to eq("DE") expect(product.shipping_destinations.second.one_item_rate_cents).to eq(1200) expect(product.shipping_destinations.second.multiple_items_rate_cents).to eq(600) end it "allows shipping options to be removed" do # Saved w/ a default shipping country due to validations, set it up to be as expected ShippingDestination.destroy_all shipping_destination1 = ShippingDestination.new(country_code: Compliance::Countries::DEU.alpha2, one_item_rate_cents: 10, multiple_items_rate_cents: 5) shipping_destination2 = ShippingDestination.new(country_code: Product::Shipping::ELSEWHERE, one_item_rate_cents: 20, multiple_items_rate_cents: 0) product.shipping_destinations << shipping_destination1 << shipping_destination2 visit_product_edit(product) expect(shipping_rows.size).to eq(2) expect(page).to_not have_text("Add shipping destinations") expect(page).to_not have_text("Choose where you're able to ship your physical product to") within shipping_rows[0] do page.select("Germany", from: "Country") page.fill_in("Amount alone", with: "0.10") page.fill_in("Amount with others", with: "0.05") end within shipping_rows[1] do page.select("Elsewhere", from: "Country") page.fill_in("Amount alone", with: "0.20") page.fill_in("Amount with others", with: "0") end within shipping_rows[0] do click_on "Remove shipping destination" end wait_for_ajax expect(shipping_rows.size).to eq(1) within shipping_rows[0] do expect(page).to have_field("Country", with: "ELSEWHERE") expect(page).to have_field("Amount alone", with: "0.20") expect(page).to have_field("Amount with others", with: "0") end save_change product.reload expect(product.shipping_destinations.size).to eq(2) expect(product.shipping_destinations.alive.size).to eq(1) expect(product.shipping_destinations.alive.first.country_code).to eq("ELSEWHERE") expect(product.shipping_destinations.alive.first.one_item_rate_cents).to eq(20) expect(product.shipping_destinations.alive.first.multiple_items_rate_cents).to eq(0) end describe "virtual countries" do it "allows shipping options to be saved" do visit_product_edit(product) expect(page).to have_text("Add shipping destinations") expect(page).to have_text("Choose where you're able to ship your physical product to") click_on("Add shipping destination") expect(shipping_rows.size).to eq(1) click_on("Add shipping destination") expect(shipping_rows.size).to eq(2) within shipping_rows[0] do page.select("Europe", from: "Country") page.fill_in("Amount alone", with: "12") page.fill_in("Amount with others", with: "6") end within shipping_rows[1] do page.select("Germany", from: "Country") page.fill_in("Amount alone", with: "12") page.fill_in("Amount with others", with: "6") end save_change expect(page).to_not have_text("Add shipping destinations") expect(page).to_not have_text("Choose where you're able to ship your physical product to") product.reload expect(product.shipping_destinations.size).to eq(2) expect(product.shipping_destinations.alive.size).to eq(2) expect(product.shipping_destinations.first.country_code).to eq("EUROPE") expect(product.shipping_destinations.first.one_item_rate_cents).to eq(1200) expect(product.shipping_destinations.first.multiple_items_rate_cents).to eq(600) expect(product.shipping_destinations.second.country_code).to eq("DE") expect(product.shipping_destinations.second.one_item_rate_cents).to eq(1200) expect(product.shipping_destinations.second.multiple_items_rate_cents).to eq(600) end it "allows shipping options to be removed" do # Saved w/ a default shipping country due to validations, set it up to be as expected ShippingDestination.all.each(&:destroy) shipping_destination1 = ShippingDestination.new(country_code: ShippingDestination::Destinations::ASIA, one_item_rate_cents: 10, multiple_items_rate_cents: 5, is_virtual_country: true) shipping_destination2 = ShippingDestination.new(country_code: Compliance::Countries::DEU.alpha2, one_item_rate_cents: 20, multiple_items_rate_cents: 0) product.shipping_destinations << shipping_destination1 << shipping_destination2 visit_product_edit(product) expect(page).to_not have_text("Add shipping destinations") expect(page).to_not have_text("Choose where you're able to ship your physical product to") expect(shipping_rows.size).to eq(2) within shipping_rows[0] do expect(page).to have_field("Country", with: "ASIA") expect(page).to have_field("Amount alone", with: "0.10") expect(page).to have_field("Amount with others", with: "0.05") end within shipping_rows[1] do expect(page).to have_field("Country", with: "DE") expect(page).to have_field("Amount alone", with: "0.20") expect(page).to have_field("Amount with others", with: "0") end within shipping_rows[0] do click_on "Remove shipping destination" end expect(shipping_rows.size).to eq(1) within shipping_rows[0] do expect(page).to have_field("Country", with: "DE") expect(page).to have_field("Amount alone", with: "0.20") expect(page).to have_field("Amount with others", with: "0") end save_change product.reload expect(product.shipping_destinations.size).to eq(2) expect(product.shipping_destinations.alive.size).to eq(1) expect(product.shipping_destinations.alive.first.country_code).to eq("DE") expect(product.shipping_destinations.alive.first.one_item_rate_cents).to eq(20) expect(product.shipping_destinations.alive.first.multiple_items_rate_cents).to eq(0) end end end it "marks the product as e-publication" do visit_product_edit(product) check "Mark product as e-publication for VAT purposes" expect do save_change end.to change { product.reload.is_epublication }.from(false).to(true) end it "does not show e-publication toggle for physical product" do product = create(:physical_product, user: seller) visit_product_edit(product) expect(page).not_to have_text("Mark product as e-publication for VAT purposes") end describe "Refund policy" do before do seller.update!(refund_policy_enabled: false) end it "creates a refund policy" do visit_product_edit(product) check "Specify a refund policy for this product" expect(page).not_to have_text("Copy from other products") select "7-day money back guarantee", from: "Refund period" fill_in "Fine print (optional)", with: "This is the fine print" expect do save_change end.to change { product.reload.product_refund_policy_enabled }.from(false).to(true) refund_policy = product.product_refund_policy expect(refund_policy.title).to eq("7-day money back guarantee") expect(refund_policy.fine_print).to eq("This is the fine print") end context "with other refund policies" do let!(:other_refund_policy) { create(:product_refund_policy, product: create(:product, user: seller)) } it "allows copying a refund policy" do visit_product_edit(product) check "Specify a refund policy for this product" select_disclosure "Copy from other products" do select_combo_box_option(other_refund_policy.product.name) click_on("Copy") end expect do save_change end.to change { product.reload.product_refund_policy_enabled }.from(false).to(true) refund_policy = product.product_refund_policy expect(refund_policy.max_refund_period_in_days).to eq(other_refund_policy.max_refund_period_in_days) expect(refund_policy.title).to eq(other_refund_policy.title) expect(refund_policy.fine_print).to eq(other_refund_policy.fine_print) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/edit/integrations/circle_integrations_spec.rb
spec/requests/products/edit/integrations/circle_integrations_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" describe("Product Edit Integrations edit - Circle", :without_circle_rate_limit, type: :system, js: true) do include ProductTieredPricingHelpers include ProductEditPageHelpers let(:seller) { create(:named_seller) } before :each do @product = create(:product_with_pdf_file, user: seller, size: 1024) @vcr_cassette_prefix = "Product Edit Integrations edit" end include_context "with switching account to user as admin for seller" describe "circle integration" do before do @vcr_cassette_prefix = "#{@vcr_cassette_prefix} circle integration" end it "modifies an existing integration correctly" do @product.active_integrations << create(:circle_integration) vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} modifies an existing integration correctly") do visit edit_link_path(@product) select("Gumroad [archived]", from: "Select a community") select("Discover", from: "Select a space group") save_change end end integration = Integration.first expect(integration.community_id).to eq("3512") expect(integration.space_group_id).to eq("30981") expect(integration.keep_inactive_members).to eq(false) end it "disables integration correctly" do @product.active_integrations << create(:circle_integration) expect do vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} disables integration correctly") do visit edit_link_path(@product) expect(page).to have_field("Type or paste your API token", with: GlobalConfig.get("CIRCLE_API_KEY")) uncheck "Invite your customers to a Circle community", allow_label_click: true save_change end end end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { @product.reload.active_integrations.count }.from(1).to(0) expect(ProductIntegration.first.deleted?).to eq(true) expect(@product.reload.live_product_integrations).to be_empty visit edit_link_path(@product) expect(page).to_not have_field("Type or paste your API token") end it "shows error on invalid api_key" do vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} shows error on invalid api_key") do visit edit_link_path(@product) end end check "Invite your customers to a Circle community", allow_label_click: true fill_in "Type or paste your API token", with: "invalid_api_key" click_on("Load communities") expect(page).to have_text("Could not retrieve communities from Circle. Please check your API key.") end context "integration for product with multiple versions" do before do @version_category = create(:variant_category, link: @product) @version_1 = create(:variant, variant_category: @version_category) @version_2 = create(:variant, variant_category: @version_category) @vcr_cassette_prefix = "#{@vcr_cassette_prefix} integration for product with multiple versions" end it "shows the integration toggle if product has an integration" do @product.active_integrations << create(:circle_integration) vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} shows the integration toggle if product has an integration") do visit edit_link_path(@product) end end expect(page).to have_text("Enable access to Circle community", count: 2) end it "hides the integration toggle if product does not have an integration" do vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} hides the integration toggle if product does not have an integration") do visit edit_link_path(@product) end end expect(page).to_not have_text("Enable access to Circle community") end it "creates an integration for the product and enables integration for a newly created version" do product = create(:product_with_pdf_file, user: seller) expect do vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} creates an integration for the product and enables integration for a newly created version") do visit edit_link_path(product) check "Invite your customers to a Circle community", allow_label_click: true fill_in "Type or paste your API token", with: GlobalConfig.get("CIRCLE_API_KEY") click_on("Load communities") select("Gumroad [archived]", from: "Select a community") select("Tests", from: "Select a space group") click_on("Add version") fill_in "Version name", with: "Files" click_on("Add version") within version_rows[0] do within version_option_rows[0] do fill_in "Version name", with: "New Version" check "Enable access to Circle community", allow_label_click: true end end save_change end end end.to change { Integration.count }.by(1) .and change { ProductIntegration.count }.by(1) .and change { BaseVariantIntegration.count }.by(1) product_integration = ProductIntegration.first base_variant_integration = BaseVariantIntegration.first integration = Integration.first expect(product_integration.integration).to eq(integration) expect(base_variant_integration.integration).to eq(integration) expect(base_variant_integration.base_variant.name).to eq("New Version") expect(product_integration.product).to eq(product) expect(integration.api_key).to eq(GlobalConfig.get("CIRCLE_API_KEY")) expect(integration.type).to eq(Integration.type_for(Integration::CIRCLE)) expect(integration.community_id).to eq("3512") expect(integration.space_group_id).to eq("43576") expect(integration.keep_inactive_members).to eq(false) end it "disables integration for a version" do integration = create(:circle_integration) @product.active_integrations << integration @version_1.active_integrations << integration version_integration = @version_1.base_variant_integrations.first expect do vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} disables integration for a version") do visit edit_link_path(@product) within version_rows[0] do within version_option_rows[0] do uncheck "Enable access to Circle community", allow_label_click: true end end save_change end end end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(0) .and change { @version_1.active_integrations.count }.from(1).to(0) expect(version_integration.reload.deleted?).to eq(true) expect(@version_1.reload.live_base_variant_integrations).to be_empty end it "disables integration for a version if integration is removed from the product" do integration = create(:circle_integration) @product.active_integrations << integration @version_1.active_integrations << integration version_integration = @version_1.base_variant_integrations.first product_integration = @product.product_integrations.first expect do vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} disables integration for a version if integration is removed from the product") do visit edit_link_path(@product) uncheck "Invite your customers to a Circle community", allow_label_click: true save_change end end end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(0) .and change { @version_1.active_integrations.count }.from(1).to(0) .and change { @product.active_integrations.count }.from(1).to(0) expect(version_integration.reload.deleted?).to eq(true) expect(product_integration.reload.deleted?).to eq(true) expect(@product.reload.live_product_integrations).to be_empty expect(@version_1.reload.live_base_variant_integrations).to be_empty end end context "integration for product with membership tiers" do before do @subscription_product = create(:membership_product_with_preset_tiered_pricing, user: seller) @tier_1 = @subscription_product.tiers[0] @tier_2 = @subscription_product.tiers[1] @vcr_cassette_prefix = "#{@vcr_cassette_prefix} integration for product with membership tiers" end it "shows the integration toggle if product has an integration" do @subscription_product.active_integrations << create(:circle_integration) vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} shows the integration toggle if product has an integration") do visit edit_link_path(@subscription_product) end end expect(page).to have_text("Enable access to Circle community", count: 2) end it "hides the integration toggle if product does not have an integration" do vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} hides the integration toggle if product does not have an integration") do visit edit_link_path(@subscription_product) end end expect(page).to_not have_text("Enable access to Circle community") end it "enables integration for a tier" do @subscription_product.active_integrations << create(:circle_integration) expect do vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} enables integration for a tier") do visit edit_link_path(@subscription_product) within tier_rows[1] do check "Enable access to Circle community", allow_label_click: true end save_change end end end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(1) integration = @subscription_product.active_integrations.first base_variant_integration = BaseVariantIntegration.first expect(base_variant_integration.integration).to eq(integration) expect(base_variant_integration.base_variant).to eq(@tier_2) end it "disables integration for a tier" do integration = create(:circle_integration) @subscription_product.active_integrations << integration @tier_1.active_integrations << integration tier_integration = @tier_1.base_variant_integrations.first expect do vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} disables integration for a tier") do visit edit_link_path(@subscription_product) within tier_rows[0] do uncheck "Enable access to Circle community", allow_label_click: true end save_change end end end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(0) .and change { @tier_1.active_integrations.count }.from(1).to(0) expect(tier_integration.reload.deleted?).to eq(true) expect(@tier_1.reload.live_base_variant_integrations).to be_empty end it "disables integration for a tier if integration is removed from the product" do integration = create(:circle_integration) @subscription_product.active_integrations << integration @tier_1.active_integrations << integration tier_integration = @tier_1.base_variant_integrations.first product_integration = @subscription_product.product_integrations.first expect do vcr_turned_on do VCR.use_cassette("#{@vcr_cassette_prefix} disables integration for a tier if integration is removed from the product") do visit edit_link_path(@subscription_product) uncheck "Invite your customers to a Circle community", allow_label_click: true save_change end end end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(0) .and change { @tier_1.active_integrations.count }.from(1).to(0) .and change { @subscription_product.active_integrations.count }.from(1).to(0) expect(tier_integration.reload.deleted?).to eq(true) expect(product_integration.reload.deleted?).to eq(true) expect(@subscription_product.reload.live_product_integrations).to be_empty expect(@tier_1.reload.live_base_variant_integrations).to be_empty end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/edit/integrations/discord_integrations_spec.rb
spec/requests/products/edit/integrations/discord_integrations_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" describe("Product Edit Integrations edit - Discord", type: :system, js: true) do include ProductTieredPricingHelpers include ProductEditPageHelpers let(:seller) { create(:named_seller) } before :each do @product = create(:product_with_pdf_file, user: seller, size: 1024) @vcr_cassette_prefix = "Product Edit Integrations edit" end describe "discord integration" do let(:discord_integration) { create(:discord_integration) } let(:server_id) { discord_integration.server_id } let(:server_name) { discord_integration.server_name } let(:username) { discord_integration.username } context "with proxy", billy: true do let(:host_with_port) { "127.0.0.1:31337" } # Specs are failing on Buildkite when the shared context below replaces the seller login; they pass on local # The issue is related to using Puffing Billy as the specs within `without proxy` work with the shared context # TODO: enable shared context (and remove before block), and investigate failure of specs on Buildkite before do login_as seller end # include_context "with switching account to user as admin for seller", host: "127.0.0.1:31337" it "adds a new integration" do proxy.stub("https://www.discord.com:443/api/oauth2/authorize").and_return(redirect_to: oauth_redirect_integrations_discord_index_url(code: "test_code", host: host_with_port)) WebMock.stub_request(:post, DISCORD_OAUTH_TOKEN_URL). to_return(status: 200, body: { access_token: "test_access_token", guild: { id: server_id, name: server_name } }.to_json, headers: { content_type: "application/json" }) WebMock.stub_request(:get, "#{Discordrb::API.api_base}/users/@me"). with(headers: { "Authorization" => "Bearer test_access_token" }). to_return(status: 200, body: { username: }.to_json, headers: { content_type: "application/json" }) expect do visit edit_link_url(@product, host: host_with_port) check "Invite your customers to a Discord server", allow_label_click: true click_on "Connect to Discord" expect(page).to have_button "Disconnect Discord" expect(page).to_not have_status(text: "Your integration is not assigned to any version. Check your versions' settings.") save_change end.to change { Integration.count }.by(1) .and change { ProductIntegration.count }.by(1) product_integration = ProductIntegration.last integration = Integration.last expect(product_integration.integration).to eq(integration) expect(product_integration.product).to eq(@product) expect(integration.type).to eq(Integration.type_for(Integration::DISCORD)) expect(integration.server_id).to eq(server_id) expect(integration.server_name).to eq(server_name) expect(integration.username).to eq(username) expect(integration.keep_inactive_members).to eq(false) end it "shows error if oauth authorization fails" do proxy.stub("https://www.discord.com:443/api/oauth2/authorize").and_return(redirect_to: oauth_redirect_integrations_discord_index_url(error: "error_message", host: host_with_port)) visit edit_link_url(@product, host: host_with_port) check "Invite your customers to a Discord server", allow_label_click: true click_on "Connect to Discord" expect_alert_message "Could not connect to your Discord account, please try again." end it "shows error if getting server info fails" do proxy.stub("https://www.discord.com:443/api/oauth2/authorize").and_return(redirect_to: oauth_redirect_integrations_discord_index_url(code: "test_code", host: host_with_port)) visit edit_link_url(@product, host: host_with_port) check "Invite your customers to a Discord server", allow_label_click: true click_on "Connect to Discord" expect_alert_message "Could not connect to your Discord account, please try again." end it "creates an integration for the product and enables integration for a newly created tier" do proxy.stub("https://www.discord.com:443/api/oauth2/authorize").and_return(redirect_to: oauth_redirect_integrations_discord_index_url(code: "test_code", host: host_with_port)) WebMock.stub_request(:post, DISCORD_OAUTH_TOKEN_URL). to_return(status: 200, body: { access_token: "test_access_token", guild: { id: server_id, name: server_name } }.to_json, headers: { content_type: "application/json" }) WebMock.stub_request(:get, "#{Discordrb::API.api_base}/users/@me"). with(headers: { "Authorization" => "Bearer test_access_token" }). to_return(status: 200, body: { username: }.to_json, headers: { content_type: "application/json" }) product = create(:membership_product, user: seller) expect do visit edit_link_url(product, host: host_with_port) check "Invite your customers to a Discord server", allow_label_click: true click_on "Connect to Discord" expect(page).to have_button "Disconnect Discord" check "Do not remove Discord access when membership ends", allow_label_click: true click_on "Add tier" within tier_rows[0] do fill_in "Name", with: "New Tier" fill_in "Amount monthly", with: 3 check "Enable access to Discord server", allow_label_click: true end within tier_rows[1] do check "Toggle recurrence option: Monthly" fill_in "Amount monthly", with: 3 end save_change end.to change { Integration.count }.by(1) .and change { ProductIntegration.count }.by(1) .and change { BaseVariantIntegration.count }.by(1) product_integration = ProductIntegration.last base_variant_integration = BaseVariantIntegration.last integration = Integration.last expect(product_integration.integration).to eq(integration) expect(base_variant_integration.integration).to eq(integration) expect(base_variant_integration.base_variant.name).to eq("New Tier") expect(product_integration.product).to eq(product) expect(integration.type).to eq(Integration.type_for(Integration::DISCORD)) expect(integration.server_id).to eq(server_id) expect(integration.server_name).to eq(server_name) expect(integration.username).to eq(username) expect(integration.keep_inactive_members).to eq(true) end it "creates an integration for the product and enables integration for a newly created version" do proxy.stub("https://www.discord.com:443/api/oauth2/authorize").and_return(redirect_to: oauth_redirect_integrations_discord_index_url(code: "test_code", host: host_with_port)) WebMock.stub_request(:post, DISCORD_OAUTH_TOKEN_URL). to_return(status: 200, body: { access_token: "test_access_token", guild: { id: server_id, name: server_name } }.to_json, headers: { content_type: "application/json" }) WebMock.stub_request(:get, "#{Discordrb::API.api_base}/users/@me"). with(headers: { "Authorization" => "Bearer test_access_token" }). to_return(status: 200, body: { username: }.to_json, headers: { content_type: "application/json" }) product = create(:product_with_pdf_file, user: seller) expect do visit edit_link_url(product, host: host_with_port) check "Invite your customers to a Discord server", allow_label_click: true click_on "Connect to Discord" expect(page).to have_button "Disconnect Discord" click_on "Add version" fill_in "Version name", with: "Files" click_on "Add version" within version_rows[0] do within version_option_rows[0] do fill_in "Version name", with: "New Version" check "Enable access to Discord server", allow_label_click: true end end save_change end.to change { Integration.count }.by(1) .and change { ProductIntegration.count }.by(1) .and change { BaseVariantIntegration.count }.by(1) product_integration = ProductIntegration.last base_variant_integration = BaseVariantIntegration.last integration = Integration.last expect(product_integration.integration).to eq(integration) expect(base_variant_integration.integration).to eq(integration) expect(base_variant_integration.base_variant.name).to eq("New Version") expect(product_integration.product).to eq(product) expect(integration.type).to eq(Integration.type_for(Integration::DISCORD)) expect(integration.server_id).to eq(server_id) expect(integration.server_name).to eq(server_name) expect(integration.username).to eq(username) expect(integration.keep_inactive_members).to eq(false) end end context "without proxy" do include_context "with switching account to user as admin for seller" it "shows correct details if saved integration exists" do @product.active_integrations << discord_integration visit edit_link_path(@product) within_section "Integrations", section_element: :section do expect(page).to have_checked_field "Invite your customers to a Discord server" expect(page).to have_button "Disconnect Discord" expect(page).to have_text "Discord account #gumbot connected" expect(page).to have_text "Server name: Gaming" end end it "disconnects discord server correctly" do @product.active_integrations << discord_integration WebMock.stub_request(:delete, "#{Discordrb::API.api_base}/users/@me/guilds/#{server_id}"). with(headers: { "Authorization" => "Bot #{DISCORD_BOT_TOKEN}" }). to_return(status: 204) expect do visit edit_link_path(@product) click_on "Disconnect Discord" expect(page).to have_button "Connect to Discord" save_change end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { @product.reload.active_integrations.count }.from(1).to(0) expect(ProductIntegration.first.deleted?).to eq(true) expect(@product.reload.live_product_integrations).to be_empty end it "shows error message if disconnection fails" do @product.active_integrations << discord_integration WebMock.stub_request(:delete, "#{Discordrb::API.api_base}/users/@me/guilds/#{server_id}"). with(headers: { "Authorization" => "Bot #{DISCORD_BOT_TOKEN}" }). to_return(status: 404, body: { code: Discordrb::Errors::UnknownMember.code }.to_json) expect do visit edit_link_path(@product) click_on "Disconnect Discord" save_change(expect_message: "Could not disconnect the discord integration, please try again.") end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { @product.reload.active_integrations.count }.by(0) end it "does not disconnect integration if product is not saved after disconnecting" do @product.active_integrations << discord_integration WebMock.stub_request(:delete, "#{Discordrb::API.api_base}/users/@me/guilds/0"). with(headers: { "Authorization" => "Bot #{DISCORD_BOT_TOKEN}" }). to_return(status: 404) expect do visit edit_link_path(@product) click_on "Disconnect Discord" expect(page).to have_button "Connect to Discord" expect(page).to_not have_button "Disconnect Discord" visit edit_link_path(@product) expect(page).to have_button "Disconnect Discord" end.to change { @product.reload.active_integrations.count }.by(0) end it "disables integration correctly" do @product.active_integrations << discord_integration WebMock.stub_request(:delete, "#{Discordrb::API.api_base}/users/@me/guilds/#{server_id}"). with(headers: { "Authorization" => "Bot #{DISCORD_BOT_TOKEN}" }). to_return(status: 204) expect do visit edit_link_path(@product) expect(page).to have_button "Disconnect Discord" uncheck "Invite your customers to a Discord server", allow_label_click: true expect(page).to have_unchecked_field "Invite your customers to a Discord server" save_change end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { @product.reload.active_integrations.count }.from(1).to(0) expect(ProductIntegration.first.deleted?).to eq(true) expect(@product.reload.live_product_integrations).to be_empty visit edit_link_path(@product) expect(page).to_not have_button "Disconnect Discord" end context "integration for product with multiple versions" do before do @version_category = create(:variant_category, link: @product) @version_1 = create(:variant, variant_category: @version_category) @version_2 = create(:variant, variant_category: @version_category) end it "shows the integration toggle if product has an integration" do @product.active_integrations << discord_integration visit edit_link_path(@product) expect(page).to have_text "Enable access to Discord server", count: 2 end it "hides the integration toggle if product does not have an integration" do visit edit_link_path(@product) expect(page).to_not have_text "Enable access to Discord server" end it "enables integration for versions" do @product.active_integrations << discord_integration visit edit_link_path(@product) within_section "Integrations", section_element: :section do expect(page).to have_unchecked_field("Enable for all versions") expect(page).to have_status(text: "Your integration is not assigned to any version. Check your versions' settings.") check "Enable for all versions", allow_label_click: true end within version_rows[0] do within version_option_rows[0] do expect(page).to have_checked_field("Enable access to Discord server") uncheck "Enable access to Discord server", allow_label_click: true end within version_option_rows[1] do expect(page).to have_checked_field("Enable access to Discord server") end end within_section "Integrations", section_element: :section do expect(page).to have_unchecked_field("Enable for all versions") expect(page).to_not have_status(text: "Your integration is not assigned to any version. Check your versions' settings.") end expect do save_change end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(1) integration = @product.active_integrations.first base_variant_integration = BaseVariantIntegration.first expect(base_variant_integration.integration).to eq(integration) expect(base_variant_integration.base_variant).to eq(@version_2) end it "disables integration for a version" do @product.active_integrations << discord_integration @version_1.active_integrations << discord_integration version_integration = @version_1.base_variant_integrations.first expect do visit edit_link_path(@product) within version_rows[0] do within version_option_rows[0] do uncheck "Enable access to Discord server", allow_label_click: true end end save_change end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(0) .and change { @version_1.active_integrations.count }.from(1).to(0) expect(version_integration.reload.deleted?).to eq(true) expect(@version_1.reload.live_base_variant_integrations).to be_empty end it "disables integration for a version if integration is disconnected" do @product.active_integrations << discord_integration @version_1.active_integrations << discord_integration version_integration = @version_1.base_variant_integrations.first product_integration = @product.product_integrations.first WebMock.stub_request(:delete, "#{Discordrb::API.api_base}/users/@me/guilds/#{server_id}"). with(headers: { "Authorization" => "Bot #{DISCORD_BOT_TOKEN}" }). to_return(status: 204) expect do visit edit_link_path(@product) click_on "Disconnect Discord" expect(page).to have_button "Connect to Discord" save_change end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(0) .and change { @version_1.active_integrations.count }.from(1).to(0) .and change { @product.active_integrations.count }.from(1).to(0) expect(version_integration.reload.deleted?).to eq(true) expect(product_integration.reload.deleted?).to eq(true) expect(@product.reload.live_product_integrations).to be_empty expect(@version_1.reload.live_base_variant_integrations).to be_empty end it "disables integration for a version if integration is removed from the product" do @product.active_integrations << discord_integration @version_1.active_integrations << discord_integration version_integration = @version_1.base_variant_integrations.first product_integration = @product.product_integrations.first WebMock.stub_request(:delete, "#{Discordrb::API.api_base}/users/@me/guilds/#{server_id}"). with(headers: { "Authorization" => "Bot #{DISCORD_BOT_TOKEN}" }). to_return(status: 204) expect do visit edit_link_path(@product) uncheck "Invite your customers to a Discord server", allow_label_click: true expect(page).to have_unchecked_field "Invite your customers to a Discord server" save_change end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(0) .and change { @version_1.active_integrations.count }.from(1).to(0) .and change { @product.active_integrations.count }.from(1).to(0) expect(version_integration.reload.deleted?).to eq(true) expect(product_integration.reload.deleted?).to eq(true) expect(@product.reload.live_product_integrations).to be_empty expect(@version_1.reload.live_base_variant_integrations).to be_empty end end context "integration for product with membership tiers" do before do @subscription_product = create(:membership_product_with_preset_tiered_pricing, user: seller) @tier_1 = @subscription_product.tiers[0] @tier_2 = @subscription_product.tiers[1] end it "shows the integration toggle if product has an integration" do @subscription_product.active_integrations << discord_integration visit edit_link_path(@subscription_product) expect(page).to have_text "Enable access to Discord server", count: 2 end it "hides the integration toggle if product does not have an integration" do visit edit_link_path(@subscription_product) expect(page).to_not have_text "Enable access to Discord server" end it "enables integration for a tier" do @subscription_product.active_integrations << discord_integration expect do visit edit_link_path(@subscription_product) within tier_rows[1] do check "Enable access to Discord server", allow_label_click: true end save_change end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(1) integration = @subscription_product.active_integrations.first base_variant_integration = BaseVariantIntegration.first expect(base_variant_integration.integration).to eq(integration) expect(base_variant_integration.base_variant).to eq(@tier_2) end it "disables integration for a tier" do @subscription_product.active_integrations << discord_integration @tier_1.active_integrations << discord_integration tier_integration = @tier_1.base_variant_integrations.first expect do visit edit_link_path(@subscription_product) within tier_rows[0] do uncheck "Enable access to Discord server", allow_label_click: true end save_change end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(0) .and change { @tier_1.active_integrations.count }.from(1).to(0) expect(tier_integration.reload.deleted?).to eq(true) expect(@tier_1.reload.live_base_variant_integrations).to be_empty end it "disables integration for a tier if integration is disconnected" do @subscription_product.active_integrations << discord_integration @tier_1.active_integrations << discord_integration tier_integration = @tier_1.base_variant_integrations.first product_integration = @subscription_product.product_integrations.first WebMock.stub_request(:delete, "#{Discordrb::API.api_base}/users/@me/guilds/#{server_id}"). with(headers: { "Authorization" => "Bot #{DISCORD_BOT_TOKEN}" }). to_return(status: 204) expect do visit edit_link_path(@subscription_product) click_on "Disconnect Discord" expect(page).to have_button "Connect to Discord" save_change end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(0) .and change { @tier_1.active_integrations.count }.from(1).to(0) .and change { @subscription_product.active_integrations.count }.from(1).to(0) expect(tier_integration.reload.deleted?).to eq(true) expect(product_integration.reload.deleted?).to eq(true) expect(@subscription_product.reload.live_product_integrations).to be_empty expect(@tier_1.reload.live_base_variant_integrations).to be_empty end it "disables integration for a tier if integration is removed from the product" do @subscription_product.active_integrations << discord_integration @tier_1.active_integrations << discord_integration tier_integration = @tier_1.base_variant_integrations.first product_integration = @subscription_product.product_integrations.first WebMock.stub_request(:delete, "#{Discordrb::API.api_base}/users/@me/guilds/#{server_id}"). with(headers: { "Authorization" => "Bot #{DISCORD_BOT_TOKEN}" }). to_return(status: 204) expect do visit edit_link_path(@subscription_product) uncheck "Invite your customers to a Discord server", allow_label_click: true expect(page).to have_unchecked_field "Invite your customers to a Discord server" save_change end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { BaseVariantIntegration.count }.by(0) .and change { @tier_1.active_integrations.count }.from(1).to(0) .and change { @subscription_product.active_integrations.count }.from(1).to(0) expect(tier_integration.reload.deleted?).to eq(true) expect(product_integration.reload.deleted?).to eq(true) expect(@subscription_product.reload.live_product_integrations).to be_empty expect(@tier_1.reload.live_base_variant_integrations).to be_empty end end it "does not show the 'Enable for all versions' toggle for a physical product" do product = create(:physical_product, user: seller) create(:variant_category, link: product, title: "Color") create(:sku, link: product, name: "Blue - large") create(:sku, link: product, name: "Green - small") product.active_integrations << discord_integration visit edit_link_path(product) within_section "Integrations", section_element: :section do expect(page).to have_button("Disconnect Discord") expect(page).to_not have_text("Enable for all versions") end end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/edit/integrations/google_calendar_integrations_spec.rb
spec/requests/products/edit/integrations/google_calendar_integrations_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" describe("Product Edit Integrations edit - Google Calendar", type: :system, js: true) do include ProductTieredPricingHelpers include ProductEditPageHelpers let(:seller) { create(:named_seller, created_at: 60.days.ago) } before :each do @product = create(:call_product, user: seller) @vcr_cassette_prefix = "Product Edit Integrations edit" Feature.activate(:google_calendar_link) end describe "google calendar integration" do let(:google_calendar_integration) { create(:google_calendar_integration) } let(:calendar_id) { google_calendar_integration.calendar_id } let(:calendar_summary) { google_calendar_integration.calendar_summary } let(:email) { google_calendar_integration.email } context "with proxy", billy: true do let(:host_with_port) { "127.0.0.1:31337" } before do login_as seller end it "adds a new integration" do proxy.stub("https://accounts.google.com:443/o/oauth2/auth").and_return(redirect_to: oauth_redirect_integrations_google_calendar_index_url(code: "test_code", host: host_with_port)) WebMock.stub_request(:post, "https://oauth2.googleapis.com/token"). to_return(status: 200, body: { access_token: "test_access_token", refresh_token: "test_refresh_token" }.to_json, headers: { content_type: "application/json" }) WebMock.stub_request(:get, "https://www.googleapis.com/oauth2/v2/userinfo"). with(query: { access_token: "test_access_token" }). to_return(status: 200, body: { email: email }.to_json, headers: { content_type: "application/json" }) WebMock.stub_request(:get, "https://www.googleapis.com/calendar/v3/users/me/calendarList"). with(headers: { "Authorization" => "Bearer test_access_token" }). to_return(status: 200, body: { items: [{ id: calendar_id, summary: calendar_summary }] }.to_json, headers: { content_type: "application/json" }) expect do visit edit_link_url(@product, host: host_with_port) check "Connect with Google Calendar to sync your calls", allow_label_click: true expect(page).to have_button "Disconnect Google Calendar" save_change end.to change { Integration.count }.by(1) .and change { ProductIntegration.count }.by(1) product_integration = ProductIntegration.last integration = Integration.last expect(product_integration.integration).to eq(integration) expect(product_integration.product).to eq(@product) expect(integration.type).to eq(Integration.type_for(Integration::GOOGLE_CALENDAR)) expect(integration.calendar_id).to eq(calendar_id) expect(integration.calendar_summary).to eq(calendar_summary) expect(integration.email).to eq(email) end it "shows error if oauth authorization fails" do proxy.stub("https://accounts.google.com:443/o/oauth2/auth").and_return(redirect_to: oauth_redirect_integrations_google_calendar_index_url(error: "error_message", host: host_with_port)) visit edit_link_url(@product, host: host_with_port) check "Connect with Google Calendar to sync your calls", allow_label_click: true click_on "Connect to Google Calendar" expect_alert_message "Could not connect to your Google Calendar account, please try again." end end context "without proxy" do include_context "with switching account to user as admin for seller" it "shows correct details if saved integration exists" do @product.active_integrations << google_calendar_integration visit edit_link_path(@product) within_section "Integrations", section_element: :section do expect(page).to have_checked_field "Connect with Google Calendar to sync your calls" expect(page).to have_button "Disconnect Google Calendar" expect(page).to have_select "calendar-select", selected: calendar_summary end end it "disconnects google calendar correctly" do @product.active_integrations << google_calendar_integration expect do visit edit_link_path(@product) click_on "Disconnect Google Calendar" expect(page).to have_button "Connect to Google Calendar" save_change end.to change { Integration.count }.by(0) .and change { ProductIntegration.count }.by(0) .and change { @product.reload.active_integrations.count }.from(1).to(0) expect(ProductIntegration.first.deleted?).to eq(true) expect(@product.reload.live_product_integrations).to be_empty end it "does not show the checkbox when the feature flag is disabled" do Feature.deactivate(:google_calendar_link) visit edit_link_path(@product) within_section "Integrations", section_element: :section do expect(page).not_to have_field "Connect with Google Calendar to sync your calls" expect(page).not_to have_button "Connect to Google Calendar" end end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/show/show_spec.rb
spec/requests/products/show/show_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" require "shared_examples/discover_layout" describe("ProductShowScenario", type: :system, js: true) do it("sets the quantity and price based on the parameters in the query string and allows purchase") do product = create(:product, customizable_price: true, quantity_enabled: true) quantity = 3 price_per_unit = 2.5 total_price = quantity * price_per_unit visit short_link_path(product, quantity:, price: price_per_unit) expect(page).to have_field("Quantity", with: 3) expect(page).to have_field("Name a fair price", with: "2.50") add_to_cart(product, quantity: 3, pwyw_price: 2.5) check_out(product) expect(product.sales.successful.last.quantity).to eq(quantity) expect(product.sales.successful.last.price_cents).to eq(total_price * 100) end it "preselects and allows purchase of a physical product sku as specified in the variant query string parameter" do product = create(:product, is_physical: true, require_shipping: true, skus_enabled: true) product.shipping_destinations << ShippingDestination.new(country_code: Compliance::Countries::USA.alpha2, one_item_rate_cents: 4_00, multiple_items_rate_cents: 2_00) product.save! variant_category_1 = create(:variant_category, link: product) %w[Red Blue Green].each { |name| create(:variant, name:, variant_category: variant_category_1) } variant_category_2 = create(:variant_category, link: product) ["Small", "Medium", "Large", "Extra Large"].each { |name| create(:variant, name:, variant_category: variant_category_2) } variant_category_3 = create(:variant_category, link: product) %w[Polo Round].each { |name| create(:variant, name:, variant_category: variant_category_3) } Product::SkusUpdaterService.new(product:).perform visit short_link_path(product, option: product.skus.find_by(name: "Blue - Extra Large - Polo").external_id) expect(page).to have_radio_button("Blue - Extra Large - Polo") add_to_cart(product, option: "Blue - Extra Large - Polo") check_out(product) end it "preselects and allows purchase of a variant as specified in the variant query string parameter" do product = create(:product, customizable_price: true, price_cents: "200") variant_category_1 = create(:variant_category, link: product) %w[Red Blue Green].each_with_index { |name, index| create(:variant, name:, variant_category: variant_category_1, price_difference_cents: index * 100) } visit short_link_path(product, price: "8") + "&option=#{variant_category_1.variants.third.external_id}" expect(page).to have_radio_button("Green", checked: true) expect(page).to have_field("Name a fair price", with: "8") add_to_cart(product, option: "Green", pwyw_price: 8) check_out(product) end it "ensures correct price formatting" do product = create(:product, customizable_price: true) visit short_link_path(product) fill_in "Name a fair price", with: "-1234,.439" expect(page).to have_field "Name a fair price", with: "1234.43" end it "discards the quantity, price, and variant query string parameters if they are not applicable to the product" do product = create(:product) visit short_link_path(product, quantity: 3, price: 11) + "&option=fake" add_to_cart(product) check_out(product) expect(product.sales.successful.last.quantity).to eq(1) expect(product.sales.successful.last.price_cents).to eq(product.default_price_cents) end it "preselects the subscription recurrence as specified in the URL and allows purchase" do product = create(:subscription_product_with_versions, subscription_duration: :monthly) create(:price, link: product, recurrence: "quarterly", price_cents: 250) create(:price, link: product, recurrence: "yearly", price_cents: 800) visit short_link_path(product, recurrence: "yearly") add_to_cart(product, option: "Untitled 1") check_out(product) end it "fills the custom fields of the product based on query string parameters and allows purchase" do product = create(:product) product.custom_fields << [ create(:custom_field, name: "First Name"), create(:custom_field, name: "Gender") ] product.save! visit short_link_path(product, "First Name" => "gumbot", "Gender" => "male") add_to_cart(product) expect(page).to have_field("First Name", with: "gumbot") expect(page).to have_field("Gender", with: "male") check_out(product) expect(product.sales.successful.last.custom_fields).to eq( [ { name: "First Name", value: "gumbot", type: CustomField::TYPE_TEXT }, { name: "Gender", value: "male", type: CustomField::TYPE_TEXT } ] ) end it "shows remaining products count" do product = create(:product, max_purchase_count: 1) visit short_link_path(product) expect(page).to have_content "1 left" end context "membership product with free trial" do let(:product) { create(:membership_product, :with_free_trial_enabled) } it "displays information about a 1 week free trial" do visit short_link_path(product) expect(page).to have_content "All memberships include a 1 week free trial" end it "displays information about a 1 month free trial" do product.update!(free_trial_duration_unit: "month", free_trial_duration_amount: 1) visit short_link_path(product) expect(page).to have_content "All memberships include a 1 month free trial" end end it "records a page view for legacy URLs" do product = create(:product, custom_permalink: "custom") visit short_link_path(product) expect(ElasticsearchIndexerWorker).to have_enqueued_sidekiq_job("index", hash_including("class_name" => "ProductPageView")) end it "records a page view for subdomain URLs" do product = create(:product, custom_permalink: "custom") visit product.long_url expect(ElasticsearchIndexerWorker).to have_enqueued_sidekiq_job("index", hash_including("class_name" => "ProductPageView")) end context "when `?wanted=true`" do before do @product = create(:product, quantity_enabled: true) @membership = create(:membership_product_with_preset_tiered_pwyw_pricing) @membership.custom_fields = [create(:custom_field, name: "your nickname")] @membership.save! @offer_code = create(:offer_code, products: [@membership], amount_cents: 500) end it "opens the checkout page and sets the appropriate values" do visit "#{@membership.long_url}/#{@offer_code.code}?wanted=true&recurrence=biannually&option=#{@membership.variant_categories.first.variants.second.external_id}&email=gumhead@gumroad.com&your nickname=moneybags" within "[role='listitem']" do expect(page).to have_text(@membership.name) expect(page).to have_text("Tier: Second Tier") expect(page).to have_text("Every 6 months") end expect(page).to have_field("Email address", with: "gumhead@gumroad.com") expect(page).to have_field("your nickname", with: "moneybags") expect(page).to have_selector("[aria-label='Discount code']", text: @offer_code.code) click_on "Remove" visit "#{@product.long_url}?wanted=true&quantity=3" within "[role='listitem']" do expect(page).to have_text(@membership.name) expect(page).to have_text("Qty: 3") end end context "with a product with an installment plan" do let!(:product) { create(:product, price_cents: 10000) } let!(:installment_plan) { create(:product_installment_plan, link: product, number_of_installments: 3) } it "passes the pay_in_installments parameter to the checkout page" do visit "#{product.long_url}?wanted=true&pay_in_installments=true" within_cart_item product.name do expect(page).to have_text("in 3 installments") end end end context "with a PWYW product" do before do @pwyw_product = create(:product, customizable_price: true, price_cents: 99) end it "opens the checkout page when the price parameter is valid" do visit "#{@pwyw_product.long_url}?wanted=true&price=99" within "[role='listitem']" do expect(page).to have_text(@membership.name) expect(page).to have_text("$99") end end it "opens the product page when the price parameter is not set" do visit "#{@pwyw_product.long_url}?wanted=true" expect(page).to_not have_text("Checkout") end it "opens the product page when the price parameter is invalid" do visit "#{@pwyw_product.long_url}?wanted=true&price=0.98" expect(page).to_not have_text("Checkout") end it "displays a decimal price input" do visit @pwyw_product.long_url expect(find_field("Name a fair price")["inputmode"]).to eq("decimal") end end end it "substitutes `<br>`s for newlines in variant descriptions" do @product = create(:product) @variant_category = create(:variant_category, link: @product) @variant = create(:variant, variant_category: @variant_category, description: "Description\nwith\nnewlines") visit @product.long_url expect(find(:radio_button, @variant.name)[:innerHTML]).to include("Description<br>with<br>newlines") end describe "Twitter meta tags" do before do @product = create(:product_with_pdf_file, preview_url: "https://staging-public-files.gumroad.com/happy_face.jpeg") end it "has the correct twitter meta tags on the product page" do visit("/l/#{@product.unique_permalink}") twitter_properties = { site: "@gumroad", image: @product.preview_url, card: "summary_large_image", title: CGI.escapeHTML(@product.name), description: @product.description } twitter_properties.each_pair do |property, value| expect(page.find("meta[property='twitter:#{property}']", visible: false).value).to eq value end end end describe "Product edit button" do let(:seller) { create(:named_user) } let(:product) { create(:product, user: seller) } shared_examples_for "with product edit button" do it "shows the product edit button" do visit short_link_url(product.unique_permalink, host: product.user.subdomain_with_protocol) expect(page).to have_link("Edit product", href: edit_link_url(product, host: DOMAIN)) end end shared_examples_for "without product edit button" do it "doesn't show the product edit button" do visit short_link_url(product.unique_permalink, host: product.user.subdomain_with_protocol) expect(page).not_to have_link("Edit product", href: edit_link_url(product, host: DOMAIN)) end end context "with seller as logged_in_user" do before do login_as(seller) end it_behaves_like "with product edit button" context "with switching account to user as admin for seller" do include_context "with switching account to user as admin for seller" it_behaves_like "with product edit button" end end context "with switching account to user as admin for seller" do include_context "with switching account to user as admin for seller" context "when accessing logged-in user's product" do let(:product) { create(:product, user: user_with_role_for_seller) } it_behaves_like "without product edit button" end end context "without user logged in" do it_behaves_like "without product edit button" end end context "when the product is a multi-seat license membership" do let(:product) { create(:membership_product, is_multiseat_license: true) } it "populates the seats field from the `quantity` query parameter" do visit "#{product.long_url}?quantity=4" expect(page).to have_field("Seats", with: "4") end end describe "Refund policy" do let(:product_refund_policy) do create( :product_refund_policy, max_refund_period_in_days: 7, fine_print: "Seriously, just email us and we'll refund you.", ) end let(:product) { product_refund_policy.product } let(:seller) { product.user } before do seller.update!(refund_policy_enabled: false) product.update!(product_refund_policy_enabled: true) end it "renders product-level refund policy" do travel_to(Time.utc(2023, 4, 17)) do product_refund_policy.update!(updated_at: Time.current) visit product.long_url click_on("7-day money back guarantee") within_modal "7-day money back guarantee" do expect(page).to have_text("Seriously, just email us and we'll refund you.") expect(page).to have_text("Last updated Apr 17, 2023") end end end context "when the account-level refund policy is enabled" do before do seller.update!(refund_policy_enabled: true) end it "renders account-level refund policy" do travel_to(Time.utc(2023, 4, 17)) do seller.refund_policy.update!(fine_print: "This is an account-level refund policy fine print") visit product.long_url click_on("30-day money back guarantee") within_modal "30-day money back guarantee" do expect(page).to have_text("This is an account-level refund policy fine print") expect(page).to have_text("Last updated Apr 17, 2023") end end end context "when the URL contains refund-policy anchor" do it "renders with the modal open and creates event" do seller.refund_policy.update!(fine_print: "This is an account-level refund policy fine print") expect do visit "#{product.long_url}#refund-policy" end.to change { Event.count }.by(1) within_modal "30-day money back guarantee" do expect(page).to have_text("This is an account-level refund policy fine print") end event = Event.last expect(event.event_name).to eq(Event::NAME_PRODUCT_REFUND_POLICY_FINE_PRINT_VIEW) expect(event.link_id).to eq(product.id) end end end context "when the URL contains refund-policy anchor" do it "renders with the modal open and creates event" do expect do visit "#{product.long_url}#refund-policy" end.to change { Event.count }.by(1) within_modal "7-day money back guarantee" do expect(page).to have_text("Seriously, just email us and we'll refund you.") end event = Event.last expect(event.event_name).to eq(Event::NAME_PRODUCT_REFUND_POLICY_FINE_PRINT_VIEW) expect(event.link_id).to eq(product.id) end end end describe "Discount expiration countdown" do let(:product) { create(:product) } let(:offer_code) { create(:offer_code, products: [product], valid_at: 1.day.ago, expires_at: 15.seconds.from_now) } it "renders the countdown and switches to an error status upon expiration" do visit "#{product.long_url}/#{offer_code.code}" expect(page).to have_selector("[role='status']", text: /This discount expires in 00:\d{2}/) expect(page).to have_selector("[role='status']", text: "Sorry, the discount code you wish to use is inactive.") end end describe "Minimum quantity discount" do let(:product) { create(:product, quantity_enabled: true) } let(:offer_code) { create(:offer_code, products: [product], minimum_quantity: 2) } it "renders the minimum quantity discount notice and displays the discounted price when the minimum quantity is met" do visit "#{product.long_url}/#{offer_code.code}" expect(page).to have_selector("[role='status']", text: "Get $1 off when you buy 2 or more (Code SXSW)") expect(page).to have_selector("[itemprop='price']", exact_text: "$1") fill_in "Quantity", with: 2 expect(page).to have_selector("[itemprop='price']", exact_text: "$1 $0") fill_in "Quantity", with: 1 expect(page).to have_selector("[itemprop='price']", exact_text: "$1") end end context "when the CTA bar is visible" do let(:product) { create(:product) } before do asset_preview = create(:asset_preview, link: product) asset_preview.file.attach Rack::Test::UploadedFile.new(Rails.root.join("spec", "support", "fixtures", "error_file.jpeg"), "image/jpeg") asset_preview.file.analyze end context "when the product is PWYW" do before do product.update(customizable_price: true) end it "focuses the PWYW button when the CTA button in the CTA bar is clicked" do visit product.long_url within "[aria-label='Product information bar']" do click_on "I want this!" end expect(page).to have_field("Name a fair price:", focused: true) expect(page).to have_alert(text: "You must input an amount") end end context "when the product only has one variant" do let(:variant_category) { create(:variant_category, link: product) } let!(:variant) { create(:variant, variant_category:) } it "navigates to the checkout page when the CTA button in the CTA bar is clicked" do visit product.long_url within "[aria-label='Product information bar']" do click_on "I want this!" end expect(page.current_path).to eq("/checkout") end end end context "the product has a collaborator" do let(:product) { create(:product, is_collab: true) } let!(:collaborator) { create(:collaborator, seller: product.user) } let!(:product_affiliate) { create(:product_affiliate, affiliate: collaborator, product:, dont_show_as_co_creator: false) } context "dont_show_as_co_creator is false" do it "shows the collaborator as a co-creator" do visit product.long_url expect(page).to have_text("#{product.user.username} with #{collaborator.affiliate_user.username}", normalize_ws: true) expect(page).to have_link(product.user.username, href: root_url(host: product.user.subdomain_with_protocol)) expect(page).to have_link(collaborator.affiliate_user.username, href: collaborator.affiliate_user.profile_url) end end context "dont_show_as_co_creator is true" do before { collaborator.update!(dont_show_as_co_creator: true) } it "doesn't show the collaborator as a co-creator" do visit product.long_url expect(page).to_not have_text("#{product.user.username} with #{collaborator.affiliate_user.username}") end end end describe "PWYW product" do context "without a discount" do context "regular product" do let(:product) { create(:product, price_cents: 1000, customizable_price: true, suggested_price_cents: 3000) } it "sets the PWYW input placeholder value correctly" do visit product.long_url expect(page).to have_field("Name a fair price:", with: "", placeholder: "30+") end it "rounds non-zero prices up to the currency minimum" do visit product.long_url pwyw_field = find_field("Name a fair price") pwyw_field.fill_in with: "0.1" find(:label, "Name a fair price:").click expect(pwyw_field["aria-invalid"]).to eq("true") expect(pwyw_field.value).to eq("0.99") end context "product is free" do before { product.update!(price_cents: 0) } it "rounds non-zero prices up to the currency minimum" do visit product.long_url pwyw_field = find_field("Name a fair price") pwyw_field.fill_in with: "0.1" find(:label, "Name a fair price:").click expect(pwyw_field["aria-invalid"]).to eq("false") expect(pwyw_field.value).to eq("0.99") add_to_cart(product, pwyw_price: 0.99) within "[role='listitem']" do select_disclosure "Edit" do fill_in "Name a fair price", with: "0.1" click_on "Save" end expect(page).to have_text("$0.99") select_disclosure "Edit" do fill_in "Name a fair price", with: "0" click_on "Save" end expect(page).to have_text("$0") end end end end context "versioned product" do let(:product) { create(:product_with_digital_versions, customizable_price: true, price_cents: 0, suggested_price_cents: 0) } before do product.alive_variants.each_with_index { _1.update(price_difference_cents: (_2 + 1) * 100) } end it "sets the PWYW input placeholder value correctly" do visit product.long_url expect(page).to have_field("Name a fair price:", with: "", placeholder: "1+") choose "Untitled 2" expect(page).to have_field("Name a fair price:", with: "", placeholder: "2+") end end context "membership product" do let(:product) { create(:membership_product_with_preset_tiered_pwyw_pricing) } it "sets the PWYW input placeholder value correctly" do visit product.long_url expect(page).to have_field("Name a fair price:", with: "", placeholder: "600+") end end end context "with a discount" do let(:seller) { create(:user) } let(:offer_code) { create(:offer_code, user: seller, universal: true, amount_cents: 100) } context "regular product" do let(:product) { create(:product, user: seller, price_cents: 1000, customizable_price: true) } it "sets the PWYW input placeholder value correctly" do visit "#{product.long_url}/#{offer_code.code}" expect(page).to have_field("Name a fair price:", with: "", placeholder: "9+") end end context "versioned product" do let(:product) { create(:product_with_digital_versions, user: seller, customizable_price: true, price_cents: 300) } it "sets the PWYW input placeholder value correctly" do visit "#{product.long_url}/#{offer_code.code}" expect(page).to have_field("Name a fair price:", with: "", placeholder: "2+") end end context "membership product" do let(:product) { create(:membership_product_with_preset_tiered_pwyw_pricing, user: seller) } before do product.alive_variants.each do |tier| tier.prices.each do |price| price.update!(suggested_price_cents: nil) end end end it "sets the PWYW input placeholder value correctly" do visit "#{product.long_url}/#{offer_code.code}" expect(page).to have_field("Name a fair price:", with: "", placeholder: "499+") end end end end it "includes a share menu with social links and copy functionality" do product = create(:product) visit product.long_url select_disclosure "Share" do expect(page).to have_link("Share on X") expect(page).to have_link("Share on Facebook") copy_button = find_button("Copy link") copy_button.hover expect(copy_button).to have_tooltip(text: "Copy product URL") copy_button.click expect(copy_button).to have_tooltip(text: "Copied") end end describe "discover layout" do let(:product) { create(:product, :recommendable, taxonomy: Taxonomy.find_by(slug: "design")) } let(:discover_url) { product.long_url(layout: Product::Layout::DISCOVER) } let(:non_discover_url) { product.long_url } it_behaves_like "discover navigation when layout is discover", selected_taxonomy: "Design" end context "sold out variants" do let!(:creator) { create(:named_user) } let!(:product) { create(:product, user: creator, name: "Test Product with Variants", hide_sold_out_variants: false) } let!(:variant_category) { create(:variant_category, link: product) } let!(:available_variant) { create(:variant, name: "Standard", variant_category: variant_category) } let!(:sold_out_variant) { create(:variant, name: "Starter", variant_category: variant_category, max_purchase_count: 1) } let!(:sold_out_variant_purchase) { create(:purchase, link: product, variant_attributes: [sold_out_variant]) } it "disables or hides the variant based on the hide_sold_out_variants setting" do # The sold out variant should be visible, but in a disabled state. product.update!(hide_sold_out_variants: false) visit product.long_url expect(page).to have_radio_button(sold_out_variant.name, disabled: true) expect(page).to have_radio_button(available_variant.name, disabled: false) # The sold out variant should not be visible on the page. product.update!(hide_sold_out_variants: true) visit product.long_url expect(page).not_to have_radio_button(sold_out_variant.name) expect(page).to have_radio_button(available_variant.name, disabled: false) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/show/reviews_spec.rb
spec/requests/products/show/reviews_spec.rb
# frozen_string_literal: true require("spec_helper") describe("Product page reviews", js: true, type: :system) do include ActionView::Helpers::TextHelper def create_review(index, rating) purchase = create(:purchase, link: product, full_name: "Purchaser #{index}") create(:product_review, rating:, purchase:, message: "This is review #{index}", created_at: index.months.ago) end let(:product) { create(:product, user: create(:named_seller), custom_permalink: "custom") } let!(:review_0) { create_review(0, 2) } let!(:review_1) { create_review(1, 3) } let!(:review_2) { create_review(2, 4) } let!(:review_3) { create_review(3, 1) } let!(:review_4) { create_review(4, 2) } let!(:review_5) { create_review(5, 5) } before(:each) do allow(Rails.cache).to receive(:read).and_return(nil) end it "displays the average rating with reviews count if product reviews are enabled for product" do expect(product.reviews_count).to eq(6) expect(product.average_rating).to eq(2.8) visit product.long_url expect(page).to have_text("Ratings") expect(page).to have_text(pluralize(product.reviews_count, "rating")) expect(page).to have_selector("[aria-label='Ratings histogram']") rating_distribution = [1 => 17, 2 => 33, 3 => 17, 4 => 17, 5 => 16] ProductReview::PRODUCT_RATING_RANGE.each do |rating| expect(page).to have_text(pluralize(rating, "star")) expect(page).to have_text("#{rating_distribution[rating]}%") end product.display_product_reviews = false product.save! visit product.long_url expect(page).not_to have_text("Ratings") expect(page).not_to have_text("0 ratings") end it "allows user to provide rating if already bought and displays the rating regardless of display_product_reviews being enabled for that product" do purchaser = create(:user) purchase = create(:purchase, link: product, purchaser:) login_as(purchaser) visit product.long_url expect(page).to have_text("Liked it? Give it a rating:") choose "3 stars" click_on "Post review" expect(page).to have_alert(text: "Review submitted successfully!") expect(purchase.reload.original_product_review.rating).to eq(3) expect(product.reload.reviews_count).to eq(7) expect(product.average_rating).to eq(2.9) page.evaluate_script "window.location.reload()" expect(page).to have_text("Your rating:") expect(page).to have_radio_button("3 stars", checked: true) product.display_product_reviews = false product.save! visit product.long_url expect(page).to have_text("Your rating:") end it "displays all the rating stars, without clipping, when a user who has already purchased the product views the page in a tablet-sized viewport", :tablet_view do purchaser = create(:user) create(:purchase, link: product, purchaser:) login_as(purchaser) visit product.long_url expect(page).to have_text("Liked it? Give it a rating:") choose "5 stars" expect(page).to have_radio_button("5 stars", checked: true) end it "displays and updates the product review of the original purchase for a subscription product" do purchaser = create(:user) subscription_product = create(:subscription_product) subscription = create(:subscription, user: create(:user, credit_card: create(:credit_card)), link: subscription_product) original_purchase = create(:purchase, link: subscription_product, is_original_subscription_purchase: true, subscription:, purchaser:) subscription.purchases << original_purchase subscription.save! login_as(purchaser) visit subscription_product.long_url expect(page).to have_text("Liked it? Give it a rating:") choose "3 stars" click_on "Post review" expect(page).to have_alert(text: "Review submitted successfully!") expect(original_purchase.reload.original_product_review.rating).to eq(3) recurring_purchase = create(:purchase, link: subscription_product, subscription:, purchaser:) subscription.purchases << recurring_purchase subscription.save! page.evaluate_script "window.location.reload()" wait_for_ajax expect(page).to have_text("Your rating:") expect(page).to have_radio_button("3 stars", checked: true) click_on "Edit" choose "2 stars" click_on "Update review" expect(page).to have_alert(text: "Review submitted successfully!") expect(original_purchase.reload.original_product_review.rating).to eq(2) end it "displays the correctly formatted reviews count text based on the number of reviews" do visit product.long_url expect(page).to have_text("Ratings") expect(page).to have_content(pluralize(product.reviews_count, "rating")) allow_any_instance_of(Link).to receive(:rating_stats).and_return( count: 100, average: 3, percentages: [20, 20, 20, 20, 20] ) visit product.long_url expect(page).to have_text("Ratings") expect(page).to have_content("3\n(100 ratings)\n5 stars\n20%") allow_any_instance_of(Link).to receive(:rating_stats).and_return( count: 100000, average: 3, percentages: [20, 20, 20, 20, 20] ) visit product.long_url expect(page).to have_text("Ratings") expect(page).to have_content("3\n(100K ratings)\n5 stars\n20%") end it "hides the ability to review product if it's in a preorder status" do purchaser = create(:user) link = create(:product_with_video_file, price_cents: 600, is_in_preorder_state: true, name: "preorder link") good_card = build(:chargeable) preorder_product = create(:preorder_link, link:) preorder = create(:preorder, preorder_link: preorder_product, seller_id: link.user.id) create(:purchase, purchaser:, link:, chargeable: good_card, purchase_state: "in_progress", preorder_id: preorder.id, is_preorder_authorization: true) preorder.authorize! preorder.mark_authorization_successful! login_as(purchaser) visit link.long_url expect(page).not_to have_text("Ratings") end describe "written reviews" do let(:purchaser) { create(:buyer_user) } let!(:purchase) { create(:purchase, link: product, purchaser:) } before do stub_const("ProductReviewsController::PER_PAGE", 2) login_as purchaser end it "allows the user to provide a review" do visit product.long_url within_section "You've purchased this product" do expect(page).to have_field("Liked it? Give it a rating:", with: "") expect(page).to have_button("Post review", disabled: true) choose "4 stars" fill_in "Want to leave a written review?", with: "This is a great product!" click_on "Post review" end expect(page).to have_alert(text: "Review submitted successfully!") expect(page).to_not have_field("Want to leave a written review?") expect(page).to have_radio_button("4 stars", checked: true) expect(page).to have_text('"This is a great product!"') expect(page).to have_button("Edit") review = purchase.reload.original_product_review expect(review.rating).to eq(4) expect(review.message).to eq("This is a great product!") end context "user has left a review" do let!(:review) { create(:product_review, purchase:, rating: 4, message: nil) } it "allows the user to update their review" do visit product.long_url expect(page).to_not have_field("Want to leave a written review?") expect(page).to have_radio_button("4 stars", checked: true) expect(page).to have_text("No written review") expect(page).to have_button("Edit") within_section "You've purchased this product" do click_on "Edit" fill_in "Want to leave a written review?", with: "This is a great product!" choose "5 stars" click_on "Update review" end expect(page).to have_alert(text: "Review submitted successfully!") expect(page).to_not have_field("Want to leave a written review?") expect(page).to have_radio_button("5 stars", checked: true) expect(page).to have_text('"This is a great product!"') expect(page).to have_button("Edit") review.reload expect(review.rating).to eq(5) expect(review.message).to eq("This is a great product!") end end context "adult keywords in review message" do it "displays an error message" do visit product.long_url within_section "You've purchased this product" do fill_in "Want to leave a written review?", with: "SAUCY abs Punch!" choose "5 stars" click_on "Post review" end expect(page).to have_alert(text: "Validation failed: Adult keywords are not allowed") end end it "displays written reviews and responses" do avatar_url = ActionController::Base.helpers.asset_url("gumroad-default-avatar-5.png") create :product_review_response, product_review: review_3, message: "Review response 3", created_at: 1.day.ago, # All review responses should attribute to the seller regardless of who actually wrote it. user: create(:user, name: "Not the seller") visit product.long_url within_section "Ratings", match: :first do within_section "Purchaser 5", match: :first do expect(page).to have_selector("[aria-label='5 stars']") expect(page).to have_text("This is review 5") expect(page).to have_image(src: avatar_url) check = first("[aria-label='Verified Buyer']") check.hover expect(check).to have_tooltip(text: "Verified Buyer") expect(page).to_not have_text("New") end within_section "Purchaser 2", match: :first do expect(page).to have_selector("[aria-label='4 stars']") expect(page).to have_text("This is review 2") expect(page).to have_image(src: avatar_url) expect(page).to_not have_text("New") end expect(page).to_not have_text("Purchaser 1") expect(page).to_not have_text("Purchaser 0") expect(page).to_not have_text("Purchaser 4") expect(page).to_not have_section("Purchaser 3") expect(page).to_not have_text("Review response 3") click_on "Load more" within_section "Purchaser 1", match: :first do expect(page).to have_selector("[aria-label='3 stars']") expect(page).to have_text("This is review 1") expect(page).to have_image(src: avatar_url) expect(page).to_not have_text("New") end within_section "Purchaser 0", match: :first do expect(page).to have_selector("[aria-label='2 stars']") expect(page).to have_text("This is review 0") expect(page).to have_image(src: avatar_url) expect(page).to have_text("New") end expect(page).to_not have_text("Purchaser 4") expect(page).to_not have_section("Purchaser 3") expect(page).to_not have_text("Review response 3") click_on "Load more" within_section "Purchaser 4", match: :first do expect(page).to have_selector("[aria-label='2 stars']") expect(page).to have_text("This is review 4") expect(page).to have_image(src: avatar_url) expect(page).to_not have_text("New") end within_section "Purchaser 3", match: :first do expect(page).to have_selector("[aria-label='1 star']") expect(page).to have_text("This is review 3") expect(page).to_not have_text("New") expect(page).to have_image(src: avatar_url) end within_section "Seller", match: :first do expect(page).to have_text("Review response 3") expect(page).to have_image(src: product.user.avatar_url) expect(page).to have_text("Creator") expect(page).to_not have_selector("[aria-label='Verified Buyer']") end expect(page).to_not have_button("Load more") end end it "allows the seller to respond to a review" do login_as product.user visit product.long_url click_on "Add response", match: :first fill_in "Add a response to the review", with: "Thank you for your review, Mr. 5!" click_on "Submit" expect(page).to have_alert(text: "Response submitted successfully!") within_section "Seller", match: :first do expect(page).to have_text("Thank you for your review, Mr. 5!") expect(page).to have_text("Creator") end expect(review_5.reload.response.message).to eq("Thank you for your review, Mr. 5!") click_on "Edit" fill_in "Add a response to the review", with: "I hate you, Mr. 5!" click_on "Update" expect(page).to have_alert(text: "Response updated successfully!") within_section "Seller", match: :first do expect(page).to have_text("I hate you, Mr. 5!") expect(page).to have_text("Creator") end expect(review_5.reload.response.message).to eq("I hate you, Mr. 5!") refresh within_section "Seller", match: :first do expect(page).to have_text("I hate you, Mr. 5!") expect(page).to have_text("Creator") end expect(page).to have_button("Edit") end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/show/preview_spec.rb
spec/requests/products/show/preview_spec.rb
# frozen_string_literal: true require("spec_helper") describe("Product page previews", js: true, type: :system) do before do @product = create(:product, user: create(:user), custom_receipt: "<h1>Hello</h1>") create(:asset_preview, link: @product) create(:asset_preview, url: "https://www.youtube.com/watch?v=5Bcpj-q0Snc", link: @product) create(:asset_preview_mov, link: @product) end it "allows switching between multiple previews" do visit @product.long_url preview = find("[aria-label='Product preview']") preview.hover within preview do expect(page).to have_selector("img[src*='#{PUBLIC_STORAGE_S3_BUCKET}']") expect(page).to have_selector("iframe", visible: false) expect(page).to have_selector("video[src*='#{PUBLIC_STORAGE_S3_BUCKET}']", visible: false) expect(page).to have_button("Show next cover") expect(page).to have_tablist("Select a cover") end within preview do expect(page).to have_button("Show next cover") expect(page).to have_tablist("Select a cover") select_tab "Show cover 2" expect(page).to have_selector("img[src*='#{PUBLIC_STORAGE_S3_BUCKET}']", visible: false) expect(page).to have_selector("iframe") expect(page).to have_selector("video[src*='#{PUBLIC_STORAGE_S3_BUCKET}']", visible: false) click_on "Show next cover" expect(page).to have_selector("img[src*='#{PUBLIC_STORAGE_S3_BUCKET}']", visible: false) expect(page).to have_selector("iframe", visible: false) expect(page).to have_selector("video[src*='#{PUBLIC_STORAGE_S3_BUCKET}']") end end context "when the preview is oembed or a video" do it "hides the tablist" do visit @product.long_url preview = find("[aria-label='Product preview']") preview.hover within preview do expect(page).to have_selector("img[src*='#{PUBLIC_STORAGE_S3_BUCKET}']") expect(page).to have_tablist("Select a cover") click_on "Show next cover" expect(page).to have_selector("iframe[src*='https://www.youtube.com/embed/5Bcpj-q0Snc?feature=oembed&showinfo=0&controls=0&rel=0&enablejsapi=1']") expect(page).to_not have_tablist("Select a cover") click_on "Show next cover" expect(page).to have_selector("video[src*='#{PUBLIC_STORAGE_S3_BUCKET}']") expect(page).to_not have_tablist("Select a cover") end end end describe "scrolling between previews" do let(:product) { create(:product) } before do create_list(:asset_preview, 3, link: product) end it "allows swiping between image previews" do visit product.long_url previews = all("[role='tabpanel']") find("[aria-label='Product preview']").hover click_on "Show next cover" scroll_to previews[2] expect(page).to_not have_button("Show next cover") expect(page).to have_button("Show previous cover") click_on "Show previous cover" scroll_to previews[0] expect(page).to have_button("Show next cover") expect(page).to_not have_button("Show previous cover") end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/show/sales_count_spec.rb
spec/requests/products/show/sales_count_spec.rb
# frozen_string_literal: true require("spec_helper") describe("Product page sales count", js: true, type: :system) do before(:each) do @user = create(:user) @product = create(:product, user: @user, should_show_sales_count: true, price_cents: 100) recreate_model_index(Purchase) end it "hides the sales count when the flag is false" do @product.update! should_show_sales_count: false visit @product.long_url expect(page).to_not have_selector("[role='status']") end it "shows the sales count when the flag is true" do visit @product.long_url expect(page).to have_text("0 sales") end it "shows preorders count when the product is in preorder state" do product_with_preorder = create(:product, user: @user, is_in_preorder_state: true, should_show_sales_count: true) create(:preorder_link, link: product_with_preorder) visit product_with_preorder.long_url expect(page).to have_text("0 pre-orders") end describe "shows correct calculation and pluralization", :sidekiq_inline, :elasticsearch_wait_for_refresh do it "includes free purchases in sales count" do @product.update! price_cents: 0 create(:free_purchase, link: @product, succeeded_at: 1.hour.ago) visit @product.long_url expect(page).to have_text("1 download") @versioned_product = create(:product_with_digital_versions, should_show_sales_count: true) create(:free_purchase, link: @versioned_product, succeeded_at: 1.hour.ago) visit @product.long_url expect(page).to have_text("1 download") end it "includes free purchases for free PWYW sales count" do @product.update! price_cents: 0 create(:free_purchase, link: @product, succeeded_at: 1.hour.ago) create(:purchase, link: @product, succeeded_at: 1.hour.ago) visit @product.long_url expect(page).to have_text("2 downloads") end it "excludes failed purchases from sales count" do create(:failed_purchase, link: @product, succeeded_at: 1.hour.ago) visit @product.long_url expect(page).to have_text("0 sales") end it "excludes fully refunded purchases from sales count" do create(:refunded_purchase, link: @product, succeeded_at: 1.hour.ago) visit @product.long_url expect(page).to have_text("0 sales") end it "excludes disputed purchases not won from sales count" do create(:disputed_purchase, :with_dispute, link: @product, succeeded_at: 1.hour.ago) visit @product.long_url expect(page).to have_text("0 sales") end it "pluralizes the label correctly" do recreate_model_index(Purchase) visit @product.long_url expect(page).to have_text("0 sales") create(:purchase, link: @product, succeeded_at: 1.hour.ago) visit @product.long_url expect(page).to have_text("1 sale") create(:purchase, link: @product, succeeded_at: 1.hour.ago) visit @product.long_url expect(page).to have_text("2 sales") end end context "free products with variants", :sidekiq_inline, :elasticsearch_wait_for_refresh do before do @product = create(:product, user: @user, should_show_sales_count: true, price_cents: 0) create(:free_purchase, link: @product, succeeded_at: 1.hour.ago) end it "includes free purchases with free variants" do visit @product.long_url expect(page).to have_text("1 download") end it "includes free purchases with paid variants" do category = create(:variant_category, link: @product) create(:variant, variant_category: category, price_difference_cents: 200) create(:purchase, link: @product, succeeded_at: 1.hour.ago, price_cents: 200) visit @product.long_url expect(page).to have_text("2 sales") end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/show/user_info_spec.rb
spec/requests/products/show/user_info_spec.rb
# frozen_string_literal: true require("spec_helper") describe("ProductUserInfoScenario", type: :system, js: true) do it("it fills the logged in user's information in the form") do user = create(:user, name: "amir", street_address: "1640 17th st", zip_code: "94103", country: "United States", city: "San Francisco", state: "CA") link = create(:product, unique_permalink: "somelink", require_shipping: true) login_as(user) visit "#{link.user.subdomain_with_protocol}/l/#{link.unique_permalink}" click_on "I want this!" expect(page).to have_field "Street address", with: "1640 17th st" expect(page).to have_field "ZIP code", with: "94103" expect(page).to have_field "State", with: "CA" expect(page).to have_field "Country", with: "US" end it "applies discount based on the offer_code parameter in the query string" do product = create(:product, price_cents: 2000) offer_code = create(:offer_code, code: "free", products: [product], amount_cents: 2000) visit "/l/#{product.unique_permalink}" expect(page).to(have_selector("[itemprop='price']", text: "$20")) visit "/l/#{product.unique_permalink}/?offer_code=#{offer_code.code}" expect(page).to have_selector("[role='status']", text: "$20 off will be applied at checkout (Code FREE)") expect(page).to have_selector("[itemprop='price']", text: "$20 $0") add_to_cart(product, offer_code:) check_out(product, is_free: true) end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/show/wishlist_selector_spec.rb
spec/requests/products/show/wishlist_selector_spec.rb
# frozen_string_literal: true require("spec_helper") describe "Product page wishlist selector", js: true, type: :system do let(:user) { create(:user) } let(:product) { create(:product, user:) } def add_to_wishlist(option) select_combo_box_option option, from: "Add to wishlist" expect(page).to have_combo_box("Add to wishlist", text: option) end def create_new_wishlist(name) find(:combo_box, "Add to wishlist").click find(:list_box_option, "New wishlist").click fill_in "Wishlist name", with: name click_button "Create wishlist" expect(page).to have_combo_box("Add to wishlist", text: name) expect(page).to have_alert(text: "Wishlist created") end context "when not logged in" do it "redirects to the login page" do visit product.long_url find(:combo_box, "Add to wishlist").click expect(current_url).to eq(login_url(host: DOMAIN, next: product.long_url)) end end context "when logged in" do before { login_as(user) } it "supports creating new wishlists" do visit product.long_url expect { create_new_wishlist("Wishlist 1") }.to change(user.wishlists, :count).by(1) expect(user.wishlists.last).to have_attributes(name: "Wishlist 1", products: [product]) expect { create_new_wishlist("Wishlist 2") }.to change(user.wishlists, :count).by(1) expect(user.wishlists.last).to have_attributes(name: "Wishlist 2", products: [product]) find(:combo_box, "Add to wishlist").click expect(page).not_to have_field("Wishlist name") find(:list_box_option, "New wishlist").click expect(find_field("Wishlist name").value).to eq("") expect(page).to have_combo_box("Add to wishlist", with_disabled_options: ["Wishlist 1", "Wishlist 2"]) end it "shows an error when creating a wishlist with empty name" do visit product.long_url find(:combo_box, "Add to wishlist").click find(:list_box_option, "New wishlist").click click_button "Create wishlist" expect(page).to have_alert(text: "Please enter a wishlist name") expect(user.wishlists.count).to eq(0) end context "with an existing wishlist" do let(:existing_wishlist) { create(:wishlist, name: "My Wishlist", user:) } let(:existing_product) { create(:product, user:) } before do create(:wishlist_product, wishlist: existing_wishlist, product: existing_product) end it "supports adding to the wishlist" do visit product.long_url expect { add_to_wishlist("My Wishlist") }.to change(existing_wishlist.wishlist_products, :count).by(1) expect(page).to have_alert(text: "Added to wishlist") find(:combo_box, "Add to wishlist").click expect(page).to have_combo_box("Add to wishlist", with_disabled_options: ["My Wishlist"]) expect(existing_wishlist.reload.products).to contain_exactly(existing_product, product) end it "supports creating a new wishlist" do visit product.long_url expect { create_new_wishlist("New Wishlist") }.to change(user.wishlists, :count).by(1) expect(existing_wishlist.reload.products).to contain_exactly(existing_product) expect(user.wishlists.last).to have_attributes(name: "New Wishlist", products: [product]) end end context "for a membership product" do let(:product) do create(:membership_product_with_preset_tiered_pricing, user:, recurrence_price_values: [ { "monthly": { enabled: true, price: 3 }, "yearly": { enabled: true, price: 30 } }, { "monthly": { enabled: true, price: 5 }, "yearly": { enabled: true, price: 50 } } ]) end it "saves the tier and recurrence" do visit product.long_url create_new_wishlist("Wishlist 1") expect(user.wishlists.last.wishlist_products.sole).to have_attributes( recurrence: "monthly", variant: product.tiers.first ) find(:combo_box, "Add to wishlist").click expect(page).to have_combo_box("Add to wishlist", with_disabled_options: ["Wishlist 1"]) choose("Second Tier") select("Yearly", from: "Recurrence") add_to_wishlist("Wishlist 1") expect(user.wishlists.last.wishlist_products.count).to eq 2 expect(user.wishlists.last.wishlist_products.reload.last).to have_attributes( recurrence: "yearly", variant: product.tiers.second ) find(:combo_box, "Add to wishlist").click expect(page).to have_combo_box("Add to wishlist", with_disabled_options: ["Wishlist 1"]) end end context "for a physical product" do let(:product) { create(:product, :is_physical) } before do variant_category = create(:variant_category, link: product) %w[Red Blue Green].each { |name| create(:variant, name:, variant_category:) } Product::SkusUpdaterService.new(product:).perform end it "saves the sku and quantity" do visit product.long_url create_new_wishlist("Wishlist 1") expect(user.wishlists.last.wishlist_products.sole).to have_attributes( quantity: 1, variant: product.skus.not_is_default_sku.first ) choose("Green") fill_in "Quantity", with: 4 add_to_wishlist("Wishlist 1") expect(user.wishlists.last.wishlist_products.count).to eq 2 expect(user.wishlists.last.wishlist_products.reload.last).to have_attributes( quantity: 4, variant: product.skus.not_is_default_sku.last ) end end context "for a rental product" do let(:product) { create(:product, purchase_type: :buy_and_rent, rental_price_cents: 100) } it "saves rental or non-rental without duplicating the wishlist item" do visit product.long_url create_new_wishlist("Wishlist 1") expect(user.wishlists.last.wishlist_products.sole).to have_attributes(product:, rent: false) choose("Rent") add_to_wishlist("Wishlist 1") expect(user.wishlists.last.wishlist_products.sole).to have_attributes(product:, rent: true) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/show/supporter_count_spec.rb
spec/requests/products/show/supporter_count_spec.rb
# frozen_string_literal: true require("spec_helper") describe("The supporter count", js: true, type: :system) do before do recreate_model_indices(Purchase) @user = create(:user) @product = create(:membership_product, user: @user, should_show_sales_count: true) end it "doesn't show the supporter count when the flag is false" do product = create(:membership_product, user: @user, should_show_sales_count: false) visit("l/#{product.unique_permalink}") expect(page).not_to have_text("0 members") end it "shows the supporter count when the flag is true" do @product.should_show_sales_count = true visit("l/#{@product.unique_permalink}") expect(page).to have_text("0 members") end describe "shows correct calculation and pluralization", :sidekiq_inline, :elasticsearch_wait_for_refresh do it "includes free membership purchases in sales count" do product = create(:membership_product, user: @user, should_show_sales_count: true) create(:membership_purchase, link: product, succeeded_at: 1.hour.ago, price_cents: 0) visit("l/#{product.unique_permalink}") expect(page).to have_text("1 member") end it "includes paid membership purchases in sales count" do product = create(:membership_product, user: @user, should_show_sales_count: true) create(:membership_purchase, link: product, succeeded_at: 1.hour.ago, price_cents: 100) visit("l/#{product.unique_permalink}") expect(page).to have_text("1 member") end it "pluralizes the label correctly" do recreate_model_index(Purchase) product = create(:membership_product, user: @user, should_show_sales_count: true) visit("/l/#{product.unique_permalink}") expect(page).to have_text("0 members") create(:membership_purchase, link: product, succeeded_at: 1.hour.ago, price_cents: 100) visit("/l/#{product.unique_permalink}") expect(page).to have_text("1 member") create(:membership_purchase, link: product, succeeded_at: 1.hour.ago, price_cents: 100) visit("/l/#{product.unique_permalink}") expect(page).to have_text("2 members") end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/products/show/sections_spec.rb
spec/requests/products/show/sections_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" describe "Profile settings on product pages", type: :system, js: true do let(:seller) { create(:user) } let(:product) { create(:product, user: seller) } it "renders sections correctly when the user is logged out" do products = create_list(:product, 3, user: seller) Link.import(refresh: true, force: true) create(:seller_profile_products_section, seller:) section1 = create(:seller_profile_products_section, seller:, product:, header: "Section 1", shown_products: products.map(&:id)) create(:published_installment, seller:, shown_on_profile: true) posts = create_list(:audience_installment, 2, published_at: Date.yesterday, seller:, shown_on_profile: true) section2 = create(:seller_profile_posts_section, seller:, product:, header: "Section 2", shown_posts: posts.pluck(:id)) section3 = create(:seller_profile_rich_text_section, seller:, product:, header: "Section 3", text: { type: "doc", content: [{ type: "heading", attrs: { level: 2 }, content: [{ type: "text", text: "Heading" }] }, { type: "paragraph", content: [{ type: "text", text: "Some more text" }] }] }) section4 = create(:seller_profile_subscribe_section, seller:, product:, header: "Section 4") section5 = create(:seller_profile_featured_product_section, seller:, product:, header: "Section 5", featured_product_id: create(:product, user: seller, name: "Featured product").id) product.update!(sections: [section1, section2, section3, section5, section4].map(&:id), main_section_index: 2) visit short_link_path(product) expect(page).to have_selector("section:nth-child(2) h2", text: "Section 1") within_section "Section 1", section_element: :section do expect_product_cards_in_order(products) end expect(page).to have_selector("section:nth-child(3) h2", text: "Section 2") within_section "Section 2", section_element: :section do expect(page).to have_link(count: 2) posts.each { expect(page).to have_link(_1.name, href: "/p/#{_1.slug}") } end expect(page).to have_selector("section:nth-child(4) article", text: product.name) expect(page).to have_selector("section:nth-child(5) h2", text: "Section 3") within_section "Section 3", section_element: :section do expect(page).to have_selector("h2", text: "Heading") expect(page).to have_text("Some more text") end expect(page).to have_selector("section:nth-child(6) h2", text: "Section 5") within_section "Section 4", section_element: :section do expect(page).to have_field "Your email address" expect(page).to have_button "Subscribe" end expect(page).to have_selector("section:nth-child(7) h2", text: "Section 4") within_section "Section 5", section_element: :section do expect(page).to have_section("Featured product", section_element: :article) end end it "allows editing sections when the user is logged in", :elasticsearch_wait_for_refresh do login_as seller product2 = create(:product, user: seller, name: "Product 2") visit short_link_path(product) select_disclosure "Add section", match: :first do click_on "Products" end select_disclosure "Edit section" do click_on "Products" check product2.name end toggle_disclosure "Edit section" click_on "Move section down" select_disclosure "Add section", match: :first do click_on "Featured Product" end sleep 1 select_disclosure "Edit section", match: :first do click_on "Featured Product" select_combo_box_option search: product2.name, from: "Featured Product" end all(:disclosure, "Add section").last.select_disclosure do click_on "Posts" end sleep 1 all(:disclosure, "Edit section").last.select_disclosure do click_on "Name" fill_in "Name", with: "Posts!" end all(:disclosure, "Add section").last.select_disclosure do click_on "Subscribe" end sleep 1 all(:disclosure, "Add section")[2].select_disclosure do click_on "Rich text" end sleep 1 edit_rich_text_disclosure = all(:disclosure, "Edit section")[1] edit_rich_text_disclosure.select_disclosure do click_on "Name" fill_in "Name", with: "Rich text!" end edit_rich_text_disclosure.toggle_disclosure # all these sleeps can hopefully be cleaned up when flashMessage is in react and less buggy sleep 1 wait_for_ajax expect(page).to_not have_alert sleep 3 rich_text_editor = find("[contenteditable=true]") rich_text_editor.send_keys "Text!\t" sleep 1 wait_for_ajax expect(page).to have_alert(text: "Changes saved!") rich_text_editor.click attach_file(file_fixture("test.jpg")) do click_on "Insert image" end wait_for_ajax expect(page).to have_alert(text: "Changes saved!") expect(page).to_not have_alert sleep 3 all(:button, "Move section up").last.click sleep 1 wait_for_ajax expect(page).to have_alert(text: "Changes saved!") products_section = seller.seller_profile_products_sections.reload.sole expect(products_section).to have_attributes(shown_products: [product2.id], product:) posts_section = seller.seller_profile_posts_sections.sole expect(posts_section).to have_attributes(header: "Posts!", product:) featured_product_section = seller.seller_profile_featured_product_sections.sole expect(featured_product_section).to have_attributes(featured_product_id: product2.id, product:) subscribe_section = seller.seller_profile_subscribe_sections.sole expect(subscribe_section).to have_attributes(header: "Subscribe to receive email updates from #{seller.name_or_username}.", product:) rich_text_section = seller.seller_profile_rich_text_sections.sole Selenium::WebDriver::Wait.new(timeout: 10).until { rich_text_section.reload.text["content"].map { _1["type"] }.include?("image") } image_url = "#{AWS_S3_ENDPOINT}/#{S3_BUCKET}/#{ActiveStorage::Blob.last.key}" expected_rich_text = { type: "doc", content: [ { type: "paragraph", content: [{ type: "text", text: "Text!" }] }, { type: "image", attrs: { src: image_url, link: nil } } ] }.as_json expect(rich_text_section).to have_attributes(header: "Rich text!", text: expected_rich_text, product:) expect(product.reload).to have_attributes(sections: [featured_product_section, rich_text_section, products_section, subscribe_section, posts_section].map(&:id), main_section_index: 1) refresh within_section "Rich text!" do expect(page).to have_text("Text!") expect(page).to have_image(src: image_url) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/customers/customers_spec.rb
spec/requests/customers/customers_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" describe "Sales page", type: :system, js: true do let(:seller) { create(:named_seller, :eligible_for_service_products) } let(:product1) { create(:product, user: seller, name: "Product 1", price_cents: 100) } let(:membership) { create(:membership_product_with_preset_tiered_pricing, user: seller, name: "Membership", is_multiseat_license: true, is_licensed: true) } let(:offer_code) { create(:offer_code, code: "code", products: [membership]) } let(:product2) { create(:product_with_digital_versions, user: seller, name: "Product 2", price_cents: 300) } let!(:purchase1) { create(:purchase, link: product1, full_name: "Customer 1", email: "customer1@gumroad.com", created_at: 1.day.ago, seller:) } let!(:purchase2) { create(:membership_purchase, link: membership, price_cents: 200, full_name: "Customer 2", email: "customer2@gumroad.com", created_at: 2.days.ago, seller:, is_original_subscription_purchase: true, offer_code:, was_product_recommended: true, recommended_by: RecommendationType::GUMROAD_MORE_LIKE_THIS_RECOMMENDATION, quantity: 2, license: create(:license)) } let!(:purchase3) { create(:purchase, link: product2, full_name: "Customer 3", email: "customer3hasaninsanelylongemailaddress@gumroad.com", created_at: 3.days.ago, country: "Uganda", seller:, variant_attributes: [product2.alive_variants.first], is_bundle_purchase: true) } let!(:purchases) do create_list(:purchase, 3, link: product1) do |customer, i| i += 4 customer.update(full_name: "Customer #{i}", email: "customer#{i}@gumroad.com", created_at: i.days.ago) end end before do index_model_records(Purchase) stub_const("CustomersController::CUSTOMERS_PER_PAGE", 3) create(:upsell_purchase, purchase: purchase1, upsell: create(:upsell, seller:, product: product1, cross_sell: true)) end def fill_in_date(field_label, date) fill_in field_label, with: date.strftime("%Y") find_field(field_label).send_keys(:tab) fill_in field_label, with: date.strftime("%m") # It appears that once the month is provided, the cursor moves automaticaly to date, without the need to use tab # MacOS 14.5 (23F79), Google Chrome Version 124.0.6367.208 (Official Build) (arm64) fill_in field_label, with: date.strftime("%d") end include_context "with switching account to user as admin for seller" describe "table" do it "displays the seller's sales in the table" do login_as seller visit customers_path expect(page).to have_table("All sales (6)") expect(page).to have_table_row({ "Email" => "customer1@gumroad.com", "Name" => "Customer 1", "Product" => "Product 1", "Price" => "$1" }) expect(page).to have_table_row({ "Email" => "customer2@gumroad.com", "Name" => "Customer 2", "Product" => "Membership", "Price" => "$2 a month" }) expect(page).to have_table_row({ "Email" => "customer3hasaninsanelylonge...", "Name" => "Customer 3", "Product" => "Product 2Bundle", "Price" => "$3" }) end it "sorts and paginates sales in the table" do login_as seller visit customers_path expect(page).to have_selector("th[aria-sort='descending']", text: "Purchase Date") expect(page).to have_selector("[aria-current='page']", text: "1") expect(page).to have_nth_table_row_record(1, "Customer 1") expect(page).to have_nth_table_row_record(2, "Customer 2") expect(page).to have_nth_table_row_record(3, "Customer 3") click_on "Next" expect(page).to have_selector("[aria-current='page']", text: "2") expect(page).to have_nth_table_row_record(1, "Customer 4") expect(page).to have_nth_table_row_record(2, "Customer 5") expect(page).to have_nth_table_row_record(3, "Customer 6") find(:columnheader, "Price").click expect(page).to have_selector("[aria-current='page']", text: "1") expect(page).to have_selector("th[aria-sort='ascending']", text: "Price") find(:columnheader, "Price").click expect(page).to have_selector("th[aria-sort='descending']", text: "Price") expect(page).to have_nth_table_row_record(1, "Customer 3") expect(page).to have_nth_table_row_record(2, "Customer 2") expect(page).to have_nth_table_row_record(3, "Customer 1") click_on "2" expect(page).to have_selector("[aria-current='page']", text: "2") expect(page).to have_nth_table_row_record(1, "Customer 4") expect(page).to have_nth_table_row_record(2, "Customer 5") expect(page).to have_nth_table_row_record(3, "Customer 6") end it "allows searching the table" do create(:purchase, link: product1, full_name: "Customer 11", email: "customer11@gumroad.com", created_at: 11.days.ago, seller:) index_model_records(Purchase) login_as seller visit customers_path(query: "customer11@gumroad.com") expect(page).to have_table_row({ "Email" => "customer11@gumroad.com", "Name" => "Customer 11" }) expect(page).to have_text("All sales (1)") select_disclosure "Search" do expect(page).to have_field("Search sales", with: "customer11@gumroad.com") fill_in "Search sales", with: "customer1" end wait_for_ajax expect(page).to have_nth_table_row_record(1, "Customer 1") expect(page).to have_nth_table_row_record(2, "Customer 11") find(:columnheader, "Purchase Date").click expect(page).to have_nth_table_row_record(1, "Customer 11") expect(page).to have_nth_table_row_record(2, "Customer 1") select_disclosure "Search" do fill_in "Search sales", with: "" end expect(page).to have_nth_table_row_record(1, "Customer 11") expect(page).to have_nth_table_row_record(2, "Customer 6") expect(page).to have_nth_table_row_record(3, "Customer 5") fill_in "Search sales", with: "nononono" expect(page).to have_section("No sales found") end it "includes the transaction URL link" do allow_any_instance_of(Purchase).to receive(:transaction_url_for_seller).and_return("https://www.google.com") visit customers_path expect(page).to have_link("$1", href: "https://www.google.com") expect(page).to have_link("$2 a month", href: "https://www.google.com") expect(page).to have_link("$3", href: "https://www.google.com") end context "when the seller has no sales" do let(:user) { create(:user) } before do Feature.activate_user(:react_customers_page, user) login_as user end it "displays a placeholder message" do visit customers_path within_section "Manage all of your sales in one place." do expect(page).to have_text("Every time a new customer purchases a product from your Gumroad, their email address and other details are added here.") expect(page).to have_link("Start selling today", href: new_product_path) end end end describe "filtering" do before do create(:membership_purchase, created_at: 1.month.ago, link: create(:membership_product, user: seller, name: "Membership", price_cents: 100), seller:).subscription.deactivate! index_model_records(Purchase) end it "filters products correctly" do login_as seller visit customers_path toggle_disclosure "Filter" select_combo_box_option search: "Product 1", from: "Customers who bought" expect(page).to have_nth_table_row_record(1, "Customer 1") expect(page).to have_nth_table_row_record(2, "Customer 4") expect(page).to have_nth_table_row_record(3, "Customer 5") click_on "2" expect(page).to have_selector("[aria-current='page']", text: "2") expect(page).to have_nth_table_row_record(1, "Customer 6") toggle_disclosure "Filter" click_on "Clear value" select_combo_box_option search: "Product 1", from: "Customers who have not bought" expect(page).to have_nth_table_row_record(1, "Customer 2") expect(page).to have_nth_table_row_record(2, "Customer 3") click_on "Clear value" fill_in "Paid more than", with: "2" expect(page).to have_nth_table_row_record(1, "Customer 3") fill_in "Paid more than", with: "" fill_in "Paid less than", with: "2" expect(page).to have_nth_table_row_record(1, "Customer 1") expect(page).to have_nth_table_row_record(2, "Customer 4") expect(page).to have_nth_table_row_record(3, "Customer 5") click_on "2" expect(page).to have_selector("[aria-current='page']", text: "2") expect(page).to have_nth_table_row_record(1, "Customer 6") toggle_disclosure "Filter" fill_in "Paid less than", with: "" fill_in_date("Before", purchase1.created_at) find(:label, "Before").click expect(page).to have_nth_table_row_record(1, "Customer 1") expect(page).to have_nth_table_row_record(2, "Customer 2") expect(page).to have_nth_table_row_record(3, "Customer 3") click_on "2" expect(page).to have_selector("[aria-current='page']", text: "2") expect(page).to have_nth_table_row_record(1, "Customer 4") expect(page).to have_nth_table_row_record(2, "Customer 5") expect(page).to have_nth_table_row_record(3, "Customer 6") toggle_disclosure "Filter" fill_in "Before", with: "" select "Uganda", from: "From" expect(page).to have_nth_table_row_record(1, "Customer 3") select "Anywhere", from: "From" fill_in_date("After", purchase3.created_at) find(:label, "After").click expect(page).to have_nth_table_row_record(1, "Customer 1") expect(page).to have_nth_table_row_record(2, "Customer 2") fill_in "After", with: "" find(:label, "Before").click expect(page).to have_button("3") check "Show active customers only" expect(page).to_not have_button("3") uncheck "Show active customers only" expect(page).to have_button("3") end it "prevents selecting the same product in both bought and not bought filters" do login_as seller visit customers_path toggle_disclosure "Filter" select_combo_box_option search: "Product 1", from: "Customers who bought" # product 1 should not be an option in the not bought (as it is already selected in the bought) within_fieldset "Customers who have not bought" do find("input", visible: :all).send_keys("Product 1") end expect(page).to have_no_selector("[role='option']", text: "Product 1") expect(page).to have_nth_table_row_record(1, "Customer 1") expect(page).to have_nth_table_row_record(2, "Customer 4") within_fieldset "Customers who bought" do click_on "Clear value" end select_combo_box_option search: "Product 1", from: "Customers who have not bought" # product 1 should not be an option in the bought (as it is already selected in the not bought) within_fieldset "Customers who bought" do find("input", visible: :all).send_keys("Product 1") end expect(page).to have_no_selector("[role='option']", text: "Product 1") expect(page).to have_nth_table_row_record(1, "Customer 2") expect(page).to have_nth_table_row_record(2, "Customer 3") end end describe "exporting" do it "downloads the CSV" do expect(Exports::PurchaseExportService).to receive(:export).with( seller:, recipient: user_with_role_for_seller, filters: ActionController::Parameters.new( { start_time: 1.month.ago.strftime("%Y-%m-%d"), end_time: Date.today.strftime("%Y-%m-%d"), } ) ) visit customers_path select_disclosure "Export" do expect(page).to have_text("This will download a CSV with each purchase on its own row.") click_on "Download" end end context "when there are products selected" do it "downloads the correct CSV" do expect(Exports::PurchaseExportService).to receive(:export).with( seller:, recipient: user_with_role_for_seller, filters: ActionController::Parameters.new( { start_time: 1.month.ago.strftime("%Y-%m-%d"), end_time: Date.today.strftime("%Y-%m-%d"), product_ids: [product1.external_id], variant_ids: [membership.alive_variants.first.external_id] } ) ) visit customers_path select_disclosure "Filter" do select_combo_box_option search: "Product 1", from: "Customers who bought" select_combo_box_option search: "Membership - First Tier", from: "Customers who bought" end select_disclosure "Export" do expect(page).to have_text("This will download sales of 'Product 1, Membership - First Tier' as a CSV, with each purchase on its own row.") click_on "Download" end end end context "when the CSV is too large to generate synchronously" do before do stub_const("Exports::PurchaseExportService::SYNCHRONOUS_EXPORT_THRESHOLD", 1) end it "displays a flash message" do visit customers_path select_disclosure "Export" do click_on "Download" end expect(page).to have_alert(text: "You will receive an email in your inbox with the data you've requested shortly.") end end end context "when the purchase has a utm link" do let(:utm_link) { create(:utm_link, utm_source: "twitter", utm_medium: "social", utm_campaign: "gumroad-twitter", utm_term: "gumroad-123", utm_content: "gumroad-456") } let(:purchase) { create(:purchase, link: product1, email: "john@example.com", seller:) } let!(:utm_link_driven_sale) { create(:utm_link_driven_sale, purchase:, utm_link:) } before do index_model_records(Purchase) end it "shows the utm link pill and details in the drawer" do login_as seller visit customers_path row = find(:table_row, { "Email" => "john@example.com" }) within row do expect(page).to have_text("UTM") end row.click within_modal "Product 1" do within_section "UTM link", section_element: :section, match: :first do expect(page).to have_text("This sale was driven by a UTM link.") expect(page).to have_link("UTM link", href: utm_link.utm_url) expect(page).to have_text("Title #{utm_link.title}", normalize_ws: true) expect(page).to have_text("Source twitter", normalize_ws: true) expect(page).to have_text("Medium social", normalize_ws: true) expect(page).to have_text("Campaign gumroad-twitter", normalize_ws: true) expect(page).to have_text("Term gumroad-123", normalize_ws: true) expect(page).to have_text("Content gumroad-456", normalize_ws: true) end end end end describe "installment plans" do let(:product_with_installment_plan) { create(:product, :with_installment_plan, price_cents: 3000, name: "Awesome Product", user: seller) } let!(:installment_plan_purchase) { create(:installment_plan_purchase, link: product_with_installment_plan, email: "installment_buyer@gumroad.com") } before { index_model_records(Purchase) } it "displays the correct information" do login_as seller visit customers_path row = find(:table_row, { "Email" => installment_plan_purchase.email }) within row do expect(page).to have_text("Awesome Product Installments", normalize_ws: true) expect(page).to have_text("$10 a month") end row.click within_modal product_with_installment_plan.name do within_section "Order information" do expect(page).to have_text("Installment plan status In progress", normalize_ws: true) end within_section "Charges", section_element: :section do expect(page).to have_text("2 charges remaining", normalize_ws: true) end expect(page).to have_button("Cancel installment plan") end end end end describe "drawer" do it "displays all attributes correctly" do allow_any_instance_of(Purchase).to receive(:transaction_url_for_seller).and_return("https://www.google.com") review = create(:product_review, purchase: purchase1, message: "Amazing!") create(:product_review_response, product_review: review, message: "Thank you!", user: seller) create(:tip, purchase: purchase1, value_cents: 100) visit customers_path find(:table_row, { "Email" => "customer1@gumroad.com" }).click within_modal "Product 1" do within_section "Order information" do expect(page).to have_link("Transaction", href: "https://www.google.com") expect(page).to have_text("Customer name Customer 1", normalize_ws: true) expect(page).to have_text("Quantity 1", normalize_ws: true) expect(page).to have_text("Price $0", normalize_ws: true) expect(page).to have_text("Upsell Upsell", normalize_ws: true) expect(page).to have_text("Tip $1", normalize_ws: true) end within_section "Review" do within_section "Rating" do expect(page).to have_selector("[aria-label='1 star']") end within_section "Message" do expect(page).to have_text("Amazing!") end within_section "Response" do expect(page).to have_text("Thank you!") end end end click_on "Close" find(:table_row, { "Email" => "customer2@gumroad.com" }).click within_modal "Membership" do within_section "Order information" do expect(page).to have_text("Customer name Customer 2", normalize_ws: true) expect(page).to have_text("Seats 2", normalize_ws: true) expect(page).to have_text("Price $6 a month $2 a month", normalize_ws: true) expect(page).to have_text("Discount $1 off with code CODE", normalize_ws: true) expect(page).to have_text("Membership status Active", normalize_ws: true) expect(page).to have_text("Referrer Gumroad Product Recommendations", normalize_ws: true) end end end describe "download count" do it "shows download count for regular products and hides it for bundle purchases" do create(:url_redirect, purchase: purchase1, uses: 42) create(:url_redirect, purchase: purchase3, uses: 25) index_model_records(Purchase) visit customers_path # Test regular product - should show download count find(:table_row, { "Email" => "customer1@gumroad.com" }).click within_modal do expect(page).to have_text("Download count 42", normalize_ws: true) end click_on "Close" # Test bundle purchase - should NOT show download count find(:table_row, { "Email" => "customer3hasaninsanelylonge..." }).click within_modal do expect(page).not_to have_text("Download count") end end it "hides download count for coffee products" do coffee_product = create(:product, user: seller, name: "Buy Me Coffee", native_type: Link::NATIVE_TYPE_COFFEE, price_cents: 500) coffee_purchase = create(:purchase, link: coffee_product, full_name: "Coffee Buyer", email: "coffee@example.com", seller:, created_at: 1.day.ago) create(:url_redirect, purchase: coffee_purchase, uses: 10) index_model_records(Purchase) visit customers_path # Test coffee product - should NOT show download count find(:table_row, { "Email" => "coffee@example.com" }).click within_modal do expect(page).not_to have_text("Download count") end end end describe "missed posts" do let!(:posts) do create_list(:installment, 11, link: product1, published_at: Time.current) do |post, i| post.update!(name: "Post #{i}") end end before do create(:customer_email_info_opened, purchase: purchase1) end it "displays the missed posts and allows re-sending them" do allow_any_instance_of(User).to receive(:sales_cents_total).and_return(Installment::MINIMUM_SALES_CENTS_VALUE) stripe_connect_account = create(:merchant_account_stripe_connect, user: seller) create(:purchase, seller:, link: product1, merchant_account: stripe_connect_account) post = posts.last visit customers_path find(:table_row, { "Name" => "Customer 1" }).click within_modal "Product 1" do within_section "Send missed posts", section_element: :section do 10.times do |i| expect(page).to have_section("Post #{i}") end expect(page).to_not have_section("Post 10") click_on "Show more" expect(page).to_not have_button("Show more") within_section "Post 10" do expect(page).to have_link("Post 10", href: post.full_url) expect(page).to have_text("Originally sent on #{post.published_at.strftime("%b %-d")}") click_on "Send" expect(page).to have_button("Sending...", disabled: true) end end end expect(page).to have_alert(text: "Email Sent") within_section("Post 10") { expect(page).to have_button("Sent", disabled: true) } expect(EmailInfo.last.installment).to eq(post) visit customers_path find(:table_row, { "Name" => "Customer 1" }).click within_modal "Product 1" do within_section "Send missed posts", section_element: :section do expect(page).to_not have_button("Show more") expect(page).to_not have_section("Post 10") end within_section "Emails received", section_element: :section do within_section "Post 10" do expect(page).to have_text("Sent #{post.published_at.strftime("%b %-d")}") click_on "Resend email" expect(page).to have_button("Sending...", disabled: true) end end end expect(page).to have_alert(text: "Sent") within_section("Post 10") { expect(page).to have_button("Sent", disabled: true) } end it "does not allow re-sending an email if the seller is not eligible to send emails" do visit customers_path find(:table_row, { "Name" => "Customer 1" }).click within_modal "Product 1" do within_section "Send missed posts", section_element: :section do click_on "Show more" within_section "Post 10" do click_on "Send" expect(page).to have_button("Sending...", disabled: true) end end end expect(page).to have_alert(text: "You are not eligible to resend this email.") expect(EmailInfo.last.installment).to be_nil end end describe "receipts" do let!(:membership_purchase) { create(:membership_purchase, link: membership, subscription: purchase2.subscription, created_at: 1.day.ago) } it "displays the receipts and allows re-sending them" do visit customers_path find(:table_row, { "Name" => "Customer 2" }).click within_modal "Membership" do within_section "Emails received", section_element: :section do expect(page).to have_section("Receipt", text: "Delivered #{purchase2.created_at.strftime("%b %-d")}") within_section "Receipt", text: "Delivered #{membership_purchase.created_at.strftime("%b %-d")}" do click_on "Resend receipt" expect(page).to have_button("Resending receipt...", disabled: true) end end end expect(page).to have_alert(text: "Receipt resent") expect(SendPurchaseReceiptJob).to have_enqueued_sidekiq_job(membership_purchase.id).on("critical") within_section "Receipt", text: "Delivered #{membership_purchase.created_at.strftime("%b %-d")}" do expect(page).to have_button("Receipt resent", disabled: true) end end end describe "additional contributions" do before do purchase1.update!(is_additional_contribution: true) end it "includes an additional contribution status" do visit customers_path find(:table_row, { "Name" => "Customer 1" }).click within_modal "Product 1" do expect(page).to have_selector("[role='status']", text: "Additional amount: This is an additional contribution, added to a previous purchase of this product.") end end end describe "PPP purchases" do before do purchase1.update!(is_purchasing_power_parity_discounted: true, ip_country: "United States") purchase1.create_purchasing_power_parity_info!(factor: 0.5) end it "includes a PPP status" do visit customers_path find(:table_row, { "Name" => "Customer 1" }).click within_modal "Product 1" do expect(page).to have_selector("[role='status']", text: "This customer received a purchasing power parity discount of 50% because they are located in United States.") end end end describe "gifts" do let(:giftee_purchase) { purchase1 } let(:gifter_purchase) { purchase2 } let!(:gift) { create(:gift, giftee_email: giftee_purchase.email, gifter_email: gifter_purchase.email, giftee_purchase:, gifter_purchase:) } before do gifter_purchase.update!(is_gift_sender_purchase: true) giftee_purchase.update!(is_gift_receiver_purchase: true) end it "includes gift statuses" do visit customers_path find(:table_row, { "Name" => "Customer 2" }).click within_modal "Membership" do expect(page).to have_selector("[role='status']", text: "customer2@gumroad.com purchased this for customer1@gumroad.com.") end end it "shows review from the giftee purchase" do review = create(:product_review, purchase: giftee_purchase, message: "Giftee review") create(:product_review_response, product_review: review, message: "Giftee review response", user: seller) visit customers_path find(:table_row, { "Name" => "Customer 2" }).click within_modal "Membership" do within_section "Review" do within_section "Rating" do expect(page).to have_selector("[aria-label='1 star']") end within_section "Message" do expect(page).to have_text("Giftee review") end within_section "Response" do expect(page).to have_text("Giftee review response") end end end end end describe "preorders" do before do purchase1.update!(is_preorder_authorization: true, preorder: create(:preorder)) end it "includes a preorder status" do visit customers_path find(:table_row, { "Name" => "Customer 1" }).click within_modal "Product 1" do expect(page).to have_selector("[role='status']", text: "Pre-order: This is a pre-order authorization. The customer's card has not been charged yet.") end end end describe "affiliates" do it "includes an affiliate status" do purchase1.update!(affiliate: create(:direct_affiliate)) visit customers_path find(:table_row, { "Name" => "Customer 1" }).click within_modal "Product 1" do expect(page).to have_selector("[role='status']", text: "Affiliate: An affiliate (#{purchase1.affiliate.affiliate_user.form_email}) helped you make this sale and received $0.") end end it "does not include affiliate status if the affiliate is a collaborator" do purchase1.update!(affiliate: create(:collaborator)) visit customers_path find(:table_row, { "Name" => "Customer 1" }).click within_modal "Product 1" do expect(page).not_to have_selector("[role='status']", text: "Affiliate: An affiliate (#{purchase1.affiliate.affiliate_user.form_email}) helped you make this sale and received $0.") end end end describe "email updates" do before do purchase2.update!(is_gift_sender_purchase: true) create(:gift, giftee_email: purchase1.email, gifter_email: purchase2.email, giftee_purchase: purchase1, gifter_purchase: purchase2) end it "allows updating emails" do visit customers_path find(:table_row, { "Name" => "Customer 2" }).click within_modal "Membership" do within_section "Email", section_element: :section do expect(page).to have_text("customer2@gumroad.com") click_on "Edit" fill_in "Email", with: "newcustomer2@gumroad.com" click_on "Save" end end expect(page).to have_alert(text: "Email updated successfully.") within_modal "Membership" do within_section "Email", section_element: :section do expect(page).to have_button("Edit") expect(page).to have_text("newcustomer2@gumroad.com") click_on "Edit" expect(page).to have_field("Email", with: "newcustomer2@gumroad.com") click_on "Cancel" expect(page).to have_button("Edit") uncheck "Receives emails", checked: true end end expect(page).to have_alert(text: "Your customer will no longer receive your posts.") within_modal "Membership" do within_section "Giftee email", section_element: :section do expect(page).to have_text("customer1@gumroad.com") click_on "Edit" fill_in "Giftee email", with: "newcustomer1@gumroad.com" click_on "Save" end end expect(page).to have_alert(text: "Email updated successfully.") expect(page).to have_selector("[role='status']", text: "newcustomer2@gumroad.com purchased this for newcustomer1@gumroad.com.") purchase2.reload expect(purchase2.email).to eq("newcustomer2@gumroad.com") expect(purchase2.giftee_email).to eq("newcustomer1@gumroad.com") expect(purchase2.can_contact).to eq(false) visit customers_path find(:table_row, { "Name" => "Customer 2" }).click within_modal "Membership" do within_section "Email", section_element: :section do check "Receives emails", unchecked: true end end expect(page).to have_alert(text: "Your customer will now receive your posts.") expect(purchase2.reload.can_contact).to eq(true) end it "shows an error when entering an invalid email" do visit customers_path find(:table_row, { "Name" => "Customer 2" }).click within_modal "Membership" do within_section "Email", section_element: :section do expect(page).to have_text("customer2@gumroad.com") click_on "Edit" fill_in "Email", with: "invalid-email" click_on "Save" end end expect(page).to have_alert(text: "Please enter a valid email") end context "customer has a Gumroad account" do before { purchase3.update!(purchaser: create(:user)) } it "doesn't allow updating the email" do visit customers_path find(:table_row, { "Name" => "Customer 3" }).click within_modal "Product 2" do within_section "Email", section_element: :section do expect(page).to have_text("customer3hasaninsanelylongemailaddress@gumroad.com") expect(page).to have_text("You cannot change the email of this purchase, because it was made by an existing user. Please ask them to go to gumroad.com/settings to update their email.") expect(page).to_not have_button("Edit") expect(page).to have_checked_field("Receives emails") end expect(page).to_not have_section("Giftee email") end end end end describe "bundle products" do
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
true
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/analytics/date_range_spec.rb
spec/requests/analytics/date_range_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" require "shared_examples/creator_dashboard_page" describe "Analytics date range", :js, :sidekiq_inline, type: :system do let(:seller) { create(:user, created_at: Date.new(2023, 1, 1)) } let(:test_date) { Date.today } include_context "with switching account to user as admin for seller" context "with an existing product" do let(:product) { create(:product, user: seller, name: "Product 1") } before do create(:purchase, link: product, price_cents: 100, created_at: test_date.beginning_of_month, ip_country: "Italy") recreate_model_index(ProductPageView) end it "allows selecting 'This month' date range" do travel_to test_date do visit sales_dashboard_path # Find and click the date range selector find('[aria-label="Date range selector"]').click click_on "This month" # The URL should be updated with the date range expect(page).to have_current_path(sales_dashboard_path(from: test_date.beginning_of_month.strftime("%Y-%m-%d"), to: test_date.strftime("%Y-%m-%d"))) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/analytics/audience_spec.rb
spec/requests/analytics/audience_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" require "shared_examples/creator_dashboard_page" describe "Audience analytics", :js, :sidekiq_inline, :elasticsearch_wait_for_refresh, type: :system do let(:seller) { create(:user, created_at: 1.year.ago) } include_context "with switching account to user as admin for seller" it_behaves_like "creator dashboard page", "Analytics" do let(:path) { audience_dashboard_path } end it "shows the empty state" do visit audience_dashboard_path(from: "2023-12-01", to: "2023-12-31") expect(page).to have_text("You don't have any followers yet.") expect(page).not_to have_disclosure("12/1/2023 – 12/31/2023") end context "with followers" do before do recreate_model_index(ConfirmedFollowerEvent) create(:follower, user: seller, created_at: "2023-12-14 12:00:00", confirmed_at: "2023-12-14 12:00:00") create(:follower, user: seller, created_at: "2023-12-16 12:00:00", confirmed_at: "2023-12-16 12:00:00") create(:follower, user: seller, created_at: "2023-12-18 12:00:00", confirmed_at: "2023-12-18 12:00:00") unfollowed = create(:follower, user: seller, created_at: "2023-12-15 12:00:00", confirmed_at: "2023-12-15 12:00:00") unfollowed.update!(confirmed_at: nil, deleted_at: "2023-12-16 12:00:00") end it "calculates total stats" do visit audience_dashboard_path(from: "2023-12-01", to: "2023-12-31") within_section("Lifetime followers") { expect(page).to have_text("3") } within_section("New followers") { expect(page).to have_text("3") } toggle_disclosure "12/1/2023 – 12/31/2023" click_on "Custom range..." fill_in "From (including)", with: "12/13/2023" fill_in "To (including)", with: "12/14/2023" find("body").click # Blur the date field to trigger the update expect(page).to have_current_path(audience_dashboard_path(from: "2023-12-13", to: "2023-12-14")) within_section("Lifetime followers") { expect(page).to have_text("3") } within_section("New followers") { expect(page).to have_text("1") } end it "shows the chart" do visit audience_dashboard_path(from: "2023-12-01", to: "2023-12-31") expect(page).to have_css('[data-testid="chart-dot"]', count: 31) chart = find('[data-testid="chart"]') chart.hover expect(chart).to have_tooltip(text: "1 new follower\n1 follower removed\n2 total followers\nSaturday, December 16") toggle_disclosure "12/1/2023 – 12/31/2023" click_on "Custom range..." fill_in "From (including)", with: "12/17/2023" fill_in "To (including)", with: "12/18/2023" find("body").click # Blur the date field to trigger the update expect(page).to have_css('[data-testid="chart-dot"]', count: 2) chart.hover expect(chart).to have_tooltip(text: "0 new followers\n2 total followers\nSunday, December 17") end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/analytics/utm_links_spec.rb
spec/requests/analytics/utm_links_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" require "shared_examples/creator_dashboard_page" describe "UTM links", :js, type: :system do let(:seller) { create(:user) } before do Feature.activate_user(:utm_links, seller) end describe "dashboard" do include_context "with switching account to user as admin for seller" describe "listing page" do it_behaves_like "creator dashboard page", "Analytics" do let(:path) { utm_links_dashboard_path } end it "shows the empty state when there are no UTM links" do visit utm_links_dashboard_path expect(page).to have_text("No links yet") expect(page).to have_text("Use UTM links to track which sources are driving the most conversions and revenue") expect(find("a", text: "Learn more about UTM tracking")[:href]).to include("/help/article/74-the-analytics-dashboard") end it "shows UTM links" do utm_link = create(:utm_link, seller:, unique_clicks: 3) create(:utm_link_driven_sale, utm_link:, purchase: create(:purchase, price_cents: 1000, seller:, link: create(:product, user: seller))) visit utm_links_dashboard_path wait_for_ajax expect(page).to have_table_row({ "Link" => utm_link.title, "Source" => utm_link.utm_source, "Medium" => utm_link.utm_medium, "Campaign" => utm_link.utm_campaign, "Destination" => "Profile page", "Clicks" => "3", "Revenue" => "$10", "Conversion" => "33.33%" }) within find(:table_row, { "Link" => utm_link.title }) do copy_button = find_button("Copy link") copy_button.hover expect(copy_button).to have_tooltip(text: "Copy short link") copy_button.click expect(copy_button).to have_tooltip(text: "Copied!") end end describe "sidebar drawer" do let!(:utm_link) { create(:utm_link, seller:, utm_content: "video-ad", unique_clicks: 3) } let!(:utm_link_driven_sale) { create(:utm_link_driven_sale, utm_link:, purchase: create(:purchase, price_cents: 1000, seller:, link: create(:product, user: seller))) } let!(:utm_link_driven_sale2) { create(:utm_link_driven_sale, utm_link:, purchase: create(:purchase, price_cents: 1000, seller:, link: create(:product, user: seller))) } it "shows the selected UTM link details" do visit utm_links_dashboard_path wait_for_ajax find(:table_row, { "Link" => utm_link.title, "Clicks" => "3", "Conversion" => "66.67%" }).click wait_for_ajax within_modal utm_link.title do within_section "Details" do expect(page).to have_text("Destination Profile page", normalize_ws: true) expect(page).to have_link("Profile page", href: seller.profile_url) expect(page).to have_text("Source #{utm_link.utm_source}", normalize_ws: true) expect(page).to have_text("Medium #{utm_link.utm_medium}", normalize_ws: true) expect(page).to have_text("Campaign #{utm_link.utm_campaign}", normalize_ws: true) expect(page).to_not have_text("Term") expect(page).to have_text("Content video-ad", normalize_ws: true) end within_section "Statistics" do expect(page).to have_text("Clicks 3", normalize_ws: true) expect(page).to have_text("Sales 2", normalize_ws: true) expect(page).to have_text("Revenue $20", normalize_ws: true) expect(page).to have_text("Conversion rate 66.67%", normalize_ws: true) end within_section "Short link" do expect(page).to have_text(utm_link.short_url) copy_button = find_button("Copy short link") copy_button.hover expect(copy_button).to have_tooltip(text: "Copy short link") end within_section "UTM link" do expect(page).to have_text(utm_link.utm_url) copy_button = find_button("Copy UTM link") copy_button.hover expect(copy_button).to have_tooltip(text: "Copy UTM link") end expect(page).to have_link("Duplicate") expect(page).to have_link("Edit") expect(page).to have_button("Delete") end end it "allows deleting a UTM link" do visit utm_links_dashboard_path find(:table_row, { "Link" => utm_link.title }).click within_modal utm_link.title do click_on "Delete" end within_modal "Delete link?" do click_on "Delete" end wait_for_ajax expect(page).to have_alert(text: "Link deleted!") expect(page).to_not have_modal expect(page).to_not have_table_row({ "Link" => utm_link.title }) expect(utm_link.reload).to be_deleted end end it "paginates the UTM links" do stub_const("PaginatedUtmLinksPresenter::PER_PAGE", 1) utm_link1 = create(:utm_link, seller:, created_at: 2.days.ago) utm_link2 = create(:utm_link, seller:, created_at: 1.day.ago) visit utm_links_dashboard_path expect(page).to have_table_row({ "Link" => utm_link2.title }) expect(page).to_not have_table_row({ "Link" => utm_link1.title }) expect(page).to have_button("1", aria: { current: "page" }) expect(page).to have_button("2") expect(page).to_not have_button("3") expect(page).to have_button("Previous", disabled: true) expect(page).to have_button("Next") click_on "Next" expect(page).to have_table_row({ "Link" => utm_link1.title }) expect(page).to_not have_table_row({ "Link" => utm_link2.title }) expect(page).to have_button("2", aria: { current: "page" }) expect(page).to have_button("1") expect(page).to_not have_button("3") expect(page).to have_button("Previous") expect(page).to have_button("Next", disabled: true) expect(page).to have_current_path(utm_links_dashboard_path({ page: 2 })) end it "sorts UTM links by different columns and direction and allows pagination" do stub_const("PaginatedUtmLinksPresenter::PER_PAGE", 1) utm_link1 = create(:utm_link, seller:, title: "A Link", utm_source: "twitter", utm_medium: "social", utm_campaign: "spring", unique_clicks: 3, created_at: 3.days.ago) utm_link2 = create(:utm_link, seller:, title: "B Link", utm_source: "newsletter", utm_medium: "email", utm_campaign: "winter", unique_clicks: 1, created_at: 1.day.ago) create(:utm_link_driven_sale, utm_link: utm_link1, purchase: create(:purchase, price_cents: 1000, seller:, link: create(:product, user: seller))) create(:utm_link_driven_sale, utm_link: utm_link1, purchase: create(:purchase, price_cents: 500, seller:, link: create(:product, user: seller))) create(:utm_link_driven_sale, utm_link: utm_link2, purchase: create(:purchase, price_cents: 2000, seller:, link: create(:product, user: seller))) visit utm_links_dashboard_path # By default, it sorts by "Date" column in descending order expect(page).to have_table_row({ "Link" => "B Link" }) expect(page).to_not have_table_row({ "Link" => "A Link" }) click_on "Next" expect(page).to have_table_row({ "Link" => "A Link" }) expect(page).to_not have_table_row({ "Link" => "B Link" }) # Sort by "Link" title find_and_click("th", text: "Link") expect(page).to have_current_path("#{utm_links_dashboard_path}?key=link&direction=asc") expect(page).to have_table_row({ "Link" => "A Link" }) expect(page).to_not have_table_row({ "Link" => "B Link" }) click_on "Next" expect(page).to have_current_path("#{utm_links_dashboard_path}?key=link&direction=asc&page=2") expect(page).to have_table_row({ "Link" => "B Link" }) expect(page).to_not have_table_row({ "Link" => "A Link" }) # Sort by "Source" column find_and_click("th", text: "Source") expect(page).to have_current_path("#{utm_links_dashboard_path}?key=source&direction=asc") expect(page).to have_table_row({ "Link" => "B Link", "Source" => "newsletter" }) expect(page).to_not have_table_row({ "Link" => "A Link", "Source" => "twitter" }) click_on "Next" expect(page).to have_current_path("#{utm_links_dashboard_path}?key=source&direction=asc&page=2") expect(page).to have_table_row({ "Link" => "A Link", "Source" => "twitter" }) expect(page).to_not have_table_row({ "Link" => "B Link", "Source" => "newsletter" }) # Sort by "Medium" column find_and_click("th", text: "Medium") expect(page).to have_current_path("#{utm_links_dashboard_path}?key=medium&direction=asc") expect(page).to have_table_row({ "Link" => "B Link", "Medium" => "email" }) expect(page).to_not have_table_row({ "Link" => "A Link", "Medium" => "social" }) click_on "Next" expect(page).to have_current_path("#{utm_links_dashboard_path}?key=medium&direction=asc&page=2") expect(page).to have_table_row({ "Link" => "A Link", "Medium" => "social" }) expect(page).to_not have_table_row({ "Link" => "B Link", "Medium" => "email" }) # Sort by "Campaign" column find_and_click("th", text: "Campaign") expect(page).to have_current_path("#{utm_links_dashboard_path}?key=campaign&direction=asc") expect(page).to have_table_row({ "Link" => "A Link", "Campaign" => "spring" }) expect(page).to_not have_table_row({ "Link" => "B Link", "Campaign" => "winter" }) click_on "Next" expect(page).to have_current_path("#{utm_links_dashboard_path}?key=campaign&direction=asc&page=2") expect(page).to have_table_row({ "Link" => "B Link", "Campaign" => "winter" }) expect(page).to_not have_table_row({ "Link" => "A Link", "Campaign" => "spring" }) # Sort by "Clicks" column find_and_click("th", text: "Clicks") expect(page).to have_current_path("#{utm_links_dashboard_path}?key=clicks&direction=asc") expect(page).to have_table_row({ "Link" => "B Link", "Clicks" => "1" }) expect(page).to_not have_table_row({ "Link" => "A Link", "Clicks" => "3" }) click_on "Next" expect(page).to have_current_path("#{utm_links_dashboard_path}?key=clicks&direction=asc&page=2") expect(page).to have_table_row({ "Link" => "A Link", "Clicks" => "3" }) expect(page).to_not have_table_row({ "Link" => "B Link", "Clicks" => "1" }) # Sort by "Revenue" column find_and_click("th", text: "Revenue") expect(page).to have_current_path("#{utm_links_dashboard_path}?key=revenue_cents&direction=asc") expect(page).to have_table_row({ "Link" => "A Link", "Revenue" => "$15" }) expect(page).to_not have_table_row({ "Link" => "B Link", "Revenue" => "$20" }) click_on "Next" expect(page).to have_current_path("#{utm_links_dashboard_path}?key=revenue_cents&direction=asc&page=2") expect(page).to have_table_row({ "Link" => "B Link", "Revenue" => "$20" }) expect(page).to_not have_table_row({ "Link" => "A Link", "Revenue" => "$15" }) # Sort by "Conversion" column find_and_click("th", text: "Conversion") expect(page).to have_current_path("#{utm_links_dashboard_path}?key=conversion_rate&direction=asc") expect(page).to have_table_row({ "Link" => "A Link", "Conversion" => "66.67%" }) expect(page).to_not have_table_row({ "Link" => "B Link", "Conversion" => "100%" }) click_on "Next" expect(page).to have_current_path("#{utm_links_dashboard_path}?key=conversion_rate&direction=asc&page=2") expect(page).to have_table_row({ "Link" => "B Link", "Conversion" => "100%" }) expect(page).to_not have_table_row({ "Link" => "A Link", "Conversion" => "66.67%" }) end it "filters UTM links by search query by adhering to the current column sort order" do stub_const("PaginatedUtmLinksPresenter::PER_PAGE", 1) utm_link1 = create(:utm_link, seller:, title: "Facebook Summer Sale", utm_source: "facebook", utm_medium: "social", utm_campaign: "summer_2024" ) utm_link2 = create(:utm_link, seller:, title: "Twitter Winter Promo", utm_source: "twitter", utm_medium: "social", utm_campaign: "winter_2024" ) visit utm_links_dashboard_path # Sort by "Link" column in descending order find_and_click("th", text: "Link") expect(page).to have_current_path("#{utm_links_dashboard_path}?key=link&direction=asc") find_and_click("th", text: "Link") expect(page).to have_current_path("#{utm_links_dashboard_path}?key=link&direction=desc") expect(page).to have_table_row({ "Link" => utm_link2.title }) expect(page).to_not have_table_row({ "Link" => utm_link1.title }) click_on "Next" expect(page).to have_table_row({ "Link" => utm_link1.title }) expect(page).to_not have_table_row({ "Link" => utm_link2.title }) expect(page).to have_current_path("#{utm_links_dashboard_path}?key=link&direction=desc&page=2") # Search by title select_disclosure "Search" do fill_in "Search", with: " Sale " end wait_for_ajax expect(page).to have_table_row({ "Link" => utm_link1.title }) expect(page).not_to have_table_row({ "Link" => utm_link2.title }) expect(page).to_not have_button("Next") # Always takes to the first page when searching regardless of the previous page number expect(page).to have_current_path("#{utm_links_dashboard_path}?key=link&direction=desc&query=+Sale+++++") # Search by source select_disclosure "Search" do fill_in "Search", with: "TwiTTer" end wait_for_ajax expect(page).to have_table_row({ "Link" => utm_link2.title }) expect(page).not_to have_table_row({ "Link" => utm_link1.title }) expect(page).to_not have_button("Next") expect(page).to have_current_path("#{utm_links_dashboard_path}?key=link&direction=desc&query=TwiTTer") # Shows filtered results on accessing the page with the 'query' param visit "#{utm_links_dashboard_path}?key=source&direction=asc&query=PROMO" expect(page).to have_table_row({ "Link" => utm_link2.title }) expect(page).not_to have_table_row({ "Link" => utm_link1.title }) expect(page).to_not have_button("Next") # Search by medium select_disclosure "Search" do fill_in "Search", with: "Social" end wait_for_ajax expect(page).to have_table_row({ "Link" => utm_link1.title }) expect(page).to_not have_table_row({ "Link" => utm_link2.title }) click_on "Next" expect(page).to have_table_row({ "Link" => utm_link2.title }) expect(page).to_not have_table_row({ "Link" => utm_link1.title }) # Search by campaign select_disclosure "Search" do fill_in "Search", with: "winter_" end wait_for_ajax expect(page).to have_table_row({ "Link" => utm_link2.title }) expect(page).to_not have_table_row({ "Link" => utm_link1.title }) expect(page).to_not have_button("Next") # Search with no matches select_disclosure "Search" do fill_in "Search", with: "nonexistent" end wait_for_ajax expect(page).to have_text('No links found for "nonexistent"') expect(page).not_to have_table_row({ "Link" => utm_link1.title }) expect(page).not_to have_table_row({ "Link" => utm_link2.title }) expect(page).to_not have_button("Next") # Clear search select_disclosure "Search" do fill_in "Search", with: "" end wait_for_ajax expect(page).to have_table_row({ "Link" => utm_link1.title }) expect(page).to_not have_table_row({ "Link" => utm_link2.title }) click_on "Next" expect(page).to have_table_row({ "Link" => utm_link2.title }) expect(page).to_not have_table_row({ "Link" => utm_link1.title }) end it "allows deleting a UTM link" do stub_const("PaginatedUtmLinksPresenter::PER_PAGE", 1) utm_link1 = create(:utm_link, seller:) utm_link2 = create(:utm_link, seller:) visit utm_links_dashboard_path find_and_click("th", text: "Link") expect(page).to have_table_row({ "Link" => utm_link1.title }) expect(page).to_not have_table_row({ "Link" => utm_link2.title }) click_on "Next" expect(page).to have_table_row({ "Link" => utm_link2.title }) expect(page).to_not have_table_row({ "Link" => utm_link1.title }) within find(:table_row, { "Link" => utm_link2.title }) do select_disclosure "Open action menu" do click_on "Delete" end end within_modal "Delete link?" do expect(page).to have_text(%Q(Are you sure you want to delete the link "#{utm_link2.title}"? This action cannot be undone.)) click_on "Cancel" end expect(page).to_not have_modal("Delete link?") expect(page).to have_table_row({ "Link" => utm_link2.title }) within find(:table_row, { "Link" => utm_link2.title }) do select_disclosure "Open action menu" do click_on "Delete" end end within_modal "Delete link?" do click_on "Delete" end wait_for_ajax expect(page).to have_alert(text: "Link deleted!") expect(page).to_not have_button("Next") expect(page).to_not have_table_row({ "Link" => utm_link2.title }) expect(page).to have_table_row({ "Link" => utm_link1.title }) expect(utm_link2.reload).to be_deleted end end describe "create page" do let!(:product) { create(:product, user: seller, name: "Product A") } let!(:post) { create(:audience_post, :published, seller:, name: "Post B", shown_on_profile: true) } let!(:existing_utm_link) do create(:utm_link, seller:, utm_campaign: "spring", utm_medium: "social", utm_source: "facebook", utm_term: "sale", utm_content: "banner" ) end it "renders the create link form" do allow(SecureRandom).to receive(:alphanumeric).and_return("unique01") visit "#{utm_links_dashboard_path}/new" expect(page).to have_text("Create link") expect(page).to have_link("Cancel", href: utm_links_dashboard_path) expect(page).to have_text("Create UTM links to track where your traffic is coming from") expect(page).to have_text("Once set up, simply share the links to see which sources are driving more conversions and revenue") expect(find("a", text: "Learn more")[:href]).to include("/help/article/74-the-analytics-dashboard") expect(page).to have_input_labelled("Title", with: "") find(:label, "Destination").click expect(page).to have_combo_box("Destination", options: ["Profile page", "Subscribe page", "Product — Product A", "Post — Post B"]) expect(page).to have_input_labelled("Link", with: "unique01") send_keys(:escape) find(:label, "Source").click expect(page).to have_combo_box("Source", options: ["facebook"]) send_keys(:escape) find(:label, "Medium").click expect(page).to have_combo_box("Medium", options: ["social"]) send_keys(:escape) find(:label, "Campaign").click expect(page).to have_combo_box("Campaign", options: ["spring"]) send_keys(:escape) find(:label, "Term").click expect(page).to have_combo_box("Term", options: ["sale"]) send_keys(:escape) find(:label, "Content").click expect(page).to have_combo_box("Content", options: ["banner"]) send_keys(:escape) expect(page).to_not have_field("Generated URL with UTM tags") find(:label, "Destination").click select_combo_box_option "Profile page", from: "Destination" expect(page).to_not have_field("Generated URL with UTM tags") find(:label, "Source").click select_combo_box_option "facebook", from: "Source" select_combo_box_option "social", from: "Medium" select_combo_box_option "spring", from: "Campaign" select_combo_box_option "sale", from: "Term" select_combo_box_option "banner", from: "Content" expect(page).to have_field("Generated URL with UTM tags", with: "#{seller.profile_url}?utm_source=facebook&utm_medium=social&utm_campaign=spring&utm_term=sale&utm_content=banner", readonly: true) find(:label, "Destination").click select_combo_box_option "Post — Post B", from: "Destination" expect(page).to have_field("Generated URL with UTM tags", with: "#{post.full_url}?utm_source=facebook&utm_medium=social&utm_campaign=spring&utm_term=sale&utm_content=banner", readonly: true) find(:label, "Destination").click select_combo_box_option "Subscribe page", from: "Destination" expect(page).to have_field("Generated URL with UTM tags", with: "#{Rails.application.routes.url_helpers.custom_domain_subscribe_url(host: seller.subdomain_with_protocol)}?utm_source=facebook&utm_medium=social&utm_campaign=spring&utm_term=sale&utm_content=banner", readonly: true) find(:label, "Destination").click select_combo_box_option "Product — Product A", from: "Destination" expect(page).to have_field("Generated URL with UTM tags", with: "#{product.long_url}?utm_source=facebook&utm_medium=social&utm_campaign=spring&utm_term=sale&utm_content=banner", readonly: true) # An arbitrary value can be entered in a UTM field, and it will be transformed into a valid value find(:label, "Term").click fill_in "Term", with: "BIG Offer!" within :fieldset, "Term" do find_and_click("[role='option']", text: "big-offer-") end find(:label, "Content").click fill_in "Content", with: "a" * 250 within :fieldset, "Content" do find_and_click("[role='option']", text: "a" * 200) end expect(page).to have_field("Generated URL with UTM tags", with: "#{product.long_url}?utm_source=facebook&utm_medium=social&utm_campaign=spring&utm_term=big-offer-&utm_content=#{'a' * 200}", readonly: true) end it "generates a new permalink when clicking the refresh button" do allow(SecureRandom).to receive(:alphanumeric).and_return("initial1", "newlink2") visit "#{utm_links_dashboard_path}/new" within :fieldset, "Link" do expect(page).to have_text(%Q(#{UrlService.short_domain_with_protocol.sub("#{PROTOCOL}://", '')}/u/)) expect(page).to have_field("Link", with: "initial1", readonly: true) click_on "Generate new short link" wait_for_ajax expect(page).to have_field("Link", with: "newlink2", readonly: true) end end it "shows validation errors" do record = UtmLink.new(seller:, title: "Test Link", target_resource_type: "profile_page", permalink: "$tesT123") record.valid? allow_any_instance_of(SaveUtmLinkService).to receive(:perform).and_raise(ActiveRecord::RecordInvalid.new(record)) visit "#{utm_links_dashboard_path}/new" click_on "Add link" expect(find_field("Title")).to have_ancestor("fieldset.danger") within :fieldset, "Title" do expect(page).to have_text("Must be present") end fill_in "Title", with: "Test Link" expect(find_field("Title")).to_not have_ancestor("fieldset.danger") expect(page).to_not have_text("Must be present") click_on "Add link" expect(find_field("Destination")).to have_ancestor("fieldset.danger") within :fieldset, "Destination" do expect(page).to have_text("Must be present") end select_combo_box_option "Product — Product A", from: "Destination" expect(page).to_not have_text("Must be present") click_on "Add link" expect(find_field("Source")).to have_ancestor("fieldset.danger") within :fieldset, "Source" do expect(page).to have_text("Must be present") end select_combo_box_option "facebook", from: "Source" expect(page).to_not have_text("Must be present") click_on "Add link" expect(find_field("Medium")).to have_ancestor("fieldset.danger") within :fieldset, "Medium" do expect(page).to have_text("Must be present") end select_combo_box_option "social", from: "Medium" expect(page).to_not have_text("Must be present") click_on "Add link" expect(find_field("Campaign")).to have_ancestor("fieldset.danger") within :fieldset, "Campaign" do expect(page).to have_text("Must be present") end select_combo_box_option "spring", from: "Campaign" expect(page).to_not have_text("Must be present") click_on "Add link" wait_for_ajax expect(find_field("Link")).to have_ancestor("fieldset.danger") within :fieldset, "Link" do expect(page).to have_text("is invalid") end expect(page).to_not have_alert(text: "Link created!") expect(page).to have_current_path("#{utm_links_dashboard_path}/new") end it "creates a UTM link" do visit "#{utm_links_dashboard_path}/new" fill_in "Title", with: "Test Link" expect(page).to_not have_field("Generated URL with UTM tags") select_combo_box_option "Product — Product A", from: "Destination" within :fieldset, "Link" do button = find_button("Copy short link") button.hover expect(button).to have_tooltip(text: "Copy short link") end expect(page).to_not have_field("Generated URL with UTM tags") select_combo_box_option "facebook", from: "Source" expect(page).to_not have_field("Generated URL with UTM tags") select_combo_box_option "social", from: "Medium" expect(page).to_not have_field("Generated URL with UTM tags") select_combo_box_option "spring", from: "Campaign" expect(page).to have_field("Generated URL with UTM tags", with: "#{product.long_url}?utm_source=facebook&utm_medium=social&utm_campaign=spring") select_combo_box_option "sale-2", from: "Term" select_combo_box_option "banner", from: "Content" expect(page).to have_field("Generated URL with UTM tags", with: "#{product.long_url}?utm_source=facebook&utm_medium=social&utm_campaign=spring&utm_term=sale-2&utm_content=banner") within :fieldset, "Generated URL with UTM tags" do button = find_button("Copy UTM link") button.hover expect(button).to have_tooltip(text: "Copy UTM link") end click_on "Add link" wait_for_ajax expect(page).to have_alert(text: "Link created!") expect(page).to have_current_path(utm_links_dashboard_path) expect(page).to have_table_row({ "Link" => "Test Link", "Source" => "facebook", "Medium" => "social", "Campaign" => "spring", "Destination" => "Product A" }) utm_link = seller.utm_links.last expect(utm_link.title).to eq("Test Link") expect(utm_link.target_resource_type).to eq("product_page") expect(utm_link.target_resource_id).to eq(product.id) expect(utm_link.utm_source).to eq("facebook") expect(utm_link.utm_medium).to eq("social") expect(utm_link.utm_campaign).to eq("spring") expect(utm_link.utm_term).to eq("sale-2") expect(utm_link.utm_content).to eq("banner") end end describe "duplicate link" do it "pre-fills the create page with the existing UTM link's values" do product = create(:product, user: seller, name: "Product A") existing_utm_link = create(:utm_link, seller:, title: "Existing UTM Link", target_resource_type: :product_page, target_resource_id: product.id, utm_source: "newsletter", utm_medium: "email", utm_campaign: "summer-sale", utm_term: "sale", utm_content: "banner") visit utm_links_dashboard_path within(:table_row, { "Link" => "Existing UTM Link" }) do select_disclosure "Open action menu" do click_on "Duplicate" end end expect(page).to have_current_path("#{utm_links_dashboard_path}/new?copy_from=#{existing_utm_link.external_id}") expect(page).to have_input_labelled("Title", with: "Existing UTM Link (copy)") within :fieldset, "Destination" do expect(page).to have_text("Product — Product A") end expect(page).to_not have_input_labelled("Link", with: existing_utm_link.permalink) within :fieldset, "Source" do expect(page).to have_text("newsletter") end within :fieldset, "Medium" do expect(page).to have_text("email") end within :fieldset, "Campaign" do expect(page).to have_text("summer-sale") end within :fieldset, "Term" do expect(page).to have_text("sale") end within :fieldset, "Content" do expect(page).to have_text("banner") end expect(page).to have_field("Generated URL with UTM tags", with: "#{product.long_url}?utm_source=newsletter&utm_medium=email&utm_campaign=summer-sale&utm_term=sale&utm_content=banner") click_on "Add link" wait_for_ajax within :fieldset, "Destination" do expect(page).to have_text("A link with similar UTM parameters already exists for this destination!") end expect(page).to have_current_path("#{utm_links_dashboard_path}/new?copy_from=#{existing_utm_link.external_id}") expect(UtmLink.sole).to eq(existing_utm_link) # Update a UTM parameter with a different value find(:label, "Campaign").click fill_in "Campaign", with: "summer-sale-2" within :fieldset, "Campaign" do find_and_click("[role='option']", text: "summer-sale-2") end click_on "Add link" wait_for_ajax expect(page).to have_alert(text: "Link created!") expect(page).to have_current_path(utm_links_dashboard_path) expect(page).to have_table_row({ "Link" => "Existing UTM Link (copy)", "Source" => "newsletter", "Medium" => "email", "Campaign" => "summer-sale-2" }) expect(UtmLink.pluck(:title)).to eq(["Existing UTM Link", "Existing UTM Link (copy)"]) expect(UtmLink.last.permalink).to_not eq(existing_utm_link.permalink) end end describe "update page" do it "performs validations and updates the UTM link" do utm_link = create(:utm_link, seller:, utm_campaign: "summer-sale-1") old_permalink = utm_link.permalink visit utm_links_dashboard_path within(:table_row, { "Link" => utm_link.title }) do select_disclosure "Open action menu" do click_on "Edit" end end expect(page).to have_current_path("#{utm_links_dashboard_path}/#{utm_link.external_id}/edit") expect(page).to have_text("Edit link") expect(page).to have_link("Cancel", href: utm_links_dashboard_path) expect(find("a", text: "Learn more")["href"]).to include("/help/") # Check that the form is pre-filled with the existing UTM link's values expect(page).to have_input_labelled("Title", with: utm_link.title) within :fieldset, "Destination" do expect(page).to have_text("Profile page") end expect(page).to have_field("Link", with: old_permalink, disabled: true) within :fieldset, "Link" do expect(page).to have_text(%Q(#{UrlService.short_domain_with_protocol.sub("#{PROTOCOL}://", '')}/u/)) button = find_button("Copy short link") button.hover
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
true
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/analytics/sales_spec.rb
spec/requests/analytics/sales_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/authorize_called" require "shared_examples/creator_dashboard_page" describe "Sales analytics", :js, :sidekiq_inline, :elasticsearch_wait_for_refresh, type: :system do let(:seller) { create(:user, created_at: Date.new(2023, 1, 1)) } include_context "with switching account to user as admin for seller" it_behaves_like "creator dashboard page", "Analytics" do let(:path) { sales_dashboard_path } end it "shows the empty state" do visit sales_dashboard_path expect(page).to have_text("You don't have any sales yet.") end context "with views and sales" do let(:product1) { create(:product, user: seller, name: "Product 1") } let(:product2) { create(:product, user: seller, name: "Product 2") } before do create(:purchase, link: product1, price_cents: 100, created_at: "2023-12-14 12:00:00", ip_country: "Italy") create(:purchase, link: product1, price_cents: 100, created_at: "2023-12-14 12:00:00", ip_country: "Italy", referrer: "https://google.com") create(:purchase, link: product2, price_cents: 500, created_at: "2023-12-16 12:00:00", ip_country: "United States", ip_state: "NY") create(:purchase, link: product2, price_cents: 500, created_at: "2023-12-20 12:00:00", ip_country: "Japan") recreate_model_index(ProductPageView) 3.times { add_page_view(product1, Time.zone.parse("2023-12-13 12:00:00").iso8601, country: "Italy", referrer_domain: "google.com") } 3.times { add_page_view(product2, Time.zone.parse("2023-12-16 12:00:00").iso8601, country: "United States", state: "CA") } end it "calculates total stats" do visit sales_dashboard_path(from: "2023-12-01", to: "2023-12-31") within_section("Sales") { expect(page).to have_text("4") } within_section("Views") { expect(page).to have_text("6") } within_section("Total") { expect(page).to have_text("$12") } select_disclosure "Select products..." do uncheck "Product 1" end within_section("Sales") { expect(page).to have_text("2") } within_section("Views") { expect(page).to have_text("3") } within_section("Total") { expect(page).to have_text("$10") } select_disclosure "12/1/2023 – 12/31/2023" do click_on "Custom range..." fill_in "From (including)", with: "12/16/2023" fill_in "To (including)", with: "12/17/2023" end find("body").click # Blur the date field to trigger the update expect(page).to have_current_path(sales_dashboard_path(from: "2023-12-16", to: "2023-12-17")) within_section("Sales") { expect(page).to have_text("1") } within_section("Views") { expect(page).to have_text("3") } within_section("Total") { expect(page).to have_text("$5") } end it "shows the sales chart" do visit sales_dashboard_path(from: "2023-12-01", to: "2023-12-31") expect(page).to have_css('[data-testid="chart-dot"]', count: 31) expect(page).to have_css('[data-testid="chart-bar"]', count: 5) chart = find('[data-testid="chart"]') chart.hover expect(chart).to have_tooltip(text: "3 views\n1 sale\n(33.3% conversion)\n$5\nSaturday, December 16") select "Monthly", from: "Aggregate by" expect(page).to have_css('[data-testid="chart-dot"]', count: 1) expect(page).to have_css('[data-testid="chart-bar"]', count: 2) chart.hover expect(chart).to have_tooltip(text: "6 views\n4 sales\n(66.7% conversion)\n$12\nDecember 2023") select "Daily", from: "Aggregate by" select_disclosure "Select products..." do uncheck "Product 1" end expect(page).to have_css('[data-testid="chart-bar"]', count: 3) select_disclosure "12/1/2023 – 12/31/2023" do click_on "Custom range..." fill_in "From (including)", with: "12/16/2023" fill_in "To (including)", with: "12/17/2023" end find("body").click # Blur the date field to trigger the update expect(page).to have_css('[data-testid="chart-dot"]', count: 2) expect(page).to have_css('[data-testid="chart-bar"]', count: 2) end it "shows the referrers table" do visit sales_dashboard_path(from: "2023-12-01", to: "2023-12-31") within_table("Referrer") do expect(page).to have_table_row({ "Source" => "Direct, email, IM", "Views" => "3", "Sales" => "3", "Conversion" => "100%", "Total" => "$11" }) expect(page).to have_table_row({ "Source" => "Google", "Views" => "3", "Sales" => "1", "Conversion" => "33.3%", "Total" => "$1" }) end select_disclosure "Select products..." do uncheck "Product 1" end within_table("Referrer") do expect(page).not_to have_table_row({ "Source" => "Google" }) expect(page).to have_table_row({ "Source" => "Direct, email, IM", "Views" => "3", "Sales" => "2", "Conversion" => "66.7%", "Total" => "$10" }) end select_disclosure "12/1/2023 – 12/31/2023" do click_on "Custom range..." fill_in "From (including)", with: "12/16/2023" fill_in "To (including)", with: "12/17/2023" end find("body").click # Blur the date field to trigger the update within_table("Referrer") do expect(page).not_to have_table_row({ "Source" => "Google" }) expect(page).to have_table_row({ "Source" => "Direct, email, IM", "Views" => "3", "Sales" => "1", "Conversion" => "33.3%", "Total" => "$5" }) end end it "shows the locations table" do visit sales_dashboard_path(from: "2023-12-01", to: "2023-12-31") within_table("Locations") do expect(page).to have_table_rows_in_order( [ { "Country" => "🇺🇸 United States", "Views" => "3", "Sales" => "1", "Total" => "$5" }, { "Country" => "🇯🇵 Japan", "Views" => "0", "Sales" => "1", "Total" => "$5" }, { "Country" => "🇮🇹 Italy", "Views" => "3", "Sales" => "2", "Total" => "$2" }, ] ) end select_disclosure "Select products..." do uncheck "Product 1" end within_table("Locations") do expect(page).not_to have_table_row({ "Country" => "🇮🇹 Italy" }) expect(page).to have_table_row({ "Country" => "🇺🇸 United States", "Views" => "3", "Sales" => "1", "Total" => "$5" }) expect(page).to have_table_row({ "Country" => "🇯🇵 Japan", "Views" => "0", "Sales" => "1", "Total" => "$5" }) end select_disclosure "12/1/2023 – 12/31/2023" do click_on "Custom range..." fill_in "From (including)", with: "12/16/2023" fill_in "To (including)", with: "12/17/2023" end find("body").click # Blur the date field to trigger the update within_table("Locations") do expect(page).not_to have_table_row({ "Country" => "🇮🇹 Italy" }) expect(page).not_to have_table_row({ "Country" => "🇯🇵 Japan" }) expect(page).to have_table_row({ "Country" => "🇺🇸 United States", "Views" => "3", "Sales" => "1", "Total" => "$5" }) end select "United States", from: "Locations" within_table("Locations") do expect(page).to have_table_rows_in_order( [ { "State" => "New York", "Views" => "0", "Sales" => "1", "Total" => "$5" }, { "State" => "California", "Views" => "3", "Sales" => "0", "Total" => "$0" }, ] ) end end it "fixes the date range when from is after to" do visit sales_dashboard_path(from: "2023-12-14", to: "2023-01-01") expect(page).to have_disclosure("12/14/2023") expect(page).to have_current_path(sales_dashboard_path(from: "2023-12-14", to: "2023-12-14")) end it "supports quarterly date range selection" do visit sales_dashboard_path # Get the initial date picker text initial_date_picker_text = find('[aria-label="Date range selector"]').text # Test "This quarter" option - verify it's available and clickable select_disclosure initial_date_picker_text do expect(page).to have_content("This quarter") click_on "This quarter" end # Verify the URL parameters changed to quarter dates expect(page.current_url).to include("from=") expect(page.current_url).to include("to=") # Get the new date picker text after selecting "This quarter" quarter_date_picker_text = find('[aria-label="Date range selector"]').text # Test "Last quarter" option - verify it's available and clickable select_disclosure quarter_date_picker_text do expect(page).to have_content("Last quarter") click_on "Last quarter" end # Verify the URL parameters changed again for last quarter expect(page.current_url).to include("from=") expect(page.current_url).to include("to=") # Verify the date picker text changed to show the last quarter range last_quarter_date_picker_text = find('[aria-label="Date range selector"]').text expect(last_quarter_date_picker_text).not_to eq(initial_date_picker_text) expect(last_quarter_date_picker_text).not_to eq(quarter_date_picker_text) end it "handles quarterly date ranges and verifies quarterly options are present" do visit sales_dashboard_path # Get the initial date picker text initial_date_picker_text = find('[aria-label="Date range selector"]').text # Verify both quarterly options are available in the dropdown select_disclosure initial_date_picker_text do expect(page).to have_content("This quarter") expect(page).to have_content("Last quarter") # Verify other expected options are also present expect(page).to have_content("This month") expect(page).to have_content("Last month") expect(page).to have_content("This year") expect(page).to have_content("Last year") # Test both quarterly options work click_on "This quarter" end # Verify "This quarter" produces valid URL parameters expect(page.current_url).to match(/from=\d{4}-\d{2}-\d{2}/) expect(page.current_url).to match(/to=\d{4}-\d{2}-\d{2}/) # Get the new date range after "This quarter" this_quarter_text = find('[aria-label="Date range selector"]').text # Test "Last quarter" option select_disclosure this_quarter_text do click_on "Last quarter" end # Verify "Last quarter" produces different valid URL parameters expect(page.current_url).to match(/from=\d{4}-\d{2}-\d{2}/) expect(page.current_url).to match(/to=\d{4}-\d{2}-\d{2}/) # Verify the date range changed last_quarter_text = find('[aria-label="Date range selector"]').text expect(last_quarter_text).not_to eq(initial_date_picker_text) expect(last_quarter_text).not_to eq(this_quarter_text) end end context "with many differrent referrers" do let(:product1) { create(:product, user: seller, name: "Product 1") } let(:product2) { create(:product, user: seller, name: "Product 2") } before do recreate_model_index(ProductPageView) %w[one two three four five six seven eight nine ten eleven twelve].each do |referrer| add_page_view(product1, Time.zone.parse("2023-12-13 12:00:00").iso8601, referrer_domain: referrer) end 3.times { add_page_view(product2, Time.zone.parse("2023-12-16 12:00:00").iso8601, referrer_domain: "one") } end it "paginates the table" do visit sales_dashboard_path(from: "2023-12-01", to: "2023-12-31") within_table("Referrer") do within(find("tbody")) { expect(page).to have_selector(:table_row, count: 10) } end click_on "Show more" within_table("Referrer") do expect(page).to have_table_row({ "Source" => "one", "Views" => "4" }) %w[two three four five six seven eight nine ten eleven twelve].each do |referrer| expect(page).to have_table_row({ "Source" => referrer, "Views" => "1" }) end end select_disclosure "Select products..." do uncheck "Product 2" end within_table("Referrer") do within(find("tbody")) { expect(page).to have_selector(:table_row, count: 10) } end click_on "Show more" within_table("Referrer") do %w[one two three four five six seven eight nine ten].each do |referrer| expect(page).to have_table_row({ "Source" => referrer, "Views" => "1" }) end end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/discover/blackfriday_spec.rb
spec/requests/discover/blackfriday_spec.rb
# frozen_string_literal: true require "spec_helper" describe("Black Friday 2025", js: true, type: :system) do let(:discover_host) { UrlService.discover_domain_with_protocol } before do allow_any_instance_of(Link).to receive(:update_asset_preview) @creator = create(:compliant_user, name: "Black Friday Seller") @buyer = create(:user) end describe "Black Friday hero section" do before do Feature.activate(:offer_codes_search) product = create(:product, :recommendable, user: @creator, price_cents: 1000) offer_code = create(:offer_code, user: @creator, code: "BLACKFRIDAY2025", amount_percentage: 25, products: [product]) create_list(:purchase, 5, link: product, offer_code:, price_cents: 750) # Stub the stats service to return the expected values allow(BlackFridayStatsService).to receive(:fetch_stats).and_return({ active_deals_count: 1, revenue_cents: 3750, # 5 purchases * 750 cents = $37.50 average_discount_percentage: 25 }) end after do Feature.deactivate(:offer_codes_search) Rails.cache.delete("black_friday_stats") end it "shows hero on discover page with CTA when feature is enabled", :sidekiq_inline do index_model_records(Link) visit discover_url(host: discover_host) wait_for_ajax expect(page).to have_selector("header img[alt='Black Friday']") expect(page).to have_text("Snag creator-made deals") expect(page).to have_link("Get Black Friday deals", href: discover_path(offer_code: SearchProducts::BLACK_FRIDAY_CODE)) expect(page).to have_text("BLACK FRIDAY IS LIVE") expect(page).to have_text("1") expect(page).to have_text("ACTIVE DEALS") expect(page).to have_text("$37.50") expect(page).to have_text("IN SALES SO FAR") expect(page).to have_text("25%") expect(page).to have_text("AVERAGE DISCOUNT") # When visiting a taxonomy page, the CTA should be the taxonomy page with the offer code click_on("Films") expect(page).to have_link("Get Black Friday deals", href: discover_taxonomy_path(taxonomy: "films", offer_code: SearchProducts::BLACK_FRIDAY_CODE)) end it "shows hero on blackfriday page without CTA when feature is enabled", :sidekiq_inline do index_model_records(Link) visit blackfriday_url(host: discover_host) wait_for_ajax expect(page).to have_selector("header img[alt='Black Friday']") expect(page).to have_text("Snag creator-made deals") expect(page).not_to have_link("Get Black Friday deals") expect(page).to have_text("BLACK FRIDAY IS LIVE") expect(page).to have_text("1") expect(page).to have_text("ACTIVE DEALS") end it "hides hero when feature is disabled", :sidekiq_inline do Feature.deactivate(:offer_codes_search) create(:product, :recommendable, user: @creator) index_model_records(Link) visit discover_url(host: discover_host) wait_for_ajax expect(page).not_to have_selector("header img[alt='Black Friday']") expect(page).not_to have_text("Snag creator-made deals") expect(page).not_to have_text("BLACK FRIDAY IS LIVE") end end describe "BLACKFRIDAY2025 offer code filtering" do before do Feature.activate(:offer_codes_search) end after do Feature.deactivate(:offer_codes_search) end it "filters products by BLACKFRIDAY2025 offer code, hides featured products, and includes offer code in product links", :sidekiq_inline, :elasticsearch_wait_for_refresh do product = create(:product, :recommendable, user: @creator, name: "Black Friday Special Product", price_cents: 5000) index_model_records(Link) # Visit the blackfriday URL before offer code association visit blackfriday_url(host: discover_host) wait_for_ajax expect(page).not_to have_product_card(text: "Black Friday Special Product") # Create the BLACKFRIDAY2025 offer code and associate it with the product blackfriday_offer_code = create(:offer_code, user: @creator, code: "BLACKFRIDAY2025", amount_percentage: 25) product.offer_codes << blackfriday_offer_code # Create some purchases to make the product potentially appear as featured create_list(:purchase, 5, link: product) # Re-index the product and purchases to include the offer code in search product.enqueue_index_update_for(["offer_codes"]) index_model_records(Link) index_model_records(Purchase) visit blackfriday_url(host: discover_host) wait_for_ajax expect(page).not_to have_section("Featured products") expect(page).to have_product_card(text: "Black Friday Special Product") find_product_card(product).click expect(page).to have_current_path(/.*\?.*code=BLACKFRIDAY2025/) expect(page).to have_text("$1 off will be applied at checkout (Code BLACKFRIDAY2025)") end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/discover/search_spec.rb
spec/requests/discover/search_spec.rb
# frozen_string_literal: true require "spec_helper" describe("Discover - Search scenarios", js: true, type: :system) do let(:discover_host) { UrlService.discover_domain_with_protocol } before do allow_any_instance_of(Link).to receive(:update_asset_preview) @buyer = create(:user) @png = Rack::Test::UploadedFile.new(Rails.root.join("spec", "support", "fixtures", "kFDzu.png"), "image/png") end def seed_products travel_to(5.seconds.ago) do @product = create(:product, :recommendable, :with_design_taxonomy, user: create(:compliant_user, name: "Gumstein"), name: "product 0") end travel_to(4.seconds.ago) do @similar_product_1 = create(:product, :recommendable, :with_design_taxonomy, user: create(:compliant_user, name: "Gumstein II"), name: "product 1", preview: @png, price_cents: 200) end travel_to(3.seconds.ago) do @similar_product_2 = create(:product, :recommendable, :with_design_taxonomy, user: create(:compliant_user, name: "Gumstein III"), name: "product 2", preview: @png, price_cents: 300) end travel_to(2.seconds.ago) do @similar_product_3 = create(:product, :recommendable, :with_design_taxonomy, user: create(:compliant_user, name: "Gumstein IV"), name: "product 3", preview: @png, price_cents: 400) end travel_to(1.second.ago) do @similar_product_4 = create(:product, :recommendable, :with_design_taxonomy, user: create(:compliant_user, name: "Gumstein V"), name: "product 4", preview: @png, price_cents: 500) end 2.times do |i| create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @product), rating: 5) create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @similar_product_1), rating: 4) end 3.times do |i| create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @product), rating: 5) create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @similar_product_2), rating: 3) end 4.times do |i| create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @product), rating: 5) create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @similar_product_3), rating: 2) end create(:purchase, email: "gumroaduser1@gmail.com", link: @product) create(:purchase, email: "gumroaduser1@gmail.com", link: @similar_product_4) create(:purchase, email: @buyer.email, purchaser: @buyer, link: @product) @films = Taxonomy.find_or_create_by(slug: "films") end def within_search_autocomplete(&block) within find(:combo_box_list_box, find(:combo_box, "Search products")), &block end describe "search" do it "shows search results for recommendable products" do recommendable_product_title = "Nothing but pine martens" recommendable_product = create(:product, :recommendable, name: recommendable_product_title, price_cents: 3378) create(:product, name: "These pine martens we don't like so much") index_model_records(Link) visit discover_url(host: discover_host) fill_in "Search products", with: "pine martens\n" expect_product_cards_in_order([recommendable_product]) click_on recommendable_product_title wait_for_ajax expect do add_to_cart(recommendable_product, cart: true) check_out(recommendable_product) end.to change { recommendable_product.sales.successful.select(&:was_discover_fee_charged?).count }.by(1) .and change { RecommendedPurchaseInfo.where(recommendation_type: "search").count }.by(1) end it "does not show product in search results if seller is not compliant" do recommendable_product_title = "Nothing but pine martens" recommendable_product = create(:product, :recommendable, name: recommendable_product_title, price_cents: 3378) index_model_records(Link) expect(recommendable_product.recommendable?).to be(true) visit discover_url(host: discover_host) fill_in "Search products", with: "pine martens\n" expect_product_cards_in_order([recommendable_product]) recommendable_product.user.update!(user_risk_state: "not_reviewed") recommendable_product.enqueue_index_update_for(%w[is_recommendable]) expect(recommendable_product.recommendable?).to be(false) visit discover_url(host: discover_host) fill_in "Search products", with: "pine martens\n" expect(page).not_to have_selector("article") end it "shows autocomplete results for products" do create(:product, :recommendable, name: "Rust: The best parts") create(:product, :recommendable, name: "Why Rust is better than C++", price_cents: 3378) create(:product, name: "We don't recommend C++") index_model_records(Link) visit discover_url(host: discover_host) find_field("Search products").click within_search_autocomplete do expect(page).to have_text("Trending") expect(page).to have_selector("[role=option]", text: "Why Rust is better than C++") expect(page).to have_selector("[role=option]", text: "Rust: The best parts") end # ensure autocomplete requests don't show up if we do a full search fill_in "Search products", with: "c++\n" sleep 1 wait_for_ajax expect(page).to_not have_selector("[role=option]", text: "Why Rust is better than C++") fill_in "Search products", with: "c++" within_search_autocomplete do expect(page).to have_text("Products") expect(page).to have_selector("[role=option]", text: "Why Rust is better than C++") expect(page).to_not have_selector("[role=option]", text: "We don't recommend C++") end end it "shows autocomplete results for recently viewed products" do login_as @buyer create(:product, :recommendable, name: "Rust: The best parts") visited1 = create(:product, :recommendable, name: "Why Rust is better than C++") visited2 = create(:product, :recommendable, name: "The ideal systems programming language") non_recommendable = create(:product, name: "We don't recommend C++") index_model_records(Link) add_page_view(visited1, Time.current, user_id: @buyer.id) add_page_view(visited2, Time.current, user_id: @buyer.id) add_page_view(non_recommendable, Time.current, user_id: @buyer.id) ProductPageView.__elasticsearch__.refresh_index! visit discover_url(host: discover_host) find_field("Search products").click within_search_autocomplete do expect(page).to have_text("Keep shopping for") expect(page).to have_selector("[role=option]", text: "The ideal systems programming language") expect(page).to have_selector("[role=option]", text: "Why Rust is better than C++") expect(page).to_not have_selector("[role=option]", text: "Rust: The best parts") expect(page).to_not have_selector("[role=option]", text: "We don't recommend C++") end end it "shows autocomplete results for recent searches" do login_as @buyer create(:discover_search_suggestion, discover_search: create(:discover_search, user: @buyer, query: "c++")) create(:discover_search_suggestion, discover_search: create(:discover_search, user: @buyer, query: "rust")) visit discover_url(host: discover_host) find_field("Search products").click expect(page).to have_selector("[role=option]", text: "rust") expect(page).to have_selector("[role=option]", text: "c++") fill_in "Search products", with: "c++" expect(page).not_to have_selector("[role=option]", text: "rust") expect(page).to have_selector("[role=option]", text: "c++") end it "supports deleting recent searches" do login_as @buyer create(:discover_search_suggestion, discover_search: create(:discover_search, user: @buyer, query: "c++")) rust_search = create(:discover_search_suggestion, discover_search: create(:discover_search, user: @buyer, query: "rust")) visit discover_url(host: discover_host) find_field("Search products").click within("[role=option]", text: "rust") do click_on "Remove" end expect(page).not_to have_selector("[role=option]", text: "rust") wait_for_ajax expect(rust_search.reload).to be_deleted end context "when searching with an empty query", :sidekiq_inline, :elasticsearch_wait_for_refresh do before do seed_products @similar_product_1.update(taxonomy: @films) @similar_product_3.update(taxonomy: @films) Link.import(refresh: true, force: true) end it "shows relevant products when no category is selected" do visit discover_url(host: discover_host) wait_for_ajax fill_in "Search products", with: "product" find_field("Search products").native.send_keys(:return) expect(page).to have_current_path("/discover?query=product") expect(page).to have_product_card(count: 5) end it "loads category page when category card is clicked" do visit discover_url(host: discover_host) wait_for_ajax within "[role=menubar]" do click_on "Design" end within("header [role=navigation]") do expect(page).to have_text("Design") end within_section("Featured products", section_element: :section) do expect(page).to have_product_card(count: 3) end end it "shows the top products for a non-empty category" do visit discover_url(host: discover_host) wait_for_ajax within "[role=menubar]" do click_on "Design" end wait_for_ajax fill_in "Search products", with: "product" find_field("Search products").native.send_keys(:return) expect(page).to have_current_path("/design?query=product") within("[role=navigation][aria-label='Breadcrumbs']") do expect(page).to have_text("Design") end expect(page).to have_product_card(count: 3) end context "when searching with tags param", :sidekiq_inline, :elasticsearch_wait_for_refresh do before do @similar_product_1.tag!("test") end it "shows top products for selected tag" do visit discover_url(host: discover_host, tags: "test") wait_for_ajax within_section("On the market", section_element: :section) do expect(page).to have_product_card(count: 1) within(find_product_card(@similar_product_1)) do expect(page).to have_content(@similar_product_1.name) expect(page).to have_link(@similar_product_1.user.name) end end end it "allows search results filtering" do @similar_product_2.tag!("test") @similar_product_3.tag!("test") visit discover_url(host: discover_host, tags: "test") wait_for_ajax within_section("On the market", section_element: :section) do expect(page).to have_product_card(count: 3) select_disclosure "Price" do fill_in("Maximum price", with: 2) end wait_for_ajax expect(page).to have_product_card(count: 1) end end end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/discover/discover_spec.rb
spec/requests/discover/discover_spec.rb
# frozen_string_literal: true require "spec_helper" describe("Discover", js: true, type: :system) do include StripeMerchantAccountHelper let(:discover_host) { UrlService.discover_domain_with_protocol } before do allow_any_instance_of(Link).to receive(:update_asset_preview) @buyer = create(:user) @png = Rack::Test::UploadedFile.new(Rails.root.join("spec", "support", "fixtures", "kFDzu.png"), "image/png") end let!(:three_d_taxonomy) { Taxonomy.find_by(slug: "3d") } let!(:design_taxonomy) { Taxonomy.find_by(slug: "design") } let!(:films_taxonomy) { Taxonomy.find_by(slug: "films") } let!(:software_development_taxonomy) { Taxonomy.find_by(slug: "software-development") } let!(:programming_taxonomy) { Taxonomy.find_by(slug: "programming", parent: software_development_taxonomy) } let!(:csharp_taxonomy) { Taxonomy.find_by(slug: "c-sharp", parent: programming_taxonomy) } def seed_products travel_to(5.seconds.ago) do @product = create(:product, user: create(:compliant_user, name: "Gumstein"), name: "product 0", taxonomy: three_d_taxonomy) end travel_to(4.seconds.ago) do @similar_product_1 = create(:product, user: create(:compliant_user, name: "Gumstein II"), name: "product 1", preview: @png, price_cents: 200, taxonomy: three_d_taxonomy) end travel_to(3.seconds.ago) do @similar_product_2 = create(:product, user: create(:compliant_user, name: "Gumstein III"), name: "product 2", preview: @png, price_cents: 300, taxonomy: three_d_taxonomy, created_at: 100.days.ago) end travel_to(2.seconds.ago) do @similar_product_3 = create(:product, user: create(:compliant_user, name: "Gumstein IV"), name: "product 3", preview: @png, price_cents: 400, taxonomy: three_d_taxonomy) end travel_to(1.second.ago) do @similar_product_4 = create(:product, user: create(:compliant_user, name: "Gumstein V"), name: "product 4", preview: @png, price_cents: 500, taxonomy: three_d_taxonomy) end 2.times do |i| create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @product), rating: 5) create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @similar_product_1), rating: 4) end 3.times do |i| create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @product), rating: 5) create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @similar_product_2), rating: 3) end 4.times do |i| create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @product), rating: 5) create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @similar_product_3), rating: 2) end create(:purchase, email: "gumroaduser1@gmail.com", link: @product) create(:purchase, email: "gumroaduser1@gmail.com", link: @similar_product_4) create(:purchase, email: @buyer.email, purchaser: @buyer, link: @product) index_model_records(Purchase) index_model_records(Link) end describe "recommended products" do before do seed_products end context "when products are re-categorised" do before do @similar_product_1.update_attribute(:taxonomy, films_taxonomy) Link.import(refresh: true, force: true) visit "#{discover_host}/#{films_taxonomy.slug}" end it "shows category and top products in the new taxonomy based section" do within_section "Featured products", section_element: :section do expect(page).to have_product_card(count: 1) find_product_card(@similar_product_1).click end wait_for_ajax expect(page).to have_current_path(/^\/l\/#{@similar_product_1.unique_permalink}\?layout=discover&recommended_by=discover/) end end describe "category and tags" do before do @similar_product_1.update_attribute(:taxonomy, design_taxonomy) @similar_product_3.update_attribute(:taxonomy, design_taxonomy) @similar_product_1.tag!("action") @similar_product_2.tag!("action") login_as @buyer Link.import(refresh: true, force: true) end it "filters products when category and tag is changed" do @similar_product_3.tag!("multi word tag") @similar_product_3.tag!("another tag") Link.import(refresh: true, force: true) visit discover_url(host: discover_host, query: "gumstein") wait_for_ajax expect(page).to have_product_card(count: 5) toggle_disclosure "Tags" # test single word term check "action (2)" wait_for_ajax expect(page).to have_product_card(count: 2) uncheck "action (2)" wait_for_ajax expect(page).to have_product_card(count: 5) within "[role=menubar]" do click_on "Design" end # test multi word term check "multi word tag (1)" wait_for_ajax within_section "On the market" do expect(page).to have_product_card(count: 1) end # test many multi word tags check "another tag (1)" wait_for_ajax within_section "On the market" do expect(page).to have_product_card(count: 1) end uncheck "another tag (1)" wait_for_ajax uncheck "multi word tag (1)" within_section "On the market" do expect(page).to have_product_card(count: 2) end check "action (1)" within_section "On the market" do expect(page).to have_product_card(count: 1) end end it "sets the tags, and search query based on the URL and displays the correct results" do Link.__elasticsearch__.create_index!(force: true) @product.tag!("action") @product.tag!("book") @product.update_attribute(:taxonomy, films_taxonomy) @product.__elasticsearch__.index_document Link.__elasticsearch__.refresh_index! visit discover_url(host: discover_host, tags: "action,book", query: @product.name.split(" ")[0]) wait_for_ajax expect(page).to have_product_card(count: 1) select_disclosure "Tags" do expect(page).to have_checked_field("book (1)") expect(page).to have_checked_field("action (1)") end end end end describe "recommended wishlists" do let!(:wishlists) { 4.times.map { |i| create(:wishlist, name: "My Wishlist #{i}", recent_follower_count: 10 - i) } } before do seed_products wishlists.each { create(:wishlist_product, wishlist: _1, product: @similar_product_1) } end it "displays top wishlists when there are no recommended products" do login_as @buyer visit discover_url(host: discover_host) wait_for_ajax expect(page).not_to have_text("Recommended") expect(page).to have_text("Featured products") within_section "Wishlists you might like", section_element: :section do expect_product_cards_in_order(wishlists) end click_on wishlists.first.name click_on @similar_product_1.name add_to_cart(@similar_product_1) check_out(@similar_product_1, email: @buyer.email, logged_in_user: @buyer) expect(Purchase.last).to have_attributes( recommended_by: RecommendationType::GUMROAD_DISCOVER_WISHLIST_RECOMMENDATION, affiliate: wishlists.first.user.global_affiliate, fee_cents: 60, affiliate_credit_cents: 14, ) end it "recommends wishlists when there are recommended products" do create(:purchase, purchaser: @buyer, link: @product) create(:sales_related_products_info, smaller_product: @product, larger_product: @similar_product_2, sales_count: 1) rebuild_srpis_cache related_wishlist = create(:wishlist, name: "Related Wishlist", recent_follower_count: 0) create(:wishlist_product, wishlist: related_wishlist, product: @similar_product_2) login_as @buyer visit discover_url(host: discover_host) wait_for_ajax within_section "Wishlists you might like", section_element: :section do expect_product_cards_in_order([related_wishlist, *wishlists.first(3)]) end end end describe "category pages" do before do seed_products index_model_records(Link) end it "displays the category page" do create(:wishlist_product, wishlist: create(:wishlist, name: "3D wishlist"), product: @similar_product_1) visit "#{discover_host}/3d" expect(page).to have_selector("[aria-label='Breadcrumbs']", text: "3D") within_section "Featured products", section_element: :section do expect(page).to have_product_card(count: 5) end within_section "Wishlists for 3D", section_element: :section do expect(page).to have_product_card(count: 1) end within_section "On the market" do expect(page).to have_tab_button("Trending", open: true) find(:tab_button, "Hot & New", open: false).click end expect(page).not_to have_section("On the market") within_section "Hot and new products" do expect(page).to have_tab_button("Trending", open: false) expect(page).to have_tab_button("Hot & New", open: true) find(:tab_button, "Best Sellers", open: false).click end expect(page).not_to have_section("Hot and new products") within_section "Best selling products" do expect(page).to have_tab_button("Trending", open: false) expect(page).to have_tab_button("Hot & New", open: false) expect(page).to have_tab_button("Best Sellers", open: true) end end it "applies search filters from the URL" do visit "#{discover_host}/3d?sort=hot_and_new&max_price=10" expect(page).to have_selector("[aria-label='Breadcrumbs']", text: "3D") within_section "Hot and new products" do expect(page).to have_tab_button("Trending", open: false) expect(page).to have_tab_button("Hot & New", open: true) select_disclosure "Price" do expect(page).to have_field("Maximum price", with: "10") end end end it "excludes recommended products from the main list" do create_list(:product, 5, :recommendable, user: create(:compliant_user), taxonomy: three_d_taxonomy) index_model_records(Link) visit "#{discover_host}/3d" within_section "Featured products", section_element: :section do expect(page).to have_product_card(count: 8) end within_section "On the market" do expect(page).to have_product_card(count: 2) end # Should also work with client-side navigation visit "#{discover_host}/discover" within "[role=menubar]" do find("[role=menuitem]", text: "3D").hover click_on "All 3D" end within_section "Featured products", section_element: :section do expect(page).to have_product_card(count: 8) end within_section "On the market" do expect(page).to have_product_card(count: 2) end end end describe "pagination" do before do creator = create(:recommendable_user) create_list(:product, 72, :recommendable, user: creator, taxonomy: films_taxonomy) do |product, index| product.name = "product #{index + 1}" product.price_cents = index * 100 product.save! end allow_any_instance_of(Link).to receive(:reviews_count).and_return(1) Link.import(refresh: true, force: true) end it "loads more results when clicking load more" do visit "#{discover_host}/films?sort=price_asc" within_section "On the market" do expect(page).to have_product_card(count: 36) expect(page).to_not have_product_card(text: "product 37") expect(page).to_not have_product_card(text: "product 38") expect(page).to_not have_product_card(text: "product 39") expect(page).to_not have_product_card(text: "product 40") click_button "Load more" wait_for_ajax expect(page).to have_product_card(count: 45) expect(page).to have_product_card(text: "product 37") expect(page).to have_product_card(text: "product 38") expect(page).to have_product_card(text: "product 44") expect(page).to have_product_card(text: "product 45") expect(page).to_not have_product_card(text: "product 46") click_button "Load more" wait_for_ajax expect(page).to have_product_card(count: 54) expect(page).to have_product_card(text: "product 46") expect(page).to have_product_card(text: "product 54") expect(page).to_not have_product_card(text: "product 55") end end it "offsets results to account for featured products" do visit "#{discover_host}/films" within_section "Featured products", section_element: :section do expect(page).to have_product_card(count: 8) end within_section "On the market" do expect(page).to have_product_card(count: 36) end end end it "displays a link to the user's profile page with the recommended_by query parameter" do Link.__elasticsearch__.create_index!(force: true) product = create(:product, name: "Nothing but pine martens", price_cents: 3378, user: create(:compliant_user, name: "Sam Smith", username: "sam")) allow(product).to receive(:recommendable?).and_return(true) allow(product).to receive(:reviews_count).and_return(1) product.__elasticsearch__.index_document Link.__elasticsearch__.refresh_index! visit discover_url(host: discover_host) fill_in "Search products", with: "pine martens\n" expect_product_cards_in_order([product]) expect(page).to have_link("Sam Smith", href: "http://sam.test.gumroad.com:31337?recommended_by=search") find_product_card(product).click expect(page).to have_current_path(/^\/l\/#{product.unique_permalink}\?layout=discover&recommended_by=search/) expect(page).to have_link("Sam Smith", href: "http://sam.test.gumroad.com:31337/?recommended_by=search") # Clicking the logo should take you back to Discover home page. click_on "Gumroad" expect(page).to have_text("On the market") end it "displays thumbnail in preview if available" do Link.__elasticsearch__.create_index!(force: true) product = create(:product, name: "Nothing but pine martens", price_cents: 3378, user: create(:compliant_user, name: "Sam Smith", username: "sam")) create(:thumbnail, product:) product.reload allow(product).to receive(:recommendable?).and_return(true) allow(product).to receive(:reviews_count).and_return(1) product.__elasticsearch__.index_document Link.__elasticsearch__.refresh_index! visit discover_url(host: discover_host) fill_in "Search products", with: "pine martens\n" expect_product_cards_in_order([product]) within find_product_card(product) do expect(find("figure")).to have_image(src: product.thumbnail.url) end end describe "affiliate cookies" do it "sets the global affiliate cookie if affiliate_id query param is present" do affiliate = create(:user).global_affiliate visit discover_url(host: discover_host, affiliate_id: affiliate.external_id_numeric) affiliate_cookie = Capybara.current_session.driver.browser.manage.all_cookies.find do |cookie| cookie[:name] == CGI.escape(affiliate.cookie_key) end expect(affiliate_cookie).to be_present end it "sets the direct affiliate cookie if affiliate_id query param is present" do affiliate = create(:direct_affiliate) visit discover_url(host: discover_host, affiliate_id: affiliate.external_id_numeric) affiliate_cookie = Capybara.current_session.driver.browser.manage.all_cookies.find do |cookie| cookie[:name] == CGI.escape(affiliate.cookie_key) end expect(affiliate_cookie).to be_present end end describe "taxonomy" do before do seed_products @similar_product_1.update_attribute(:taxonomy, software_development_taxonomy) @similar_product_2.update_attribute(:taxonomy, programming_taxonomy) @similar_product_2.tag!("my-tag") @similar_product_3.update_attribute(:taxonomy, csharp_taxonomy) @product.update_attribute(:taxonomy, csharp_taxonomy) @product.tag!("my-tag") @product.tag!("othertag") index_model_records(Link) end it "shows tags and taxonomy in title, plus breadcrumbs in page" do visit "#{discover_host}/software-development/programming/c-sharp?tags=some-tag" expect(page).to have_title("some tag | Software Development » Programming » C# | Gumroad") expect(page).to have_selector("[aria-label='Breadcrumbs']", text: "Software Development\n/Programming\n/C#") end it "shows breadcrumbs with taxonomy links and handles back and forward buttons" do visit "#{discover_host}/software-development/programming/c-sharp?sort=featured" expect(page).to have_title("Software Development » Programming » C# | Gumroad") within_section "Featured products", section_element: :section do expect_product_cards_with_names("product 0", "product 3") end select_disclosure "Tags" do check "my-tag (1)" end wait_for_ajax expect(page).to have_current_path("/software-development/programming/c-sharp?sort=featured&tags=my-tag") expect(page).to have_title("my tag | Software Development » Programming » C# | Gumroad") within_section "On the market" do expect_product_cards_with_names("product 0") end within("[role=navigation][aria-label='Breadcrumbs']") do click_on "Programming" end expect(page).to have_current_path("/software-development/programming") expect(page).to have_title("Software Development » Programming | Gumroad") expect(page).to have_selector("[aria-label='Breadcrumbs']", text: "Software Development\n/Programming") within_section "Featured products", section_element: :section do expect_product_cards_with_names("product 0", "product 2", "product 3") end wait_for_ajax page.go_back wait_for_ajax expect(page).to have_current_path("/software-development/programming/c-sharp?sort=featured&tags=my-tag") page.go_back wait_for_ajax expect(page).to have_current_path("/software-development/programming/c-sharp?sort=featured") expect(page).to have_title("Software Development » Programming » C# | Gumroad") expect(page).to have_selector("[aria-label='Breadcrumbs']", text: "Software Development\n/Programming\n/C#") within_section "Featured products", section_element: :section do expect_product_cards_with_names("product 0", "product 3") end page.go_forward wait_for_ajax page.go_forward wait_for_ajax expect(page).to have_current_path("/software-development/programming") expect(page).to have_title("Software Development » Programming | Gumroad") expect(page).to have_selector("[aria-label='Breadcrumbs']", text: "Software Development\n/Programming") within_section "Featured products", section_element: :section do expect_product_cards_with_names("product 0", "product 2", "product 3") end end it "sets the affiliate cookie" do affiliate = create(:direct_affiliate) visit "#{discover_host}/software-development/programming/c-sharp?tags=some-tag&#{Affiliate::SHORT_QUERY_PARAM}=#{affiliate.external_id_numeric}" affiliate_cookie = Capybara.current_session.driver.browser.manage.all_cookies.find do |cookie| cookie[:name] == CGI.escape(affiliate.cookie_key) end expect(affiliate_cookie).to be_present end describe "discover nav" do it "sets aria-current on the active category" do visit "#{discover_host}/business-and-money" within "[role=menubar]" do find("[role=menuitem]", text: "Business & Money").hover click_on "All Business & Money" expect(page).to have_selector("[aria-current=true]", text: "Business & Money") end end it "changes current selected taxonomy category via nav, and sets aria-current is its top-level category" do visit discover_url(host: discover_host) find("[role=menuitem]", text: "Business & Money").hover click_on "Entrepreneurship" click_on "Courses" expect(page).to have_selector("[aria-label='Breadcrumbs']", text: "Business & Money\n/Entrepreneurship\n/Courses") within "[role=menubar]" do expect(page).to have_selector("[aria-current=true]", text: "Business & Money") end end it "allows returning to previous menu using 'Back' and selection of non-leaf categories via 'All'" do visit discover_url(host: discover_host) find("[role=menuitem]", text: "Business & Money").hover click_on "Entrepreneurship" click_on "Back" click_on "All Business & Money" expect(page).to have_selector("[aria-label='Breadcrumbs']", text: "Business & Money") end it "places categories that didn't fit the screen under 'More', which becomes aria-current if one of those categories is selected" do visit discover_url(host: discover_host) find("[role=menuitem]", text: "More").hover click_on "Writing & Publishing" within "[role=menubar]" do expect(page).to have_selector("[aria-current=true]", text: "More") end end it "resets filters when category is changed via nav" do visit "#{discover_host}/software-development?tags=othertag" within_section "On the market" do expect_product_cards_with_names("product 0") end within "[role=menubar]" do find("[role=menuitem]", text: "More").hover click_on "Software Development" click_on "All Software Development" end within_section "Featured products", section_element: :section do expect_product_cards_with_names("product 0", "product 3", "product 2", "product 1") end within_section "On the market" do expect(page).not_to have_product_card end end it "opens discover home when clicking the 'All' menubar item " do visit "#{discover_host}/business-and-money/entrepreneurship" click_on "All" within "[role=menubar]" do expect(page).to have_selector("[aria-current=true]", text: "All") end end it "changes category when clicking menubar item" do visit "#{discover_host}/software-development" within "[role=menubar]" do click_on "Business & Money" end expect(page).to have_selector("[aria-label='Breadcrumbs']", text: "Business & Money") end it "sorts categories based on sales recommendations, falling back to 30-day sales count" do seed_products UpdateTaxonomyStatsJob.new.perform visit discover_url(host: discover_host) expect(find("[role=menubar]")).to have_text("All 3D Software Development Audio Business & Money Comics & Graphic Novels Design Drawing & Painting Education Fiction Books More", normalize_ws: true) login_as @buyer visit discover_url(host: discover_host) # limit to 5 categories for logged in users expect(find("[role=menubar]")).to have_text("All 3D Software Development Audio Business & Money Comics & Graphic Novels More", normalize_ws: true) SalesRelatedProductsInfo.find_or_create_info(@product.id, create(:product, taxonomy: films_taxonomy).id).update!(sales_count: 20) rebuild_srpis_cache visit discover_url(host: discover_host) expect(find("[role=menubar]")).to have_text("All Films 3D Software Development Audio Business & Money More", normalize_ws: true) SalesRelatedProductsInfo.find_or_create_info(@product.id, create(:product, taxonomy: three_d_taxonomy).id).update!(sales_count: 21) rebuild_srpis_cache visit discover_url(host: discover_host) expect(find("[role=menubar]")).to have_text("All 3D Films Software Development Audio Business & Money More", normalize_ws: true) end end end it "shows the correct header CTAs when the user is logged out vs in" do visit discover_url(host: discover_host) header = first("header") expect(header).to_not have_link "Dashboard" expect(header).to have_link "Start selling", href: signup_url(host: UrlService.domain_with_protocol) expect(header).to have_link "Log in", href: login_url(host: UrlService.domain_with_protocol) expect(header).to_not have_link "Library" login_as create(:buyer_user) visit discover_url(host: discover_host) header = first("header") expect(header).to have_link "Dashboard", href: dashboard_url(host: UrlService.domain_with_protocol) expect(header).to_not have_link "Log in" expect(header).to have_link "Start selling", href: products_url(host: UrlService.domain_with_protocol) expect(header).to have_link "Library", href: library_url(host: UrlService.domain_with_protocol) login_as create(:compliant_user) visit discover_url(host: discover_host) header = first("header") expect(header).to_not have_link "Log in" expect(header).to have_link "Start selling", href: products_url(host: UrlService.domain_with_protocol) expect(header).to have_link "Library", href: library_url(host: UrlService.domain_with_protocol) end it "shows the footer" do visit "#{discover_host}/#{films_taxonomy.slug}" expect(page).to have_text("Subscribe to get tips and tactics to grow the way you want.") end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/discover/recommendations_spec.rb
spec/requests/discover/recommendations_spec.rb
# frozen_string_literal: true require "spec_helper" describe("Discover recommendations", js: true, type: :system) do let(:host) { UrlService.discover_domain_with_protocol } let(:user) { create(:buyer_user) } let(:product) { create(:product) } let(:products) do create_list(:product, 5) do |product, i| product.name = "Product #{i}" product.save! end end it "recommends products and correctly tracks the recommender model name" do allow(RecommendedProductsService).to receive(:fetch).and_return(Link.where(id: products.map(&:id))) visit discover_url(host:) within_section "Recommended", section_element: :section do click_on products.first.name end recommender_model_name = CGI.parse(page.current_url)["recommender_model_name"].first add_to_cart(products.first, cart: true) check_out(products.first) purchase = Purchase.last expect(purchase.recommended_purchase_info.recommendation_type).to eq(RecommendationType::GUMROAD_PRODUCTS_FOR_YOU_RECOMMENDATION) expect(purchase.recommender_model_name).to eq(recommender_model_name) end it "shows top products when there are no personalized recommendations" do searchable_product = create(:product, :recommendable, name: "searchable product") index_model_records(Link) allow(RecommendedProductsService).to receive(:fetch).and_return(Link.none) visit discover_url(host:) expect(page).to_not have_text("Recommended") within_section "Featured products", section_element: :section do click_on searchable_product.name end add_to_cart(searchable_product, cart: true) check_out(searchable_product) purchase = Purchase.last expect(purchase.recommended_purchase_info.recommendation_type).to eq(RecommendationType::GUMROAD_DISCOVER_RECOMMENDATION) end it "boosts curated products to the top of the search results when they overflow the carousel" do curated_products = create_list(:product, 10, :recommendable) { _1.update!(name: "Curated #{_2}") } other_products = 7.times.map { |i| create(:product, :recommendable, price_cents: 10_00 * (i + 1), name: "Other #{i}") } allow(RecommendedProductsService).to receive(:fetch).and_return(Link.where(id: curated_products.map(&:id))) stub_const("DiscoverController::INITIAL_PRODUCTS_COUNT", 9) Purchase.import(refresh: true, force: true) Link.import(refresh: true, force: true) login_as user visit discover_url(host:) within_section "Recommended", section_element: :section do expect_product_cards_in_order(curated_products.first(8)) end within_section "Curated for you", section_element: :section do expect_product_cards_in_order(curated_products.last(2) + other_products.reverse) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/discover/filtering_spec.rb
spec/requests/discover/filtering_spec.rb
# frozen_string_literal: true require "spec_helper" describe("Discover - Filtering scenarios", js: true, type: :system) do let(:discover_host) { UrlService.discover_domain_with_protocol } before do @audio_taxonomy = Taxonomy.find_by(slug: "audio") @wallpapers_taxonomy = Taxonomy.find_by(slug: "wallpapers", parent: Taxonomy.find_by(slug: "design")) allow_any_instance_of(Link).to receive(:update_asset_preview) @buyer = create(:user) @png = Rack::Test::UploadedFile.new(Rails.root.join("spec", "support", "fixtures", "kFDzu.png"), "image/png") seed_products end def seed_products travel_to(91.days.ago) do recurrence_price_values = [ { BasePrice::Recurrence::MONTHLY => { enabled: true, price: 10 } }, { BasePrice::Recurrence::MONTHLY => { enabled: true, price: 1 } } ] @similar_product_5 = create(:membership_product_with_preset_tiered_pricing, name: "product 5 (with tiers)", recurrence_price_values:, user: create(:compliant_user, name: "Gumstein VI"), taxonomy: @wallpapers_taxonomy) end travel_to(5.minutes.ago) do @product = create(:product, taxonomy: @audio_taxonomy, user: create(:compliant_user, name: "Gumstein"), name: "product 0") end travel_to(4.minutes.ago) do @similar_product_1 = create(:product, taxonomy: @audio_taxonomy, user: create(:compliant_user, name: "Gumstein II"), name: "product 1", preview: @png, price_cents: 200, discover_fee_per_thousand: 300) end travel_to(3.minutes.ago) do @similar_product_2 = create(:product, taxonomy: @audio_taxonomy, user: create(:compliant_user, name: "Gumstein III"), name: "product 2", preview: @png, price_cents: 300, discover_fee_per_thousand: 400) end travel_to(2.minutes.ago) do @similar_product_3 = create(:product, taxonomy: @audio_taxonomy, user: create(:compliant_user, name: "Gumstein IV"), name: "product 3", preview: @png, price_cents: 400) end travel_to(1.minute.ago) do @similar_product_4 = create(:product, taxonomy: @audio_taxonomy, user: create(:compliant_user, name: "Gumstein V"), name: "product 4", preview: @png, price_cents: 500) end create(:product_review, purchase: create(:purchase, email: "gumroaduser1@gmail.com", link: @similar_product_5), rating: 1) create(:product_review, purchase: create(:purchase, link: @similar_product_4)) 2.times do |i| create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @product), rating: 5) create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @similar_product_1), rating: 4) end 3.times do |i| create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @product), rating: 5) create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @similar_product_2), rating: 3) end 4.times do |i| create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @product), rating: 5) create(:product_review, purchase: create(:purchase, email: "gumroaduser#{i}@gmail.com", link: @similar_product_3), rating: 2) end create(:purchase, email: "gumroaduser1@gmail.com", link: @product) create(:purchase, email: "gumroaduser1@gmail.com", link: @similar_product_4) create(:purchase, email: @buyer.email, purchaser: @buyer, link: @product) index_model_records(Purchase) index_model_records(Link) end it "filters products by filetype" do product = create(:product, :recommendable, name: "product with a PDF", created_at: 1.minute.ago) create(:product_file, link: product) product2 = create(:product, :recommendable, name: "product with a MP3", created_at: 2.minutes.ago) create(:listenable_audio, link: product2) index_model_records(Purchase) index_model_records(Link) visit discover_url(host: discover_host) fill_in("Search products", with: "product\n") toggle_disclosure "Contains" check "pdf (1)" wait_for_ajax expect_product_cards_with_names("product with a PDF") check "mp3 (1)" wait_for_ajax expect_product_cards_with_names("product with a MP3", "product with a PDF") uncheck "pdf (1)" wait_for_ajax expect_product_cards_with_names("product with a MP3") uncheck "mp3 (1)" wait_for_ajax expect_product_cards_in_order([@product, @similar_product_3, @similar_product_2, @similar_product_4, @similar_product_1, product, product2, @similar_product_5]) visit discover_url(host: discover_host, query: "product", filetypes: "mp3,pdf") expect_product_cards_with_names("product with a MP3", "product with a PDF") select_disclosure "Contains" do expect(page).to have_checked_field("mp3") expect(page).to have_checked_field("pdf") end end it "filters products by tags" do @similar_product_2.tag!("tag1") @similar_product_2.tag!("tag2") @similar_product_3.tag!("tag2") index_model_records(Purchase) index_model_records(Link) visit discover_url(host: discover_host) fill_in("Search products", with: "product\n") expect_product_cards_in_order([@product, @similar_product_3, @similar_product_2, @similar_product_4, @similar_product_1, @similar_product_5]) toggle_disclosure "Tags" check "tag1" expect_product_cards_in_order([@similar_product_2]) check "tag2" expect_product_cards_in_order([@similar_product_3, @similar_product_2]) uncheck "tag1" expect_product_cards_in_order([@similar_product_3, @similar_product_2]) uncheck "tag2" expect_product_cards_in_order([@product, @similar_product_3, @similar_product_2, @similar_product_4, @similar_product_1, @similar_product_5]) visit discover_url(host: discover_host, query: "product", tags: "tag1,tag2") expect_product_cards_in_order([@similar_product_3, @similar_product_2]) select_disclosure "Tags" do expect(page).to have_checked_field("tag1") expect(page).to have_checked_field("tag2") end end it "sorts products" do visit discover_url(host: discover_host) fill_in("Search products", with: "product\n") wait_for_ajax select_disclosure "Sort by" do expect(page).to have_checked_field "Default" end expect_product_cards_in_order([@product, @similar_product_3, @similar_product_2, @similar_product_4, @similar_product_1, @similar_product_5]) choose "Newest" wait_for_ajax expect_product_cards_in_order([@similar_product_4, @similar_product_3, @similar_product_2, @similar_product_1, @product, @similar_product_5]) choose "Highest rated" wait_for_ajax expect_product_cards_in_order([@product, @similar_product_1, @similar_product_2, @similar_product_3, @similar_product_4, @similar_product_5]) choose "Most reviewed" wait_for_ajax expect_product_cards_in_order([@product, @similar_product_3, @similar_product_2, @similar_product_1, @similar_product_4, @similar_product_5]) choose "Price (Low to High)" wait_for_ajax expect_product_cards_in_order([@product, @similar_product_5, @similar_product_1, @similar_product_2, @similar_product_3, @similar_product_4]) choose "Price (High to Low)" wait_for_ajax expect_product_cards_in_order([@similar_product_4, @similar_product_3, @similar_product_2, @similar_product_1, @product, @similar_product_5]) choose "Default" wait_for_ajax expect_product_cards_in_order([@product, @similar_product_3, @similar_product_2, @similar_product_4, @similar_product_1, @similar_product_5]) choose "Hot and new" wait_for_ajax expect_product_cards_in_order([@similar_product_3, @product, @similar_product_4, @similar_product_2, @similar_product_1]) end it "filters products by price" do visit discover_url(host: discover_host) fill_in("Search products", with: "product\n") toggle_disclosure "Price" fill_in "Minimum price", with: "3" wait_for_ajax expect_product_cards_in_order([@similar_product_3, @similar_product_2, @similar_product_4, @similar_product_5]) fill_in "Maximum price", with: "4" wait_for_ajax expect_product_cards_in_order([@similar_product_3, @similar_product_2]) visit discover_url(host: discover_host, query: "product") toggle_disclosure "Price" fill_in "Maximum price", with: "4" wait_for_ajax expect_product_cards_in_order([@product, @similar_product_3, @similar_product_2, @similar_product_1, @similar_product_5]) fill_in "Maximum price", with: "0" wait_for_ajax expect(page).to have_content("No products found") expect(page).to have_product_card(count: 0) fill_in "Maximum price", with: "4" fill_in "Minimum price", with: "8" wait_for_ajax expect_alert_message("Please set the price minimum to be lower than the maximum.") end it "filters products by ratings" do visit discover_url(host: discover_host) fill_in("Search products", with: "product\n") expect(page).to have_product_card(count: 6) toggle_disclosure "Rating" # filter by 4 star + ratings choose "4 stars and up" wait_for_ajax expect_product_cards_in_order([@product, @similar_product_1]) # filter by 3 star + ratings choose "3 stars and up" wait_for_ajax expect_product_cards_in_order([@product, @similar_product_2, @similar_product_1]) # filter by 2 star + ratings choose "2 stars and up" wait_for_ajax expect_product_cards_in_order([@product, @similar_product_3, @similar_product_2, @similar_product_1]) # filter by 1 star + ratings choose "1 star and up" wait_for_ajax expect_product_cards_in_order([@product, @similar_product_3, @similar_product_2, @similar_product_4, @similar_product_1, @similar_product_5]) end it "properly restores state and taxonomy category when pressing back" do visit discover_url(host: discover_host, query: "product") select_disclosure "Price" do fill_in "Maximum price", with: "3" end wait_for_ajax expect_product_cards_in_order([@product, @similar_product_2, @similar_product_1, @similar_product_5]) find("[role=menuitem]", text: "Audio").hover click_on "All Audio" wait_for_ajax expect(page).to have_selector("[aria-label='Breadcrumbs']", text: "Audio") within_section "Featured products", section_element: :section do expect_product_cards_in_order([@product, @similar_product_3, @similar_product_2, @similar_product_4, @similar_product_1]) end select_disclosure "Price" do fill_in "Maximum price", with: "2" end wait_for_ajax within_section "On the market" do expect_product_cards_in_order([@product, @similar_product_1]) end page.go_back wait_for_ajax select_disclosure "Price" do expect(page).to have_field "Maximum price", with: "" end expect(page).to have_selector("[aria-label='Breadcrumbs']", text: "Audio") page.go_back wait_for_ajax select_disclosure "Price" do expect(page).to have_field "Maximum price", with: "3" end expect_product_cards_in_order([@product, @similar_product_2, @similar_product_1, @similar_product_5]) page.go_back wait_for_ajax select_disclosure "Price" do expect(page).to have_field "Maximum price", with: "" end end it "filters from url params and updates UI" do visit discover_url(host: discover_host, query: "product", rating: "3", min_price: "2", max_price: "4", sort: "highest_rated") wait_for_ajax expect_product_cards_in_order([@similar_product_1, @similar_product_2]) select_disclosure "Sort by" do expect(page).to have_checked_field("Highest rated") end select_disclosure "Rating" do expect(page).to have_checked_field("3 stars and up") end end describe "nsfw filter" do let(:software_taxonomy) { Taxonomy.find_by(slug: "software-development") } let!(:sfw_product) { create(:product, :recommendable, name: "sfw product", taxonomy: software_taxonomy) } let!(:adult_product) { create(:product, :recommendable, name: "adult product", taxonomy: software_taxonomy, is_adult: true) } before do index_model_records(Purchase) index_model_records(Link) end context "when show_nsfw_products flag is off" do let(:user) { create(:user, show_nsfw_products: false) } it "hides adult products" do login_as(user) visit "#{discover_host}/software-development?sort=featured" within_section "On the market" do expect_product_cards_with_names("sfw product") end expect(page).not_to have_product_card(text: "adult product") end end context "when show_nsfw_products flag is on" do let(:user) { create(:user, show_nsfw_products: true) } it "shows adult products" do login_as(user) visit "#{discover_host}/software-development?sort=featured" within_section "On the market" do expect_product_cards_with_names("sfw product", "adult product") end end end end describe "taxonomy", :elasticsearch_wait_for_refresh do before do csharp_taxonomy = Taxonomy.find_by(slug: "c-sharp", parent: Taxonomy.find_by(slug: "programming", parent: Taxonomy.find_by(slug: "software-development"))) 4.times do |i| product = create(:product, user: create(:compliant_user), name: "C# #{i}", price_cents: 100 * i, taxonomy: csharp_taxonomy) create(:product_review, purchase: create(:purchase, email: "taxonomy#{i}@gmail.com", link: product), rating: i + 1) end visit "#{discover_host}/software-development/programming/c-sharp?query=c&sort=featured" end it "filters results by taxonomy" do expect_product_cards_with_names("C# 0", "C# 1", "C# 2", "C# 3") end it "correctly handles the search filters" do expect(page).to have_product_card(count: 4) toggle_disclosure "Price" fill_in("Maximum price", with: 2) wait_for_ajax expect_product_cards_with_names("C# 0", "C# 1", "C# 2") fill_in("Minimum price", with: 1) wait_for_ajax expect_product_cards_with_names("C# 1", "C# 2") click_on("Clear") wait_for_ajax expect(page).to have_product_card(count: 4) toggle_disclosure "Rating" choose("3 stars and up") wait_for_ajax expect_product_cards_with_names("C# 2", "C# 3") end it "correctly handles the search queries" do expect(page).to have_product_card(count: 4) fill_in("Search products", with: "C# 0\n") wait_for_ajax expect_product_cards_with_names("C# 0") fill_in("Search products", with: "product 0\n") wait_for_ajax expect(page).to_not have_product_card end end context "when having many active search filters" do it "has the tag as content title" do visit discover_url(host: discover_host, tags: "nevernever", rating: "4", min_price: "9999", max_price: "99999", sort: "most_reviewed", filetypes: "ftx") select_disclosure "Tags" do expect(page).to have_content("nevernever (0)") end select_disclosure "Contains" do expect(page).to have_content("ftx (0)") end end it "clears filters, but not category, when filling search box" do visit "#{discover_host}/design/wallpapers?max_price=99999&min_price=9999&rating=4&sort=most_reviewed&filetypes=ftx&tags=never" fill_in "Search products", with: "product" find_field("Search products").native.send_keys(:return) expect_product_cards_with_names("product 5 (with tiers)") end it "clears filters when changing categories" do visit discover_url(host: discover_host, tags: "never", rating: "4", min_price: "9999", max_price: "99999", sort: "most_reviewed", filetypes: "ftx") within "[role=menubar]" do click_on "Audio" end fill_in "Search products", with: "product" find_field("Search products").native.send_keys(:return) expect_product_cards_with_names("product 0", "product 1", "product 2", "product 3", "product 4") end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/discover/discover_mobile_spec.rb
spec/requests/discover/discover_mobile_spec.rb
# frozen_string_literal: true require "spec_helper" describe("Discover - Nav - Mobile", :js, :mobile_view, type: :system) do let(:discover_host) { UrlService.discover_domain_with_protocol } before do software_taxonomy = Taxonomy.find_by(slug: "software-development") @software_product = create(:product, user: create(:compliant_user), name: "Software Product", price_cents: 100, taxonomy: software_taxonomy) create(:product_review, purchase: create(:purchase, link: @software_product), rating: 1) programming_taxonomy = Taxonomy.find_by(slug: "programming", parent: software_taxonomy) @programming_product = create(:product, user: create(:compliant_user), name: "Programming Product", price_cents: 200, taxonomy: programming_taxonomy) create(:product_review, purchase: create(:purchase, link: @programming_product), rating: 2) index_model_records(Purchase) index_model_records(Link) end it "allows navigation to top-level and nested categories" do visit discover_url(host: discover_host) click_on "Categories" within "[role=menu]" do click_on "Software Development" click_on "All Software Development" end within_section "Featured products", section_element: :section do expect_product_cards_in_order([@programming_product, @software_product]) end click_on "Categories" within "[role=menu]" do click_on "Software Development" click_on "Programming" click_on "All Programming" end within_section "Featured products", section_element: :section do expect_product_cards_in_order([@programming_product]) end end it "dismisses the menu by pressing escape key" do visit discover_url(host: discover_host) # 'All' is the first option of the nav expect(page).not_to have_selector("[role=menuitem]", text: "All") click_on "Categories" expect(page).to have_text("All") send_keys(:escape) expect(page).not_to have_text("All") end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/discover/discover_domain_spec.rb
spec/requests/discover/discover_domain_spec.rb
# frozen_string_literal: true require "spec_helper" describe "DiscoverDomainScenario", type: :system, js: true do let(:films_best_selling_section) do find("section", text: "Best selling products") end before do @port = Capybara.current_session.server.port @discover_domain = "discover.test.gumroad.com" stub_const("VALID_DISCOVER_REQUEST_HOST", @discover_domain) @product1 = create(:product, :recommendable, name: "product 1") @product2 = create(:product, :recommendable, name: "product 2") index_model_records(Link) end def expect_discover_page_logged_in_links(page) host = UrlService.domain_with_protocol expect(page).to have_selector "a[aria-label='Primary logo'][href='#{dashboard_url(host:)}']" expect(page).to have_link "Discover", href: UrlService.discover_domain_with_protocol expect(page).to have_link "Library", href: library_url(host:) expect(page).to have_link "Settings", href: settings_main_url(host:) expect(page).to have_link "Logout", href: logout_url(host:) end def expect_product_and_creator_links(product, recommended_by: "discover") expect(page).to have_link product.user.name, href: product.user.profile_url(recommended_by:) find_product_card(product).hover expect(page).to have_selector "a[href='#{product.long_url(recommended_by:, layout: "discover")}']" end describe "on discover domain" do it "correctly attributes purchase to discover and search" do featured_products = create_list(:product, 6, :recommendable, price_cents: 99) other_product = create(:product, :recommendable, name: "not featured product") # Set sales_volume to make sure featured products are predictable [@product1, @product2, *featured_products].each { create(:purchase, link: _1) } index_model_records(Purchase) index_model_records(Link) visit "http://#{@discover_domain}:#{@port}/#{@product1.taxonomy.slug}" within_section "Featured products", section_element: :section do expect_product_and_creator_links(@product1) expect_product_and_creator_links(@product2) end within_section "On the market" do expect_product_and_creator_links(other_product, recommended_by: "search") end within_section "Featured products", section_element: :section do find_product_card(@product1).click end wait_for_ajax expect do add_to_cart(@product1, cart: true) check_out(@product1, credit_card: { number: "4000002500003155" }, sca: true) end.to change { Purchase.successful.select(&:was_discover_fee_charged?).count }.by(1) .and change { Purchase.successful.select(&:was_product_recommended?).count }.by(1) .and change { RecommendedPurchaseInfo.where(recommendation_type: "discover").count }.by(1) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/wishlists/wishlist_index_spec.rb
spec/requests/wishlists/wishlist_index_spec.rb
# frozen_string_literal: true require "spec_helper" describe "Wishlist index page", :js, type: :system do let(:wishlist) { create(:wishlist, name: "My Wishlist", user: create(:user, name: "Wishlist User")) } let(:quantity_item) { create(:wishlist_product, :with_quantity, wishlist:) } let(:variant_item) { create(:wishlist_product, :with_recurring_variant, wishlist:) } before do login_as(wishlist.user) quantity_item.product.update!(name: "Quantity Product") variant_item.product.update!(name: "Variant Product") end it "lists wishlists and links to the public page" do visit wishlists_path within find(:table_row, { "Name" => wishlist.name }) do new_window = window_opened_by { click_link(wishlist.name) } within_window new_window do expect(page).to have_current_path(Rails.application.routes.url_helpers.wishlist_path(wishlist.url_slug)) expect(page).to have_text("My Wishlist") end end end it "allows the user to delete a wishlist" do visit wishlists_path within find(:table_row, { "Name" => wishlist.name }) do find("button[aria-label='Delete wishlist']").click end click_on "Yes, delete" expect(page).to have_text("Wishlist deleted") expect(page).not_to have_text(wishlist.name) expect(wishlist.reload).to be_deleted end context "for a different user" do before { wishlist.update!(user: create(:user)) } it "does not show the wishlist" do visit wishlists_path within_section "Save products you are wishing for" do expect(page).to have_text("Bookmark and organize your desired products with ease") end end end context "reviews_page feature flag is disabled" do it "does not show the reviews tab" do visit wishlists_path expect(page).to_not have_link("Reviews") end end context "reviews_page feature flag is enabled" do before { Feature.activate_user(:reviews_page, wishlist.user) } it "shows the reviews tab" do visit wishlists_path expect(page).to have_selector("a[role='tab'][href='#{reviews_path}'][aria-selected='false']", text: "Reviews") end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/wishlists/wishlist_show_spec.rb
spec/requests/wishlists/wishlist_show_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/discover_layout" describe "Wishlist show page", :js, type: :system do include Rails.application.routes.url_helpers let(:physical_product) { create(:product, :recommendable, name: "Quantity Product", price_cents: 1000, quantity_enabled: true) } let(:pwyw_product) { create(:product, name: "PWYW Product", price_cents: 1000, customizable_price: true) } let(:versioned_product) { create(:product_with_digital_versions, price_cents: 800, name: "Versioned Product") } let(:membership_product) do create(:membership_product_with_preset_tiered_pricing, :recommendable, name: "Membership Product", description: "A recurring membership", recurrence_price_values: [ { "monthly": { enabled: true, price: 2 }, "yearly": { enabled: true, price: 4 } }, { "monthly": { enabled: true, price: 5 }, "yearly": { enabled: true, price: 12 } } ]) end let(:rental_product) { create(:product, name: "Rental Product", purchase_type: "buy_and_rent", price_cents: 500, rental_price_cents: 300) } let(:wishlist) { create(:wishlist, name: "My Wishlist", description: "My wishlist description", user: create(:user, name: "Wishlist User")) } before do create(:wishlist_product, wishlist:, product: physical_product, quantity: 2) create(:wishlist_product, wishlist:, product: pwyw_product) create(:wishlist_product, wishlist:, product: versioned_product, variant: versioned_product.variant_categories.first.variants.first) create(:wishlist_product, wishlist:, product: membership_product, recurrence: "yearly", variant: membership_product.variant_categories.first.variants.first) create(:wishlist_product, wishlist:, product: rental_product, rent: true) end it "shows products" do visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) expect(page).to have_text("My Wishlist") expect(page).to have_text("My wishlist description") expect(page).to have_link("Wishlist User", href: wishlist.user.profile_url) within find_product_card(physical_product) do expect(page).to have_selector("[itemprop='name']", text: "Quantity Product") expect(page).to have_selector("[itemprop='price']", text: "$10") end within find_product_card(membership_product) do expect(page).to have_selector("[itemprop='name']", text: "Membership Product - First Tier - Yearly") end within find_product_card(versioned_product) do expect(page).to have_selector("[itemprop='name']", text: "Versioned Product - Untitled 1") end find_product_card(membership_product).click expect(page).to have_text(membership_product.description) end it "supports buying single products with options prefilled" do visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) expect(page).to have_text("My Wishlist") within find_product_card(physical_product) do click_on "Add to cart" end within_cart_item(physical_product.name) do expect(page).to have_text("US$20") expect(page).to have_text("Qty: 2") end page.go_back within find_product_card(versioned_product) do click_on "Add to cart" end within_cart_item(versioned_product.name) do expect(page).to have_text("Version: Untitled 1") end page.go_back within find_product_card(membership_product) do click_on "Add to cart" end within_cart_item(membership_product.name) do expect(page).to have_text("Tier: First Tier") expect(page).to have_text("Yearly") end page.go_back within find_product_card(rental_product) do click_on "Add to cart" end within_cart_item(rental_product.name) do expect(page).to have_text("US$3") end end it "supports buying all products at once" do visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) click_link "Buy this wishlist" within_cart_item(physical_product.name) do expect(page).to have_text("US$20") expect(page).to have_text("Qty: 2") end within_cart_item(pwyw_product.name) do expect(page).to have_text("US$10") end within_cart_item(versioned_product.name) do expect(page).to have_text("Version: Untitled 1") end within_cart_item(membership_product.name) do expect(page).to have_text("Tier: First Tier") expect(page).to have_text("Yearly") end within_cart_item(rental_product.name) do expect(page).to have_text("US$3") end end it "sets recommended_by and links global affiliate" do wishlist.wishlist_products.where.not(product: [physical_product, pwyw_product]).each(&:mark_deleted!) physical_product.update!(allow_double_charges: true) visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) click_link "Buy this wishlist" check_out(physical_product) expect(Purchase.where(link: physical_product).last).to have_attributes( recommended_by: RecommendationType::WISHLIST_RECOMMENDATION, affiliate_id: wishlist.user.global_affiliate.id, ) expect(Purchase.where(link: pwyw_product).last).to have_attributes( recommended_by: RecommendationType::WISHLIST_RECOMMENDATION, affiliate_id: nil, # Not recommendable, so not eligible for global affiliate ) visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) click_link physical_product.name add_to_cart(physical_product) check_out(physical_product) expect(Purchase.last).to have_attributes( link: physical_product, recommended_by: RecommendationType::WISHLIST_RECOMMENDATION, affiliate_id: wishlist.user.global_affiliate.id, ) login_as wishlist.user visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) click_link physical_product.name add_to_cart(physical_product) check_out(physical_product, email: wishlist.user.email, logged_in_user: wishlist.user) expect(Purchase.last).to have_attributes( link: physical_product, recommended_by: RecommendationType::WISHLIST_RECOMMENDATION, affiliate_id: nil, # Can't get global affiliate commission for your own wishlist ) end it "prefills gift checkout" do visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) within find_product_card(membership_product) do click_on("Gift this product") end expect(page).to have_checked_field("Give as a gift") expect(page).to have_text("Wishlist User's email has been hidden for privacy purposes.") fill_in "Message", with: "Happy birthday!" check_out(membership_product, gift: { name: "Wishlist User" }) expect(Purchase.last).to have_attributes( link: membership_product, recommended_by: RecommendationType::WISHLIST_RECOMMENDATION, affiliate_id: nil, # Can't get global affiliate commission for being gifted a product from your own wishlist ) expect(Gift.last).to have_attributes( link: membership_product, gift_note: "Happy birthday!", is_recipient_hidden: true ) expect(Gift.last.giftee_purchase).to have_attributes( email: wishlist.user.email, purchaser: wishlist.user, variant_attributes: [membership_product.variant_categories.first.variants.first], is_original_subscription_purchase: false ) expect(Gift.last.gifter_purchase).to have_attributes( email: "test@gumroad.com", purchaser: nil, variant_attributes: [membership_product.variant_categories.first.variants.first], is_original_subscription_purchase: true ) expect(Subscription.last).to have_attributes( user: wishlist.user, credit_card: nil ) end it "clears existing cart items when doing gift checkout" do visit physical_product.long_url add_to_cart(physical_product) visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) within find_product_card(versioned_product) do click_on("Gift this product") end expect(page).not_to have_text(physical_product.name) expect(page).to have_text(versioned_product.name) expect(page).to have_text("Total US$8", normalize_ws: true) end it "supports cancelling a gift checkout" do visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) within find_product_card(versioned_product) do click_on("Gift this product") end click_button "Cancel gift option" click_button "Yes, reset" expect(page).to have_unchecked_field("Give as a gift") expect(page).not_to have_text("Wishlist User's email has been hidden for privacy purposes.") check "Give as a gift" expect(page).to have_field("Recipient email address") expect(page).not_to have_text("Wishlist User's email has been hidden for privacy purposes.") end context "when no products are purchasable" do before do wishlist.wishlist_products.each { |wishlist_product| wishlist_product.product.unpublish! } end it "disables the buy links" do visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) buy_link = find_link("Buy this wishlist", inert: true) buy_link.hover expect(buy_link).to have_tooltip(text: "None of the products on this wishlist are available for purchase") expect(page).not_to have_link("Gift this product") end end it "supports editing your own wishlist" do visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) expect(page).not_to have_button("Edit") login_as wishlist.user refresh click_button "Edit" within_modal wishlist.name do fill_in "Name", with: "New Wishlist Name" click_on "Close" end expect(page).to have_text("New Wishlist Name") click_button "Edit" within_modal "New Wishlist Name" do fill_in "Description", with: "Description Goes Here" click_on "Close" end within find_product_card(wishlist.wishlist_products.first.product) do expect(page).to have_text("$10") end within find_product_card(wishlist.wishlist_products.fourth.product) do expect(page).to have_text("$2 a month") end wishlist.wishlist_products.each do |wishlist_product| within find_product_card(wishlist_product.product) do click_on "Remove this product" end end expect(page).to have_text("Description Goes Here") expect(page).to have_text("Products from your wishlist will be displayed here") wishlist.wishlist_products.reload.each do |wishlist_product| expect(wishlist_product).to be_deleted expect(page).not_to have_text(wishlist_product.product.name) end end context "when viewing as a non-editor" do it "shows the empty state message for viewers" do logout wishlist.wishlist_products.destroy_all visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) expect(page).to have_text("This wishlist is currently empty") end end describe "discover layout" do let(:discover_url) { wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol, layout: Product::Layout::DISCOVER) } let(:non_discover_url) { wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) } it_behaves_like "discover navigation when layout is discover" end describe "following wishlists" do let(:user) { create(:buyer_user) } it "redirects when not logged in" do url = wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) visit url click_button "Follow wishlist" expect(current_url).to eq(login_url(host: DOMAIN, next: url)) end it "supports following and unfollowing wishlists" do login_as user visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) expect do click_button "Follow wishlist" expect(page).to have_alert(text: "You are now following My Wishlist!") end.to change(WishlistFollower, :count).from(0).to(1) wishlist_follower = WishlistFollower.last expect(wishlist_follower).to have_attributes(wishlist:, follower_user: user) click_button "Following" expect(page).to have_alert(text: "You are no longer following My Wishlist.") expect(wishlist_follower.reload).to be_deleted end it "does not show the button when the feature is disabled" do Feature.deactivate(:follow_wishlists) visit wishlist_url(wishlist.external_id_numeric, host: wishlist.user.subdomain_with_protocol) expect(page).not_to have_button("Follow wishlist") end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/requests/wishlists/wishlist_following_spec.rb
spec/requests/wishlists/wishlist_following_spec.rb
# frozen_string_literal: true require "spec_helper" describe "Wishlist following page", :js, type: :system do include Rails.application.routes.url_helpers let(:user) { create(:user, name: "Follower") } let(:wishlist) { create(:wishlist, name: "Followed Wishlist") } let!(:wishlist_follower) { create(:wishlist_follower, wishlist:, follower_user: user) } let!(:own_wishlist) { create(:wishlist, name: "Own Wishlist", user:) } let!(:not_following_wishlist) { create(:wishlist, name: "Not Following") } before do login_as(user) end it "lists followed wishlists and allows the user to unfollow" do visit wishlists_following_index_path expect(page).not_to have_selector(:table_row, { "Name" => own_wishlist.name }) expect(page).not_to have_selector(:table_row, { "Name" => not_following_wishlist.name }) within find(:table_row, { "Name" => wishlist.name }) do expect(page).to have_link(wishlist.name, href: wishlist_url(wishlist.url_slug, host: wishlist.user.subdomain_with_protocol)) select_disclosure "Actions" do click_on "Unfollow" end end expect(page).to have_alert(text: "You are no longer following Followed Wishlist.") expect(page).not_to have_text(wishlist.name) expect(wishlist_follower.reload).to be_deleted expect(page).to have_text("Follow wishlists that inspire you") end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/installments_helper_spec.rb
spec/helpers/installments_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe InstallmentsHelper do describe "#post_title_displayable" do let(:url) { nil } let(:post) { create(:installment) } subject { helper.post_title_displayable(post:, url:) } context "when url is missing" do it "displays the post title as plain text" do is_expected.to eq("<span class=\"title\">#{post.subject}</span>") end end context "when url is present" do let(:url) { "https://example.com/p/#{post.slug}" } it "displays the post title as an anchor tag" do is_expected.to eq("<a target=\"_blank\" class=\"title\" href=\"#{url}\">#{post.subject}</a>") end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/mailer_helper_spec.rb
spec/helpers/mailer_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe MailerHelper do describe "#from_email_address_name" do context "when name doesn't contain special characters" do it "returns the name" do expect(from_email_address_name("John The Creator")).to eq("John The Creator") end end context "when name contains colon character" do it "strips the colon and returns the cleaned name" do expect(from_email_address_name("John: The Creator")).to eq("John The Creator") end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/cdn_url_helper_spec.rb
spec/helpers/cdn_url_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe CdnUrlHelper do before do stub_const("CDN_URL_MAP", { "#{AWS_S3_ENDPOINT}/gumroad/" => "https://static-2.gumroad.com/res/gumroad/" }) end describe "#cdn_url_for" do it "returns CDN URL" do s3_url = "#{AWS_S3_ENDPOINT}/gumroad/sample.png" expect(cdn_url_for(s3_url)).to eq "https://static-2.gumroad.com/res/gumroad/sample.png" end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/card_params_helper_spec.rb
spec/helpers/card_params_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe CardParamsHelper do describe ".get_card_data_handling_mode" do describe "with valid mode" do let(:params) { { card_data_handling_mode: "stripejs.0" } } it "returns the mode" do expect(CardParamsHelper.get_card_data_handling_mode(params)).to eq "stripejs.0" end end describe "with invalid mode" do let(:params) { { card_data_handling_mode: "jedi-force" } } it "returns nil" do expect(CardParamsHelper.get_card_data_handling_mode(params)).to be(nil) end end end describe ".check_for_errors" do describe "with no errors" do let(:params) { { card_data_handling_mode: "stripejs.0" } } it "returns nil" do expect(CardParamsHelper.check_for_errors(params)).to be(nil) end end describe "with invalid card data handling mode" do let(:params) { { card_data_handling_mode: "jedi-force" } } it "returns nil" do expect(CardParamsHelper.check_for_errors(params)).to be(nil) end end describe "with errors (stripe)" do let(:params) do { card_data_handling_mode: "stripejs.0", stripe_error: { message: "The card was declined.", code: "card_declined" } } end it "returns an error object" do expect(CardParamsHelper.check_for_errors(params)).to be_a(CardDataHandlingError) end it "returns the error message" do expect(CardParamsHelper.check_for_errors(params).error_message).to eq "The card was declined." end it "returns the error code" do expect(CardParamsHelper.check_for_errors(params).card_error_code).to eq "card_declined" end end end describe ".build_chargeable" do describe "with invalid card data handling mode" do let(:params) { { card_data_handling_mode: "jedi-force" } } it "returns nil" do expect(CardParamsHelper.build_chargeable(params)).to be(nil) end end describe "with valid card data handling mode" do let(:params) { { card_data_handling_mode: "stripejs.0" } } it "returns nil" do chargeable_double = double("chargeable") expect(ChargeProcessor).to receive(:get_chargeable_for_params).with(params, nil).and_return(chargeable_double) expect(CardParamsHelper.build_chargeable(params)).to eq chargeable_double end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/currency_helper_spec.rb
spec/helpers/currency_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe CurrencyHelper do describe "#get_rate" do it "returns the correct value" do expect(get_rate("JPY")).to eq "78.3932" expect(get_rate("GBP")).to eq "0.652571" end end describe "#get_usd_cents" do it "converts money amounts correctly" do expect(get_usd_cents("JPY", 100)).to eq 128 expect(get_usd_cents("GBP", 100)).to eq 153 end end describe "#usd_cents_to_currency" do it "converts money amounts correctly" do expect(usd_cents_to_currency("JPY", 127)).to eq 100 expect(usd_cents_to_currency("GBP", 153)).to eq 100 end end describe "#symbol_for" do it "returns the correct value" do expect(symbol_for(:usd)).to eq "$" expect(symbol_for(:gbp)).to eq "£" end end describe "#min_price_for" do it "returns the correct value" do expect(min_price_for(:usd)).to eq 99 expect(min_price_for(:gbp)).to eq 59 end end describe "#string_to_price_cents" do it "ignores the comma" do expect(string_to_price_cents(:usd, "1,200")).to eq 120_000 expect(string_to_price_cents(:usd, "1,200.99")).to eq 120_099 end end describe "#unit_scaling_factor" do it "returns the correct value" do expect(unit_scaling_factor("jpy")).to eq(1) expect(unit_scaling_factor("usd")).to eq(100) expect(unit_scaling_factor("gbp")).to eq(100) end end describe "#formatted_amount_in_currency" do it "returns the formatted amount with currency code and no symbol" do amount_cents = 1234 %w(usd cad aud gbp).each do |currency| expect(formatted_amount_in_currency(amount_cents, currency)).to eq("#{(amount_cents / 100.0)} #{currency.upcase}") end end end describe "#format_just_price_in_cents" do it "returns the correct value in USD" do expect(format_just_price_in_cents(1299, "usd")).to eq("$12.99") expect(format_just_price_in_cents(99, "usd")).to eq("99¢") end it "returns the correct value in other currencies" do expect(format_just_price_in_cents(799, "aud")).to eq("A$7.99") expect(format_just_price_in_cents(799, "gbp")).to eq("£7.99") expect(format_just_price_in_cents(799, "jpy")).to eq("¥799") end end describe "#formatted_price_with_recurrence" do let(:formatted_price) { "$19.99" } let(:recurrence) { BasePrice::Recurrence::MONTHLY } let(:charge_occurrence_count) { 2 } context "with format :short" do it "returns the correct value in short format" do expect( formatted_price_with_recurrence(formatted_price, recurrence, charge_occurrence_count, format: :short) ).to eq("$19.99 / month x 2") end end context "with format :long" do it "returns the correct value in long format" do expect( formatted_price_with_recurrence(formatted_price, recurrence, charge_occurrence_count, format: :long) ).to eq("$19.99 a month x 2") end end context "when there is no charge_occurrence_count" do let(:charge_occurrence_count) { nil } it "returns the correct value without count" do expect( formatted_price_with_recurrence(formatted_price, recurrence, charge_occurrence_count, format: :short) ).to eq("$19.99 / month") end end end describe "#product_card_formatted_price" do let(:price) { 1999 } let(:currency_code) { "usd" } let(:is_pay_what_you_want) { false } let(:recurrence) { nil } let(:duration_in_months) { nil } it "returns the correct value" do expect( product_card_formatted_price(price:, currency_code:, is_pay_what_you_want:, recurrence:, duration_in_months:) ).to eq("$19.99") end context "when is_pay_what_you_want is true" do let(:is_pay_what_you_want) { true } it "adds the plus sign after the price" do expect( product_card_formatted_price(price:, currency_code:, is_pay_what_you_want:, recurrence:, duration_in_months:) ).to eq("$19.99+") end context "with a recurrence" do let(:recurrence) { BasePrice::Recurrence::MONTHLY } it "adds recurrence" do expect( product_card_formatted_price(price:, currency_code:, is_pay_what_you_want:, recurrence:, duration_in_months:) ).to eq("$19.99+ a month") end context "with a duration_in_months" do let(:duration_in_months) { 3 } it "add duration in months" do expect( product_card_formatted_price(price:, currency_code:, is_pay_what_you_want:, recurrence:, duration_in_months:) ).to eq("$19.99+ a month x 3") end end end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/payouts_helper_spec.rb
spec/helpers/payouts_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe PayoutsHelper do def verify_balance(user, expected_balance) expect(user.unpaid_balance_cents).to eq expected_balance end describe "formatted_payout_date" do before do @user = create(:singaporean_user_with_compliance_info, payment_address: "balance@gumroad.com") WebMock.stub_request(:post, PAYPAL_ENDPOINT) .to_return(body: "TIMESTAMP=2012%2d10%2d26T20%3a29%3a14Z&CORRELATIONID=c51c5e0cecbce&ACK=Success&VERSION=90%2e0&BUILD=4072860") end it "returns the proper date for the current payout period given balance of 0" do payout_period_data = helper.payout_period_data(@user) expect(payout_period_data[:is_user_payable]).to be(false) end it "show proper date" do travel_to(Time.find_zone("UTC").local(2015, 3, 1)) do expect(formatted_payout_date(Date.current)).to eq("March 1st, 2015") end end it "returns the proper date for the current payout period given sales that span 2 payout periods" do @user = create(:singaporean_user_with_compliance_info, payment_address: "balance@gumroad.com") link = create(:product, user: @user, price_cents: 20_00) (0..13).each do |days_count| travel_to(Date.parse("2013-08-14") - days_count.days) do create(:purchase_with_balance, link:) end end travel_to(Date.parse("2013-08-14")) do payout_period_data = helper.payout_period_data(@user) expect(payout_period_data[:is_user_payable]).to be(true) expect(payout_period_data[:displayable_payout_period_range]) .to eq "Activity up to #{formatted_payout_date(Date.parse("2013-08-09"))}" expect(payout_period_data[:payout_cents]).to eq 149_58 expect(payout_period_data[:payout_date_formatted]).to eq formatted_payout_date(@user.next_payout_date) expect(payout_period_data[:paypal_address]).to eq @user.payment_address expect(payout_period_data[:arrival_date]).to be nil end end it "returns the proper sales amount for the current payout period given sales that span 2 payout periods" do @user = create(:singaporean_user_with_compliance_info, payment_address: "balance@gumroad.com") link = create(:product, user: @user, price_cents: 20_00) (0..1).each do |days_count| travel_to(Date.parse("2013-09-7") - days_count.days) do create(:purchase_with_balance, link:) end end travel_to(Date.parse("2013-09-7")) do payout_period_data = helper.payout_period_data(@user) expect(payout_period_data[:is_user_payable]).to be(true) expect(payout_period_data[:displayable_payout_period_range]) .to eq "Activity up to #{formatted_payout_date(Date.parse("2013-09-06"))}" expect(payout_period_data[:payout_cents]).to eq 1662 expect(payout_period_data[:payout_displayed_amount]).to eq("$16.62") expect(payout_period_data[:sales_cents]).to eq 2000 expect(payout_period_data[:payout_date_formatted]).to eq formatted_payout_date(@user.next_payout_date) expect(payout_period_data[:paypal_address]).to eq @user.payment_address end end it "returns the proper data for the current payout period given sales that span 2 payout periods and one old payment" do @user = create(:singaporean_user_with_compliance_info, user_risk_state: "compliant", payment_address: "balance@gumroad.com") link = create(:product, user: @user, price_cents: 20_00) # 8/16 is a payout friday (0..13).each do |days_count| travel_to(Date.parse("2013-08-20") - days_count.days) do create(:purchase_with_balance, link:) end end travel_to(Date.parse("2013-08-16")) do Payouts.create_payments_for_balances_up_to_date_for_users(Date.parse("2013-08-09"), PayoutProcessorType::PAYPAL, [@user]) payment = Payment.last payment.update!(processor_fee_cents: 10, txn_id: "test") payment.mark_completed! end travel_to(Date.parse("2013-08-20")) do payout_period_data = helper.payout_period_data(@user) expect(payout_period_data[:is_user_payable]).to be(true) expect(payout_period_data[:displayable_payout_period_range]) expect(payout_period_data[:displayable_payout_period_range]).to eq "Activity from #{formatted_payout_date(Date.parse("2013-08-10"))} to #{formatted_payout_date(Date.parse("2013-08-16"))}" expect(payout_period_data[:payout_cents]).to eq 116_34 expect(payout_period_data[:payout_date_formatted]).to eq formatted_payout_date(@user.next_payout_date) expect(payout_period_data[:paypal_address]).to eq @user.payment_address end end it "returns the proper date for the current payout period given sales that span 2 payout periods \ given that today falls after the latest unpaid payout period" do @user = create(:singaporean_user_with_compliance_info, user_risk_state: "compliant", payment_address: "balance@gumroad.com") link = create(:product, user: @user, price_cents: 20_00) # 8/16 is a payout friday (0..20).each do |days_count| travel_to(Date.parse("2013-08-25") - days_count.days) do create(:purchase_with_balance, link:) end end travel_to(Date.parse("2013-08-16")) do Payouts.create_payments_for_balances_up_to_date_for_users(Date.parse("2013-08-09"), PayoutProcessorType::PAYPAL, [@user]) payment = Payment.last payment.update!(processor_fee_cents: 10, txn_id: "test") payment.mark_completed! end travel_to(Date.parse("2013-08-25")) do payout_period_data = helper.payout_period_data(@user) expect(payout_period_data[:is_user_payable]).to be(true) expect(payout_period_data[:displayable_payout_period_range]) .to eq "Activity from #{formatted_payout_date(Date.parse("2013-08-10"))} to #{formatted_payout_date(Date.parse("2013-08-23"))}" expect(payout_period_data[:payout_cents]).to eq 232_68 expect(payout_period_data[:payout_date_formatted]).to eq formatted_payout_date(@user.next_payout_date) expect(payout_period_data[:paypal_address]).to eq @user.payment_address end end it "returns the proper date for an old payment" do @user = create(:singaporean_user_with_compliance_info, payment_address: "balance@gumroad.com") link = create(:product, user: @user, price_cents: 20_00) # 8/16 is a payout friday (0..20).each do |days_count| travel_to(Date.parse("2013-08-25") - days_count.days) do create(:purchase_with_balance, link:) end end travel_to(Date.parse("2013-08-16")) do Payouts.create_payments_for_balances_up_to_date_for_users(Date.parse("2013-08-09"), PayoutProcessorType::PAYPAL, [@user]) payment = Payment.last payment.update!(processor_fee_cents: 10, txn_id: "test") payment.mark_completed! end travel_to(Date.parse("2013-08-25")) do payment = Payment.last payout_period_data = helper.payout_period_data(@user, payment) expect(payout_period_data[:displayable_payout_period_range]).to eq "Activity up to #{formatted_payout_date(Date.parse("2013-08-09"))}" expect(payout_period_data[:payout_currency]).to eq Currency::USD expect(payout_period_data[:payout_cents]).to eq 8143 expect(payout_period_data[:payout_displayed_amount]).to eq "$81.43" expect(payout_period_data[:payout_date_formatted]).to eq formatted_payout_date(payment.created_at) expect(payout_period_data[:paypal_address]).to eq @user.payment_address end end describe "displayable_payout_period_range" do let(:seller) { create(:compliant_user, unpaid_balance_cents: 10_01) } let!(:merchant_account) { create(:merchant_account_stripe_connect, user: seller) } let(:stripe_connect_account_id) { merchant_account.charge_processor_merchant_id } before do create(:ach_account, user: seller, stripe_bank_account_id: stripe_connect_account_id) create(:user_compliance_info, user: seller) end it "renders the correct displayable_payout_period_range when 2 payouts have the same date" do travel_to(Date.parse("2024-11-22")) do instant_payout = create(:payment, user: seller, processor: PayoutProcessorType::STRIPE, state: "processing", stripe_connect_account_id:, json_data: { payout_type: Payouts::PAYOUT_TYPE_INSTANT, gumroad_fee_cents: 125 }) standard_payout = create(:payment, user: seller, processor: PayoutProcessorType::STRIPE, state: "processing", stripe_connect_account_id:) instant_payout_period_data = helper.payout_period_data(seller, instant_payout) expect(instant_payout_period_data[:displayable_payout_period_range]).to eq("Activity up to November 21st, 2024") expect(instant_payout_period_data[:fees_cents]).to eq(125) standard_payout_period_data = helper.payout_period_data(seller, standard_payout) expect(standard_payout_period_data[:displayable_payout_period_range]).to eq("Activity on November 21st, 2024") end end end end describe "refund for affiliate credits" do it "makes sure that balances are correct for seller and affiliate user when an affiliate credit \ is paid out in one pay period and then refunded in the next", :vcr do @seller = create(:singaporean_user_with_compliance_info, payment_address: "sahil@gumroad.com") link = create(:product, user: @seller, price_cents: 20_000) @affiliate_user = create(:singaporean_user_with_compliance_info, payment_address: "balance@gumroad.com") @direct_affiliate = create(:direct_affiliate, affiliate_user: @affiliate_user, seller: @seller, affiliate_basis_points: 1500, products: [link]) travel_to(1.week.ago) do @purchase = create(:purchase_in_progress, link:, affiliate: @direct_affiliate) @purchase.process! @purchase.update_balance_and_mark_successful! end Payouts.create_payments_for_balances_up_to_date_for_users(Date.current.yesterday, PayoutProcessorType::PAYPAL, User.holding_balance) @purchase_refund_flow_of_funds = FlowOfFunds.build_simple_flow_of_funds(Currency::USD, @purchase.total_transaction_cents) @purchase.refund_purchase!(@purchase_refund_flow_of_funds, @seller.id) @affiliate_user.reload @seller.reload verify_balance(@affiliate_user, @affiliate_user.affiliate_credits.last.affiliate_credit_refund_balance.amount_cents) expect(@affiliate_user.balances.count).to eq 2 expect(@affiliate_user.balances.first.amount_cents).to eq @purchase.affiliate_credit_cents expect(@affiliate_user.balances.first.state).to eq "processing" expect(@affiliate_user.balances.last.amount_cents).to eq(-@purchase.affiliate_credit_cents) expect(@affiliate_user.balances.last.state).to eq "unpaid" fully_refunded_sale = @seller.sales.successful.where("purchases.price_cents > 0 AND purchases.stripe_refunded = 1").last verify_balance(@seller, fully_refunded_sale.purchase_refund_balance.amount_cents) expect(@seller.balances.count).to eq 2 expect(@seller.balances.first.amount_cents).to eq Purchase.sum("price_cents - fee_cents - affiliate_credit_cents") expect(@seller.balances.first.state).to eq "processing" retained_fee_cents = (@purchase.price_cents * Purchase::PROCESSOR_FEE_PER_THOUSAND / 1000.0).round + Purchase::PROCESSOR_FIXED_FEE_CENTS expect(@seller.balances.last.amount_cents).to eq(-@purchase.price_cents + @purchase.affiliate_credit_cents + @purchase.fee_cents - retained_fee_cents) expect(@seller.balances.last.state).to eq "unpaid" end end describe "payout period data" do it "shows minimum payout volume for user without enough balance" do user = create(:user) expect(self.payout_period_data(user)[:is_user_payable]).to eq(false) expect(self.payout_period_data(user)[:minimum_payout_amount_cents]).to eq(1000) end it "shows payout data without payout given and no previous payouts" do travel_to(Time.find_zone("UTC").local(2015, 3, 1)) do user = create(:user) create(:ach_account, user:) create(:balance, user:, amount_cents: 10_00, date: Date.current) create(:bank, routing_number: "110000000", name: "Bank of America") expect(self.payout_period_data(user)[:is_user_payable]).to eq(true) expect(self.payout_period_data(user)[:displayable_payout_period_range]).to eq("Activity up to now") expect(self.payout_period_data(user)[:payout_date_formatted]).to eq("March 13th, 2015") expect(self.payout_period_data(user)[:payout_cents]).to eq(1000) expect(self.payout_period_data(user)[:bank_number]).to eq("110000000") expect(self.payout_period_data(user)[:account_number]).to eq("******1234") expect(self.payout_period_data(user)[:bank_account_type]).to eq("ACH") expect(self.payout_period_data(user)[:bank_name]).to eq("Bank of America") end end it "shows payout data without payout given and previous payouts" do travel_to(Time.find_zone("UTC").local(2015, 3, 1)) do user = create(:user) create(:ach_account, user:) payment = create(:payment, user:, amount_cents: 10_00) balance = create(:balance, user:, amount_cents: 10_00, date: Date.current - 30.days, state: "paid") payment.balances << balance create(:balance, user:, amount_cents: 10_00, date: Date.current) create(:bank, routing_number: "110000000", name: "Bank of America") expect(self.payout_period_data(user)[:is_user_payable]).to eq(true) expect(self.payout_period_data(user)[:displayable_payout_period_range]).to eq("Activity since March 1st, 2015") expect(self.payout_period_data(user)[:payout_date_formatted]).to eq("March 13th, 2015") expect(self.payout_period_data(user)[:payout_cents]).to eq(1000) expect(self.payout_period_data(user)[:bank_number]).to eq("110000000") expect(self.payout_period_data(user)[:account_number]).to eq("******1234") expect(self.payout_period_data(user)[:bank_name]).to eq("Bank of America") end end it "shows user's payout data with payout given and previous payouts" do travel_to(Time.find_zone("UTC").local(2015, 3, 1)) do user = create(:user) payment = create(:payment, user:, amount_cents: 10_00, arrival_date: 1.week.ago.to_i) balance = create(:balance, user:, amount_cents: 10_00, date: 30.days.ago, state: "paid") payment.balances << balance bank_account = create(:ach_account) bank_account.payments << payment create(:balance, user:, amount_cents: 10_00, date: Date.current) create(:bank, routing_number: "110000000", name: "Bank of America") expect(self.payout_period_data(user, payment)[:is_user_payable]).to eq(nil) expect(self.payout_period_data(user, payment)[:displayable_payout_period_range]).to eq("Activity up to February 28th, 2015") expect(self.payout_period_data(user, payment)[:payout_date_formatted]).to eq("March 1st, 2015") expect(self.payout_period_data(user, payment)[:payout_currency]).to eq(Currency::USD) expect(self.payout_period_data(user, payment)[:payout_cents]).to eq(1000) expect(self.payout_period_data(user, payment)[:payout_displayed_amount]).to eq("$10") expect(self.payout_period_data(user, payment)[:bank_number]).to eq("110000000") expect(self.payout_period_data(user, payment)[:account_number]).to eq("******1234") expect(self.payout_period_data(user, payment)[:bank_account_type]).to eq("ACH") expect(self.payout_period_data(user, payment)[:bank_name]).to eq("Bank of America") expect(self.payout_period_data(user, payment)[:arrival_date]).to eq(1.week.ago.strftime("%B #{1.week.ago.day.ordinalize}, %Y")) end end it "shows user's affiliate credits and fees separately", :vcr do user, seller, affiliate_user = 3.times.map { create(:singaporean_user_with_compliance_info) } product = create(:product, user:, price_cents: 20_000) affiliate_product = create(:product, user: seller, price_cents: 50_000) affiliate_owed = create(:direct_affiliate, affiliate_user:, seller: user, affiliate_basis_points: 1500) affiliate_owed.products << product affiliate = create(:direct_affiliate, affiliate_user: user, seller:, affiliate_basis_points: 1000, products: [affiliate_product]) product_purchase = create(:purchase_in_progress, link: product, affiliate: affiliate_owed) affiliate_purchase = create(:purchase_in_progress, link: affiliate_product, affiliate:) [product_purchase, affiliate_purchase].each do |purchase| purchase.process! purchase.update_balance_and_mark_successful! end travel_to(1.week.from_now) do Payouts.create_payments_for_balances_up_to_date_for_users(Date.current - 1, PayoutProcessorType::PAYPAL, User.holding_balance) period_data = self.payout_period_data(user, user.payments.last) expect(period_data[:affiliate_credits_cents]).to eq(43_47) expect(period_data[:affiliate_fees_cents]).to eq(26_01) end end it "includes the payout fee for instant payout under the fees head" do travel_to(Time.find_zone("UTC").local(2015, 3, 1)) do user = create(:user) payment = create(:payment, user:, amount_cents: 10_00, arrival_date: 1.week.ago.to_i, payout_type: Payouts::PAYOUT_TYPE_INSTANT, gumroad_fee_cents: 30) balance = create(:balance, user:, amount_cents: 10_00, date: 30.days.ago, state: "paid") purchase = create(:purchase, link: create(:product, user:), purchase_success_balance_id: balance.id) payment.balances << balance bank_account = create(:ach_account) bank_account.payments << payment create(:bank, routing_number: "110000000", name: "Bank of America") payout_data = self.payout_period_data(user, payment) expect(payout_data[:is_user_payable]).to eq(nil) expect(payout_data[:displayable_payout_period_range]).to eq("Activity up to February 28th, 2015") expect(payout_data[:payout_date_formatted]).to eq("March 1st, 2015") expect(payout_data[:payout_currency]).to eq(Currency::USD) expect(payout_data[:payout_cents]).to eq(1000) expect(payout_data[:payout_displayed_amount]).to eq("$10") expect(payout_data[:bank_number]).to eq("110000000") expect(payout_data[:account_number]).to eq("******1234") expect(payout_data[:bank_account_type]).to eq("ACH") expect(payout_data[:bank_name]).to eq("Bank of America") expect(payout_data[:arrival_date]).to eq(1.week.ago.strftime("%B #{1.week.ago.day.ordinalize}, %Y")) expect(purchase.fee_cents).to eq(93) expect(payment.gumroad_fee_cents).to eq(30) expect(payout_data[:fees_cents]).to eq(123) # 93 cents fee on purchase + 30 cents on instant payout expect(payout_data[:direct_fees_cents]).to eq(123) # 93 cents fee on purchase + 30 cents on instant payout expect(payout_data[:discover_fees_cents]).to eq(0) end end it "shows the payout fee for instant payout under the direct fees head even if there is no other fee" do travel_to(Time.find_zone("UTC").local(2015, 3, 1)) do user = create(:user) payment = create(:payment, user:, amount_cents: 10_00, arrival_date: 1.week.ago.to_i, payout_type: Payouts::PAYOUT_TYPE_INSTANT, gumroad_fee_cents: 30) balance = create(:balance, user:, amount_cents: 10_00, date: 30.days.ago, state: "paid") payment.balances << balance bank_account = create(:ach_account) bank_account.payments << payment create(:bank, routing_number: "110000000", name: "Bank of America") payout_data = self.payout_period_data(user, payment) expect(payout_data[:is_user_payable]).to eq(nil) expect(payout_data[:displayable_payout_period_range]).to eq("Activity up to February 28th, 2015") expect(payout_data[:payout_date_formatted]).to eq("March 1st, 2015") expect(payout_data[:payout_currency]).to eq(Currency::USD) expect(payout_data[:payout_cents]).to eq(1000) expect(payout_data[:payout_displayed_amount]).to eq("$10") expect(payout_data[:bank_number]).to eq("110000000") expect(payout_data[:account_number]).to eq("******1234") expect(payout_data[:bank_account_type]).to eq("ACH") expect(payout_data[:bank_name]).to eq("Bank of America") expect(payout_data[:arrival_date]).to eq(1.week.ago.strftime("%B #{1.week.ago.day.ordinalize}, %Y")) expect(payment.gumroad_fee_cents).to eq(30) expect(payout_data[:fees_cents]).to eq(30) # 30 cents on instant payout expect(payout_data[:direct_fees_cents]).to eq(30) # 30 cents on instant payout expect(payout_data[:discover_fees_cents]).to eq(0) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/button_helper_spec.rb
spec/helpers/button_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe ButtonHelper do describe "#navigation_helper" do it "renders button without options" do output = navigation_button("New product", new_product_path) expect(output).to eq('<a class="button accent" href="/products/new">New product</a>') end it "renders button with options" do output = navigation_button("New product", new_product_path, class: "one two", title: "Give me a title", disabled: true, color: "success") expect(output).to eq('<a title="Give me a title" class="one two button success" inert="inert" href="/products/new">New product</a>') end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/affiliates_helper_spec.rb
spec/helpers/affiliates_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe AffiliatesHelper do describe "#affiliate_products_select_data" do let(:product) { create(:product) } let(:products) do [create(:product), product, create(:product)] end let(:direct_affiliate) { create(:direct_affiliate, products: [product]) } it "returns the ids of selected tags and the list of all tags" do tag_ids, tag_list = affiliate_products_select_data(direct_affiliate, products) expect(tag_ids).to eq([product.external_id]) expect(tag_list).to eq(products.map { |product| { id: product.external_id, label: product.name } }) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/preorder_helper_spec.rb
spec/helpers/preorder_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe PreorderHelper do describe "formatter release time in the seller's timezone" do it "returns the proper date based on the timezone" do release_at = DateTime.parse("Aug 3rd 2018 11AM") seller_timezone = "Pacific Time (US & Canada)" expect(helper.displayable_release_at_date(release_at, seller_timezone)).to eq "August 3, 2018" end it "returns the previous day" do release_at = DateTime.parse("Aug 3rd 2018 3AM") seller_timezone = "Pacific Time (US & Canada)" expect(helper.displayable_release_at_date(release_at, seller_timezone)).to eq "August 2, 2018" # Aug 3rd at 3AM UTC is actually Aug 2nd in PDT end it "returns the proper time based on the timezone" do release_at = DateTime.parse("Aug 3rd 2018 11AM") seller_timezone = "Pacific Time (US & Canada)" expect(helper.displayable_release_at_time(release_at, seller_timezone)).to eq " 4AM" # 11AM UTC is 4AM PDT; the space before 4 is the result of %l end it "returns the proper time based on the timezone" do release_at = DateTime.parse("Dec 3rd 2018 7AM") seller_timezone = "Pacific Time (US & Canada)" expect(helper.displayable_release_at_time(release_at, seller_timezone)).to eq "11PM" # 7AM UTC is 11PM PST end it "returns the proper date and time based on the timezone" do release_at = DateTime.parse("Dec 3rd 2018 7AM") seller_timezone = "Pacific Time (US & Canada)" expect(helper.displayable_release_at_date_and_time(release_at, seller_timezone)).to eq "December 2nd, 11PM PST" end it "returns the proper date and time based on the timezone (DST)" do release_at = DateTime.parse("Aug 3rd 2018 5AM") seller_timezone = "Pacific Time (US & Canada)" expect(helper.displayable_release_at_date_and_time(release_at, seller_timezone)).to eq "August 2nd, 10PM PDT" end it "includes the minute in displayable_release_at_date_and_time if it's not 0" do release_at = DateTime.parse("Dec 3rd 2018 7:12AM") seller_timezone = "Pacific Time (US & Canada)" expect(helper.displayable_release_at_date_and_time(release_at, seller_timezone)).to eq "December 2nd, 11:12PM PST" end it "includes the minute if it's not 0" do release_at = DateTime.parse("Dec 3rd 2018 7:12AM") seller_timezone = "Pacific Time (US & Canada)" expect(helper.displayable_release_at_time(release_at, seller_timezone)).to eq "11:12PM" end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/admin_helper_spec.rb
spec/helpers/admin_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe AdminHelper do include ApplicationHelper describe "markdown" do it "changes a string of plain text to the exact same, with paragraph tags and linebreak" do input = "To be, or not to be, that is the question" output = markdown(input) expect("<p>#{input}</p>\n").to eq(output) end it "handles headers appropriately" do input = "#Act I\n##Scene i" output = markdown(input) expect(output).to include("<h1>Act I</h1>") expect(output).to include("<h2>Scene i</h2>") end it "strips user included html (prevent xss)" do input = "<script>alert(Your ass has been p0wned)</script>" output = markdown(input) expect(output).to eq("<p>alert(Your ass has been p0wned)</p>\n") end end describe "#product_type_label" do subject(:product_type) { product_type_label(product) } context "when it is a digital product" do let(:product) { create(:product_with_pdf_file) } it { is_expected.to eq("Product") } end context "when it is a legacy subscription product" do let(:product) { create(:subscription_product) } it { is_expected.to eq("Subscription") } end context "when it is a membership product" do let(:product) { create(:membership_product) } it { is_expected.to eq("Membership") } end end describe "#link_to_processor" do let(:charge_processor_id) { "dummy_charge_processor_id" } let(:charge_id) { "dummy_charge_id" } let(:charged_using_gumroad_account) { true } let(:transaction_url) { "https://example.com" } it "returns nil if charge_id is nil" do result = link_to_processor(charge_processor_id, nil, target: "_blank") expect(result).to be(nil) end it "returns a link if transaction url present" do allow(ChargeProcessor).to receive(:transaction_url_for_admin).with(charge_processor_id, charge_id, charged_using_gumroad_account).and_return(transaction_url) result = link_to_processor(charge_processor_id, charge_id, charged_using_gumroad_account, target: "_blank") expect(result).to eq link_to(charge_id, transaction_url, target: "_blank") end it "returns charge id without link if transaction url is not present" do allow(ChargeProcessor).to receive(:transaction_url_for_admin).with(charge_processor_id, charge_id, charged_using_gumroad_account).and_return(nil) result = link_to_processor(charge_processor_id, charge_id, charged_using_gumroad_account, target: "_blank") expect(result).to eq(charge_id) end end describe "#format_datetime_with_relative_tooltip" do it "returns placeholder when value is nil" do result = format_datetime_with_relative_tooltip(nil, placeholder: "Nope") expect(result).to eq("Nope") end it "returns exact date with relative time in tooltip for future dates" do datetime = DateTime.parse("2022-02-22 10:00:01") travel_to(datetime) do puts Time.current result = format_datetime_with_relative_tooltip(1.day.from_now) expect(result).to eq('<span title="1 day from now">Feb 23, 2022 at 10:00 AM UTC</span>') end end it "returns exact date with relative time in tooltip for past dates" do datetime = DateTime.parse("2022-02-22 10:00:01") travel_to(datetime) do puts Time.current result = format_datetime_with_relative_tooltip(1.day.ago) expect(result).to eq('<span title="1 day ago">Feb 21, 2022 at 10:00 AM UTC</span>') end end end describe "#with_tooltip" do it "returns element with tooltip" do result = with_tooltip(tip: "Tooltip info goes here", position: "s") { "I have a tooltip!" } expect(result).to include("I have a tooltip!") expect(result).to include("Tooltip info goes here".html_safe) end end describe "#blocked_email_tooltip" do let(:email) { "john@example.com" } let!(:user) { create(:user, email:) } let!(:email_blocked_object) { BlockedObject.block!(:email, email, nil) } let!(:email_domain_blocked_object) { BlockedObject.block!(:email_domain, Mail::Address.new(email).domain, nil) } it "includes email and email domain tooltip information" do result = blocked_email_tooltip(user) expect(result).to include("Email blocked") expect(result).to include("example.com blocked") end context "with unblocked email domain" do before do email_blocked_object.unblock! end it "includes email information" do result = blocked_email_tooltip(user) expect(result).to_not include("Email blocked") expect(result).to include("example.com blocked") end end context "with unblocked email domain" do before do email_domain_blocked_object.unblock! end it "includes email information" do result = blocked_email_tooltip(user) expect(result).to include("Email blocked") expect(result).to_not include("example.com blocked") end end context "with both email and email domain unblocked" do before do email_blocked_object.unblock! email_domain_blocked_object.unblock! end it "returns nil" do result = blocked_email_tooltip(user) expect(result).to be(nil) end end end describe "#current_user_props" do let(:admin) { create(:admin_user, username: "gumroadian") } let(:seller) { create(:named_seller) } it "returns the current user props" do expect(current_user_props(admin, seller)).to eq( { name: "gumroadian", avatar_url: admin.avatar_url, impersonated_user: { name: "Seller", avatar_url: seller.avatar_url, }, } ) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/social_share_url_helper_spec.rb
spec/helpers/social_share_url_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe SocialShareUrlHelper do describe "#twitter_url" do it "generates twitter share url" do twitter_url = "https://twitter.com/intent/tweet?text=You+%26+I:%20https://example.com" expect(helper.twitter_url("https://example.com", "You & I")).to eq twitter_url end end describe "#facebook_url" do context "when text is present" do it "generates facebook share url with text" do facebook_url = "https://www.facebook.com/sharer/sharer.php?u=https://example.com&quote=You+%2A+I" expect(helper.facebook_url("https://example.com", "You * I")).to eq facebook_url end end context "when text is not present" do it "generates facebook share url without text" do facebook_url = "https://www.facebook.com/sharer/sharer.php?u=https://example.com" expect(helper.facebook_url("https://example.com")).to eq facebook_url end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/icon_helper_spec.rb
spec/helpers/icon_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe IconHelper do describe "#icon" do it "renders icon without options" do output = icon("solid-search") expect(output).to eq('<span class="icon icon-solid-search"></span>') end it "renders icon with options" do output = icon("solid-search", class: "warning", title: "Search") expect(output).to eq('<span class="icon icon-solid-search warning" title="Search"></span>') end end describe "#icon_yes" do it "renders the icon" do expect(icon_yes).to eq('<span aria-label="Yes" style="color: rgb(var(--success))" class="icon icon-solid-check-circle"></span>') end end describe "#icon_no" do it "renders the icon" do expect(icon_no).to eq('<span aria-label="No" style="color: rgb(var(--danger))" class="icon icon-x-circle-fill"></span>') end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/users_helper_spec.rb
spec/helpers/users_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe UsersHelper do describe "#allowed_avatar_extensions" do it "returns supported profile picture extensions separated by comma" do extensions = User::ALLOWED_AVATAR_EXTENSIONS.map { |extension| ".#{extension}" }.join(",") expect(helper.allowed_avatar_extensions).to eq extensions end end describe "#is_third_party_analytics_enabled?" do let(:seller) { create(:named_seller) } let(:logged_in_seller) { create(:user) } context "without seller known" do it "returns 'true' by default" do expect( helper.is_third_party_analytics_enabled?(seller: nil, logged_in_seller: nil) ).to eq true end end context "with seller known" do context "when environment is production or staging" do before do allow(Rails.env).to receive(:production?).and_return(true) end context "with seller param" do context "with logged_in_seller param and signed in" do before do sign_in logged_in_seller logged_in_seller.disable_third_party_analytics = true end it "uses seller's preference" do seller.disable_third_party_analytics = false expect( helper.is_third_party_analytics_enabled?(seller:, logged_in_seller:) ).to eq true seller.disable_third_party_analytics = true expect( helper.is_third_party_analytics_enabled?(seller:, logged_in_seller:) ).to eq false end end context "without logged_in_seller param" do it "uses seller's preference" do seller.disable_third_party_analytics = false expect( helper.is_third_party_analytics_enabled?(seller:, logged_in_seller: nil) ).to eq true seller.disable_third_party_analytics = true expect( helper.is_third_party_analytics_enabled?(seller:, logged_in_seller: nil) ).to eq false end end end context "without seller param" do context "with logged_in_seller param" do context "with logged_in_seller not signed in" do it "ignores logged_in_seller param and returns 'true' by default" do logged_in_seller.disable_third_party_analytics = false expect( helper.is_third_party_analytics_enabled?(seller: nil, logged_in_seller: nil) ).to eq true logged_in_seller.disable_third_party_analytics = true expect( helper.is_third_party_analytics_enabled?(seller: nil, logged_in_seller: nil) ).to eq true end end context "with logged_in_seller signed in" do before do sign_in logged_in_seller end it "uses logged_in_seller's preference" do logged_in_seller.disable_third_party_analytics = false expect( helper.is_third_party_analytics_enabled?(seller: nil, logged_in_seller:) ).to eq true logged_in_seller.disable_third_party_analytics = true expect( helper.is_third_party_analytics_enabled?(seller: nil, logged_in_seller:) ).to eq false end end end end end context "when environment is not production or staging" do context "with seller param" do it "ignores seller param and returns 'false'" do seller.disable_third_party_analytics = false expect( helper.is_third_party_analytics_enabled?(seller:, logged_in_seller: nil) ).to eq false seller.disable_third_party_analytics = false expect( helper.is_third_party_analytics_enabled?(seller:, logged_in_seller: nil) ).to eq false end end end end end describe "#signed_in_user_home" do before do @user = create(:user) end context "when next_url is not present" do it "returns dashboard path by default" do expect(signed_in_user_home(@user)).to eq Rails.application.routes.url_helpers.dashboard_path end it "returns library path if not a seller and there are successful purchases" do create(:purchase, purchaser_id: @user.id) expect(signed_in_user_home(@user)).to eq Rails.application.routes.url_helpers.library_path end end context "when next_url is present" do it "returns next_url" do expect(signed_in_user_home(@user, next_url: "/sample")).to eq "/sample" end end context "when include_host is present" do it "returns library path with host when is_buyer? returns true" do allow(@user).to receive(:is_buyer?).and_return(true) expect(signed_in_user_home(@user, include_host: true)).to eq Rails.application.routes.url_helpers.library_url(host: UrlService.domain_with_protocol) end it "returns dashboard path with host by default" do expect(signed_in_user_home(@user, include_host: true)).to eq Rails.application.routes.url_helpers.dashboard_url(host: UrlService.domain_with_protocol) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/infos_helper_spec.rb
spec/helpers/infos_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe InfosHelper do describe "#pagelength_displayable" do let(:pagelength) { 100 } context "when filetype is 'epub'" do it "returns a string indicating the number of sections" do allow(helper).to receive(:pagelength).and_return(pagelength) allow(helper).to receive(:epub?).and_return(true) expect(helper.pagelength_displayable).to eq("100 sections") end end context "when filetype is not 'epub'" do it "returns a string indicating the number of pages" do allow(helper).to receive(:pagelength).and_return(pagelength) allow(helper).to receive(:epub?).and_return(false) expect(helper.pagelength_displayable).to eq("100 pages") end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/application_helper_spec.rb
spec/helpers/application_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe ApplicationHelper do describe "#current_user_props" do let(:admin) { create(:admin_user, username: "gumroadian") } let(:seller) { create(:named_seller) } it "returns the current user props" do expect(current_user_props(admin, seller)).to eq( { name: "gumroadian", avatar_url: admin.avatar_url, impersonated_user: { name: "Seller", avatar_url: seller.avatar_url, }, } ) end end describe "#number_to_si" do context "with numbers < 1000" do it "returns the number as a string" do expect(helper.number_to_si(0)).to eq "0" expect(helper.number_to_si(123)).to eq "123" expect(helper.number_to_si(999)).to eq "999" end end context "with numbers >= 1000 and < 1000000" do it "uses a 'K' suffix and displays one decimal point, if applicable" do expect(helper.number_to_si(1000)).to eq "1K" expect(helper.number_to_si(1100)).to eq "1.1K" expect(helper.number_to_si(10000)).to eq "10K" expect(helper.number_to_si(10100)).to eq "10.1K" expect(helper.number_to_si(10010)).to eq "10K" end end context "with numbers >= 1000000 and < 1000000000" do it "uses a 'M' suffix and displays one decimal point, if applicable" do expect(helper.number_to_si(1000000)).to eq "1M" expect(helper.number_to_si(1002000)).to eq "1M" expect(helper.number_to_si(1200000)).to eq "1.2M" end end it "does not round up" do expect(helper.number_to_si(99999)).to eq "99.9K" expect(helper.number_to_si(999999)).to eq "999.9K" expect(helper.number_to_si(9999999)).to eq "9.9M" end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/products_helper_spec.rb
spec/helpers/products_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe ProductsHelper do describe "#view_content_button_text" do it "shows the custom button text when available" do product = create(:product) product.custom_view_content_button_text = "Custom Text" product.save! expect(product.custom_view_content_button_text).not_to be_nil expect(helper.view_content_button_text(product)).to eq "Custom Text" end it "shows the correct default button text" do product = create(:product) expect(product.custom_view_content_button_text).to be_nil expect(helper.view_content_button_text(product)).to eq "View content" end end describe "#cdn_url_for" do before :each do filename = "kFDzu.png" @product = create(:product, preview: fixture_file_upload(filename, "image/png")) stub_const("CDN_URL_MAP", "#{AWS_S3_ENDPOINT}/gumroad/" => "https://asset.host.example.com/res/gumroad/", "#{AWS_S3_ENDPOINT}/gumroad-staging/" => "https://asset.host.example.com/res/gumroad-staging/", "#{AWS_S3_ENDPOINT}/#{S3_BUCKET}/" => "https://asset.host.example.com/res/gumroad-specs/") end it "returns correct cloudfront url for gumroad-specs bucket" do expect(cdn_url_for(@product.preview_url)).to match("https://asset.host.example.com/res/gumroad-specs/#{@product.preview.retina_variant.key}") end it "returns correct cloudfront url for gumroad-staging bucket" do expect(@product).to receive(:preview_url).and_return("#{AWS_S3_ENDPOINT}/gumroad-staging/#{@product.preview.file.key}") expect(cdn_url_for(@product.preview_url)).to eq("https://asset.host.example.com/res/gumroad-staging/#{@product.preview.file.key}") end it "returns unchanged s3 url for other bucket" do url = "#{AWS_S3_ENDPOINT}/gumroad_other/#{@product.preview.file.key}" expect(@product).to receive(:preview_url).and_return(url) expect(cdn_url_for(@product.preview_url)).to eq(url) end it "returns embed url" do expect(OEmbedFinder).to receive(:embeddable_from_url).and_return(html: "<iframe src=\"https://madeup.url\"></iframe>", info: { "thumbnail_url" => "https://madeup.thumbnail.url", "width" => "100", "height" => "100" }) @product.asset_previews.each(&:mark_deleted!) @product.preview_url = "https://www.youtube.com/watch?v=ljPFZrRD3J8" @product.save! expect(cdn_url_for(@product.preview_url)).to eq "https://madeup.url" end it "returns empty url" do expect(@product).to receive(:preview_url).and_return("") expect(cdn_url_for(@product.preview_url)).to eq("") end end describe "#sort_and_paginate_products" do let!(:seller) { create(:recommendable_user) } let!(:product1) { create(:product, user: seller, name: "p1", price_cents: 100, display_product_reviews: true, taxonomy: create(:taxonomy), purchase_disabled_at: Time.current, updated_at: Time.current) } let!(:product2) { create(:product, user: seller, name: "p2", price_cents: 300, display_product_reviews: false, taxonomy: create(:taxonomy), updated_at: Time.current + 1) } let!(:product3) { create(:subscription_product, user: seller, name: "p3", price_cents: 200, display_product_reviews: false, purchase_disabled_at: Time.current, updated_at: Time.current - 1) } let!(:product4) { create(:subscription_product, user: seller, name: "p4", price_cents: 600, display_product_reviews: true, updated_at: Time.current - 2) } before do index_model_records(Link) end it "properly sorts and paginates products" do pagination, products = sort_and_paginate_products(key: "name", direction: "asc", page: 1, collection: seller.products, per_page: 2, user_id: seller.id) expect(pagination).to eq({ page: 1, pages: 2 }) expect(products).to eq([product1, product2]) pagination, products = sort_and_paginate_products(key: "name", direction: "asc", page: 2, collection: seller.products, per_page: 2, user_id: seller.id) expect(pagination).to eq({ page: 2, pages: 2 }) expect(products).to eq([product3, product4]) pagination, products = sort_and_paginate_products(key: "display_price_cents", direction: "asc", page: 1, collection: seller.products, per_page: 2, user_id: seller.id) expect(pagination).to eq({ page: 1, pages: 2 }) expect(products.map(&:name)).to eq([product1, product3].map(&:name)) pagination, products = sort_and_paginate_products(key: "display_price_cents", direction: "asc", page: 2, collection: seller.products, per_page: 2, user_id: seller.id) expect(pagination).to eq({ page: 2, pages: 2 }) expect(products.map(&:name)).to eq([product2, product4].map(&:name)) end end describe "#url_for_product_page" do let(:product) { create(:product) } shared_examples "long url" do it "returns the long_url" do expect(helper.url_for_product_page(product, request: @request, recommended_by: "test")).to eq product.long_url(recommended_by: "test") end context "when offer_code is present" do it "returns the long_url with the offer_code" do expect(helper.url_for_product_page(product, request: @request, recommended_by: "test", offer_code: "BLACKFRIDAY2025")).to eq product.long_url(recommended_by: "test", code: "BLACKFRIDAY2025") end end end shared_examples "relative url" do context "when recommended_by is present" do it "returns product link request's host and port, and includes recommended_by param" do expect(helper.url_for_product_page(product, request: @request, recommended_by: "test")).to eq "http://#{@request.host_with_port}/l/#{product.general_permalink}?recommended_by=test" end end context "when recommended_by is not present" do it "returns product link with request's host and port" do expect(helper.url_for_product_page(product, request: @request, recommended_by: "")).to eq "http://#{@request.host_with_port}/l/#{product.general_permalink}" end end end context "when no request exists" do before do allow(helper).to receive(:request).and_return(nil) end include_examples "long url" end context "when host is DOMAIN" do before do stub_const("DOMAIN", "127.0.0.1") @request.host = DOMAIN end include_examples "long url" end context "when host is VALID_DISCOVER_REQUEST_HOST" do before do @request.host = VALID_DISCOVER_REQUEST_HOST end include_examples "long url" end context "when on the user's subdomain" do before do @request.host = product.user.subdomain end include_examples "relative url" end context "when on the user's custom domain" do before do @request.host = "example.com" create(:custom_domain, user: product.user, domain: "example.com") end include_examples "relative url" end context "when on a different user's subdomain" do let(:other_user) { create(:user) } before do @request.host = other_user.subdomain end include_examples "long url" end context "when on a different user's custom domain" do before do @request.host = "example.com" create(:custom_domain, user: create(:user), domain: "example.com") end include_examples "long url" end end describe "#variant_names_displayable" do context "when no names are provided" do it "returns nil" do expect(helper.variant_names_displayable([])).to eq nil end end context "when only Untitled is provided" do it "returns nil" do expect(helper.variant_names_displayable([])).to eq nil end end context "when provided wit names" do it "returns the names joined" do expect(helper.variant_names_displayable(%w[name1 name2])).to eq "name1, name2" end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/signed_url_helper_spec.rb
spec/helpers/signed_url_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe SignedUrlHelper do let(:pdf_path) { "attachments/23b2d41ac63a40b5afa1a99bf38a0982/original/nyt.pdf" } let(:pdf_uri) { URI.parse("#{AWS_S3_ENDPOINT}/#{S3_BUCKET}/#{pdf_path}").to_s } let(:s3_object_double) { double(:s3_object) } let!(:file) { create(:product_file, url: pdf_uri.to_s) } before do s3_double = double(:s3_client) s3_res_double = double(:s3_resource) response_double = double(:response) bucket_double = double(:bucket) allow(Aws::S3::Client).to receive(:new).times.and_return(s3_double) allow(s3_double).to receive(:list_objects).times.and_return([response_double]) allow(response_double).to receive_message_chain(:contents, :map).and_return([pdf_path]) allow(Aws::S3::Resource).to receive(:new).times.and_return(s3_res_double) allow(s3_res_double).to receive(:bucket).times.and_return(bucket_double) allow(bucket_double).to receive(:object).times.and_return(s3_object_double) allow(s3_object_double).to receive(:public_url).times.and_return(pdf_uri) end context "when using minio" do it "returns a minio presigned url" do presigned_url = "#{AWS_S3_ENDPOINT}/#{S3_BUCKET}/#{pdf_path}?X-Amz-Signature=test" allow(s3_object_double).to receive(:content_length).and_return(1000) expect(s3_object_double).to receive(:presigned_url).with( :get, expires_in: 10.minutes.to_i, response_content_disposition: "attachment; filename=\"#{file.s3_filename}\"" ).and_return(presigned_url) expect(signed_download_url_for_s3_key_and_filename(file.s3_key, file.s3_filename)).to eq(presigned_url) end end context "when not using minio" do before do stub_const("USING_MINIO", false) stub_const("CLOUDFRONT_DOWNLOAD_DISTRIBUTION_URL", "https://cloudfront.net") stub_const("FILE_DOWNLOAD_DISTRIBUTION_URL", "https://staging-files.gumroad.com") end it "returns the correct validation duration" do expect(signed_url_validity_time_for_file_size(10)).to eq SignedUrlHelper::SIGNED_S3_URL_VALID_FOR_MINIMUM expect(signed_url_validity_time_for_file_size(1_000_000_000)).to eq SignedUrlHelper::SIGNED_S3_URL_VALID_FOR_MAXIMUM expect(signed_url_validity_time_for_file_size(200_000_000)).to eq((200_000_000 / 1_024 / 50).seconds) end it "returns a CloudFront read url with the proper cache_group paramter if file size >= 8GB" do allow(s3_object_double).to receive(:content_length).and_return(8_000_000_000) expect(signed_download_url_for_s3_key_and_filename(file.s3_key, file.s3_filename, cache_group: "read")) .to match(/cloudfront\.net.*cache_group=read/) end it "returns a Cloudflare read url with the proper cache_group paramter if file size < 8GB" do allow(s3_object_double).to receive(:content_length).and_return(1_000_000_000) expect(signed_download_url_for_s3_key_and_filename(file.s3_key, file.s3_filename, cache_group: "read")) .to match(/staging-files\.gumroad\.com.*cache_group=read.*verify=/) end it "contains the cache_key parameter in the query string for files with specific extensions" do allow(s3_object_double).to receive(:content_length).and_return(1_000_000_000) expect(signed_download_url_for_s3_key_and_filename(file.s3_key, file.s3_filename)) .to_not include("cache_key=caIWHGT4Qhqo6KoxDMNXwQ") %w(jpg jpeg png epub brushset scrivtemplate zip).each do |extension| file_path = "#{AWS_S3_ENDPOINT}/#{S3_BUCKET}/attachments/23b2d41ac63a40b5afa1a99bf38a0982/original/nyt.#{extension}" file = create(:product_file, url: URI.parse(file_path).to_s) expect(signed_download_url_for_s3_key_and_filename(file.s3_key, file.s3_filename)) .to match(/staging-files\.gumroad\.com.*cache_key=caIWHGT4Qhqo6KoxDMNXwQ.*/) end end it "raises a descriptive exception if the S3 object doesn't exist" do RSpec::Mocks.space.proxy_for(Aws::S3::Client).reset RSpec::Mocks.space.proxy_for(Aws::S3::Resource).reset expect do signed_download_url_for_s3_key_and_filename("attachments/missing.txt", "filename") end.to raise_error(Aws::S3::Errors::NotFound, /Key = attachments\/missing.txt/) end describe "#file_needs_cache_key?" do context "when cache key is needed" do it "returns true" do expect(file_needs_cache_key?("file.jpg")).to be_truthy end end context "when cache key is not needed" do it "returns false" do expect(file_needs_cache_key?("file.mp3")).to be_falsey end end end describe "#cf_worker_cache_extensions_and_keys" do it "returns a hash with extensions and cache keys" do expect(cf_worker_cache_extensions_and_keys).to be_a(Hash) expect(cf_worker_cache_extensions_and_keys[".jpg"]).to eq "caIWHGT4Qhqo6KoxDMNXwQ" end end describe "#cf_cache_key" do context "when cache key is configured for the extension" do it "returns the cache key" do expect(cf_cache_key("filename.zip")).to eq "caIWHGT4Qhqo6KoxDMNXwQ" end end context "when cache key is not configured for the extension" do it "returns nil" do expect(cf_cache_key("filename.mp3")).to be_nil end end describe "set keys from Redis" do before do Rails.cache.clear end it "Overrides cache key with the key from Redis" do expect(cf_worker_cache_extensions_and_keys[".jpg"]).to eq "caIWHGT4Qhqo6KoxDMNXwQ" $redis.hset(RedisKey.cf_cache_invalidated_extensions_and_cache_keys, ".jpg", Digest::SHA1.hexdigest("2020-10-09")) Rails.cache.delete("set_cf_worker_cache_keys_from_redis") expect(cf_worker_cache_extensions_and_keys[".jpg"]).to eq Digest::SHA1.hexdigest("2020-10-09") end it "uses Rails.cache to read the value from Redis only once as long as the Rails cache is present" do expect(cf_worker_cache_extensions_and_keys[".mp3"]).to be_nil $redis.hset(RedisKey.cf_cache_invalidated_extensions_and_cache_keys, ".mp4", Digest::SHA1.hexdigest("2020-10-09")) expect(cf_worker_cache_extensions_and_keys[".mp3"]).to be_nil end end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/admin/purchase_helper_spec.rb
spec/helpers/admin/purchase_helper_spec.rb
# frozen_string_literal: true require "spec_helper" describe Admin::PurchaseHelper, type: :helper do let(:purchase) { create(:purchase) } let(:chargebacked_purchase) { create(:purchase, chargeback_date: Time.current) } describe "#purchase_states" do context "when purchase is successful" do it "returns capitalized purchase state" do expect(helper.purchase_states(purchase)).to eq(["Successful"]) end end context "when purchase is refunded" do before { allow(purchase).to receive(:stripe_refunded?).and_return(true) } it "includes refunded status" do expect(helper.purchase_states(purchase)).to eq(["Successful", "(refunded)"]) end end context "when purchase is partially refunded" do before { allow(purchase).to receive(:stripe_partially_refunded?).and_return(true) } it "includes partially refunded status" do expect(helper.purchase_states(purchase)).to eq(["Successful", "(partially refunded)"]) end end context "when purchase has chargeback" do before { allow(purchase).to receive(:chargedback_not_reversed?).and_return(true) } it "includes chargeback status" do expect(helper.purchase_states(purchase)).to eq(["Successful", "(chargeback)"]) end end context "when purchase has reversed chargeback" do before { allow(purchase).to receive(:chargeback_reversed?).and_return(true) } it "includes chargeback reversed status" do expect(helper.purchase_states(purchase)).to eq(["Successful", "(chargeback reversed)"]) end end context "when purchase has multiple statuses" do before do allow(purchase).to receive(:stripe_refunded?).and_return(true) allow(purchase).to receive(:chargedback_not_reversed?).and_return(true) end it "includes all relevant statuses" do expect(helper.purchase_states(purchase)).to eq(["Successful", "(refunded)", "(chargeback)"]) end end end describe "#purchase_error_code" do context "when purchase is not failed" do it "returns nil" do expect(helper.purchase_error_code(purchase)).to be_nil end end context "when purchase is failed" do before do allow(purchase).to receive(:failed?).and_return(true) allow(purchase).to receive(:formatted_error_code).and_return("card_declined") allow(purchase).to receive(:find_past_chargebacked_purchases).and_return([]) end it "returns formatted error code" do expect(helper.purchase_error_code(purchase)).to eq("(card_declined)") end context "when error code is buyer charged back" do context "when there are past chargebacked purchases" do before do allow(purchase).to receive(:error_code).and_return("buyer_has_charged_back") allow(purchase).to receive(:find_past_chargebacked_purchases).and_return([chargebacked_purchase]) end it "returns linked error code" do result = helper.purchase_error_code(purchase) expect(result).to include("card_declined") expect(result).to include(admin_purchase_path(chargebacked_purchase.external_id)) end end context "when there are no past chargebacked purchases" do before do allow(purchase).to receive(:error_code).and_return("buyer_has_charged_back") allow(purchase).to receive(:find_past_chargebacked_purchases).and_return([]) end it "returns unlinked error code" do result = helper.purchase_error_code(purchase) expect(result).to eq("(card_declined)") end end end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/helpers/products_helper/offer_codes_spec.rb
spec/helpers/products_helper/offer_codes_spec.rb
# frozen_string_literal: true require "spec_helper" describe ProductsHelper, "url_for_product_page with offer codes" do include Rails.application.routes.url_helpers let(:creator) { create(:user, name: "Testy", username: "testy") } let(:product) { create(:product, unique_permalink: "test", name: "hello", user: creator) } describe "when offer_code is BLACKFRIDAY2025" do context "when on user's domain" do let(:request) { instance_double(ActionDispatch::Request, host: "testy.test.gumroad.com", host_with_port: "testy.test.gumroad.com:1234", protocol: "http") } before do allow_any_instance_of(ProductsHelper).to receive(:user_by_domain).with("testy.test.gumroad.com").and_return(creator) end it "adds code parameter to URL" do url = url_for_product_page(product, request:, offer_code: "BLACKFRIDAY2025") expect(url).to include("code=BLACKFRIDAY2025") end it "includes other parameters along with code" do url = url_for_product_page( product, request:, offer_code: "BLACKFRIDAY2025", recommended_by: "discover", query: "search term" ) expect(url).to include("code=BLACKFRIDAY2025") expect(url).to include("recommended_by=discover") expect(url).to include("query=search+term") end end context "when not on user's domain" do let(:request) { instance_double(ActionDispatch::Request, host: "test.gumroad.com", host_with_port: "test.gumroad.com:1234", protocol: "http") } it "adds code parameter to URL" do url = url_for_product_page(product, request:, offer_code: "BLACKFRIDAY2025") expect(url).to include("code=BLACKFRIDAY2025") end it "includes other parameters except query" do url = url_for_product_page( product, request:, offer_code: "BLACKFRIDAY2025", recommended_by: "discover", query: "search term" ) expect(url).to include("code=BLACKFRIDAY2025") expect(url).to include("recommended_by=discover") expect(url).not_to include("query=") end end end describe "when offer_code is nil" do let(:request) { instance_double(ActionDispatch::Request, host: "test.gumroad.com", host_with_port: "test.gumroad.com:1234", protocol: "http") } it "does not add code parameter to URL" do url = url_for_product_page(product, request:, offer_code: nil) expect(url).not_to include("code=") end end describe "when offer_code is empty string" do let(:request) { instance_double(ActionDispatch::Request, host: "test.gumroad.com", host_with_port: "test.gumroad.com:1234", protocol: "http") } it "does not add code parameter to URL" do url = url_for_product_page(product, request:, offer_code: "") expect(url).not_to include("code=") end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/affiliate_request_policy_spec.rb
spec/policies/affiliate_request_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe AffiliateRequestPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :update?, :approve_all? do it "accountant access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, AffiliateRequest) end it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, AffiliateRequest) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, AffiliateRequest) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, AffiliateRequest) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, AffiliateRequest) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/audience_policy_spec.rb
spec/policies/audience_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe AudiencePolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index?, :export? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, :audience) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, :audience) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :audience) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :audience) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, :audience) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/product_review_policy_spec.rb
spec/policies/product_review_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe ProductReviewPolicy do subject { described_class } let(:seller) { create(:user) } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index? do context "reviews_page feature flag is disabled" do it "denies access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to_not permit(seller_context) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to_not permit(seller_context) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to_not permit(seller_context) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to_not permit(seller_context) end end context "reviews_page feature flag is enabled" do before { Feature.activate_user(:reviews_page, seller) } it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to_not permit(seller_context) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to_not permit(seller_context) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to_not permit(seller_context) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/wishlist_product_policy_spec.rb
spec/policies/wishlist_product_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe WishlistProductPolicy do subject { described_class } let(:seller) { create(:named_seller) } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:wishlist_product) { create(:wishlist_product, wishlist: create(:wishlist, user: seller)) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index?, :destroy? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, wishlist_product) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, wishlist_product) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to_not permit(seller_context, wishlist_product) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to_not permit(seller_context, wishlist_product) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to_not permit(seller_context, wishlist_product) end end permissions :destroy? do let(:wishlist_product) { create(:wishlist_product, wishlist: create(:wishlist, user: create(:user))) } it "denies access to another user's wishlist product" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to_not permit(seller_context, wishlist_product) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/link_policy_spec.rb
spec/policies/link_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe LinkPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Link) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, Link) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Link) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Link) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, Link) end end permissions :new?, :create?, :show?, :unpublish?, :publish?, :destroy?, :release_preorder? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Link) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, Link) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Link) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Link) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, Link) end end permissions :edit? do let(:team_member) { create(:user, is_team_member: true) } it "grants access to team member" do seller_context = SellerContext.new(user: team_member, seller: team_member) expect(subject).to permit(seller_context, Link) end end permissions :edit?, :update? do context "when product belongs to seller" do let(:product) { create(:product, user: seller) } let(:collaborating_user) { create(:collaborator, seller:, products: [product]).affiliate_user } it "grants access to a collaborator" do seller_context = SellerContext.new(user: collaborating_user, seller: collaborating_user) expect(subject).to permit(seller_context, product) end it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, product) end it "denies accountant to support" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, product) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, product) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, product) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, product) end end context "when product belongs to other user" do let(:product_of_other_user) { create(:product) } it "denies access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, product_of_other_user) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, product_of_other_user) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, product_of_other_user) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, product_of_other_user) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, product_of_other_user) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/comment_context_policy_spec.rb
spec/policies/comment_context_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe CommentContextPolicy do subject { described_class } let(:accountant_for_seller) { create(:user, username: "accountantforseller") } let(:admin_for_seller) { create(:user, username: "adminforseller") } let(:marketing_for_seller) { create(:user, username: "marketingforseller") } let(:support_for_seller) { create(:user, username: "supportforseller") } let(:seller) { create(:named_seller) } let(:buyer) { create(:user) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end it "assigns accessors" do context = SellerContext.new(user: admin_for_seller, seller:) policy = described_class.new(context, :record) expect(policy.user).to eq(admin_for_seller) expect(policy.seller).to eq(seller) expect(policy.record).to eq(:record) end shared_examples "when purchase is specified" do context "when installment is a product post" do let(:product) { create(:product) } let(:commentable) { create(:product_installment, link: product, published_at: 1.day.ago) } context "when purchased product and post's product is same" do let(:purchase) { create(:purchase, link: product, created_at: 1.second.ago) } let(:comment) { build(:comment, commentable:) } it "grants access" do expect(subject).to permit(seller_context, comment_context) end end context "when purchased product does not match with post's product" do let(:purchase) { create(:purchase, created_at: 1.second.ago) } let(:comment) { build(:comment, commentable:) } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end end end context "when post is a variant post" do let(:product) { create(:product) } let!(:variant_category) { create(:variant_category, link: product) } let!(:standard_variant) { create(:variant, variant_category:, name: "Standard") } let!(:premium_variant) { create(:variant, variant_category:, name: "Premium") } let(:commentable) { create(:variant_installment, link: product, published_at: 1.day.ago, base_variant: premium_variant) } context "when post's base variant matches with purchase's variants" do let(:purchase) { create(:purchase, link: product, variant_attributes: [premium_variant], created_at: 1.second.ago) } let(:comment) { build(:comment, commentable:) } it "grants access" do expect(subject).to permit(seller_context, comment_context) end end context "when post's base variant does not match with purchase's variants" do let(:purchase) { create(:purchase, link: product, variant_attributes: [standard_variant], created_at: 1.second.ago) } let(:comment) { build(:comment, commentable:) } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end end end context "when installment is a seller post" do let!(:product) { create(:product, user: seller) } let(:commentable) { create(:seller_installment, seller:, published_at: 1.day.ago) } context "when purchased product's creator is same as the post's creator" do let(:purchase) { create(:purchase, link: product, created_at: 1.second.ago) } let(:comment) { build(:comment, commentable:) } it "grants access" do expect(subject).to permit(seller_context, comment_context) end end context "when purchased product's creator does not match with the post's creator" do let(:another_product) { create(:product, user: create(:user)) } let(:purchase) { create(:purchase, link: another_product, created_at: 1.second.ago) } let(:comment) { build(:comment, commentable:) } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end end end end permissions :index? do let(:product) { create(:product, user: seller) } let(:commentable) { create(:published_installment, seller:, link: product) } context "without purchase" do let(:comment_context) { CommentContext.new(comment: nil, commentable:, purchase: nil) } it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, comment_context) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, comment_context) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, comment_context) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, comment_context) end context "when buyer has a purchase" do let!(:purchase) { create(:purchase, link: product, purchaser: buyer, created_at: 1.second.ago) } it "grants access to buyer" do seller_context = SellerContext.new(user: buyer, seller: buyer) expect(subject).to permit(seller_context, comment_context) end end context "when buyer does not have a purchase" do it "denies access to buyer" do seller_context = SellerContext.new(user: buyer, seller: buyer) expect(subject).not_to permit(seller_context, comment_context) end end context "without user logged in" do let(:seller_context) { SellerContext.logged_out } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end context "when post is public" do let(:commentable) { create(:published_installment, installment_type: Installment::AUDIENCE_TYPE, shown_on_profile: true) } it "grants access" do expect(subject).to permit(seller_context, comment_context) end end end end context "with purchase" do let(:seller_context) { SellerContext.logged_out } let(:comment_context) { CommentContext.new(comment: nil, commentable:, purchase:) } it_behaves_like "when purchase is specified" end end permissions :create? do context "when user is logged in" do let(:comment_context) { CommentContext.new(comment:, commentable: nil, purchase: nil) } context "when user is author of the comment's post" do let(:comment) { build(:comment, commentable: create(:published_installment, seller:)) } it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, comment_context) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, comment_context) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, comment_context) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, comment_context) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, comment_context) end end context "when the post is visible to buyer" do let(:product) { create(:product) } let!(:purchase) { create(:purchase, link: product, purchaser: buyer, created_at: 1.second.ago) } let(:comment) { build(:comment, commentable: create(:published_installment, link: product)) } let(:seller_context) { SellerContext.new(user: buyer, seller: buyer) } it "grants access" do expect(subject).to permit(seller_context, comment_context) end end context "when the post is not visible to user" do let(:comment) { build(:comment, commentable: create(:published_installment)) } let(:seller_context) { SellerContext.new(user: buyer, seller: buyer) } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end end end context "when user is not logged in" do let(:seller_context) { SellerContext.logged_out } context "with purchase" do let(:comment_context) { CommentContext.new(comment:, commentable: nil, purchase:) } it_behaves_like "when purchase is specified" end context "without purchase" do let(:comment) { build(:comment, commentable: create(:published_installment)) } let(:comment_context) { CommentContext.new(comment:, commentable: nil, purchase: nil) } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end end end end permissions :update? do context "when user is logged in" do let(:comment_context) { CommentContext.new(comment:, commentable: nil, purchase: nil) } context "when user is author of the comment" do let(:comment_author) { create(:user) } let(:comment) { create(:comment, author: comment_author) } it "grants access" do seller_context = SellerContext.new(user: comment_author, seller: comment_author) expect(subject).to permit(seller_context, comment_context) end end context "when seller is author of the comment's post" do let(:comment) { create(:comment) } before do comment.commentable.update!(seller:) end it "denies access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, comment_context) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, comment_context) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, comment_context) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, comment_context) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, comment_context) end end context "when user is neither author of the comment nor author of the comment's post" do let(:visitor) { create(:user) } let(:comment) { create(:comment) } let(:seller_context) { SellerContext.new(user: visitor, seller: visitor) } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end end end context "when purchase is specified" do let(:seller_context) { SellerContext.logged_out } let(:purchase) { create(:purchase, created_at: 1.second.ago) } let(:comment) { create(:comment, purchase:) } context "when purchase matches associated purchase" do let(:comment_context) { CommentContext.new(comment:, commentable: nil, purchase:) } it "grants access" do expect(subject).to permit(seller_context, comment_context) end end context "when comment does not match associated purchase" do let(:other_purchase) { create(:purchase, created_at: 1.second.ago) } let(:comment_context) { CommentContext.new(comment:, commentable: nil, purchase: other_purchase) } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end end end context "when both user and purchase are not specified" do let(:seller_context) { SellerContext.logged_out } let(:comment) { create(:comment) } let(:comment_context) { CommentContext.new(comment:, commentable: nil, purchase: nil) } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end end end permissions :destroy? do context "when user is logged in" do let(:comment_context) { CommentContext.new(comment:, commentable: nil, purchase: nil) } context "when user is author of the comment" do let(:user) { create(:user) } let(:comment) { create(:comment, author: user) } let(:seller_context) { SellerContext.new(user:, seller: user) } it "grants access to owner" do expect(subject).to permit(seller_context, comment_context) end end context "when seller is author of the comment's post" do let(:comment) { create(:comment) } let(:seller_context) { SellerContext.new(user: seller, seller:) } before do comment.commentable.update!(seller:) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, comment_context) end it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, comment_context) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, comment_context) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, comment_context) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, comment_context) end end context "when user is neither author of the comment nor author of the comment's post" do let(:visitor) { create(:user) } let(:comment) { create(:comment) } let(:seller_context) { SellerContext.new(user: visitor, seller: visitor) } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end end end context "when purchase is specified" do let(:seller_context) { SellerContext.logged_out } let(:purchase) { create(:purchase, created_at: 1.second.ago) } let(:comment) { create(:comment, purchase:) } context "when purchase matches associated purchase" do let(:comment_context) { CommentContext.new(comment:, commentable: nil, purchase:) } it "grants access" do expect(subject).to permit(seller_context, comment_context) end end context "when comment does not match associated purchase" do let(:other_purchase) { create(:purchase, created_at: 1.second.ago) } let(:comment_context) { CommentContext.new(comment:, commentable: nil, purchase: other_purchase) } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end end end context "when both user and purchase are not specified" do let(:seller_context) { SellerContext.logged_out } let(:comment) { create(:comment) } let(:comment_context) { CommentContext.new(comment:, commentable: nil, purchase: nil) } it "denies access" do expect(subject).not_to permit(seller_context, comment_context) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/product_review_video_policy_spec.rb
spec/policies/product_review_video_policy_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/policy_examples" describe ProductReviewVideoPolicy do subject { described_class } let(:seller) { create(:named_seller) } let(:admin_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_ADMIN, ).user end let(:support_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_SUPPORT, ).user end let(:accountant_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_ACCOUNTANT, ).user end let(:marketing_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_MARKETING, ).user end let(:purchaser) { create(:user) } let(:another_user) { create(:user) } let(:product) { create(:product, user: seller) } let(:purchase) { create(:purchase, link: product, seller:, purchaser:) } let(:product_review) { create(:product_review, purchase:) } let(:product_review_video_for_seller) do create( :product_review_video, :pending_review, product_review: product_review ) end let(:product_review_video_for_another_seller) do another_seller = create(:user) another_product = create(:product, user: another_seller) another_purchase = create(:purchase, link: another_product, seller: another_seller) another_product_review = create(:product_review, purchase: another_purchase) create( :product_review_video, product_review: another_product_review, approval_status: :pending_review ) end let(:context_seller) { seller } permissions :approve?, :reject? do context "when the video is for the seller's product review" do let(:record) { product_review_video_for_seller } it_behaves_like "an access-granting policy for roles", [ :seller, :admin_for_seller, :support_for_seller, ] it_behaves_like "an access-denying policy for roles", [ :accountant_for_seller, :marketing_for_seller, ] end context "when the video is for another seller's product review" do let(:record) { product_review_video_for_another_seller } it_behaves_like "an access-denying policy for roles", [ :seller, :admin_for_seller, :support_for_seller, :accountant_for_seller, :marketing_for_seller, ] end end permissions :stream? do context "when the video has been approved" do let(:record) { product_review_video_for_another_seller } before { record.approved! } it_behaves_like "an access-granting policy for roles", [:seller] end context "when the video has not been approved" do let(:record) { product_review_video_for_another_seller } before { record.pending_review! } it_behaves_like "an access-denying policy for roles", [:seller] end context "when the video is for the seller's product review" do let(:record) { product_review_video_for_seller } before { record.pending_review! } it_behaves_like "an access-granting policy for roles", [ :seller, :admin_for_seller, :support_for_seller, :accountant_for_seller, :marketing_for_seller, ] end context "when the video is for the purchaser's product review" do let(:context_seller) { purchaser } let(:record) { product_review_video_for_seller } before { record.pending_review! } it_behaves_like "an access-granting policy for roles", [ :purchaser, ] it_behaves_like "an access-denying policy for roles", [ :seller, ] end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/asset_preview_policy_spec.rb
spec/policies/asset_preview_policy_spec.rb
# frozen_string_literal: true require "spec_helper" # Products section # describe AssetPreviewPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :create?, :destroy? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, AssetPreview) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, AssetPreview) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, AssetPreview) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, AssetPreview) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, AssetPreview) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/call_policy_spec.rb
spec/policies/call_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe CallPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:call) { create(:call) } let!(:seller) { call.purchase.seller } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :update? do context "when the call belongs to the seller" do before do allow(call.purchase).to receive(:seller).and_return(seller) end it "grants access to seller" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, call) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, call) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, call) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, call) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, call) end end context "when the call belongs to another seller" do let!(:other_call) { create(:call) } it "denies access to seller" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, other_call) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, other_call) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, other_call) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, other_call) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, other_call) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/user_policy_spec.rb
spec/policies/user_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe UserPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :deactivate? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, seller) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to_not permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to_not permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end permissions :generate_product_details_with_ai? do context "when ai_product_generation feature is inactive" do it "denies access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, seller) end end context "when ai_product_generation feature is active" do before do Feature.activate_user(:ai_product_generation, seller) end context "when seller is not confirmed" do before do seller.update!(confirmed_at: nil) end it "denies access" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, seller) end end context "when seller is confirmed" do before do seller.confirm end context "when seller is suspended" do before do seller.update!(user_risk_state: :suspended_for_fraud) end it "denies access" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, seller) end end context "when seller has insufficient sales" do before do product = create(:product, user: seller) create(:purchase, seller: seller, link: product, purchase_state: :successful, price_cents: 5_000) allow(seller).to receive(:sales_cents_total).and_return(5_000) end it "denies access when sales are below $100" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, seller) end end context "when seller has sufficient sales but no completed payouts" do before do product = create(:product, user: seller) create(:purchase, seller: seller, link: product, purchase_state: :successful, price_cents: 15_000) allow(seller).to receive(:sales_cents_total).and_return(15_000) # No payment created, so has_completed_payouts? returns false end it "denies access when seller has not completed payouts" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, seller) end end context "when seller meets all requirements" do before do product = create(:product, user: seller) create(:purchase, seller: seller, link: product, purchase_state: :successful, price_cents: 15_000) create(:payment_completed, user: seller) allow(seller).to receive(:sales_cents_total).and_return(15_000) end it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end end end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/analytics_policy_spec.rb
spec/policies/analytics_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe AnalyticsPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, :analytics) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, :analytics) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :analytics) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :analytics) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, :analytics) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/installment_policy_spec.rb
spec/policies/installment_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe InstallmentPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index?, :preview? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Installment) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, Installment) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Installment) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Installment) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, Installment) end end permissions :new?, :edit?, :create?, :update?, :destroy?, :publish?, :schedule?, :delete?, :redirect_from_purchase_id?, :updated_recipient_count? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Installment) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, Installment) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Installment) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Installment) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, Installment) end end permissions :send_for_purchase? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Installment) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, Installment) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Installment) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Installment) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, Installment) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/commission_policy_spec.rb
spec/policies/commission_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe CommissionPolicy, :vcr do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:commission) { create(:commission) } let!(:seller) { commission.deposit_purchase.seller } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :update? do context "when the commission belongs to the seller" do before do allow(commission.deposit_purchase).to receive(:seller).and_return(seller) end it "graints access to seller" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, commission) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, commission) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, commission) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, commission) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, commission) end end context "when the commission belongs to another seller" do let!(:other_commission) { create(:commission) } it "denies access to seller" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, other_commission) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, other_commission) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, other_commission) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, other_commission) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, other_commission) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/dropbox_files_policy_spec.rb
spec/policies/dropbox_files_policy_spec.rb
# frozen_string_literal: true require "spec_helper" # Products section # describe DropboxFilesPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :create?, :index?, :cancel_upload? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, :dropbox_files) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, :dropbox_files) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :dropbox_files) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :dropbox_files) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, :dropbox_files) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/collaborator_policy_spec.rb
spec/policies/collaborator_policy_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/policy_examples" describe CollaboratorPolicy do subject { described_class } let(:seller) { create(:named_seller) } let(:admin_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_ADMIN, ).user end let(:accountant_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_ACCOUNTANT, ).user end let(:marketing_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_MARKETING, ).user end let(:support_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_SUPPORT, ).user end let(:context_seller) { seller } let(:collaboration_initiated_by_seller) { create(:collaborator, seller:) } let(:collaboration_adding_seller) { create(:collaborator, affiliate_user: seller) } let(:collaboration_between_other_people) { create(:collaborator) } permissions :index?, :new?, :create? do let(:record) { Collaborator } it_behaves_like "an access-granting policy for roles", [ :seller, :admin_for_seller, ] it_behaves_like "an access-denying policy for roles", [ :accountant_for_seller, :marketing_for_seller, :support_for_seller, ] end permissions :edit?, :update? do context "collaboration initiated by seller" do let(:record) { collaboration_initiated_by_seller } it_behaves_like "an access-granting policy for roles", [ :seller, :admin_for_seller, ] it_behaves_like "an access-denying policy for roles", [ :accountant_for_seller, :marketing_for_seller, :support_for_seller, ] end context "collaboration adding seller" do let(:record) { collaboration_adding_seller } it_behaves_like "an access-denying policy for roles", [ :seller, :admin_for_seller, :accountant_for_seller, :marketing_for_seller, :support_for_seller, ] end context "collaboration between other people" do let(:record) { collaboration_between_other_people } it_behaves_like "an access-denying policy for roles", [ :seller, :admin_for_seller, :accountant_for_seller, :marketing_for_seller, :support_for_seller, ] end end permissions :destroy? do context "collaboration initiated by seller" do let(:record) { collaboration_initiated_by_seller } it_behaves_like "an access-granting policy for roles", [ :seller, :admin_for_seller, ] it_behaves_like "an access-denying policy for roles", [ :accountant_for_seller, :marketing_for_seller, :support_for_seller, ] end context "collaboration adding seller" do let(:record) { collaboration_adding_seller } it_behaves_like "an access-granting policy for roles", [ :seller, :admin_for_seller, ] it_behaves_like "an access-denying policy for roles", [ :accountant_for_seller, :marketing_for_seller, :support_for_seller, ] end context "collaboration between other people" do let(:record) { collaboration_between_other_people } it_behaves_like "an access-denying policy for roles", [ :seller, :admin_for_seller, :accountant_for_seller, :marketing_for_seller, :support_for_seller, ] end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/imported_customer_policy_spec.rb
spec/policies/imported_customer_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe ImportedCustomerPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, ImportedCustomer) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, ImportedCustomer) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, ImportedCustomer) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, ImportedCustomer) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, ImportedCustomer) end end permissions :update? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, ImportedCustomer) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, ImportedCustomer) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, ImportedCustomer) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, ImportedCustomer) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, ImportedCustomer) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/dashboard_policy_spec.rb
spec/policies/dashboard_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe DashboardPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, :dashboard) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, :dashboard) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :dashboard) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :dashboard) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, :dashboard) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/thumbnail_policy_spec.rb
spec/policies/thumbnail_policy_spec.rb
# frozen_string_literal: true require "spec_helper" # Products section # describe ThumbnailPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } let(:product) { create(:product, user: seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :create?, :destroy? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Thumbnail) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, Thumbnail) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Thumbnail) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Thumbnail) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, Thumbnail) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/application_policy_spec.rb
spec/policies/application_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe ApplicationPolicy do describe ".allow_anonymous_user_access!" do it "does not affect other policy classes" do policy_class_1 = Class.new(ApplicationPolicy) policy_class_2 = Class.new(ApplicationPolicy) policy_class_1.allow_anonymous_user_access! expect(policy_class_1.allow_anonymous_user_access).to be true expect(policy_class_2.allow_anonymous_user_access).to be false expect(ApplicationPolicy.allow_anonymous_user_access).to be false end end describe "#initialize" do let(:user) { create(:user) } let(:seller) { create(:named_seller) } it "assigns accessors" do context = SellerContext.new(user:, seller:) policy = described_class.new(context, :record) expect(policy.user).to eq(user) expect(policy.seller).to eq(seller) expect(policy.record).to eq(:record) end context "when anonymous user access is not allowed" do it "raises when user is nil" do context = SellerContext.new(user: nil, seller:) expect do described_class.new(context, :record) end.to raise_error(Pundit::NotAuthorizedError).with_message "must be logged in" end it "does not raise when user is present" do context = SellerContext.new(user:, seller:) expect do described_class.new(context, :record) end.not_to raise_error end end context "when anonymous user access is allowed" do let(:policy_class) do Class.new(ApplicationPolicy) do allow_anonymous_user_access! end end it "does not raise when user is nil" do context = SellerContext.new(user: nil, seller:) policy = policy_class.new(context, :record) expect(policy.user).to be_nil expect(policy.seller).to eq(seller) expect(policy.record).to eq(:record) end it "still works normally when user is present" do context = SellerContext.new(user:, seller:) policy = policy_class.new(context, :record) expect(policy.user).to eq(user) expect(policy.seller).to eq(seller) expect(policy.record).to eq(:record) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/collaborator_invitation_policy_spec.rb
spec/policies/collaborator_invitation_policy_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/policy_examples" describe CollaboratorInvitationPolicy do subject { described_class } let(:seller) { create(:named_seller) } let(:admin_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_ADMIN, ).user end let(:accountant_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_ACCOUNTANT, ).user end let(:marketing_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_MARKETING, ).user end let(:support_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_SUPPORT, ).user end let(:collaborator_invitation_for_seller) do create( :collaborator_invitation, collaborator: create(:collaborator, affiliate_user: seller), ) end let(:collaborator_invitation_for_another_seller) do create( :collaborator_invitation, collaborator: create(:collaborator, affiliate_user: create(:user)), ) end let(:context_seller) { seller } permissions :accept?, :decline? do context "when the invitation is for the seller" do let(:record) { collaborator_invitation_for_seller } it_behaves_like "an access-granting policy for roles", [ :seller, :admin_for_seller, ] it_behaves_like "an access-denying policy for roles", [ :accountant_for_seller, :marketing_for_seller, :support_for_seller, ] end context "when the invitation is for another seller" do let(:record) { collaborator_invitation_for_another_seller } it_behaves_like "an access-denying policy for roles", [ :seller, :admin_for_seller, :accountant_for_seller, :marketing_for_seller, :support_for_seller, ] end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/product_review_response_policy_spec.rb
spec/policies/product_review_response_policy_spec.rb
# frozen_string_literal: true require "spec_helper" require "shared_examples/policy_examples" describe ProductReviewResponsePolicy do subject { described_class } let(:seller) { create(:named_seller) } let(:admin_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_ADMIN, ).user end let(:support_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_SUPPORT, ).user end let(:accountant_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_ACCOUNTANT, ).user end let(:marketing_for_seller) do create( :team_membership, seller: seller, role: TeamMembership::ROLE_MARKETING, ).user end let(:product) { create(:product, user: seller) } let(:purchase) { create(:purchase, link: product, seller:) } let(:product_review) { create(:product_review, purchase:) } let(:product_review_response_for_seller) do create(:product_review_response, product_review: product_review) end let(:product_review_response_for_another_seller) { create(:product_review_response) } let(:context_seller) { seller } permissions :update? do context "when the response is for the seller's product review" do let(:record) { product_review_response_for_seller } it_behaves_like "an access-granting policy for roles", [ :seller, :admin_for_seller, :support_for_seller, ] it_behaves_like "an access-denying policy for roles", [ :accountant_for_seller, :marketing_for_seller, ] end context "when the response is for another seller's product review" do let(:record) { product_review_response_for_another_seller } it_behaves_like "an access-denying policy for roles", [ :seller, :admin_for_seller, :support_for_seller, :accountant_for_seller, :marketing_for_seller, ] end end permissions :destroy? do context "when the response is for the seller's product review" do let(:record) { product_review_response_for_seller } it_behaves_like "an access-granting policy for roles", [ :seller, :admin_for_seller, :support_for_seller, ] it_behaves_like "an access-denying policy for roles", [ :accountant_for_seller, :marketing_for_seller, ] end context "when the response is for another seller's product review" do let(:record) { product_review_response_for_another_seller } it_behaves_like "an access-denying policy for roles", [ :seller, :admin_for_seller, :support_for_seller, :accountant_for_seller, :marketing_for_seller, ] end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/direct_affiliate_policy_spec.rb
spec/policies/direct_affiliate_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe DirectAffiliatePolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index?, :statistics? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, DirectAffiliate) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, DirectAffiliate) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, DirectAffiliate) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, DirectAffiliate) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, DirectAffiliate) end end permissions :create?, :update?, :destroy? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, DirectAffiliate) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, DirectAffiliate) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, DirectAffiliate) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, DirectAffiliate) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, DirectAffiliate) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/subscription_policy_spec.rb
spec/policies/subscription_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe SubscriptionPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } let(:product) { create(:product, user: seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end context "with subscription belongs to seller's product" do let(:subscription) { create(:subscription, link: product) } permissions :unsubscribe_by_seller? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, subscription) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, subscription) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, subscription) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, subscription) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, subscription) end end end context "with subscription belongs to other seller's product" do let(:subscription) { create(:subscription) } permissions :unsubscribe_by_seller? do it "denies access" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to_not permit(seller_context, subscription) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/wishlist_policy_spec.rb
spec/policies/wishlist_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe WishlistPolicy do subject { described_class } let(:seller) { create(:named_seller) } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:wishlist) { create(:wishlist, user: seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index?, :update?, :destroy? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, wishlist) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, wishlist) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to_not permit(seller_context, wishlist) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to_not permit(seller_context, wishlist) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to_not permit(seller_context, wishlist) end end permissions :update?, :destroy? do let(:wishlist) { create(:wishlist, user: create(:user)) } it "denies access to another user's wishlist" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to_not permit(seller_context, wishlist) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/purchase_policy_spec.rb
spec/policies/purchase_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe PurchasePolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index?, :archive?, :unarchive?, :delete? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Purchase) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, Purchase) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to_not permit(seller_context, Purchase) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to_not permit(seller_context, Purchase) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, Purchase) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/service_charge_policy_spec.rb
spec/policies/service_charge_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe ServiceChargePolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :create?, :confirm?, :resend_receipt?, :send_invoice?, :generate_service_charge_invoice? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, ServiceCharge) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, ServiceCharge) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to_not permit(seller_context, ServiceCharge) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to_not permit(seller_context, ServiceCharge) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, ServiceCharge) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/s3_utility_policy_spec.rb
spec/policies/s3_utility_policy_spec.rb
# frozen_string_literal: true require "spec_helper" # Products section # describe S3UtilityPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } let(:product) { create(:product, user: seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :generate_multipart_signature?, :current_utc_time_string?, :cdn_url_for_blob? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, S3UtilityPolicy) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, S3UtilityPolicy) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, S3UtilityPolicy) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, S3UtilityPolicy) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, S3UtilityPolicy) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/workflow_policy_spec.rb
spec/policies/workflow_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe WorkflowPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Workflow) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, Workflow) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Workflow) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Workflow) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, Workflow) end end permissions :create?, :new?, :edit?, :update?, :create_post_and_rule?, :create_and_publish_post_and_rule?, :delete?, :destroy?, :save_installments? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Workflow) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, Workflow) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Workflow) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Workflow) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, Workflow) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/balance_policy_spec.rb
spec/policies/balance_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe BalancePolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index?, :export? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, :balance) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, :dashboard) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :balance) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, :balance) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, :dashboard) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/utm_link_policy_spec.rb
spec/policies/utm_link_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe UtmLinkPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) Feature.activate_user(:utm_links, seller) end permissions :index? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end end permissions :new? do it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "denies access to other roles" do [accountant_for_seller, support_for_seller].each do |user| seller_context = SellerContext.new(user:, seller:) expect(subject).not_to permit(seller_context, :utm_link) end end end permissions :create? do it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "denies access to other roles" do [accountant_for_seller, support_for_seller].each do |user| seller_context = SellerContext.new(user:, seller:) expect(subject).not_to permit(seller_context, :utm_link) end end end permissions :edit? do it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "denies access to other roles" do [accountant_for_seller, support_for_seller].each do |user| seller_context = SellerContext.new(user:, seller:) expect(subject).not_to permit(seller_context, :utm_link) end end end permissions :update? do it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "denies access to other roles" do [accountant_for_seller, support_for_seller].each do |user| seller_context = SellerContext.new(user:, seller:) expect(subject).not_to permit(seller_context, :utm_link) end end end permissions :destroy? do it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :utm_link) end it "denies access to other roles" do [accountant_for_seller, support_for_seller].each do |user| seller_context = SellerContext.new(user:, seller:) expect(subject).not_to permit(seller_context, :utm_link) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/community_policy_spec.rb
spec/policies/community_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe CommunityPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } let(:buyer) { create(:user) } let(:product) { create(:product, user: seller, community_chat_enabled: true) } let!(:community) { create(:community, seller: seller, resource: product) } let(:other_product) { create(:product, community_chat_enabled: true) } let!(:other_community) { create(:community, seller: other_product.user, resource: other_product) } let!(:purchase) { create(:purchase, purchaser: buyer, link: other_product) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index? do context "when user has accessible communities" do before do Feature.activate_user(:communities, seller) Feature.activate_user(:communities, other_product.user) end it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Community) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, Community) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, Community) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, Community) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, Community) end it "grants access to buyer with purchased product" do seller_context = SellerContext.new(user: buyer, seller: other_product.user) expect(subject).to permit(seller_context, Community) end it "denies access to seller who has at least one product but no active communities" do another_seller = create(:user) create(:product, user: another_seller) Feature.activate_user(:communities, another_seller) seller_context = SellerContext.new(user: another_seller, seller:) expect(subject).not_to permit(seller_context, Community) end end context "when user has no accessible communities" do before do Feature.deactivate_user(:communities, seller) Feature.deactivate_user(:communities, other_product.user) end it "denies access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, Community) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, Community) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, Community) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, Community) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, Community) end it "denies access to buyer with purchased product" do purchase seller_context = SellerContext.new(user: buyer, seller: other_product.user) expect(subject).not_to permit(seller_context, Community) end end end permissions :show? do context "when user has access to the community" do before do Feature.activate_user(:communities, seller) Feature.activate_user(:communities, other_product.user) end context "when user is a seller" do it "grants access to own community" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, community) end it "denies access to other seller's community" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, other_community) end end context "when user is a buyer" do it "grants access to community of purchased product" do purchase seller_context = SellerContext.new(user: buyer, seller:) expect(subject).to permit(seller_context, other_community) end it "denies access to community of unpurchased product" do seller_context = SellerContext.new(user: buyer, seller:) expect(subject).not_to permit(seller_context, community) end end context "when user is a team member" do it "denies access to seller's community" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, community) end it "denies access to other seller's community" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, other_community) end end end context "when user has no access to the community" do before do Feature.deactivate_user(:communities, seller) Feature.deactivate_user(:communities, other_product.user) end it "denies access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, community) end it "denies access to buyer" do purchase seller_context = SellerContext.new(user: buyer, seller:) expect(subject).not_to permit(seller_context, other_community) end it "denies access to team members" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, community) end end context "when community's resource is deleted" do before do Feature.activate_user(:communities, seller) product.mark_deleted! end it "denies access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, community) end it "denies access to team members" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, community) end end context "when community chat is disabled" do before do Feature.activate_user(:communities, seller) product.update!(community_chat_enabled: false) end it "denies access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, community) end it "denies access to team members" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, community) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/instant_payout_policy_spec.rb
spec/policies/instant_payout_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe InstantPayoutPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :create? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, :instant_payout) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, :instant_payout) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :instant_payout) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, :instant_payout) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, :instant_payout) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/community_chat_message_policy_spec.rb
spec/policies/community_chat_message_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe CommunityChatMessagePolicy do subject { described_class } let(:seller) { create(:named_seller) } let(:buyer) { create(:user) } let(:other_buyer) { create(:user) } let(:product) { create(:product, user: seller) } let!(:community) { create(:community, seller:, resource: product) } let!(:message) { create(:community_chat_message, community:, user: buyer) } permissions :update? do context "when user is the message creator" do it "grants access" do seller_context = SellerContext.new(user: buyer, seller:) expect(subject).to permit(seller_context, message) end end context "when user is not the message creator" do it "denies access to the community seller" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).not_to permit(seller_context, message) end it "denies access to other buyer" do seller_context = SellerContext.new(user: other_buyer, seller:) expect(subject).not_to permit(seller_context, message) end end end permissions :destroy? do context "when user is the message creator" do it "grants access" do seller_context = SellerContext.new(user: buyer, seller:) expect(subject).to permit(seller_context, message) end end context "when user is the community seller" do it "grants access" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, message) end end context "when user is neither the message creator nor the community seller" do it "denies access" do seller_context = SellerContext.new(user: other_buyer, seller:) expect(subject).not_to permit(seller_context, message) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/audience/purchase_policy_spec.rb
spec/policies/audience/purchase_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Audience::PurchasePolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Purchase) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, Purchase) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Purchase) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Purchase) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, Purchase) end end permissions :update?, :refund?, :change_can_contact?, :cancel_preorder_by_seller?, :mark_as_shipped?, :manage_license? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Follower) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, Purchase) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Follower) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, Follower) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, Purchase) end end permissions :revoke_access? do let(:purchase) { create(:purchase) } let(:seller_context) { SellerContext.new(user: seller, seller:) } it "grants access to owner" do expect(subject).to permit(seller_context, purchase) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, Purchase) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, purchase) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, purchase) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, purchase) end context "when access has been revoked" do before do purchase.update!(is_access_revoked: true) end it "denies access" do expect(subject).not_to permit(seller_context, purchase) end end context "when purchase is refunded" do before do purchase.update!(stripe_refunded: true) end it "denies access" do expect(subject).not_to permit(seller_context, purchase) end end context "when product is physical" do let(:purchase) { create(:physical_purchase, link: create(:physical_product)) } it "denies access" do expect(subject).not_to permit(seller_context, purchase) end end context "when purchase is subscription" do let(:purchase) { create(:membership_purchase) } it "denies access" do expect(subject).not_to permit(seller_context, purchase) end end end permissions :undo_revoke_access? do let(:purchase) { create(:purchase, is_access_revoked: true) } let(:seller_context) { SellerContext.new(user: seller, seller:) } it "grants access to owner" do expect(subject).to permit(seller_context, purchase) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, Purchase) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, purchase) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, purchase) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, Purchase) end context "when access has not been revoked" do before do purchase.update!(is_access_revoked: false) end it "denies access" do expect(subject).not_to permit(seller_context, purchase) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/audience/follower_policy_spec.rb
spec/policies/audience/follower_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Audience::FollowerPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Follower) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, Follower) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Follower) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Follower) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, Follower) end end permissions :update?, :destroy? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Follower) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, Follower) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Follower) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, Follower) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, Follower) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/stripe_account_sessions/user_policy_spec.rb
spec/policies/stripe_account_sessions/user_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe StripeAccountSessions::UserPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :create? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/affiliate_requests/onboarding_form_policy_spec.rb
spec/policies/affiliate_requests/onboarding_form_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe AffiliateRequests::OnboardingFormPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :update? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, :onboarding_form) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, :onboarding_form) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :onboarding_form) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, :onboarding_form) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/settings/profile_policy_spec.rb
spec/policies/settings/profile_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Settings::ProfilePolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :show? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, seller) end end permissions :update? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end permissions :update_username?, :manage_social_connections? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end describe "#permitted_attributes" do it "allows owner to update the username" do policy = described_class.new(SellerContext.new(user: seller, seller:), seller) expect(policy.permitted_attributes).to include(a_hash_including(user: a_collection_including(:username))) end it "does not allow accountant to update the username" do policy = described_class.new(SellerContext.new(user: accountant_for_seller, seller:), seller) expect(policy.permitted_attributes).to_not include(a_hash_including(user: a_collection_including(:username))) end it "does not allow admin to update the username" do policy = described_class.new(SellerContext.new(user: admin_for_seller, seller:), seller) expect(policy.permitted_attributes).to_not include(a_hash_including(user: a_collection_including(:username))) end it "does not allow marketing to update the username" do policy = described_class.new(SellerContext.new(user: marketing_for_seller, seller:), seller) expect(policy.permitted_attributes).to_not include(a_hash_including(user: a_collection_including(:username))) end it "does not allow support to update the username" do policy = described_class.new(SellerContext.new(user: support_for_seller, seller:), seller) expect(policy.permitted_attributes).to_not include(a_hash_including(user: a_collection_including(:username))) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/settings/payments/user_policy_spec.rb
spec/policies/settings/payments/user_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Settings::Payments::UserPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :show? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end permissions :update?, :set_country?, :verify_document?, :verify_identity?, :remove_credit_card? do context "with owner as seller" do let(:seller_context) { SellerContext.new(user: seller, seller:) } it "grants access to owner" do expect(subject).to permit(seller_context, seller) end it "denies access to owner when record is other user" do expect(subject).not_to permit(seller_context, create(:user)) end end context "with admin for seller" do let(:seller_context) { SellerContext.new(user: admin_for_seller, seller:) } it "denies access to admin" do expect(subject).not_to permit(seller_context, seller) end end context "with marketing for seller" do let(:seller_context) { SellerContext.new(user: marketing_for_seller, seller:) } it "denies access to marketing" do expect(subject).not_to permit(seller_context, seller) end end context "with accountant for seller" do let(:seller_context) { SellerContext.new(user: accountant_for_seller, seller:) } it "denies access to accountant" do expect(subject).not_to permit(seller_context, seller) end end context "with support for seller" do let(:seller_context) { SellerContext.new(user: marketing_for_seller, seller:) } it "denies access to support" do expect(subject).not_to permit(seller_context, seller) end end end permissions :paypal_connect?, :stripe_connect? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/settings/advanced/user_policy_spec.rb
spec/policies/settings/advanced/user_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Settings::Advanced::UserPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :show?, :update?, :test_ping? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/settings/password/user_policy_spec.rb
spec/policies/settings/password/user_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Settings::Password::UserPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :show?, :update? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/settings/team/team_invitation_policy_spec.rb
spec/policies/settings/team/team_invitation_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Settings::Team::TeamInvitationPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :create?, :update?, :destroy?, :restore?, :resend_invitation? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end permissions :accept? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/settings/team/user_policy_spec.rb
spec/policies/settings/team/user_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Settings::Team::UserPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :show? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, seller) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/settings/team/team_membership_policy_spec.rb
spec/policies/settings/team/team_membership_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Settings::Team::TeamMembershipPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } let(:team_membership_of_other_member) { create(:team_membership, seller:) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :update?, :destroy?, :restore? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, team_membership_of_other_member) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, team_membership_of_other_member) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, team_membership_of_other_member) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, team_membership_of_other_member) end end permissions :destroy? do before do team_membership_of_other_member.update(role: TeamMembership::ROLE_MARKETING) end it "grants access to other member" do seller_context = SellerContext.new(user: team_membership_of_other_member.user, seller:) expect(subject).to permit(seller_context, team_membership_of_other_member) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/settings/main/user_policy_spec.rb
spec/policies/settings/main/user_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Settings::Main::UserPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :show? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end permissions :update?, :resend_confirmation_email?, :invalidate_active_sessions? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/settings/third_party_analytics/user_policy_spec.rb
spec/policies/settings/third_party_analytics/user_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Settings::ThirdPartyAnalytics::UserPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :show?, :update? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/settings/authorized_applications/oauth_application_policy_spec.rb
spec/policies/settings/authorized_applications/oauth_application_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Settings::AuthorizedApplications::OauthApplicationPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index?, :create?, :edit?, :update?, :destroy? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, seller) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, seller) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/admin/impersonators/user_policy_spec.rb
spec/policies/admin/impersonators/user_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Admin::Impersonators::UserPolicy do subject { described_class } let(:user) { create(:user) } let(:admin_user) { create(:admin_user) } let(:seller_context) { SellerContext.new(user: admin_user, seller: admin_user) } permissions :create? do context "when record is a regular user" do it "grants access" do expect(subject).to permit(seller_context, user) end end context "when user is deleted" do let(:user) { create(:user, :deleted) } it "denies access with message" do expect(subject).not_to permit(seller_context, user) end end context "when user is a team member" do it "denies access" do expect(subject).not_to permit(seller_context, admin_user) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/admin/charges/charge_policy_spec.rb
spec/policies/admin/charges/charge_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Admin::Charges::ChargePolicy, :vcr do subject { described_class } let(:admin_user) { create(:admin_user) } let(:seller_context) { SellerContext.new(user: admin_user, seller: admin_user) } permissions :refund? do let(:charge) do purchase = create(:purchase_in_progress, chargeable: create(:chargeable)) purchase.process! purchase.update_balance_and_mark_successful! purchase_2 = create(:purchase_in_progress, chargeable: create(:chargeable)) purchase_2.process! purchase_2.update_balance_and_mark_successful! charge = create(:charge, purchases: [purchase, purchase_2]) charge end context "when charge has non-refunded purchases" do it "grants access" do expect(subject).to permit(seller_context, charge) end end context "when all purchases are already refunded" do before do charge.purchases.each { _1.update!(stripe_refunded: 1) } end it "denies access" do expect(subject).not_to permit(seller_context, charge) end end end permissions :sync_status_with_charge_processor? do let(:charge) do purchase = create(:purchase_in_progress, chargeable: create(:chargeable)) purchase_2 = create(:purchase_in_progress, chargeable: create(:chargeable)) charge = create(:charge, purchases: [purchase, purchase_2]) charge end context "when charge has in_progress purchases" do it "grants access" do expect(subject).to permit(seller_context, charge) end end context "when charge has failed purchases" do before do charge.purchases.each { _1.mark_failed! } end it "grants access" do expect(subject).to permit(seller_context, charge) end end context "when no purchases are in progress or failed" do before do charge.purchases.each do |purchase| purchase.process! purchase.update_balance_and_mark_successful! end end it "denies access" do expect(subject).not_to permit(seller_context, charge) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/admin/products/staff_picked/link_policy_spec.rb
spec/policies/admin/products/staff_picked/link_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Admin::Products::StaffPicked::LinkPolicy do subject { described_class } let(:admin_user) { create(:admin_user) } let(:seller_context) { SellerContext.new(user: admin_user, seller: admin_user) } let(:product) { create(:product, :recommendable) } permissions :create? do context "when record does not exist" do it "grants access" do expect(subject).to permit(seller_context, product) end end context "when record exists and is deleted" do before do product.create_staff_picked_product!(deleted_at: Time.current) end it "grants access" do expect(subject).to permit(seller_context, product) end end context "when record exists and is not deleted" do before do product.create_staff_picked_product! end it "denies access" do expect(subject).not_to permit(seller_context, product) end end context "when product is not recommendable" do before do allow_any_instance_of(Link).to receive(:recommendable?).and_return(false) end it "denies access" do expect(subject).not_to permit(seller_context, product) end end end permissions :destroy? do context "when record exists and is not deleted" do before do product.create_staff_picked_product! end it "grants access" do expect(subject).to permit(seller_context, product) end end context "when record exists and is already deleted" do before do product.create_staff_picked_product!(deleted_at: Time.current) end it "denies access" do expect(subject).not_to permit(seller_context, product) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/checkout/upsell_policy_spec.rb
spec/policies/checkout/upsell_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Checkout::UpsellPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index?, :paged? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Upsell) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, Upsell) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Upsell) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Upsell) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, Upsell) end end permissions :create? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, Upsell) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, Upsell) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, Upsell) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, Upsell) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, Upsell) end end permissions :update?, :destroy?, :pause?, :unpause? do context "when the upsell belongs to seller" do let(:upsell) { create(:upsell, seller:) } it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, upsell) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, upsell) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, upsell) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, upsell) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, upsell) end end context "when the upsell belongs to other user" do let(:upsell) { create(:upsell) } it "denies access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to_not permit(seller_context, upsell) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, upsell) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to_not permit(seller_context, upsell) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to_not permit(seller_context, upsell) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, upsell) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/checkout/form_policy_spec.rb
spec/policies/checkout/form_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Checkout::FormPolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :show? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, :form) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, :form) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :form) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :form) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, :form) end end permissions :update? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, :form) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, :form) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, :form) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, :form) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, :form) end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false
antiwork/gumroad
https://github.com/antiwork/gumroad/blob/638f6c3a40b23b907c09f6881d4df18339da069c/spec/policies/checkout/offer_code_policy_spec.rb
spec/policies/checkout/offer_code_policy_spec.rb
# frozen_string_literal: true require "spec_helper" describe Checkout::OfferCodePolicy do subject { described_class } let(:accountant_for_seller) { create(:user) } let(:admin_for_seller) { create(:user) } let(:marketing_for_seller) { create(:user) } let(:support_for_seller) { create(:user) } let(:seller) { create(:named_seller) } before do create(:team_membership, user: accountant_for_seller, seller:, role: TeamMembership::ROLE_ACCOUNTANT) create(:team_membership, user: admin_for_seller, seller:, role: TeamMembership::ROLE_ADMIN) create(:team_membership, user: marketing_for_seller, seller:, role: TeamMembership::ROLE_MARKETING) create(:team_membership, user: support_for_seller, seller:, role: TeamMembership::ROLE_SUPPORT) end permissions :index?, :paged? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, OfferCode) end it "grants access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to permit(seller_context, OfferCode) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, OfferCode) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, OfferCode) end it "grants access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).to permit(seller_context, OfferCode) end end permissions :create? do it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, OfferCode) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, OfferCode) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, OfferCode) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, OfferCode) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, OfferCode) end end permissions :update?, :destroy? do context "when the offer code belongs to seller" do let(:offer_code) { create(:offer_code, user: seller) } it "grants access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to permit(seller_context, offer_code) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, offer_code) end it "grants access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to permit(seller_context, offer_code) end it "grants access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to permit(seller_context, offer_code) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, offer_code) end end context "when the offer code belongs to other user" do let(:offer_code) { create(:offer_code) } it "denies access to owner" do seller_context = SellerContext.new(user: seller, seller:) expect(subject).to_not permit(seller_context, offer_code) end it "denies access to accountant" do seller_context = SellerContext.new(user: accountant_for_seller, seller:) expect(subject).to_not permit(seller_context, offer_code) end it "denies access to admin" do seller_context = SellerContext.new(user: admin_for_seller, seller:) expect(subject).to_not permit(seller_context, offer_code) end it "denies access to marketing" do seller_context = SellerContext.new(user: marketing_for_seller, seller:) expect(subject).to_not permit(seller_context, offer_code) end it "denies access to support" do seller_context = SellerContext.new(user: support_for_seller, seller:) expect(subject).not_to permit(seller_context, offer_code) end end end end
ruby
MIT
638f6c3a40b23b907c09f6881d4df18339da069c
2026-01-04T15:39:11.815677Z
false