require "rails_helper" RSpec.describe "Public club billing", type: :request do let!(:coach) do User.create!(email: "billing-coach@test.it", name: "Coach", password: "password123", role: "coach") end let!(:club) do c = Club.create!(name: "Billing HTTP Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff", billing_legal_name: "ASD Billing", 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") ClubMembership.create!(user: coach, club: c, role: "owner") c.teams.create!(name: "U15", sport: "volleyball") load Rails.root.join("db/seeds/plans.rb") Billing::AssignPlan.call(club: c, plan_slug: "free") c end def login! post public_login_path, params: { email: coach.email, password: "password123" } end it "mostra pagamenti e fatture in billing" do club.billing_payments.create!( amount_cents: 4000, currency: "eur", status: "paid", paid_at: Time.current, description: "Premium Light", stripe_invoice_id: "in_spec_1" ) club.billing_invoices.create!( number: "2026/100", issued_on: Date.current, amount_cents: 4000, status: "draft" ) login! get public_club_billing_path(club) expect(response).to have_http_status(:ok) expect(response.body).to include("Pagamenti effettuati") expect(response.body).to include("Fatture") expect(response.body).not_to include("Aruba") expect(response.body).to include("Premium Light") expect(response.body).to include("2026/100") end it "salva profilo fatturazione" do club.update!(billing_recipient_code: nil, billing_pec: nil) login! patch public_club_billing_profile_path(club), params: { club: { billing_recipient_code: "XYZABC1", billing_pec: "" } } expect(response).to redirect_to(public_club_billing_path(club)) expect(club.reload.billing_recipient_code).to eq("XYZABC1") end it "blocca checkout premium senza profilo completo" do club.update!(billing_vat_number: nil, billing_fiscal_code: nil) allow(MatchLiveTv).to receive(:stripe_enabled?).and_return(true) login! get public_club_checkout_path(club, plan: "premium_light") expect(response).to redirect_to(public_club_billing_profile_path(club)) end end