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

159 lines
5.7 KiB
Ruby
Raw Normal View History

require 'spec_helper'
require "jam_ruby/recurly_client"
describe RecurlyClient do
2015-03-09 14:44:12 +00:00
let(:jamtrack) { FactoryGirl.create(:jam_track, plan_code: 'jamtrack-acdc-backinblack') }
before :all do
@client = RecurlyClient.new
2015-03-09 14:44:12 +00:00
@jamtrack = FactoryGirl.create(:jam_track, plan_code: 'jamtrack-acdc-backinblack')
end
before(:each) do
@user = FactoryGirl.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] = '2017'
@billing_info[:verification_value] = '111'
end
after(:each) do
if (@user.recurly_code)
account = Recurly::Account.find(@user.recurly_code)
if account.present?
account.destroy
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[:verification_value] = '1111'
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
it "purchases jamtrack" do
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
it "can place order" do
sale = Sale.create(@user)
sale = Sale.find(sale.id)
shopping_cart = ShoppingCart.create @user, @jamtrack, 1, true
history_items = @client.payment_history(@user).length
@client.find_or_create_account(@user, @billing_info)
expect{@client.place_order(@user, @jamtrack, shopping_cart, sale)}.not_to raise_error()
# verify jam_track_rights data
@user.jam_track_rights.should_not be_nil
@user.jam_track_rights.should have(1).items
@user.jam_track_rights.last.jam_track.id.should eq(@jamtrack.id)
# verify sales data
sale = Sale.find(sale.id)
sale.sale_line_items.length.should == 1
sale_line_item = sale.sale_line_items[0]
sale_line_item.product_type.should eq(JamTrack::PRODUCT_TYPE)
sale_line_item.unit_price.should eq(@jamtrack.price)
sale_line_item.quantity.should eq(1)
sale_line_item.free.should eq(1)
sale_line_item.sales_tax.should be_nil
sale_line_item.shipping_handling.should eq(0)
sale_line_item.recurly_plan_code.should eq(@jamtrack.plan_code)
sale_line_item.product_id.should eq(@jamtrack.id)
sale_line_item.recurly_subscription_uuid.should_not be_nil
sale_line_item.recurly_subscription_uuid.should eq(@user.jam_track_rights.last.recurly_subscription_uuid)
# verify subscription is in Recurly
subs = @client.get_account(@user).subscriptions
subs.should_not be_nil
subs.should have(1).items
@client.payment_history(@user).should have(history_items+1).items
end
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.should have(1).items
# 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.should have(0).items
@jamtrack.reload
@jamtrack.jam_track_rights.should have(0).items
end
it "detects error on double order" do
sale = Sale.create(@user)
shopping_cart = ShoppingCart.create @user, @jamtrack, 1
@client.find_or_create_account(@user, @billing_info)
jam_track_right = @client.place_order(@user, @jamtrack, shopping_cart, sale)
2015-03-09 14:44:12 +00:00
jam_track_right.recurly_subscription_uuid.should_not be_nil
shopping_cart = ShoppingCart.create @user, @jamtrack, 1
jam_track_right2 = @client.place_order(@user, @jamtrack, shopping_cart, sale)
2015-03-09 14:44:12 +00:00
jam_track_right.should eq(jam_track_right2)
jam_track_right.recurly_subscription_uuid.should eq(jam_track_right.recurly_subscription_uuid)
end
end