Files
openfoodnetwork/spec/models/stripe_account_spec.rb
2025-10-21 17:34:40 +02:00

67 lines
2.1 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
require 'stripe/oauth'
RSpec.describe StripeAccount do
describe "deauthorize_and_destroy", :vcr, :stripe_version do
let!(:enterprise) { create(:enterprise) }
let(:stripe_user_id) { ENV.fetch('STRIPE_ACCOUNT', nil) }
let!(:stripe_account) {
create(:stripe_account, enterprise:, stripe_user_id:)
}
context "when the Stripe API disconnect fails" do
before { Stripe.client_id = "bogus_client_id" }
it "destroys the record and notifies Bugsnag" do
# returns status 401
expect(Bugsnag).to receive(:notify) # and receives Bugsnag notification
expect {
stripe_account.deauthorize_and_destroy
}.to change { StripeAccount.where(stripe_user_id:).count }.from(1).to(0)
end
end
context "when the Stripe API disconnect succeeds" do
let!(:connected_account) do
Stripe::Account.create({
type: 'standard',
country: 'AU',
email: 'jumping.jack@example.com'
})
end
before do
Stripe.client_id = ENV.fetch('STRIPE_CLIENT_ID', nil)
stripe_account.update!(stripe_user_id: connected_account.id)
end
it "destroys the record" do
# returns status 200
expect(Bugsnag).not_to receive(:notify) # and does not receive Bugsnag notification
expect {
stripe_account.deauthorize_and_destroy
}.to change {
StripeAccount.where(stripe_user_id: connected_account.id).count
}.from(1).to(0)
end
end
context "if the account is also associated with another Enterprise" do
let!(:enterprise2) { create(:enterprise) }
before do
create(:stripe_account, enterprise: enterprise2, stripe_user_id:)
end
it "Doesn't make a Stripe API disconnection request " do
expect(Stripe::OAuth).not_to receive(:deauthorize)
stripe_account.deauthorize_and_destroy
expect(StripeAccount.all).not_to include(stripe_account)
end
end
end
end