mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-11-27 00:03:06 -05:00
active_card is no longer a thing
This commit is contained in:
commit
f9b13dfc2a
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
/stripe-*.gem
|
||||
/Gemfile.lock
|
||||
.rvmrc
|
||||
Gemfile.lock
|
||||
|
||||
12
Rakefile
12
Rakefile
@ -1,9 +1,15 @@
|
||||
task :default => [:test]
|
||||
task :default => [:all]
|
||||
|
||||
task :test do
|
||||
ret = true
|
||||
Dir["test/**/*.rb"].each do |f|
|
||||
Dir["test/**/*_test.rb"].each do |f|
|
||||
ret = ret && ruby(f, '')
|
||||
end
|
||||
exit(ret)
|
||||
end
|
||||
|
||||
task :all do
|
||||
Rake::Task["test"].invoke
|
||||
require 'active_support/all'
|
||||
Rake::Task["test"].reenable
|
||||
Rake::Task["test"].invoke
|
||||
end
|
||||
14
test/stripe/account_test.rb
Normal file
14
test/stripe/account_test.rb
Normal file
@ -0,0 +1,14 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
module Stripe
|
||||
class AccountTest < Test::Unit::TestCase
|
||||
should "account should be retrievable" do
|
||||
resp = {:email => "test+bindings@stripe.com", :charge_enabled => false, :details_submitted => false}
|
||||
@mock.expects(:get).once.returns(test_response(resp))
|
||||
a = Stripe::Account.retrieve
|
||||
assert_equal "test+bindings@stripe.com", a.email
|
||||
assert !a.charge_enabled
|
||||
assert !a.details_submitted
|
||||
end
|
||||
end
|
||||
end
|
||||
345
test/stripe/api_resource_test.rb
Normal file
345
test/stripe/api_resource_test.rb
Normal file
@ -0,0 +1,345 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
module Stripe
|
||||
class ApiResourceTest < Test::Unit::TestCase
|
||||
should "creating a new APIResource should not fetch over the network" do
|
||||
@mock.expects(:get).never
|
||||
c = Stripe::Customer.new("someid")
|
||||
end
|
||||
|
||||
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",
|
||||
:card => {:id => "somecard", :object => "card"},
|
||||
:object => "customer"
|
||||
})
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
should "accessing id should not issue a fetch" do
|
||||
@mock.expects(:get).never
|
||||
c = Stripe::Customer.new("test_customer");
|
||||
c.id
|
||||
end
|
||||
|
||||
should "not specifying api credentials should raise an exception" do
|
||||
Stripe.api_key = nil
|
||||
assert_raises Stripe::AuthenticationError do
|
||||
Stripe::Customer.new("test_customer").refresh
|
||||
end
|
||||
end
|
||||
|
||||
should "specifying api credentials containing whitespace should raise an exception" do
|
||||
Stripe.api_key = "key "
|
||||
assert_raises Stripe::AuthenticationError do
|
||||
Stripe::Customer.new("test_customer").refresh
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
|
||||
Stripe::Customer.retrieve("failing_customer")
|
||||
end
|
||||
end
|
||||
|
||||
should "AuthenticationErrors should have an http status, http body, and JSON body" do
|
||||
Stripe.api_key = "invalid"
|
||||
response = test_response(test_invalid_api_key_error, 401)
|
||||
begin
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
|
||||
Stripe::Customer.retrieve("failing_customer")
|
||||
rescue Stripe::AuthenticationError => e
|
||||
assert_equal(401, e.http_status)
|
||||
assert_equal(true, !!e.http_body)
|
||||
assert_equal(true, !!e.json_body[:error][:message])
|
||||
assert_equal(test_invalid_api_key_error['error']['message'], e.json_body[:error][:message])
|
||||
end
|
||||
end
|
||||
|
||||
context "when specifying per-object credentials" do
|
||||
context "with no global API key set" do
|
||||
should "use the per-object credential when creating" do
|
||||
Stripe.expects(:execute_request).with do |opts|
|
||||
opts[:headers][:authorization] == 'Bearer sk_test_local'
|
||||
end.returns(test_response(test_charge))
|
||||
|
||||
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
|
||||
'sk_test_local')
|
||||
end
|
||||
end
|
||||
|
||||
context "with a global API key set" do
|
||||
setup do
|
||||
Stripe.api_key = "global"
|
||||
end
|
||||
|
||||
teardown do
|
||||
Stripe.api_key = nil
|
||||
end
|
||||
|
||||
should "use the per-object credential when creating" do
|
||||
Stripe.expects(:execute_request).with do |opts|
|
||||
opts[:headers][:authorization] == 'Bearer local'
|
||||
end.returns(test_response(test_charge))
|
||||
|
||||
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
|
||||
'local')
|
||||
end
|
||||
|
||||
should "use the per-object credential when retrieving and making other calls" do
|
||||
Stripe.expects(:execute_request).with do |opts|
|
||||
opts[:url] == "#{Stripe.api_base}/v1/charges/ch_test_charge" &&
|
||||
opts[:headers][:authorization] == 'Bearer local'
|
||||
end.returns(test_response(test_charge))
|
||||
Stripe.expects(:execute_request).with do |opts|
|
||||
opts[:url] == "#{Stripe.api_base}/v1/charges/ch_test_charge/refund" &&
|
||||
opts[:headers][:authorization] == 'Bearer local'
|
||||
end.returns(test_response(test_charge))
|
||||
|
||||
ch = Stripe::Charge.retrieve('ch_test_charge', 'local')
|
||||
ch.refund
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with valid credentials" do
|
||||
should "urlencode values in GET params" do
|
||||
response = test_response(test_charge_array)
|
||||
@mock.expects(:get).with("#{Stripe.api_base}/v1/charges?customer=test%20customer", nil, nil).returns(response)
|
||||
charges = Stripe::Charge.all(:customer => 'test customer').data
|
||||
assert charges.kind_of? Array
|
||||
end
|
||||
|
||||
should "construct URL properly with base query parameters" do
|
||||
response = test_response(test_invoice_customer_array)
|
||||
@mock.expects(:get).with("#{Stripe.api_base}/v1/invoices?customer=test_customer", nil, nil).returns(response)
|
||||
invoices = Stripe::Invoice.all(:customer => 'test_customer')
|
||||
|
||||
@mock.expects(:get).with("#{Stripe.api_base}/v1/invoices?customer=test_customer&paid=true", nil, nil).returns(response)
|
||||
invoices.all(:paid => true)
|
||||
end
|
||||
|
||||
should "a 400 should give an InvalidRequestError with http status, body, and JSON body" do
|
||||
response = test_response(test_missing_id_error, 400)
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
||||
begin
|
||||
Stripe::Customer.retrieve("foo")
|
||||
rescue Stripe::InvalidRequestError => e
|
||||
assert_equal(400, e.http_status)
|
||||
assert_equal(true, !!e.http_body)
|
||||
assert_equal(true, e.json_body.kind_of?(Hash))
|
||||
end
|
||||
end
|
||||
|
||||
should "a 401 should give an AuthenticationError with http status, body, and JSON body" do
|
||||
response = test_response(test_missing_id_error, 401)
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
||||
begin
|
||||
Stripe::Customer.retrieve("foo")
|
||||
rescue Stripe::AuthenticationError => e
|
||||
assert_equal(401, e.http_status)
|
||||
assert_equal(true, !!e.http_body)
|
||||
assert_equal(true, e.json_body.kind_of?(Hash))
|
||||
end
|
||||
end
|
||||
|
||||
should "a 402 should give a CardError with http status, body, and JSON body" do
|
||||
response = test_response(test_missing_id_error, 402)
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
||||
begin
|
||||
Stripe::Customer.retrieve("foo")
|
||||
rescue Stripe::CardError => e
|
||||
assert_equal(402, e.http_status)
|
||||
assert_equal(true, !!e.http_body)
|
||||
assert_equal(true, e.json_body.kind_of?(Hash))
|
||||
end
|
||||
end
|
||||
|
||||
should "a 404 should give an InvalidRequestError with http status, body, and JSON body" do
|
||||
response = test_response(test_missing_id_error, 404)
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
||||
begin
|
||||
Stripe::Customer.retrieve("foo")
|
||||
rescue Stripe::InvalidRequestError => e
|
||||
assert_equal(404, e.http_status)
|
||||
assert_equal(true, !!e.http_body)
|
||||
assert_equal(true, e.json_body.kind_of?(Hash))
|
||||
end
|
||||
end
|
||||
|
||||
should "setting a nil value for a param should exclude that param from the request" do
|
||||
@mock.expects(:get).with do |url, api_key, params|
|
||||
uri = URI(url)
|
||||
query = CGI.parse(uri.query)
|
||||
(url =~ %r{^#{Stripe.api_base}/v1/charges?} &&
|
||||
query.keys.sort == ['offset', 'sad'])
|
||||
end.returns(test_response({ :count => 1, :data => [test_charge] }))
|
||||
c = Stripe::Charge.all(:count => nil, :offset => 5, :sad => false)
|
||||
|
||||
@mock.expects(:post).with do |url, api_key, params|
|
||||
url == "#{Stripe.api_base}/v1/charges" &&
|
||||
api_key.nil? &&
|
||||
CGI.parse(params) == { 'amount' => ['50'], 'currency' => ['usd'] }
|
||||
end.returns(test_response({ :count => 1, :data => [test_charge] }))
|
||||
c = Stripe::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil })
|
||||
end
|
||||
|
||||
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("#{Stripe.api_base}/v1/customers/%E2%98%83", nil, nil).raises(RestClient::ExceptionWithResponse.new(response, 404))
|
||||
c = Stripe::Customer.new("☃")
|
||||
assert_raises(Stripe::InvalidRequestError) { c.refresh }
|
||||
end
|
||||
|
||||
should "requesting with no ID should result in an InvalidRequestError with no request" do
|
||||
c = Stripe::Customer.new
|
||||
assert_raises(Stripe::InvalidRequestError) { c.refresh }
|
||||
end
|
||||
|
||||
should "making a GET request with parameters should have a query string and no body" do
|
||||
params = { :limit => 1 }
|
||||
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/charges?limit=1", nil, nil).returns(test_response([test_charge]))
|
||||
c = Stripe::Charge.all(params)
|
||||
end
|
||||
|
||||
should "making a POST request with parameters should have a body and no query string" do
|
||||
params = { :amount => 100, :currency => 'usd', :card => 'sc_token' }
|
||||
@mock.expects(:post).once.with do |url, get, post|
|
||||
get.nil? && CGI.parse(post) == {'amount' => ['100'], 'currency' => ['usd'], 'card' => ['sc_token']}
|
||||
end.returns(test_response(test_charge))
|
||||
c = Stripe::Charge.create(params)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
assert_equal c.created, c[:created]
|
||||
assert_equal c.created, c['created']
|
||||
c['created'] = 12345
|
||||
assert_equal c.created, 12345
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
should "updating an object should issue a POST request with only the changed properties" do
|
||||
@mock.expects(:post).with do |url, api_key, params|
|
||||
url == "#{Stripe.api_base}/v1/customers/c_test_customer" && api_key.nil? && CGI.parse(params) == {'description' => ['another_mn']}
|
||||
end.once.returns(test_response(test_customer))
|
||||
c = Stripe::Customer.construct_from(test_customer)
|
||||
c.description = "another_mn"
|
||||
c.save
|
||||
end
|
||||
|
||||
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.description = "another_mn"
|
||||
c.save
|
||||
assert_equal false, c.livemode
|
||||
end
|
||||
|
||||
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("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).once.returns(test_response({ "id" => "test_customer", "deleted" => true }))
|
||||
|
||||
c = Stripe::Customer.construct_from(test_customer)
|
||||
c.delete
|
||||
assert_equal true, c.deleted
|
||||
|
||||
assert_raises NoMethodError do
|
||||
c.livemode
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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.data
|
||||
assert c.kind_of? Array
|
||||
assert c[0].kind_of? Stripe::Charge
|
||||
assert c[0].card.kind_of?(Stripe::StripeObject) && c[0].card.object == 'card'
|
||||
end
|
||||
|
||||
context "error checking" 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))
|
||||
|
||||
begin
|
||||
Stripe::Customer.new("test_customer").refresh
|
||||
assert false #shouldn't get here either
|
||||
rescue Stripe::InvalidRequestError => e # we don't use assert_raises because we want to examine e
|
||||
assert e.kind_of? Stripe::InvalidRequestError
|
||||
assert_equal "id", e.param
|
||||
assert_equal "Missing id", e.message
|
||||
return
|
||||
end
|
||||
|
||||
assert false #shouldn't get here
|
||||
end
|
||||
|
||||
should "5XXs should raise an APIError" do
|
||||
response = test_response(test_api_error, 500)
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 500))
|
||||
|
||||
begin
|
||||
Stripe::Customer.new("test_customer").refresh
|
||||
assert false #shouldn't get here either
|
||||
rescue Stripe::APIError => e # we don't use assert_raises because we want to examine e
|
||||
assert e.kind_of? Stripe::APIError
|
||||
return
|
||||
end
|
||||
|
||||
assert false #shouldn't get here
|
||||
end
|
||||
|
||||
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))
|
||||
|
||||
begin
|
||||
Stripe::Customer.new("test_customer").refresh
|
||||
assert false #shouldn't get here either
|
||||
rescue Stripe::CardError => e # we don't use assert_raises because we want to examine e
|
||||
assert e.kind_of? Stripe::CardError
|
||||
assert_equal "invalid_expiry_year", e.code
|
||||
assert_equal "exp_year", e.param
|
||||
assert_equal "Your card's expiration year is invalid", e.message
|
||||
return
|
||||
end
|
||||
|
||||
assert false #shouldn't get here
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
67
test/stripe/charge_test.rb
Normal file
67
test/stripe/charge_test.rb
Normal file
@ -0,0 +1,67 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
module Stripe
|
||||
class ChargeTest < Test::Unit::TestCase
|
||||
should "charges should be listable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_charge_array))
|
||||
c = Stripe::Charge.all
|
||||
assert c.data.kind_of? Array
|
||||
c.each do |charge|
|
||||
assert charge.kind_of?(Stripe::Charge)
|
||||
end
|
||||
end
|
||||
|
||||
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")
|
||||
c.refund
|
||||
assert c.refunded
|
||||
end
|
||||
|
||||
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")
|
||||
c.delete
|
||||
end
|
||||
end
|
||||
|
||||
should "charges should be updateable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_charge))
|
||||
@mock.expects(:post).once.returns(test_response(test_charge))
|
||||
c = Stripe::Charge.new("test_charge")
|
||||
c.refresh
|
||||
c.mnemonic = "New charge description"
|
||||
c.save
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
should "execute should return a new, fully executed charge when passed correct parameters" do
|
||||
@mock.expects(:post).with do |url, api_key, params|
|
||||
url == "#{Stripe.api_base}/v1/charges" && api_key.nil? && CGI.parse(params) == {
|
||||
'currency' => ['usd'], 'amount' => ['100'],
|
||||
'card[exp_year]' => ['2012'],
|
||||
'card[number]' => ['4242424242424242'],
|
||||
'card[exp_month]' => ['11']
|
||||
}
|
||||
end.once.returns(test_response(test_charge))
|
||||
|
||||
c = Stripe::Charge.create({
|
||||
:amount => 100,
|
||||
:card => {
|
||||
:number => "4242424242424242",
|
||||
:exp_month => 11,
|
||||
:exp_year => 2012,
|
||||
},
|
||||
:currency => "usd"
|
||||
})
|
||||
assert c.paid
|
||||
end
|
||||
end
|
||||
end
|
||||
11
test/stripe/coupon_test.rb
Normal file
11
test/stripe/coupon_test.rb
Normal file
@ -0,0 +1,11 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
module Stripe
|
||||
class CouponTest < Test::Unit::TestCase
|
||||
should "create should return a new coupon" do
|
||||
@mock.expects(:post).once.returns(test_response(test_coupon))
|
||||
c = Stripe::Coupon.create
|
||||
assert_equal "co_test_coupon", c.id
|
||||
end
|
||||
end
|
||||
end
|
||||
70
test/stripe/customer_test.rb
Normal file
70
test/stripe/customer_test.rb
Normal file
@ -0,0 +1,70 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
module Stripe
|
||||
class CustomerTest < Test::Unit::TestCase
|
||||
should "customers should be listable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer_array))
|
||||
c = Stripe::Customer.all.data
|
||||
assert c.kind_of? Array
|
||||
assert c[0].kind_of? Stripe::Customer
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
assert_equal c.mnemonic, "foo"
|
||||
c.mnemonic = "bar"
|
||||
c.save
|
||||
assert_equal c.mnemonic, "bar"
|
||||
end
|
||||
|
||||
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 customer's subscription" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
c = Stripe::Customer.retrieve("test_customer")
|
||||
|
||||
@mock.expects(:post).once.with do |url, api_key, params|
|
||||
url == "#{Stripe.api_base}/v1/customers/c_test_customer/subscription" && api_key.nil? && CGI.parse(params) == {'plan' => ['silver']}
|
||||
end.returns(test_response(test_subscription('silver')))
|
||||
s = c.update_subscription({:plan => 'silver'})
|
||||
|
||||
assert_equal 'subscription', s.object
|
||||
assert_equal 'silver', s.plan.identifier
|
||||
end
|
||||
|
||||
should "be able to cancel a customer's subscription" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
c = Stripe::Customer.retrieve("test_customer")
|
||||
|
||||
# Not an accurate response, but whatever
|
||||
|
||||
@mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscription?at_period_end=true", nil, nil).returns(test_response(test_subscription('silver')))
|
||||
s = c.cancel_subscription({:at_period_end => 'true'})
|
||||
|
||||
@mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscription", nil, nil).returns(test_response(test_subscription('silver')))
|
||||
s = c.cancel_subscription
|
||||
end
|
||||
|
||||
should "be able to delete a customer's discount" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
c = Stripe::Customer.retrieve("test_customer")
|
||||
|
||||
@mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/discount", nil, nil).returns(test_response(test_delete_discount_response))
|
||||
s = c.delete_discount
|
||||
assert_equal nil, c.discount
|
||||
end
|
||||
end
|
||||
end
|
||||
20
test/stripe/invoice_test.rb
Normal file
20
test/stripe/invoice_test.rb
Normal file
@ -0,0 +1,20 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
module Stripe
|
||||
class InvoiceTest < Test::Unit::TestCase
|
||||
should "retrieve should retrieve invoices" do
|
||||
@mock.expects(:get).once.returns(test_response(test_invoice))
|
||||
i = Stripe::Invoice.retrieve('in_test_invoice')
|
||||
assert_equal 'in_test_invoice', i.id
|
||||
end
|
||||
|
||||
should "pay should pay an invoice" do
|
||||
@mock.expects(:get).once.returns(test_response(test_invoice))
|
||||
i = Stripe::Invoice.retrieve('in_test_invoice')
|
||||
|
||||
@mock.expects(:post).once.with('https://api.stripe.com/v1/invoices/in_test_invoice/pay', nil, '').returns(test_response(test_paid_invoice))
|
||||
i.pay
|
||||
assert_equal i.next_payment_attempt, nil
|
||||
end
|
||||
end
|
||||
end
|
||||
16
test/stripe/list_object_test.rb
Normal file
16
test/stripe/list_object_test.rb
Normal file
@ -0,0 +1,16 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
module Stripe
|
||||
class ListObjectTest < Test::Unit::TestCase
|
||||
should "be able to retrieve full lists given a listobject" do
|
||||
@mock.expects(:get).twice.returns(test_response(test_charge_array))
|
||||
c = Stripe::Charge.all
|
||||
assert c.kind_of?(Stripe::ListObject)
|
||||
assert_equal('/v1/charges', c.url)
|
||||
all = c.all
|
||||
assert all.kind_of?(Stripe::ListObject)
|
||||
assert_equal('/v1/charges', all.url)
|
||||
assert all.data.kind_of?(Array)
|
||||
end
|
||||
end
|
||||
end
|
||||
29
test/stripe/util_test.rb
Normal file
29
test/stripe/util_test.rb
Normal file
@ -0,0 +1,29 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
module Stripe
|
||||
class UtilTest < Test::Unit::TestCase
|
||||
should "symbolize_names should convert names to symbols" do
|
||||
start = {
|
||||
'foo' => 'bar',
|
||||
'array' => [{ 'foo' => 'bar' }],
|
||||
'nested' => {
|
||||
1 => 2,
|
||||
:symbol => 9,
|
||||
'string' => nil
|
||||
}
|
||||
}
|
||||
finish = {
|
||||
:foo => 'bar',
|
||||
:array => [{ :foo => 'bar' }],
|
||||
:nested => {
|
||||
1 => 2,
|
||||
:symbol => 9,
|
||||
:string => nil
|
||||
}
|
||||
}
|
||||
|
||||
symbolized = Stripe::Util.symbolize_names(start)
|
||||
assert_equal(finish, symbolized)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,8 +1,8 @@
|
||||
require 'stringio'
|
||||
require 'test/unit'
|
||||
require 'stripe'
|
||||
require 'test/unit'
|
||||
require 'mocha/setup'
|
||||
include Mocha
|
||||
require 'stringio'
|
||||
require 'shoulda'
|
||||
|
||||
#monkeypatch request methods
|
||||
module Stripe
|
||||
@ -334,3 +334,19 @@ def test_delete_discount_response
|
||||
:id => "di_test_coupon"
|
||||
}
|
||||
end
|
||||
|
||||
class Test::Unit::TestCase
|
||||
include Mocha
|
||||
|
||||
setup do
|
||||
@mock = mock
|
||||
Stripe.mock_rest_client = @mock
|
||||
Stripe.api_key="foo"
|
||||
end
|
||||
|
||||
teardown do
|
||||
Stripe.mock_rest_client = nil
|
||||
Stripe.api_key=nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -1,767 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
require File.expand_path('../test_helper', __FILE__)
|
||||
require 'test/unit'
|
||||
require 'shoulda'
|
||||
require 'mocha/setup'
|
||||
require 'pp'
|
||||
require 'rest-client'
|
||||
require 'cgi'
|
||||
require 'uri'
|
||||
|
||||
class TestStripeRuby < Test::Unit::TestCase
|
||||
include Mocha
|
||||
|
||||
context "Util" do
|
||||
should "symbolize_names should convert names to symbols" do
|
||||
start = {
|
||||
'foo' => 'bar',
|
||||
'array' => [{ 'foo' => 'bar' }],
|
||||
'nested' => {
|
||||
1 => 2,
|
||||
:symbol => 9,
|
||||
'string' => nil
|
||||
}
|
||||
}
|
||||
finish = {
|
||||
:foo => 'bar',
|
||||
:array => [{ :foo => 'bar' }],
|
||||
:nested => {
|
||||
1 => 2,
|
||||
:symbol => 9,
|
||||
:string => nil
|
||||
}
|
||||
}
|
||||
|
||||
symbolized = Stripe::Util.symbolize_names(start)
|
||||
assert_equal(finish, symbolized)
|
||||
end
|
||||
end
|
||||
|
||||
context "API Bindings" do
|
||||
setup do
|
||||
@mock = mock
|
||||
Stripe.mock_rest_client = @mock
|
||||
end
|
||||
|
||||
teardown do
|
||||
Stripe.mock_rest_client = nil
|
||||
end
|
||||
|
||||
should "creating a new APIResource should not fetch over the network" do
|
||||
@mock.expects(:get).never
|
||||
c = Stripe::Customer.new("someid")
|
||||
end
|
||||
|
||||
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",
|
||||
:card => {:id => "somecard", :object => "card"},
|
||||
:object => "customer"
|
||||
})
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
should "accessing id should not issue a fetch" do
|
||||
@mock.expects(:get).never
|
||||
c = Stripe::Customer.new("test_customer");
|
||||
c.id
|
||||
end
|
||||
|
||||
should "not specifying api credentials should raise an exception" do
|
||||
Stripe.api_key = nil
|
||||
assert_raises Stripe::AuthenticationError do
|
||||
Stripe::Customer.new("test_customer").refresh
|
||||
end
|
||||
end
|
||||
|
||||
should "specifying api credentials containing whitespace should raise an exception" do
|
||||
Stripe.api_key = "key "
|
||||
assert_raises Stripe::AuthenticationError do
|
||||
Stripe::Customer.new("test_customer").refresh
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
|
||||
Stripe::Customer.retrieve("failing_customer")
|
||||
end
|
||||
end
|
||||
|
||||
should "AuthenticationErrors should have an http status, http body, and JSON body" do
|
||||
Stripe.api_key = "invalid"
|
||||
response = test_response(test_invalid_api_key_error, 401)
|
||||
begin
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
|
||||
Stripe::Customer.retrieve("failing_customer")
|
||||
rescue Stripe::AuthenticationError => e
|
||||
assert_equal(401, e.http_status)
|
||||
assert_equal(true, !!e.http_body)
|
||||
assert_equal(true, !!e.json_body[:error][:message])
|
||||
assert_equal(test_invalid_api_key_error['error']['message'], e.json_body[:error][:message])
|
||||
end
|
||||
end
|
||||
|
||||
context "when specifying per-object credentials" do
|
||||
context "with no global API key set" do
|
||||
should "use the per-object credential when creating" do
|
||||
Stripe.expects(:execute_request).with do |opts|
|
||||
opts[:headers][:authorization] == 'Bearer sk_test_local'
|
||||
end.returns(test_response(test_charge))
|
||||
|
||||
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
|
||||
'sk_test_local')
|
||||
end
|
||||
end
|
||||
|
||||
context "with a global API key set" do
|
||||
setup do
|
||||
Stripe.api_key = "global"
|
||||
end
|
||||
|
||||
teardown do
|
||||
Stripe.api_key = nil
|
||||
end
|
||||
|
||||
should "use the per-object credential when creating" do
|
||||
Stripe.expects(:execute_request).with do |opts|
|
||||
opts[:headers][:authorization] == 'Bearer local'
|
||||
end.returns(test_response(test_charge))
|
||||
|
||||
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
|
||||
'local')
|
||||
end
|
||||
|
||||
should "use the per-object credential when retrieving and making other calls" do
|
||||
Stripe.expects(:execute_request).with do |opts|
|
||||
opts[:url] == "#{Stripe.api_base}/v1/charges/ch_test_charge" &&
|
||||
opts[:headers][:authorization] == 'Bearer local'
|
||||
end.returns(test_response(test_charge))
|
||||
Stripe.expects(:execute_request).with do |opts|
|
||||
opts[:url] == "#{Stripe.api_base}/v1/charges/ch_test_charge/refund" &&
|
||||
opts[:headers][:authorization] == 'Bearer local'
|
||||
end.returns(test_response(test_charge))
|
||||
|
||||
ch = Stripe::Charge.retrieve('ch_test_charge', 'local')
|
||||
ch.refund
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with valid credentials" do
|
||||
setup do
|
||||
Stripe.api_key="foo"
|
||||
end
|
||||
|
||||
teardown do
|
||||
Stripe.api_key=nil
|
||||
end
|
||||
|
||||
should "urlencode values in GET params" do
|
||||
response = test_response(test_charge_array)
|
||||
@mock.expects(:get).with("#{Stripe.api_base}/v1/charges?customer=test%20customer", nil, nil).returns(response)
|
||||
charges = Stripe::Charge.all(:customer => 'test customer').data
|
||||
assert charges.kind_of? Array
|
||||
end
|
||||
|
||||
should "construct URL properly with base query parameters" do
|
||||
response = test_response(test_invoice_customer_array)
|
||||
@mock.expects(:get).with("#{Stripe.api_base}/v1/invoices?customer=test_customer", nil, nil).returns(response)
|
||||
invoices = Stripe::Invoice.all(:customer => 'test_customer')
|
||||
|
||||
@mock.expects(:get).with("#{Stripe.api_base}/v1/invoices?customer=test_customer&paid=true", nil, nil).returns(response)
|
||||
invoices.all(:paid => true)
|
||||
end
|
||||
|
||||
should "a 400 should give an InvalidRequestError with http status, body, and JSON body" do
|
||||
response = test_response(test_missing_id_error, 400)
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
||||
begin
|
||||
Stripe::Customer.retrieve("foo")
|
||||
rescue Stripe::InvalidRequestError => e
|
||||
assert_equal(400, e.http_status)
|
||||
assert_equal(true, !!e.http_body)
|
||||
assert_equal(true, e.json_body.kind_of?(Hash))
|
||||
end
|
||||
end
|
||||
|
||||
should "a 401 should give an AuthenticationError with http status, body, and JSON body" do
|
||||
response = test_response(test_missing_id_error, 401)
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
||||
begin
|
||||
Stripe::Customer.retrieve("foo")
|
||||
rescue Stripe::AuthenticationError => e
|
||||
assert_equal(401, e.http_status)
|
||||
assert_equal(true, !!e.http_body)
|
||||
assert_equal(true, e.json_body.kind_of?(Hash))
|
||||
end
|
||||
end
|
||||
|
||||
should "a 402 should give a CardError with http status, body, and JSON body" do
|
||||
response = test_response(test_missing_id_error, 402)
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
||||
begin
|
||||
Stripe::Customer.retrieve("foo")
|
||||
rescue Stripe::CardError => e
|
||||
assert_equal(402, e.http_status)
|
||||
assert_equal(true, !!e.http_body)
|
||||
assert_equal(true, e.json_body.kind_of?(Hash))
|
||||
end
|
||||
end
|
||||
|
||||
should "a 404 should give an InvalidRequestError with http status, body, and JSON body" do
|
||||
response = test_response(test_missing_id_error, 404)
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
||||
begin
|
||||
Stripe::Customer.retrieve("foo")
|
||||
rescue Stripe::InvalidRequestError => e
|
||||
assert_equal(404, e.http_status)
|
||||
assert_equal(true, !!e.http_body)
|
||||
assert_equal(true, e.json_body.kind_of?(Hash))
|
||||
end
|
||||
end
|
||||
|
||||
should "setting a nil value for a param should exclude that param from the request" do
|
||||
@mock.expects(:get).with do |url, api_key, params|
|
||||
uri = URI(url)
|
||||
query = CGI.parse(uri.query)
|
||||
(url =~ %r{^#{Stripe.api_base}/v1/charges?} &&
|
||||
query.keys.sort == ['offset', 'sad'])
|
||||
end.returns(test_response({ :count => 1, :data => [test_charge] }))
|
||||
c = Stripe::Charge.all(:count => nil, :offset => 5, :sad => false)
|
||||
|
||||
@mock.expects(:post).with do |url, api_key, params|
|
||||
url == "#{Stripe.api_base}/v1/charges" &&
|
||||
api_key.nil? &&
|
||||
CGI.parse(params) == { 'amount' => ['50'], 'currency' => ['usd'] }
|
||||
end.returns(test_response({ :count => 1, :data => [test_charge] }))
|
||||
c = Stripe::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil })
|
||||
end
|
||||
|
||||
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("#{Stripe.api_base}/v1/customers/%E2%98%83", nil, nil).raises(RestClient::ExceptionWithResponse.new(response, 404))
|
||||
c = Stripe::Customer.new("☃")
|
||||
assert_raises(Stripe::InvalidRequestError) { c.refresh }
|
||||
end
|
||||
|
||||
should "requesting with no ID should result in an InvalidRequestError with no request" do
|
||||
c = Stripe::Customer.new
|
||||
assert_raises(Stripe::InvalidRequestError) { c.refresh }
|
||||
end
|
||||
|
||||
should "making a GET request with parameters should have a query string and no body" do
|
||||
params = { :limit => 1 }
|
||||
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/charges?limit=1", nil, nil).returns(test_response([test_charge]))
|
||||
c = Stripe::Charge.all(params)
|
||||
end
|
||||
|
||||
should "making a POST request with parameters should have a body and no query string" do
|
||||
params = { :amount => 100, :currency => 'usd', :card => 'sc_token' }
|
||||
@mock.expects(:post).once.with do |url, get, post|
|
||||
get.nil? && CGI.parse(post) == {'amount' => ['100'], 'currency' => ['usd'], 'card' => ['sc_token']}
|
||||
end.returns(test_response(test_charge))
|
||||
c = Stripe::Charge.create(params)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
assert_equal c.created, c[:created]
|
||||
assert_equal c.created, c['created']
|
||||
c['created'] = 12345
|
||||
assert_equal c.created, 12345
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
should "updating an object should issue a POST request with only the changed properties" do
|
||||
@mock.expects(:post).with do |url, api_key, params|
|
||||
url == "#{Stripe.api_base}/v1/customers/c_test_customer" && api_key.nil? && CGI.parse(params) == {'description' => ['another_mn']}
|
||||
end.once.returns(test_response(test_customer))
|
||||
c = Stripe::Customer.construct_from(test_customer)
|
||||
c.description = "another_mn"
|
||||
c.save
|
||||
end
|
||||
|
||||
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.description = "another_mn"
|
||||
c.save
|
||||
assert_equal false, c.livemode
|
||||
end
|
||||
|
||||
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("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).once.returns(test_response({ "id" => "test_customer", "deleted" => true }))
|
||||
|
||||
c = Stripe::Customer.construct_from(test_customer)
|
||||
c.delete
|
||||
assert_equal true, c.deleted
|
||||
|
||||
assert_raises NoMethodError do
|
||||
c.livemode
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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.data
|
||||
assert c.kind_of? Array
|
||||
assert c[0].kind_of? Stripe::Charge
|
||||
assert c[0].card.kind_of?(Stripe::StripeObject) && c[0].card.object == 'card'
|
||||
end
|
||||
|
||||
context "account tests" do
|
||||
should "account should be retrievable" do
|
||||
resp = {:email => "test+bindings@stripe.com", :charge_enabled => false, :details_submitted => false}
|
||||
@mock.expects(:get).once.returns(test_response(resp))
|
||||
a = Stripe::Account.retrieve
|
||||
assert_equal "test+bindings@stripe.com", a.email
|
||||
assert !a.charge_enabled
|
||||
assert !a.details_submitted
|
||||
end
|
||||
end
|
||||
|
||||
context "balance tests" do
|
||||
should "balance should be retrievable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_balance))
|
||||
b = Stripe::Balance.retrieve
|
||||
assert_equal 12345, b.pending.first.amount
|
||||
assert_equal 6789, b.available.first.amount
|
||||
end
|
||||
end
|
||||
|
||||
context "balance transaction tests" do
|
||||
should "balance transactions should be listable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_balance_transaction_array))
|
||||
bt = Stripe::BalanceTransaction.all
|
||||
assert bt.data.kind_of?(Array)
|
||||
bt.each do |balance_transaction|
|
||||
assert balance_transaction.kind_of?(Stripe::BalanceTransaction)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "list tests" do
|
||||
should "be able to retrieve full lists given a listobject" do
|
||||
@mock.expects(:get).twice.returns(test_response(test_charge_array))
|
||||
c = Stripe::Charge.all
|
||||
assert c.kind_of?(Stripe::ListObject)
|
||||
assert_equal('/v1/charges', c.url)
|
||||
all = c.all
|
||||
assert all.kind_of?(Stripe::ListObject)
|
||||
assert_equal('/v1/charges', all.url)
|
||||
assert all.data.kind_of?(Array)
|
||||
end
|
||||
end
|
||||
|
||||
context "charge tests" do
|
||||
|
||||
should "charges should be listable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_charge_array))
|
||||
c = Stripe::Charge.all
|
||||
assert c.data.kind_of? Array
|
||||
c.each do |charge|
|
||||
assert charge.kind_of?(Stripe::Charge)
|
||||
end
|
||||
end
|
||||
|
||||
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")
|
||||
c.refund
|
||||
assert c.refunded
|
||||
end
|
||||
|
||||
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")
|
||||
c.delete
|
||||
end
|
||||
end
|
||||
|
||||
should "charges should be updateable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_charge))
|
||||
@mock.expects(:post).once.returns(test_response(test_charge))
|
||||
c = Stripe::Charge.new("test_charge")
|
||||
c.refresh
|
||||
c.description = "New charge description"
|
||||
c.save
|
||||
end
|
||||
|
||||
should "charge id should not be changeable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_charge))
|
||||
c = Stripe::Charge.new("test_charge")
|
||||
c.refresh
|
||||
assert_raises NoMethodError do
|
||||
c.id = "my new id"
|
||||
end
|
||||
end
|
||||
|
||||
should "charge descriptions should not be settable to an empty string" do
|
||||
@mock.expects(:get).once.returns(test_response(test_charge))
|
||||
c = Stripe::Charge.new("test_charge")
|
||||
c.refresh
|
||||
assert_raises ArgumentError do
|
||||
c.description = ""
|
||||
end
|
||||
end
|
||||
|
||||
should "charges descriptions should pass nil as an empty string" do
|
||||
@mock.expects(:get).once.returns(test_response(test_charge))
|
||||
@mock.expects(:post).once.with do |url, api_key, params|
|
||||
params == 'description='
|
||||
end.returns(test_response(test_charge))
|
||||
c = Stripe::Charge.new("test_charge")
|
||||
c.refresh
|
||||
c.description = nil
|
||||
c.save
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
should "execute should return a new, fully executed charge when passed correct parameters" do
|
||||
@mock.expects(:post).with do |url, api_key, params|
|
||||
url == "#{Stripe.api_base}/v1/charges" && api_key.nil? && CGI.parse(params) == {
|
||||
'currency' => ['usd'], 'amount' => ['100'],
|
||||
'card[exp_year]' => ['2012'],
|
||||
'card[number]' => ['4242424242424242'],
|
||||
'card[exp_month]' => ['11']
|
||||
}
|
||||
end.once.returns(test_response(test_charge))
|
||||
|
||||
c = Stripe::Charge.create({
|
||||
:amount => 100,
|
||||
:card => {
|
||||
:number => "4242424242424242",
|
||||
:exp_month => 11,
|
||||
:exp_year => 2012,
|
||||
},
|
||||
:currency => "usd"
|
||||
})
|
||||
assert c.paid
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "customer tests" do
|
||||
|
||||
should "customers should be listable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer_array))
|
||||
c = Stripe::Customer.all.data
|
||||
assert c.kind_of? Array
|
||||
assert c[0].kind_of? Stripe::Customer
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
should "customers should be updateable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer({:description => "foo"})))
|
||||
@mock.expects(:post).once.returns(test_response(test_customer({:description => "bar"})))
|
||||
c = Stripe::Customer.new("test_customer").refresh
|
||||
assert_equal c.description, "foo"
|
||||
c.description = "bar"
|
||||
c.save
|
||||
assert_equal c.description, "bar"
|
||||
end
|
||||
|
||||
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 customer's subscription" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
c = Stripe::Customer.retrieve("test_customer")
|
||||
|
||||
@mock.expects(:post).once.with do |url, api_key, params|
|
||||
url == "#{Stripe.api_base}/v1/customers/c_test_customer/subscription" && api_key.nil? && CGI.parse(params) == {'plan' => ['silver']}
|
||||
end.returns(test_response(test_subscription('silver')))
|
||||
s = c.update_subscription({:plan => 'silver'})
|
||||
|
||||
assert_equal 'subscription', s.object
|
||||
assert_equal 'silver', s.plan.identifier
|
||||
end
|
||||
|
||||
should "be able to cancel a customer's subscription" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
c = Stripe::Customer.retrieve("test_customer")
|
||||
|
||||
# Not an accurate response, but whatever
|
||||
|
||||
@mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscription?at_period_end=true", nil, nil).returns(test_response(test_subscription('silver')))
|
||||
s = c.cancel_subscription({:at_period_end => 'true'})
|
||||
|
||||
@mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscription", nil, nil).returns(test_response(test_subscription('silver')))
|
||||
s = c.cancel_subscription
|
||||
end
|
||||
|
||||
should "be able to delete a customer's discount" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
c = Stripe::Customer.retrieve("test_customer")
|
||||
|
||||
@mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/discount", nil, nil).returns(test_response(test_delete_discount_response))
|
||||
s = c.delete_discount
|
||||
assert_equal nil, c.discount
|
||||
end
|
||||
|
||||
should "be able to update a customer without refreshing it first" do
|
||||
@mock.expects(:post).once.with("#{Stripe.api_base}/v1/customers/test_customer", nil, 'description=bar').returns(test_response(test_customer({:description => "bar"})))
|
||||
c = Stripe::Customer.new("test_customer")
|
||||
c.description = "bar"
|
||||
c.save
|
||||
assert_equal c.description, "bar"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "card tests" do
|
||||
should "be able to create a new card for a customer" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
customer = Stripe::Customer.retrieve("test_customer")
|
||||
|
||||
@mock.expects(:post).once.returns(test_response(test_card))
|
||||
card = customer.cards.create(:card => 'card')
|
||||
assert card.kind_of? Stripe::Card
|
||||
end
|
||||
|
||||
should "be able to retrieve a card for a customer" do
|
||||
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).returns(test_response(test_customer))
|
||||
customer = Stripe::Customer.retrieve("c_test_customer")
|
||||
|
||||
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards/cc_test_card", nil, nil).returns(test_response(test_card))
|
||||
card = customer.cards.retrieve("cc_test_card")
|
||||
assert card.kind_of? Stripe::Card
|
||||
end
|
||||
|
||||
should "be able to list all cards for a customer" do
|
||||
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).returns(test_response(test_customer))
|
||||
customer = Stripe::Customer.retrieve("c_test_customer")
|
||||
|
||||
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards", nil, nil).returns(test_response(test_card_array("c_test_customer")))
|
||||
cards = customer.cards.all()
|
||||
assert cards.data.kind_of? Array
|
||||
cards.each do |card|
|
||||
assert card.kind_of?(Stripe::Card)
|
||||
end
|
||||
end
|
||||
|
||||
should "be able to update a card for a customer" do
|
||||
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).returns(test_response(test_customer))
|
||||
customer = Stripe::Customer.retrieve("c_test_customer")
|
||||
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards/cc_test_card", nil, nil).returns(test_response(test_card))
|
||||
card = customer.cards.retrieve("cc_test_card")
|
||||
|
||||
@mock.expects(:post).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards/cc_test_card", nil, "address_zip=zippy").returns(test_response(test_card(:address_zip => 'zippy')))
|
||||
card.address_zip = "zippy"
|
||||
card.save
|
||||
|
||||
assert_equal "zippy", card.address_zip
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
context "coupon tests" do
|
||||
should "create should return a new coupon" do
|
||||
@mock.expects(:post).once.returns(test_response(test_coupon))
|
||||
c = Stripe::Coupon.create
|
||||
assert_equal "co_test_coupon", c.id
|
||||
end
|
||||
end
|
||||
|
||||
context "invoice tests" do
|
||||
should "retrieve should retrieve invoices" do
|
||||
@mock.expects(:get).once.returns(test_response(test_invoice))
|
||||
i = Stripe::Invoice.retrieve('in_test_invoice')
|
||||
assert_equal 'in_test_invoice', i.id
|
||||
end
|
||||
|
||||
should "pay should pay an invoice" do
|
||||
@mock.expects(:get).once.returns(test_response(test_invoice))
|
||||
i = Stripe::Invoice.retrieve('in_test_invoice')
|
||||
|
||||
@mock.expects(:post).once.with('https://api.stripe.com/v1/invoices/in_test_invoice/pay', nil, '').returns(test_response(test_paid_invoice))
|
||||
i.pay
|
||||
assert_equal i.next_payment_attempt, nil
|
||||
end
|
||||
end
|
||||
|
||||
context "transfer tests" do
|
||||
|
||||
should "transfers should be listable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_transfer_array))
|
||||
t = Stripe::Transfer.all
|
||||
assert t.data.kind_of? Array
|
||||
t.each do |transfer|
|
||||
assert transfer.kind_of?(Stripe::Transfer)
|
||||
end
|
||||
end
|
||||
|
||||
should "transfers should not be deletable" do
|
||||
assert_raises NoMethodError do
|
||||
@mock.expects(:get).once.returns(test_response(test_transfer))
|
||||
t = Stripe::Transfer.retrieve("test_transfer")
|
||||
t.delete
|
||||
end
|
||||
end
|
||||
|
||||
should "transfers should have BankAccount objects associated with their account property" do
|
||||
@mock.expects(:get).once.returns(test_response(test_transfer))
|
||||
t = Stripe::Transfer.retrieve("test_transfer")
|
||||
assert t.account.kind_of?(Stripe::StripeObject) && t.account.object == 'bank_account'
|
||||
end
|
||||
|
||||
should "create a transfer should return a new transfer when passed correct parameters" do
|
||||
@mock.expects(:post).with do |url, api_key, params|
|
||||
url == "#{Stripe.api_base}/v1/transfers" && api_key.nil? && CGI.parse(params) == {
|
||||
'currency' => ['usd'], 'amount' => ['100'],
|
||||
'recipient' => ['test_recipient']
|
||||
}
|
||||
end.once.returns(test_response(test_transfer))
|
||||
|
||||
t = Stripe::Transfer.create({
|
||||
:amount => 100,
|
||||
:currency => "usd",
|
||||
:recipient => "test_recipient"
|
||||
})
|
||||
|
||||
assert_equal 'pending', t.status
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "recipient tests" do
|
||||
|
||||
should "recipients should be listable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_recipient_array))
|
||||
r = Stripe::Recipient.all.data
|
||||
assert r.kind_of? Array
|
||||
assert r[0].kind_of? Stripe::Recipient
|
||||
end
|
||||
|
||||
should "recipients should be deletable" do
|
||||
@mock.expects(:delete).once.returns(test_response(test_recipient({:deleted => true})))
|
||||
r = Stripe::Recipient.new("test_recipient")
|
||||
r.delete
|
||||
assert r.deleted
|
||||
end
|
||||
|
||||
should "recipients should be updateable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_recipient({:description => "foo"})))
|
||||
@mock.expects(:post).once.returns(test_response(test_recipient({:description => "bar"})))
|
||||
r = Stripe::Recipient.new("test_recipient").refresh
|
||||
assert_equal r.description, "foo"
|
||||
r.description = "bar"
|
||||
r.save
|
||||
assert_equal r.description, "bar"
|
||||
end
|
||||
|
||||
should "recipients should have BankAccount objects associated with their active_account property" do
|
||||
@mock.expects(:get).once.returns(test_response(test_recipient))
|
||||
r = Stripe::Recipient.retrieve("test_recipient")
|
||||
assert r.active_account.kind_of?(Stripe::StripeObject) && r.active_account.object == 'bank_account'
|
||||
end
|
||||
|
||||
should "create should return a new recipient" do
|
||||
@mock.expects(:post).once.returns(test_response(test_recipient))
|
||||
r = Stripe::Recipient.create(:name => 'Stripe User', :type => 'individual')
|
||||
assert_equal "rp_test_recipient", r.id
|
||||
end
|
||||
end
|
||||
|
||||
context "error checking" 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))
|
||||
|
||||
begin
|
||||
Stripe::Customer.new("test_customer").refresh
|
||||
assert false #shouldn't get here either
|
||||
rescue Stripe::InvalidRequestError => e # we don't use assert_raises because we want to examine e
|
||||
assert e.kind_of? Stripe::InvalidRequestError
|
||||
assert_equal "id", e.param
|
||||
assert_equal "Missing id", e.message
|
||||
return
|
||||
end
|
||||
|
||||
assert false #shouldn't get here
|
||||
end
|
||||
|
||||
should "5XXs should raise an APIError" do
|
||||
response = test_response(test_api_error, 500)
|
||||
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 500))
|
||||
|
||||
begin
|
||||
Stripe::Customer.new("test_customer").refresh
|
||||
assert false #shouldn't get here either
|
||||
rescue Stripe::APIError => e # we don't use assert_raises because we want to examine e
|
||||
assert e.kind_of? Stripe::APIError
|
||||
return
|
||||
end
|
||||
|
||||
assert false #shouldn't get here
|
||||
end
|
||||
|
||||
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))
|
||||
|
||||
begin
|
||||
Stripe::Customer.new("test_customer").refresh
|
||||
assert false #shouldn't get here either
|
||||
rescue Stripe::CardError => e # we don't use assert_raises because we want to examine e
|
||||
assert e.kind_of? Stripe::CardError
|
||||
assert_equal "invalid_expiry_year", e.code
|
||||
assert_equal "exp_year", e.param
|
||||
assert_equal "Your card's expiration year is invalid", e.message
|
||||
return
|
||||
end
|
||||
|
||||
assert false #shouldn't get here
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,2 +0,0 @@
|
||||
require 'active_support/all'
|
||||
load File.expand_path(File.join(File.dirname(__FILE__), 'test_stripe.rb'))
|
||||
Loading…
x
Reference in New Issue
Block a user