require "rails_helper" RSpec.describe Billing::Stripe::CheckoutSession do let(:user) { User.create!(email: "checkout@test.it", name: "Owner", password: "password123", role: "coach") } let(:club) do Club.create!( name: "Checkout Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff", billing_legal_name: "ASD Checkout", billing_email: "bill@test.it", billing_address_line: "Via 1", billing_city: "Milano", billing_postal_code: "20100", billing_vat_number: "12345678901", billing_recipient_code: "ABCDEFG" ) end before do load Rails.root.join("db/seeds/plans.rb") allow(MatchLiveTv).to receive(:stripe_enabled?).and_return(true) allow(MatchLiveTv).to receive(:stripe_premium_light_price_id).and_return("price_light") allow(MatchLiveTv).to receive(:stripe_premium_full_price_id).and_return("price_full") allow(MatchLiveTv).to receive(:app_public_url).and_return("http://localhost:3000") Plan.find_by!(slug: "premium_light").update!(stripe_price_id: "price_light") Plan.find_by!(slug: "premium_full").update!(stripe_price_id: "price_full") end it "crea checkout senza subscription esistente" do session = double(url: "https://checkout.stripe.com/test") expect(Stripe::Customer).to receive(:create).and_return(double(id: "cus_new")) expect(Billing::Stripe::CustomerSync).to receive(:call).with(club: club, customer_id: "cus_new") expect(Stripe::Checkout::Session).to receive(:create).with( hash_including( mode: "subscription", customer: "cus_new", line_items: [{ price: "price_light", quantity: 1 }], subscription_data: { metadata: { club_id: club.id, plan_slug: "premium_light" } } ) ).and_return(session) url = described_class.new(club: club, user: user, plan_slug: "premium_light").url expect(url).to eq("https://checkout.stripe.com/test") end it "crea checkout per cambio piano con subscription esistente" do Billing::AssignPlan.call( club: club, plan_slug: "premium_light", status: "active", stripe_attrs: { stripe_customer_id: "cus_x", stripe_subscription_id: "sub_x" } ) session = double(url: "https://checkout.stripe.com/change") expect(Billing::Stripe::CustomerSync).to receive(:call).with(club: club, customer_id: "cus_x") expect(Stripe::Checkout::Session).to receive(:create).with( hash_including( customer: "cus_x", subscription: "sub_x", line_items: [{ price: "price_full", quantity: 1 }] ) ).and_return(session) url = described_class.new(club: club, user: user, plan_slug: "premium_full").url expect(url).to eq("https://checkout.stripe.com/change") end end