Skip to content

Instantly share code, notes, and snippets.

@trystant
Created April 2, 2009 13:52
Show Gist options
  • Save trystant/89194 to your computer and use it in GitHub Desktop.
Save trystant/89194 to your computer and use it in GitHub Desktop.
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Subscription, 'associations' do
before :each do
@subscription = Subscription.new
end
it 'should belong to an account' do
@subscription.should belong_to(:account)
end
it 'should belong to an account plan' do
@subscription.should belong_to(:account_plan)
end
it 'should have many invoices' do
@subscription.should have_many(:invoices)
end
it 'should have many subsciption transactions' do
@subscription.should have_many(:transactions)
end
end
describe Subscription, 'validations' do
before :each do
@subscription = Subscription.create
end
it 'should be valid' do
@subscription.should be_valid
@subscription.state.should_not be_nil
end
it 'should not be valid if the last transaction is not successful' do
@subscription.decline_transaction!
subscription_transaction = SubscriptionTransactionSim.save_record_without_validation!(false, {:subscription => @subscription})
@subscription.transactions << subscription_transaction
@subscription.should_not be_valid
end
end
describe Subscription, 'fields' do
before :each do
@subscription = Subscription.new
end
it 'should hold a record in the database when deleted' do
@subscription.attributes.keys.should include('deleted_at')
end
it 'should have a field for the state machine' do
@subscription.attributes.keys.should include('state')
end
end
describe Subscription, 'states' do
before :each do
@subscription = Subscription.create
end
it 'should have a "pending authorization" state' do
@subscription.pending_authorization?.should be_true
end
it 'should have an "authorized" state' do
@subscription.authorize_payment!
@subscription.authorized?.should be_true
end
it 'should have a "subscribed" state' do
@subscription.authorize_payment!
@subscription.start_billing_plan!
@subscription.subscribed?.should be_true
end
it 'should have an "error" state' do
@subscription.error_transaction!
@subscription.error?.should be_true
end
it 'should have a "declined" state' do
@subscription.decline_transaction!
@subscription.declined?.should be_true
end
it 'should have a "cancelled" state' do
@subscription.authorize_payment!
@subscription.start_billing_plan!
@subscription.cancel!
@subscription.cancelled?.should be_true
end
it 'should have a "suspended" state' do
@subscription.suspend!
@subscription.suspended?.should be_true
end
end
describe Subscription, 'methods' do
before :each do
@subscription = Subscription.new
end
it 'should have a method for authorizing credit cards' do
@subscription.respond_to?("authorize_payment").should be_true
end
it 'should have a method for creating a recurring subscription' do
Subscription.respond_to?("create_recurring_subscription").should be_true
end
it 'should have a method for updating a recurring subscription' do
@subscription.respond_to?("edit_arb").should be_true
end
it 'should have a method for cancelling a recurring subscription' do
@subscription.respond_to?("cancel_arb").should be_true
end
it 'should have a method for building a hash of order information' do
@subscription.respond_to?("build_order_options_hash").should be_true
Subscription.respond_to?("build_order_options_hash").should be_true
end
it 'should have a method for building a hash of address information for orders' do
@subscription.respond_to?("build_address_hash").should be_true
end
end
describe Subscription, "#create_recurring_subscription" do
before :each do
@user = UserSim.create_record
@user.account.account_plan.price_per_month = 3700
@subscription = Subscription.create({:account_id => @user.account.id, :amount => @user.account.account_plan.price_per_month, :account_plan_id => @user.account.account_plan.id, :external_subscription_id => 330033, :state => 'subscribed'})
@created_subscription = nil
@subscription.stub!(:start_billing_plan!)
@credit_card = ActiveMerchantCreditCardSim.new_record
end
describe 'on success' do
before :each do
@credit_card = ActiveMerchantCreditCardSim.new_record
Subscription.stub!(:create).and_return(@subscription)
Subscription.stub!(:authorize_payment).and_return(@aim_response)
end
it 'should have two successful subscription transactions' do
Subscription.stub!(:create_recurring_subscription).and_return(@subscription)
@aim_response = SubscriptionTransactionSim.create_record(true, {:subscription_id => @subscription.id})
@arb_response = SubscriptionTransactionSim.create_record(true, {:subscription_id => @subscription.id})
@subscription.stub!(:transactions).and_return([@aim_response, @arb_response])
Subscription.create_recurring_subscription(@user, @credit_card)
@subscription.transactions.size.should == 2
end
it 'should not raise an exception' do
lambda { @created_subscription = Subscription.create_recurring_subscription(@user, @credit_card)}.should_not raise_error(ActiveMerchant::Billing::Error)
end
after :each do
@created_subscription.cancel_arb unless @created_subscription.nil?
end
end
describe 'on subscription transaction failure' do
before :each do
@bad_credit_card = ActiveMerchant::Billing::CreditCard.new({ 'first_name' => 'Test', 'last_name' => 'User', 'type' => 'Visa','verification_value' => '111','month' => '1', 'year' => '2009'})
@aim_failed_response = SubscriptionTransactionSim.create_record(false, {:subscription_id => @subscription.id})
@arb_failed_response = SubscriptionTransactionSim.create_record(false, {:subscription_id => @subscription.id})
Subscription.stub!(:create).and_return(@subscription)
end
it 'should raise a billing exception' do
lambda { @created_subscription = Subscription.create_recurring_subscription(@user, @bad_credit_card)}.should raise_error(ActiveMerchant::Billing::Error)
end
it 'should have a customized error message' do
begin
Subscription.create_recurring_subscription(@user, @bad_credit_card)
rescue ActiveMerchant::Billing::Error => e
e.message.should_not be_empty
end
end
after :each do
@created_subscription.cancel_arb unless @created_subscription.nil?
end
end
end
describe Subscription, ".build_address_hash" do
before :each do
@user = UserSim.create_record
@subscription = Subscription.new
end
it 'should return a hash with company name, first name and last name' do
address_hash = @subscription.build_address_hash(@user)
address_hash.should_not be_empty
address_hash.should be_a_kind_of(Hash)
address_hash.should have_key(:company)
address_hash.should have_key(:first_name)
address_hash.should have_key(:last_name)
end
it 'should return nil if user is nil' do
address_hash = @subscription.build_address_hash(nil)
address_hash.should be_nil
end
it 'should return nil if user.first_name is nil' do
@user.stub!(:first_name).and_return(nil)
address_hash = @subscription.build_address_hash(@user)
address_hash.should be_nil
end
it 'should return nil if user.last_name is nil' do
@user.stub!(:last_name).and_return(nil)
address_hash = @subscription.build_address_hash(@user)
address_hash.should be_nil
end
it 'should return nil if account.name is nil' do
@user.account.stub!(:name).and_return(nil)
address_hash = @subscription.build_address_hash(@user)
address_hash.should be_nil
end
end
describe Subscription, "#build_order_options_hash" do
before :each do
@user = UserSim.create_record
@active_merchant_credit_card = ActiveMerchantCreditCardSim.new_record
@subscription = Subscription.new
address = {
:first_name => 'Test',
:last_name => 'User',
:company => "Company, LLC"
}
@options = {
:customer => { :email => 'test@example.com', :id => 1},
:order => {:description => "Bantam Solo Plan for Test User"},
:subscription_name => "Bantam Solo Plan for Test User",
:billing_address => address,
:shipping_address => address
}
end
it 'should return a hash of all needed order options for creating a subscription' do
options = @subscription.build_order_options_hash(@user, "create")
options.should_not be_empty
options.should be_a_kind_of(Hash)
attributes = [:customer, :order, :subscription_name, :billing_address, :shipping_address, :interval, :duration, :trial]
attributes.each {|attribute|
requires!(options, attribute).should_not raise_error(ArgumentError)
}
end
it 'should return a hash of all needed order options for editing a subscription' do
options = @subscription.build_order_options_hash(@user, "update", @active_merchant_credit_card)
options.should_not be_empty
options.should be_a_kind_of(Hash)
attributes = [:customer, :order, :subscription_name, :billing_address, :shipping_address, :subscription_id, :amount, :trial]
attributes.each {|attribute|
requires!(options, attribute).should_not raise_error(ArgumentError)
}
end
it 'should return nil if user is nil' do
@user.stub!(:full_name).and_return('Test User')
options = @subscription.build_order_options_hash(nil, 'create')
options.should be_nil
end
end
describe Subscription, '#last_transaction' do
before :each do
@account_plan = AccountPlanSim.create_record(:price_per_month => rand(29900))
@account = AccountSim.create_record(:account_plan => @account_plan)
@credit_card = ActiveMerchantCreditCardSim.new_record
@subscription = Subscription.create!
@first_subscription_transaction = SubscriptionTransactionSim.create_record(true, {:subscription_id => @subscription.id})
@second_subscription_transaction = SubscriptionTransactionSim.create_record(true, {:subscription_id => @subscription.id})
@subscription.stub!(:transactions).and_return([@first_subscription_transaction, @second_subscription_transaction])
end
it 'should return the latest transaction if it exists' do
@subscription.last_transaction.should == @subscription.transactions[1]
end
it 'should return nil if there are no transactions' do
@subscription.transactions = []
@subscription.last_transaction.should be_nil
end
end
describe Subscription, '.authorization_constant' do
it 'should return a number' do
Subscription.authorization_constant.should be_a_kind_of(Fixnum)
end
end
describe Subscription, 'due_today' do
before :each do
@due_subscription = Subscription.create!(:next_invoice_due => DateTime.now)
@not_due_subscription = Subscription.create!(:next_invoice_due => DateTime.now.next_week)
end
it 'should find the subscriptions that are due today' do
Subscription.all.size.should == 2
Subscription.due_today.size.should == 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment