Fix for updating subscriptions.

Also switches tests to 'shoulda' from 'context'.
This commit is contained in:
Ross Boucher 2011-06-20 11:49:15 -07:00
parent e5e57bf992
commit 98a5766381
3 changed files with 68 additions and 36 deletions

View File

@ -314,7 +314,7 @@ module Stripe
end
def update_subscription(params)
response, api_key = Stripe.request(:post, subscription_url, @api_key)
response, api_key = Stripe.request(:post, subscription_url, @api_key, params)
refresh_from({ :subscription => response }, api_key, true)
subscription
end

View File

@ -97,6 +97,27 @@ def test_card(params={})
}.merge(params)
end
#FIXME nested overrides would be better than hardcoding plan_id
def test_subscription(plan_id="gold")
{
:current_period_end => 1308681468,
:status => "trialing",
:plan => {
:interval => "month",
:amount => 7500,
:trial_period_days => 30,
:object => "plan",
:identifier => plan_id
},
:current_period_start => 1308595038,
:start => 1308595038,
:object => "subscription",
:trial_start => 1308595038,
:trial_end => 1308681468,
:customer => "c_test_customer"
}
end
def test_invalid_api_key_error
{
"error" => {

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/test_helper.rb'
require 'test/unit'
require 'context'
require 'shoulda'
require 'mocha'
require 'pp'
require 'rest-client'
@ -10,21 +10,21 @@ class TestStripeRuby < Test::Unit::TestCase
include Mocha
context "API Bindings" do
before do
setup do
@mock = mock
Stripe.mock_rest_client = @mock
end
after do
teardown do
Stripe.mock_rest_client = nil
end
test "creating a new APIResource should not fetch over the network" do
should "creating a new APIResource should not fetch over the network" do
@mock.expects(:get).never
c = Stripe::Customer.new("someid")
end
test "creating a new APIResource from a hash should not fetch over the network" do
should "creating a new APIResource from a hash should not fetch over the network" do
@mock.expects(:get).never
c = Stripe::Customer.construct_from({
:id => "somecustomer",
@ -33,26 +33,26 @@ class TestStripeRuby < Test::Unit::TestCase
})
end
test "setting an attribute should not cause a network request" do
should "setting an attribute should not cause a network request" do
@mock.expects(:get).never
@mock.expects(:post).never
c = Stripe::Customer.new("test_customer");
c.card = {:id => "somecard", :object => "card"}
end
test "accessing id should not issue a fetch" do
should "accessing id should not issue a fetch" do
@mock.expects(:get).never
c = Stripe::Customer.new("test_customer");
c.id
end
test "not specifying api credentials should raise an exception" do
should "not specifying api credentials should raise an exception" do
assert_raises Stripe::AuthenticationError do
Stripe::Customer.new("test_customer").refresh
end
end
test "specifying invalid api credentials should raise an exception" do
should "specifying invalid api credentials should raise an exception" do
Stripe.api_key="invalid"
response = test_response(test_invalid_api_key_error, 401)
assert_raises Stripe::AuthenticationError do
@ -62,33 +62,33 @@ class TestStripeRuby < Test::Unit::TestCase
end
context "with valid credentials" do
before do
setup do
Stripe.api_key="foo"
end
after do
teardown do
Stripe.api_key=nil
end
test "requesting with a unicode ID should result in a request" do
should "requesting with a unicode ID should result in a request" do
response = test_response(test_missing_id_error, 404)
@mock.expects(:get).once.with("https://api.stripe.com/v1/customers/%E2%98%83").raises(RestClient::ExceptionWithResponse.new(response, 404))
c = Stripe::Customer.new("")
assert_raises(Stripe::InvalidRequestError) { c.refresh }
end
test "requesting with no ID shuold result in an InvalidRequestError with no request" do
should "requesting with no ID shuold result in an InvalidRequestError with no request" do
c = Stripe::Customer.new
assert_raises(Stripe::InvalidRequestError) { c.refresh }
end
test "loading an object should issue a GET request" do
should "loading an object should issue a GET request" do
@mock.expects(:get).once.returns(test_response(test_customer))
c = Stripe::Customer.new("test_customer")
c.refresh
end
test "using array accessors should be the same as the method interface" do
should "using array accessors should be the same as the method interface" do
@mock.expects(:get).once.returns(test_response(test_customer))
c = Stripe::Customer.new("test_customer")
c.refresh
@ -98,20 +98,20 @@ class TestStripeRuby < Test::Unit::TestCase
assert_equal c.created, 12345
end
test "accessing a property other than id or parent on an unfetched object should fetch it" do
should "accessing a property other than id or parent on an unfetched object should fetch it" do
@mock.expects(:get).once.returns(test_response(test_customer))
c = Stripe::Customer.new("test_customer")
c.charges
end
test "updating an object should issue a POST request with only the changed properties" do
should "updating an object should issue a POST request with only the changed properties" do
@mock.expects(:post).with("https://api.stripe.com/v1/customers/c_test_customer", {:mnemonic => 'another_mn'}).once.returns(test_response(test_customer))
c = Stripe::Customer.construct_from(test_customer)
c.mnemonic = "another_mn"
c.save
end
test "updating should merge in returned properties" do
should "updating should merge in returned properties" do
@mock.expects(:post).once.returns(test_response(test_customer))
c = Stripe::Customer.new("c_test_customer")
c.mnemonic = "another_mn"
@ -119,7 +119,7 @@ class TestStripeRuby < Test::Unit::TestCase
assert_equal false, c.livemode
end
test "deleting should send no props and result in an object that has no props other deleted" do
should "deleting should send no props and result in an object that has no props other deleted" do
@mock.expects(:get).never
@mock.expects(:post).never
@mock.expects(:delete).with("https://api.stripe.com/v1/customers/c_test_customer").once.returns(test_response({ "id" => "test_customer", "deleted" => true }))
@ -133,13 +133,13 @@ class TestStripeRuby < Test::Unit::TestCase
end
end
test "loading an object with properties that have specific types should instantiate those classes" do
should "loading an object with properties that have specific types should instantiate those classes" do
@mock.expects(:get).once.returns(test_response(test_charge))
c = Stripe::Charge.retrieve("test_charge")
assert c.card.kind_of?(Stripe::StripeObject) && c.card.object == 'card'
end
test "loading all of an APIResource should return an array of recursively instantiated objects" do
should "loading all of an APIResource should return an array of recursively instantiated objects" do
@mock.expects(:get).once.returns(test_response(test_charge_array))
c = Stripe::Charge.all
assert c.kind_of? Array
@ -149,13 +149,13 @@ class TestStripeRuby < Test::Unit::TestCase
context "charge tests" do
test "charges should be listable" do
should "charges should be listable" do
@mock.expects(:get).once.returns(test_response(test_charge_array))
c = Stripe::Charge.all
assert c.kind_of? Array
end
test "charges should be refundable" do
should "charges should be refundable" do
@mock.expects(:get).never
@mock.expects(:post).once.returns(test_response({:id => "ch_test_charge", :refunded => true}))
c = Stripe::Charge.new("test_charge")
@ -163,7 +163,7 @@ class TestStripeRuby < Test::Unit::TestCase
assert c.refunded
end
test "charges should not be deletable" do
should "charges should not be deletable" do
assert_raises NoMethodError do
@mock.expects(:get).once.returns(test_response(test_charge))
c = Stripe::Charge.retrieve("test_charge")
@ -171,7 +171,7 @@ class TestStripeRuby < Test::Unit::TestCase
end
end
test "charges should not be updateable" do
should "charges should not be updateable" do
assert_raises NoMethodError do
@mock.expects(:get).once.returns(test_response(test_charge))
c = Stripe::Charge.new("test_charge")
@ -181,13 +181,13 @@ class TestStripeRuby < Test::Unit::TestCase
end
end
test "charges should have Card objects associated with their Card property" do
should "charges should have Card objects associated with their Card property" do
@mock.expects(:get).once.returns(test_response(test_charge))
c = Stripe::Charge.retrieve("test_charge")
assert c.card.kind_of?(Stripe::StripeObject) && c.card.object == 'card'
end
test "execute should return a new, fully executed charge when passed correct parameters" do
should "execute should return a new, fully executed charge when passed correct parameters" do
@mock.expects(:post).with('https://api.stripe.com/v1/charges', {
:currency => 'usd', :amount => 100,
:card => {:exp_year => 2012, :number => '4242424242424242', :exp_month => 11}
@ -209,21 +209,21 @@ class TestStripeRuby < Test::Unit::TestCase
context "customer tests" do
test "customers should be listable" do
should "customers should be listable" do
@mock.expects(:get).once.returns(test_response(test_customer_array))
c = Stripe::Customer.all
assert c.kind_of? Array
assert c[0].kind_of? Stripe::Customer
end
test "customers should be deletable" do
should "customers should be deletable" do
@mock.expects(:delete).once.returns(test_response(test_customer({:deleted => true})))
c = Stripe::Customer.new("test_customer")
c.delete
assert c.deleted
end
test "customers should be updateable" do
should "customers should be updateable" do
@mock.expects(:get).once.returns(test_response(test_customer({:mnemonic => "foo"})))
@mock.expects(:post).once.returns(test_response(test_customer({:mnemonic => "bar"})))
c = Stripe::Customer.new("test_customer").refresh
@ -233,18 +233,29 @@ class TestStripeRuby < Test::Unit::TestCase
assert_equal c.mnemonic, "bar"
end
test "customers should have Card objects associated with their active_ard property" do
should "customers should have Card objects associated with their active_ard property" do
@mock.expects(:get).once.returns(test_response(test_customer))
c = Stripe::Customer.retrieve("test_customer")
assert c.active_card.kind_of?(Stripe::StripeObject) && c.active_card.object == 'card'
end
test "create should return a new customer" do
should "create should return a new customer" do
@mock.expects(:post).once.returns(test_response(test_customer))
c = Stripe::Customer.create
assert_equal "c_test_customer", c.id
end
should "be able to update a customers subscription" do
@mock.expects(:get).once.returns(test_response(test_customer))
c = Stripe::Customer.retrieve("test_customer")
@mock.expects(:post).once.with("https://api.stripe.com/v1/customers/c_test_customer/subscription", {:plan => 'silver'}).returns(test_response(test_subscription('silver')))
s = c.update_subscription({:plan => 'silver'})
assert_equal 'subscription', s.object
assert_equal 'silver', s.plan.identifier
end
end
context "card tests" do
@ -252,7 +263,7 @@ class TestStripeRuby < Test::Unit::TestCase
context "error checking" do
test "404s should raise an InvalidRequestError" do
should "404s should raise an InvalidRequestError" do
response = test_response(test_missing_id_error, 404)
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
@ -269,7 +280,7 @@ class TestStripeRuby < Test::Unit::TestCase
assert false #shouldn't get here
end
test "5XXs should raise an APIError" do
should "5XXs should raise an APIError" do
response = test_response(test_api_error, 500)
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 500))
@ -284,7 +295,7 @@ class TestStripeRuby < Test::Unit::TestCase
assert false #shouldn't get here
end
test "402s should raise a CardError" do
should "402s should raise a CardError" do
response = test_response(test_invalid_exp_year_error, 402)
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 402))