require "test_helper" require "stripe" class PayoutServiceTest < ActiveSupport::TestCase setup do @user = users(:one) @event = events(:concert_event) @payout = Payout.create!(user: @user, event: @event, amount_cents: 9000, fee_cents: 1000) Stripe.api_key = "test_key" end test "process! throws error for manual workflow" do @payout.update(status: :pending) service = PayoutService.new(@payout) error = assert_raises(RuntimeError) do service.process! end assert_includes error.message, "Automatic payout processing is disabled" end test "generate_transfer_summary returns payout details" do @user.update!(iban: "FR1420041010050500013M02606", bank_name: "Test Bank", account_holder_name: "John Doe") @payout.update(status: :approved) service = PayoutService.new(@payout) summary = service.generate_transfer_summary assert_not_nil summary assert_equal @payout.id, summary[:payout_id] assert_equal @user.name, summary[:recipient] assert_equal @user.account_holder_name, summary[:account_holder] assert_equal @user.bank_name, summary[:bank_name] assert_equal @user.iban, summary[:iban] end test "validate_banking_info returns errors for missing data" do service = PayoutService.new(@payout) errors = service.validate_banking_info assert_includes errors, "Missing IBAN" assert_includes errors, "Missing bank name" assert_includes errors, "Missing account holder name" end test "validate_banking_info returns no errors for complete data" do @user.update!(iban: "FR1420041010050500013M02606", bank_name: "Test Bank", account_holder_name: "John Doe") service = PayoutService.new(@payout) errors = service.validate_banking_info assert_empty errors end test "process! handles manual processing when user has no stripe account" do # Create a user without a stripe account user_without_stripe = User.create!( email: "test@example.com", password: "password123", is_professionnal: true ) event = Event.create!( user: user_without_stripe, name: "Test Event", slug: "test-event", description: "Test event description", venue_name: "Test Venue", venue_address: "Test Address", latitude: 48.8566, longitude: 2.3522, start_time: 1.day.ago, end_time: 1.hour.ago, state: :published ) payout = Payout.create!(user: user_without_stripe, event: event, amount_cents: 9000, fee_cents: 1000, status: :pending) # Mock that Stripe is not available for this user user_without_stripe.stubs(:has_stripe_account?).returns(false) service = PayoutService.new(payout) service.process! payout.reload assert_equal :completed, payout.status assert payout.manual_payout? assert_match /MANUAL_/, payout.stripe_payout_id end end