jam-cloud/ruby/spec/jam_ruby/recurly_client_spec.rb

116 lines
3.9 KiB
Ruby

require 'spec_helper'
require "jam_ruby/recurly_client"
describe RecurlyClient do
LIVE_RECURLY_PRIVATE_API_KEY = ENV['RECURLY_PRIVATE_API_KEY'] || '55f2fdfa4d014e64a94eaba1e93f39bb'
LIVE_RECURLY_SUBDOMAIN = ENV['RECURLY_SUBDOMAIN'] || 'jamkazam-development'
let(:jamtrack) { FactoryBot.create(:jam_track, plan_code: 'jamtrack-acdc-backinblack') }
before :all do
Recurly.api_key = LIVE_RECURLY_PRIVATE_API_KEY
Recurly.subdomain = LIVE_RECURLY_SUBDOMAIN
@client = RecurlyClient.new
@jamtrack = FactoryBot.create(:jam_track, plan_code: 'jamtrack-acdc-backinblack')
end
before(:each) do
@user = FactoryBot.create(:user)
@billing_info = {}
@billing_info[:first_name] = @user.first_name
@billing_info[:last_name] = @user.last_name
@billing_info[:address1] = 'Test Address 1'
@billing_info[:address2] = 'Test Address 2'
@billing_info[:city] = @user.city
@billing_info[:state] = @user.state
@billing_info[:country] = @user.country
@billing_info[:zip] = '12345'
@billing_info[:number] = '4111-1111-1111-1111'
@billing_info[:month] = '08'
@billing_info[:year] = (Time.now.year + 2).to_s
@billing_info[:verification_value] = '111'
end
after(:each) do
if @user && @user.recurly_code
begin
account = Recurly::Account.find(@user.recurly_code)
account.destroy if account.present?
rescue Recurly::Resource::NotFound, Recurly::API::ResourceNotFound
end
end
end
it "can create account" do
account = @client.create_account(@user, @billing_info)
account.should_not be_nil
@user.recurly_code.should eq(account.account_code)
end
it "can create account with errors" do
@billing_info[:number] = '123'
expect { @client.create_account(@user, @billing_info) }.to raise_error(RecurlyClientError)
end
describe "with account" do
before :each do
@account = @client.find_or_create_account(@user, @billing_info)
end
it "can find account" do
@user.recurly_code.should_not be_nil
found = @client.get_account(@user)
found.should_not be_nil
found.first_name.should eq(@user.first_name)
found.last_name.should eq(@user.last_name)
found.email.should eq(@user.email)
end
it "can update account" do
@user.first_name = "Foobar"
@client.update_account(@user, @billing_info)
found = @client.get_account(@user)
found.first_name.should eq(@user.first_name)
end
it "can update billing" do
@billing_info['address1'] = "2112 Foobar Lane"
@client.update_billing_info(@user, @billing_info)
found = @client.get_account(@user)
found.billing_info.address1.should eq(@billing_info['address1'])
end
end
it "can remove account" do
@client.find_or_create_account(@user, @billing_info)
@client.get_account(@user).should_not be_nil
@client.delete_account(@user)
found=@client.get_account(@user)
found.should_not be_nil
found.state.should eq('closed')
end
=begin
it "can refund subscription" do
sale = Sale.create(@user)
shopping_cart = ShoppingCart.create @user, @jamtrack, 1
@client.find_or_create_account(@user, @billing_info)
# Place order:
expect{@client.place_order(@user, @jamtrack, shopping_cart, sale)}.not_to raise_error()
active_subs=@client.get_account(@user).subscriptions.find_all{|t|t.state=='active'}
@jamtrack.reload
@jamtrack.jam_track_rights.size.should == 1
# Refund:
expect{@client.refund_user_subscription(@user, @jamtrack)}.not_to raise_error()
active_subs=@client.get_account(@user).subscriptions.find_all{|t|t.state=='active'}
active_subs.size.should == 0
@jamtrack.reload
@jamtrack.jam_track_rights.size.should == 0
end
=end
end