jam-cloud/ruby/spec/jam_ruby/integration/recurly_live_integration_sp...

108 lines
3.2 KiB
Ruby

require 'spec_helper'
require 'jam_ruby/recurly_client'
# Live Recurly integration checks.
# Default: enabled.
# Opt-out: SKIP_LIVE_RECURLY=1 bundle exec rspec spec/jam_ruby/integration/recurly_live_integration_spec.rb
# Uses sandbox/development Recurly credentials by default.
describe 'Recurly live integration', live_recurly: true do
let(:client) { RecurlyClient.new }
let(:recurly_private_api_key) do
ENV['RECURLY_PRIVATE_API_KEY'] || '55f2fdfa4d014e64a94eaba1e93f39bb'
end
let(:recurly_subdomain) do
ENV['RECURLY_SUBDOMAIN'] || 'jamkazam-development'
end
let(:billing_info) do
{
first_name: user.first_name,
last_name: user.last_name,
address1: '100 Test Lane',
address2: 'Suite 1',
city: user.city,
state: user.state,
country: user.country,
zip: '78701',
number: '4111-1111-1111-1111',
month: '12',
year: (Time.now.year + 2).to_s,
verification_value: '123'
}
end
let(:user) do
FactoryBot.create(
:user,
email: "recurly-live-#{SecureRandom.hex(6)}@example.com",
subscription_trial_ends_at: 1.day.ago,
desired_plan_code: nil,
subscription_plan_code: nil,
recurly_code: nil,
recurly_subscription_id: nil
)
end
before do
skip('Set SKIP_LIVE_RECURLY=0 (or unset it) to run live Recurly integration tests') if ENV['SKIP_LIVE_RECURLY'] == '1'
Recurly.api_key = recurly_private_api_key
Recurly.subdomain = recurly_subdomain
end
after do
begin
if user.recurly_code.present?
account = Recurly::Account.find(user.recurly_code)
account.destroy if account
end
rescue Recurly::API::ResourceNotFound
# already gone
rescue Recurly::Resource::NotFound
# already gone
end
end
it 'creates and fetches a Recurly account via RecurlyClient' do
account = client.find_or_create_account(user, billing_info)
expect(account).not_to be_nil
expect(account.account_code).to eq(user.id)
expect(account.billing_info).not_to be_nil
expect(account.billing_info.last_four).to eq('1111')
fetched = client.get_account(user)
expect(fetched).not_to be_nil
expect(fetched.account_code).to eq(user.id)
end
it 'creates a paid subscription and returns parsed payment/invoice history hashes' do
account = client.find_or_create_account(user, billing_info)
expect(account).not_to be_nil
plan = Recurly::Plan.find('jamsubgold')
expect(plan.plan_code).to eq('jamsubgold')
result, subscription, _account = client.update_desired_subscription(user, 'jamsubgold')
expect(result).to eq(true)
expect(subscription).not_to be_nil
expect(subscription.uuid).not_to be_nil
payments = client.payment_history(user, limit: 5)
expect(payments).to be_a(Array)
unless payments.empty?
expect(payments.first).to include(:created_at, :amount_in_cents, :status, :action, :currency)
end
invoices, invoice_account = client.invoice_history(user, limit: 5)
expect(invoice_account).not_to be_nil
expect(invoices).to be_a(Array)
unless invoices.empty?
expect(invoices.first).to include(:created_at, :subtotal_in_cents, :tax_in_cents, :total_in_cents, :state, :currency)
end
end
end