Merge branch 'develop' into feat/shopify-app-store-install-flow
This commit is contained in:
@@ -357,10 +357,32 @@ RSpec.describe 'Enterprise Billing APIs', type: :request do
|
||||
context 'when it is an admin' do
|
||||
before do
|
||||
# Create the installation config for cloud environment
|
||||
InstallationConfig.where(name: 'DEPLOYMENT_ENV').first_or_create(value: 'cloud')
|
||||
InstallationConfig.where(name: 'DEPLOYMENT_ENV').first_or_initialize.update!(value: 'cloud')
|
||||
end
|
||||
|
||||
it 'marks the account for deletion when action is delete' do
|
||||
cancellation_service = instance_double(Enterprise::Billing::CancelCloudSubscriptionsService, perform: true)
|
||||
allow(Enterprise::Billing::CancelCloudSubscriptionsService).to receive(:new).with(account: account)
|
||||
.and_return(cancellation_service)
|
||||
|
||||
post "/enterprise/api/v1/accounts/#{account.id}/toggle_deletion",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: { action_type: 'delete' },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(account.reload.custom_attributes['marked_for_deletion_at']).to be_present
|
||||
expect(account.custom_attributes['marked_for_deletion_reason']).to eq('manual_deletion')
|
||||
expect(Enterprise::Billing::CancelCloudSubscriptionsService).to have_received(:new).with(account: account)
|
||||
expect(cancellation_service).to have_received(:perform)
|
||||
end
|
||||
|
||||
it 'returns success even if stripe cancellation fails' do
|
||||
cancellation_service = instance_double(Enterprise::Billing::CancelCloudSubscriptionsService)
|
||||
allow(Enterprise::Billing::CancelCloudSubscriptionsService).to receive(:new).with(account: account)
|
||||
.and_return(cancellation_service)
|
||||
allow(cancellation_service).to receive(:perform).and_raise(Stripe::APIError.new('stripe unavailable'))
|
||||
|
||||
post "/enterprise/api/v1/accounts/#{account.id}/toggle_deletion",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: { action_type: 'delete' },
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Enterprise::Billing::CancelCloudSubscriptionsService do
|
||||
subject(:service) { described_class.new(account: account) }
|
||||
|
||||
let(:account) { create(:account, custom_attributes: custom_attributes) }
|
||||
let(:custom_attributes) { { 'stripe_customer_id' => 'cus_123' } }
|
||||
|
||||
describe '#perform' do
|
||||
context 'when deployment is not cloud' do
|
||||
it 'does not call stripe subscriptions api' do
|
||||
allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(false)
|
||||
allow(Stripe::Subscription).to receive(:list)
|
||||
|
||||
service.perform
|
||||
|
||||
expect(Stripe::Subscription).not_to have_received(:list)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when stripe customer id is missing' do
|
||||
let(:custom_attributes) { {} }
|
||||
|
||||
it 'does not call stripe subscriptions api' do
|
||||
allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(true)
|
||||
allow(Stripe::Subscription).to receive(:list)
|
||||
|
||||
service.perform
|
||||
|
||||
expect(Stripe::Subscription).not_to have_received(:list)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when account is cloud with active subscriptions' do
|
||||
let(:subscription_response) { Struct.new(:data).new([sub_1, sub_2]) }
|
||||
let(:sub_1) { instance_double(Stripe::Subscription, id: 'sub_1', cancel_at_period_end: false) }
|
||||
let(:sub_2) { instance_double(Stripe::Subscription, id: 'sub_2', cancel_at_period_end: true) }
|
||||
|
||||
it 'marks only active subscriptions that are not yet set to cancel at period end' do
|
||||
allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(true)
|
||||
allow(Stripe::Subscription).to receive(:list).and_return(subscription_response)
|
||||
allow(Stripe::Subscription).to receive(:update)
|
||||
|
||||
service.perform
|
||||
|
||||
expect(Stripe::Subscription).to have_received(:list).with(customer: 'cus_123', status: 'active', limit: 100)
|
||||
expect(Stripe::Subscription).to have_received(:update).with('sub_1', cancel_at_period_end: true).once
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user