stripe-ruby-mock 1.10.1.7 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/README.md +70 -3
  4. data/Rakefile +1 -1
  5. data/lib/stripe_mock/api/client.rb +1 -0
  6. data/lib/stripe_mock/api/instance.rb +1 -0
  7. data/lib/stripe_mock/api/live.rb +15 -0
  8. data/lib/stripe_mock/api/server.rb +24 -21
  9. data/lib/stripe_mock/api/test_helpers.rb +24 -0
  10. data/lib/stripe_mock/client.rb +4 -8
  11. data/lib/stripe_mock/data.rb +54 -30
  12. data/lib/stripe_mock/instance.rb +15 -5
  13. data/lib/stripe_mock/request_handlers/cards.rb +29 -18
  14. data/lib/stripe_mock/request_handlers/charges.rb +34 -6
  15. data/lib/stripe_mock/request_handlers/coupons.rb +1 -3
  16. data/lib/stripe_mock/request_handlers/customers.rb +3 -9
  17. data/lib/stripe_mock/request_handlers/events.rb +1 -3
  18. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +16 -9
  19. data/lib/stripe_mock/request_handlers/helpers/charge_helpers.rb +16 -0
  20. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +9 -2
  21. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +3 -1
  22. data/lib/stripe_mock/request_handlers/invoice_items.rb +32 -2
  23. data/lib/stripe_mock/request_handlers/invoices.rb +7 -3
  24. data/lib/stripe_mock/request_handlers/plans.rb +2 -5
  25. data/lib/stripe_mock/request_handlers/recipients.rb +26 -4
  26. data/lib/stripe_mock/request_handlers/subscriptions.rb +26 -33
  27. data/lib/stripe_mock/request_handlers/tokens.rb +24 -4
  28. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +18 -0
  29. data/lib/stripe_mock/server.rb +4 -5
  30. data/lib/stripe_mock/test_strategies/base.rb +27 -0
  31. data/lib/stripe_mock/test_strategies/live.rb +22 -0
  32. data/lib/stripe_mock/test_strategies/mock.rb +19 -0
  33. data/lib/stripe_mock/util.rb +5 -0
  34. data/lib/stripe_mock/version.rb +1 -1
  35. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +3 -2
  36. data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +16 -9
  37. data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +3 -2
  38. data/lib/stripe_mock/webhook_fixtures/customer.card.created.json +1 -0
  39. data/lib/stripe_mock/webhook_fixtures/customer.card.deleted.json +1 -0
  40. data/lib/stripe_mock/webhook_fixtures/customer.card.updated.json +1 -0
  41. data/lib/stripe_mock/webhook_fixtures/customer.created.json +1 -0
  42. data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +2 -1
  43. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +1 -0
  44. data/lib/stripe_mock.rb +9 -1
  45. data/spec/fixtures/create_refund.yml +126 -0
  46. data/spec/instance_spec.rb +4 -2
  47. data/spec/integration_examples/charge_token_examples.rb +49 -0
  48. data/spec/integration_examples/customer_card_examples.rb +42 -0
  49. data/spec/integration_examples/prepare_error_examples.rb +18 -0
  50. data/spec/readme_spec.rb +2 -1
  51. data/spec/server_spec.rb +12 -3
  52. data/spec/shared_stripe_examples/card_examples.rb +108 -3
  53. data/spec/shared_stripe_examples/card_token_examples.rb +26 -0
  54. data/spec/shared_stripe_examples/charge_examples.rb +55 -39
  55. data/spec/shared_stripe_examples/coupon_examples.rb +2 -17
  56. data/spec/shared_stripe_examples/customer_examples.rb +30 -39
  57. data/spec/shared_stripe_examples/error_mock_examples.rb +1 -1
  58. data/spec/shared_stripe_examples/invoice_examples.rb +31 -15
  59. data/spec/shared_stripe_examples/invoice_item_examples.rb +62 -10
  60. data/spec/shared_stripe_examples/plan_examples.rb +29 -18
  61. data/spec/shared_stripe_examples/recipient_examples.rb +55 -5
  62. data/spec/shared_stripe_examples/refund_examples.rb +90 -0
  63. data/spec/shared_stripe_examples/subscription_examples.rb +159 -82
  64. data/spec/shared_stripe_examples/validation_examples.rb +19 -0
  65. data/spec/spec_helper.rb +32 -1
  66. data/spec/stripe_mock_spec.rb +70 -0
  67. data/spec/support/stripe_examples.rb +7 -14
  68. data/spec/util_spec.rb +8 -0
  69. data/stripe-ruby-mock.gemspec +2 -2
  70. metadata +38 -34
  71. data/lib/stripe_mock/api/strict.rb +0 -11
@@ -19,6 +19,7 @@
19
19
  "object": "card",
20
20
  "last4": "4242",
21
21
  "type": "Visa",
22
+ "brand": "Visa",
22
23
  "exp_month": 12,
23
24
  "exp_year": 2013,
24
25
  "fingerprint": "wXWJT135mEK107G8",
@@ -36,15 +37,21 @@
36
37
  "address_zip_check": null
37
38
  },
38
39
  "captured": true,
39
- "refunds": [
40
- {
41
- "amount": 1000,
42
- "currency": "usd",
43
- "created": 1381080103,
44
- "object": "refund",
45
- "balance_transaction": "txn_2hkjgg43ucu7K1"
46
- }
47
- ],
40
+ "refunds": {
41
+ "object": "list",
42
+ "total_count": 1,
43
+ "has_more": false,
44
+ "data": [
45
+ {
46
+ "amount": 1000,
47
+ "currency": "usd",
48
+ "created": 1381080103,
49
+ "object": "refund",
50
+ "balance_transaction": "txn_2hkjgg43ucu7K1",
51
+ "id": "re_00000000000000"
52
+ }
53
+ ]
54
+ },
48
55
  "balance_transaction": "txn_00000000000000",
49
56
  "failure_message": null,
50
57
  "failure_code": null,
@@ -19,6 +19,7 @@
19
19
  "object": "card",
20
20
  "last4": "4242",
21
21
  "type": "Visa",
22
+ "brand": "Visa",
22
23
  "exp_month": 12,
23
24
  "exp_year": 2013,
24
25
  "fingerprint": "wXWJT135mEK107G8",
@@ -36,9 +37,9 @@
36
37
  "address_zip_check": null
37
38
  },
38
39
  "captured": true,
39
- "refunds": [
40
+ "refunds": {
40
41
 
41
- ],
42
+ },
42
43
  "balance_transaction": "txn_00000000000000",
43
44
  "failure_message": null,
44
45
  "failure_code": null,
@@ -10,6 +10,7 @@
10
10
  "object": "card",
11
11
  "last4": "4242",
12
12
  "type": "Visa",
13
+ "brand": "Visa",
13
14
  "exp_month": 3,
14
15
  "exp_year": 2020,
15
16
  "fingerprint": "wXWJT135mEK107G8",
@@ -10,6 +10,7 @@
10
10
  "object": "card",
11
11
  "last4": "4242",
12
12
  "type": "Visa",
13
+ "brand": "Visa",
13
14
  "exp_month": 3,
14
15
  "exp_year": 2020,
15
16
  "fingerprint": "wXWJT135mEK107G8",
@@ -10,6 +10,7 @@
10
10
  "object": "card",
11
11
  "last4": "4242",
12
12
  "type": "Visa",
13
+ "brand": "Visa",
13
14
  "exp_month": 3,
14
15
  "exp_year": 2020,
15
16
  "fingerprint": "wXWJT135mEK107G8",
@@ -28,6 +28,7 @@
28
28
  "object": "card",
29
29
  "last4": "0341",
30
30
  "type": "Visa",
31
+ "brand": "Visa",
31
32
  "exp_month": 12,
32
33
  "exp_year": 2013,
33
34
  "fingerprint": "fWvZEzdbEIFF8QrK",
@@ -20,7 +20,8 @@
20
20
  "last4": "4242",
21
21
  "name": "1231",
22
22
  "object": "card",
23
- "type": "Visa"
23
+ "type": "Visa",
24
+ "brand": "Visa"
24
25
  },
25
26
  "created": 1359947599,
26
27
  "delinquent": false,
@@ -28,6 +28,7 @@
28
28
  "object": "card",
29
29
  "last4": "0341",
30
30
  "type": "Visa",
31
+ "brand": "Visa",
31
32
  "exp_month": 12,
32
33
  "exp_year": 2013,
33
34
  "fingerprint": "fWvZEzdbEIFF8QrK",
data/lib/stripe_mock.rb CHANGED
@@ -28,13 +28,17 @@ require 'stripe_mock/api/card_tokens'
28
28
  require 'stripe_mock/api/debug'
29
29
  require 'stripe_mock/api/errors'
30
30
  require 'stripe_mock/api/global_id_prefix'
31
- require 'stripe_mock/api/strict'
31
+ require 'stripe_mock/api/live'
32
+ require 'stripe_mock/api/test_helpers'
32
33
  require 'stripe_mock/api/webhooks'
33
34
 
34
35
  require 'stripe_mock/request_handlers/helpers/card_helpers.rb'
36
+ require 'stripe_mock/request_handlers/helpers/charge_helpers.rb'
35
37
  require 'stripe_mock/request_handlers/helpers/subscription_helpers.rb'
36
38
  require 'stripe_mock/request_handlers/helpers/token_helpers.rb'
37
39
 
40
+ require 'stripe_mock/request_handlers/validators/param_validators.rb'
41
+
38
42
  require 'stripe_mock/request_handlers/charges.rb'
39
43
  require 'stripe_mock/request_handlers/cards.rb'
40
44
  require 'stripe_mock/request_handlers/customers.rb'
@@ -48,6 +52,10 @@ require 'stripe_mock/request_handlers/subscriptions.rb'
48
52
  require 'stripe_mock/request_handlers/tokens.rb'
49
53
  require 'stripe_mock/instance'
50
54
 
55
+ require 'stripe_mock/test_strategies/base.rb'
56
+ require 'stripe_mock/test_strategies/mock.rb'
57
+ require 'stripe_mock/test_strategies/live.rb'
58
+
51
59
  module StripeMock
52
60
 
53
61
  lib_dir = File.expand_path(File.dirname(__FILE__), '../..')
@@ -0,0 +1,126 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://api.stripe.com/v1/charges/ch_4fX3f4kErVLRcj
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - ! '*/*; q=0.5, application/xml'
12
+ Accept-Encoding:
13
+ - gzip, deflate
14
+ User-Agent:
15
+ - Stripe/v1 RubyBindings/1.15.0
16
+ Authorization:
17
+ - Bearer sk_test_sZQU6LreNksFlmewtO4JqBK1
18
+ Content-Type:
19
+ - application/x-www-form-urlencoded
20
+ X-Stripe-Client-User-Agent:
21
+ - ! '{"bindings_version":"1.15.0","lang":"ruby","lang_version":"1.9.3 p125 (2012-02-16)","platform":"x86_64-darwin10.8.0","publisher":"stripe","uname":"Darwin
22
+ airwater.local 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST
23
+ 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64"}'
24
+ response:
25
+ status:
26
+ code: 200
27
+ message: OK
28
+ headers:
29
+ Server:
30
+ - nginx
31
+ Date:
32
+ - Wed, 27 Aug 2014 19:21:23 GMT
33
+ Content-Type:
34
+ - application/json;charset=utf-8
35
+ Content-Length:
36
+ - '1158'
37
+ Access-Control-Allow-Credentials:
38
+ - 'true'
39
+ Access-Control-Allow-Methods:
40
+ - GET, POST, HEAD, OPTIONS, DELETE
41
+ Access-Control-Max-Age:
42
+ - '300'
43
+ Cache-Control:
44
+ - no-cache, no-store
45
+ Stripe-Version:
46
+ - '2014-08-04'
47
+ Strict-Transport-Security:
48
+ - max-age=31556926; includeSubDomains
49
+ body:
50
+ encoding: US-ASCII
51
+ string: ! "{\n \"id\": \"ch_4fX3f4kErVLRcj\",\n \"object\": \"charge\",\n
52
+ \ \"created\": 1409167282,\n \"livemode\": false,\n \"paid\": true,\n \"amount\":
53
+ 555,\n \"currency\": \"usd\",\n \"refunded\": false,\n \"card\": {\n \"id\":
54
+ \"card_4fX3v7mm7FZQxN\",\n \"object\": \"card\",\n \"last4\": \"4242\",\n
55
+ \ \"brand\": \"Visa\",\n \"funding\": \"credit\",\n \"exp_month\":
56
+ 9,\n \"exp_year\": 2018,\n \"fingerprint\": \"wXWJT135mEK107G8\",\n
57
+ \ \"country\": \"US\",\n \"name\": null,\n \"address_line1\": null,\n
58
+ \ \"address_line2\": null,\n \"address_city\": null,\n \"address_state\":
59
+ null,\n \"address_zip\": null,\n \"address_country\": null,\n \"cvc_check\":
60
+ \"pass\",\n \"address_line1_check\": null,\n \"address_zip_check\":
61
+ null,\n \"customer\": null\n },\n \"captured\": true,\n \"refunds\":
62
+ {\n \"object\": \"list\",\n \"total_count\": 0,\n \"has_more\": false,\n
63
+ \ \"url\": \"/v1/charges/ch_4fX3f4kErVLRcj/refunds\",\n \"data\": []\n
64
+ \ },\n \"balance_transaction\": \"txn_4fX3u6H5sC09M0\",\n \"failure_message\":
65
+ null,\n \"failure_code\": null,\n \"amount_refunded\": 0,\n \"customer\":
66
+ null,\n \"invoice\": null,\n \"description\": null,\n \"dispute\": null,\n
67
+ \ \"metadata\": {},\n \"statement_description\": null,\n \"receipt_email\":
68
+ null\n}\n"
69
+ http_version:
70
+ recorded_at: Wed, 27 Aug 2014 19:21:22 GMT
71
+ - request:
72
+ method: post
73
+ uri: https://api.stripe.com/v1/charges/ch_4fX3f4kErVLRcj/refunds
74
+ body:
75
+ encoding: US-ASCII
76
+ string: amount=555
77
+ headers:
78
+ Accept:
79
+ - ! '*/*; q=0.5, application/xml'
80
+ Accept-Encoding:
81
+ - gzip, deflate
82
+ User-Agent:
83
+ - Stripe/v1 RubyBindings/1.15.0
84
+ Authorization:
85
+ - Bearer sk_test_sZQU6LreNksFlmewtO4JqBK1
86
+ Content-Type:
87
+ - application/x-www-form-urlencoded
88
+ X-Stripe-Client-User-Agent:
89
+ - ! '{"bindings_version":"1.15.0","lang":"ruby","lang_version":"1.9.3 p125 (2012-02-16)","platform":"x86_64-darwin10.8.0","publisher":"stripe","uname":"Darwin
90
+ airwater.local 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST
91
+ 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64"}'
92
+ Content-Length:
93
+ - '10'
94
+ response:
95
+ status:
96
+ code: 200
97
+ message: OK
98
+ headers:
99
+ Server:
100
+ - nginx
101
+ Date:
102
+ - Wed, 27 Aug 2014 19:21:25 GMT
103
+ Content-Type:
104
+ - application/json;charset=utf-8
105
+ Content-Length:
106
+ - '215'
107
+ Access-Control-Allow-Credentials:
108
+ - 'true'
109
+ Access-Control-Allow-Methods:
110
+ - GET, POST, HEAD, OPTIONS, DELETE
111
+ Access-Control-Max-Age:
112
+ - '300'
113
+ Cache-Control:
114
+ - no-cache, no-store
115
+ Stripe-Version:
116
+ - '2014-08-04'
117
+ Strict-Transport-Security:
118
+ - max-age=31556926; includeSubDomains
119
+ body:
120
+ encoding: US-ASCII
121
+ string: ! "{\n \"id\": \"re_4fX3sdHZN1mfba\",\n \"amount\": 555,\n \"currency\":
122
+ \"usd\",\n \"created\": 1409167284,\n \"object\": \"refund\",\n \"balance_transaction\":
123
+ \"txn_4fX3vOQMq1HTQt\",\n \"metadata\": {},\n \"charge\": \"ch_4fX3f4kErVLRcj\"\n}\n"
124
+ http_version:
125
+ recorded_at: Wed, 27 Aug 2014 19:21:23 GMT
126
+ recorded_with: VCR 2.6.0
@@ -3,6 +3,8 @@ require_stripe_examples
3
3
 
4
4
  describe StripeMock::Instance do
5
5
 
6
+ let(:stripe_helper) { StripeMock.create_test_helper }
7
+
6
8
  it_behaves_like_stripe do
7
9
  def test_data_source(type); StripeMock.instance.send(type); end
8
10
  end
@@ -11,10 +13,10 @@ describe StripeMock::Instance do
11
13
  after { StripeMock.stop }
12
14
 
13
15
  it "handles both string and symbol hash keys" do
14
- string_params = {
16
+ string_params = stripe_helper.create_plan_params(
15
17
  "id" => "str_abcde",
16
18
  :name => "String Plan"
17
- }
19
+ )
18
20
  res, api_key = StripeMock.instance.mock_request('post', '/v1/plans', 'api_key', string_params)
19
21
  expect(res[:id]).to eq('str_abcde')
20
22
  expect(res[:name]).to eq('String Plan')
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Charging with Tokens' do
4
+
5
+ describe "With OAuth" do
6
+
7
+ before do
8
+ @cus = Stripe::Customer.create(
9
+ :card => stripe_helper.generate_card_token({ :number => '4242424242424242', :brand => 'Visa' })
10
+ )
11
+
12
+ @card_token = Stripe::Token.create({
13
+ :customer => @cus.id,
14
+ :card => @cus.cards.first.id
15
+ }, ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN'])
16
+ end
17
+
18
+ it "creates with an oauth access token", :oauth => true do
19
+ charge = Stripe::Charge.create({
20
+ :amount => 1099,
21
+ :currency => 'usd',
22
+ :card => @card_token.id
23
+ }, ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN'])
24
+
25
+ expect(charge.card.id).to_not eq @cus.cards.first.id
26
+ expect(charge.card.fingerprint).to eq @cus.cards.first.fingerprint
27
+ expect(charge.card.last4).to eq '4242'
28
+ expect(charge.card.brand).to eq 'Visa'
29
+
30
+ retrieved_charge = Stripe::Charge.retrieve(charge.id)
31
+
32
+ expect(retrieved_charge.card.id).to_not eq @cus.cards.first.id
33
+ expect(retrieved_charge.card.fingerprint).to eq @cus.cards.first.fingerprint
34
+ expect(retrieved_charge.card.last4).to eq '4242'
35
+ expect(retrieved_charge.card.brand).to eq 'Visa'
36
+ end
37
+
38
+ it "throws an error when the card is not an id", :oauth => true do
39
+ expect {
40
+ charge = Stripe::Charge.create({
41
+ :amount => 1099,
42
+ :currency => 'usd',
43
+ :card => @card_token
44
+ }, ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN'])
45
+ }.to raise_error(Stripe::InvalidRequestError, /Invalid token id/)
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples "Multiple Customer Cards" do
4
+ it "handles multiple cards", :live => true do
5
+ tok1 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4242424242424242"
6
+ tok2 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4012888888881881"
7
+
8
+ cus = Stripe::Customer.create(:email => 'alice@bob.com', :card => tok1.id)
9
+ default_card = cus.cards.first
10
+ cus.cards.create(:card => tok2.id)
11
+
12
+ cus = Stripe::Customer.retrieve(cus.id)
13
+ expect(cus.cards.count).to eq(2)
14
+ expect(cus.default_card).to eq default_card.id
15
+ end
16
+
17
+ it "gives the same two card numbers the same fingerprints", :live => true do
18
+ tok1 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4242424242424242"
19
+ tok2 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4242424242424242"
20
+
21
+ cus = Stripe::Customer.create(:email => 'alice@bob.com', :card => tok1.id)
22
+
23
+ cus = Stripe::Customer.retrieve(cus.id)
24
+ card = cus.cards.find do |existing_card|
25
+ existing_card.fingerprint == tok2.card.fingerprint
26
+ end
27
+ expect(card).to_not be_nil
28
+ end
29
+
30
+ it "gives different card numbers different fingerprints", :live => true do
31
+ tok1 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4242424242424242"
32
+ tok2 = Stripe::Token.retrieve stripe_helper.generate_card_token :number => "4012888888881881"
33
+
34
+ cus = Stripe::Customer.create(:email => 'alice@bob.com', :card => tok1.id)
35
+
36
+ cus = Stripe::Customer.retrieve(cus.id)
37
+ card = cus.cards.find do |existing_card|
38
+ existing_card.fingerprint == tok2.card.fingerprint
39
+ end
40
+ expect(card).to be_nil
41
+ end
42
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Card Error Prep' do
4
+
5
+ # it "prepares a card error" do
6
+ # StripeMock.prepare_card_error(:card_declined, :new_charge)
7
+ # cus = Stripe::Customer.create :email => 'alice@bob.com',
8
+ # :card => stripe_helper.generate_card_token({ :number => '4242424242424242', :brand => 'Visa' })
9
+
10
+ # expect {
11
+ # charge = Stripe::Charge.create({
12
+ # :amount => 999, :currency => 'usd',
13
+ # :customer => cus, :card => cus.cards.first,
14
+ # :description => 'hello'
15
+ # })
16
+ # }.to raise_error Stripe::CardError
17
+ # end
18
+ end
data/spec/readme_spec.rb CHANGED
@@ -1,5 +1,6 @@
1
1
 
2
2
  describe 'README examples' do
3
+ let(:stripe_helper) { StripeMock.create_test_helper }
3
4
 
4
5
  before { StripeMock.start }
5
6
  after { StripeMock.stop }
@@ -9,7 +10,7 @@ describe 'README examples' do
9
10
  # This doesn't touch stripe's servers nor the internet!
10
11
  customer = Stripe::Customer.create({
11
12
  email: 'johnny@appleseed.com',
12
- card: 'void_card_token'
13
+ card: stripe_helper.generate_card_token
13
14
  })
14
15
  expect(customer.email).to eq('johnny@appleseed.com')
15
16
  end
data/spec/server_spec.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require 'spec_helper'
2
2
  require_stripe_examples
3
3
 
4
- describe 'StripeMock Server' do
4
+ describe 'StripeMock Server', :mock_server => true do
5
+
6
+ let(:stripe_helper) { StripeMock.create_test_helper }
5
7
 
6
8
  it_behaves_like_stripe do
7
9
  def test_data_source(type); StripeMock.client.get_server_data(type); end
@@ -22,7 +24,7 @@ describe 'StripeMock Server' do
22
24
  charge = Stripe::Charge.create(
23
25
  amount: 987,
24
26
  currency: 'USD',
25
- card: 'card_token_abcde',
27
+ card: stripe_helper.generate_card_token,
26
28
  description: 'card charge'
27
29
  )
28
30
  expect(charge.amount).to eq(987)
@@ -49,7 +51,7 @@ describe 'StripeMock Server' do
49
51
 
50
52
 
51
53
  it "returns a response with symbolized hash keys" do
52
- Stripe::Plan.create(id: 'x')
54
+ stripe_helper.create_plan(id: 'x')
53
55
  response, api_key = StripeMock.redirect_to_mock_server('get', '/v1/plans/x', 'xxx')
54
56
  response.keys.each {|k| expect(k).to be_a(Symbol) }
55
57
  end
@@ -85,6 +87,13 @@ describe 'StripeMock Server' do
85
87
  StripeMock.default_server_pid_path = orig
86
88
  end
87
89
 
90
+ it "can set the default server log path" do
91
+ expect(StripeMock.default_server_log_path).to eq('./stripe-mock-server.log')
92
+
93
+ StripeMock.default_server_log_path = 'yule.log'
94
+ expect(StripeMock.default_server_log_path).to eq('yule.log')
95
+ end
96
+
88
97
 
89
98
  it "doesn't create multiple clients" do
90
99
  result = StripeMock.start_client
@@ -43,13 +43,36 @@ shared_examples 'Card API' do
43
43
  expect(card.exp_year).to eq(3031)
44
44
  end
45
45
 
46
- it 'create does not change the customers default card' do
46
+
47
+ it "creates a single card with a generated card token", :live => true do
48
+ customer = Stripe::Customer.create
49
+ expect(customer.cards.count).to eq 0
50
+
51
+ customer.cards.create :card => stripe_helper.generate_card_token
52
+ # Yes, stripe-ruby does not actually add the new card to the customer instance
53
+ expect(customer.cards.count).to eq 0
54
+
55
+ customer2 = Stripe::Customer.retrieve(customer.id)
56
+ expect(customer2.cards.count).to eq 1
57
+ expect(customer2.default_card).to eq customer2.cards.first.id
58
+ end
59
+
60
+ it 'create does not change the customers default card if already set' do
61
+ customer = Stripe::Customer.create(id: 'test_customer_sub', default_card: "test_cc_original")
62
+ card_token = StripeMock.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
63
+ card = customer.cards.create(card: card_token)
64
+
65
+ customer = Stripe::Customer.retrieve('test_customer_sub')
66
+ expect(customer.default_card).to eq("test_cc_original")
67
+ end
68
+
69
+ it 'create updates the customers default card if not set' do
47
70
  customer = Stripe::Customer.create(id: 'test_customer_sub')
48
71
  card_token = StripeMock.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
49
72
  card = customer.cards.create(card: card_token)
50
73
 
51
74
  customer = Stripe::Customer.retrieve('test_customer_sub')
52
- expect(customer.default_card).to be_nil
75
+ expect(customer.default_card).to_not be_nil
53
76
  end
54
77
 
55
78
  context "retrieval and deletion" do
@@ -62,14 +85,59 @@ shared_examples 'Card API' do
62
85
  expect(retrieved.to_s).to eq(card.to_s)
63
86
  end
64
87
 
88
+ it "retrieves a customer's card after re-fetching the customer" do
89
+ retrieved = Stripe::Customer.retrieve(customer.id).cards.retrieve(card.id)
90
+ expect(retrieved.id).to eq card.id
91
+ end
92
+
65
93
  it "deletes a customers card" do
66
94
  card.delete
67
95
  retrieved_cus = Stripe::Customer.retrieve(customer.id)
68
96
  expect(retrieved_cus.cards.data).to be_empty
69
97
  end
70
98
 
71
- it "updates the default card if deleted"
99
+ it "deletes a customers card then set the default_card to nil" do
100
+ card.delete
101
+ retrieved_cus = Stripe::Customer.retrieve(customer.id)
102
+ expect(retrieved_cus.default_card).to be_nil
103
+ end
104
+
105
+ it "updates the default card if deleted" do
106
+ card.delete
107
+ retrieved_cus = Stripe::Customer.retrieve(customer.id)
108
+ expect(retrieved_cus.default_card).to be_nil
109
+ end
110
+
111
+ context "deletion when the user has two cards" do
112
+ let!(:card_token_2) { StripeMock.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099) }
113
+ let!(:card_2) { customer.cards.create(card: card_token_2) }
114
+
115
+ it "has just one card anymore" do
116
+ card.delete
117
+ retrieved_cus = Stripe::Customer.retrieve(customer.id)
118
+ expect(retrieved_cus.cards.data.count).to eq 1
119
+ expect(retrieved_cus.cards.data.first.id).to eq card_2.id
120
+ end
121
+
122
+ it "sets the default_card id to the last card remaining id" do
123
+ card.delete
124
+ retrieved_cus = Stripe::Customer.retrieve(customer.id)
125
+ expect(retrieved_cus.default_card).to eq card_2.id
126
+ end
127
+ end
128
+ end
72
129
 
130
+ describe "Errors", :live => true do
131
+ it "throws an error when the customer does not have the retrieving card id" do
132
+ customer = Stripe::Customer.create
133
+ card_id = "card_123"
134
+ expect { customer.cards.retrieve(card_id) }.to raise_error {|e|
135
+ expect(e).to be_a Stripe::InvalidRequestError
136
+ expect(e.message).to include "Customer", customer.id, "does not have", card_id
137
+ expect(e.param).to eq 'card'
138
+ expect(e.http_status).to eq 404
139
+ }
140
+ end
73
141
  end
74
142
 
75
143
  context "update card" do
@@ -92,4 +160,41 @@ shared_examples 'Card API' do
92
160
  end
93
161
  end
94
162
 
163
+ context "retrieve multiple cards" do
164
+
165
+ it "retrieves a list of multiple cards" do
166
+ customer = Stripe::Customer.create(id: 'test_customer_card')
167
+
168
+ card_token = StripeMock.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
169
+ card1 = customer.cards.create(card: card_token)
170
+ card_token = StripeMock.generate_card_token(last4: "1124", exp_month: 12, exp_year: 2098)
171
+ card2 = customer.cards.create(card: card_token)
172
+
173
+ customer = Stripe::Customer.retrieve('test_customer_card')
174
+
175
+ list = customer.cards.all
176
+
177
+ expect(list.object).to eq("list")
178
+ expect(list.count).to eq(2)
179
+ expect(list.data.length).to eq(2)
180
+
181
+ expect(list.data.first.object).to eq("card")
182
+ expect(list.data.first.to_hash).to eq(card1.to_hash)
183
+
184
+ expect(list.data.last.object).to eq("card")
185
+ expect(list.data.last.to_hash).to eq(card2.to_hash)
186
+ end
187
+
188
+ it "retrieves an empty list if there's no subscriptions" do
189
+ Stripe::Customer.create(id: 'no_cards')
190
+ customer = Stripe::Customer.retrieve('no_cards')
191
+
192
+ list = customer.cards.all
193
+
194
+ expect(list.object).to eq("list")
195
+ expect(list.count).to eq(0)
196
+ expect(list.data.length).to eq(0)
197
+ end
198
+ end
199
+
95
200
  end
@@ -85,6 +85,32 @@ shared_examples 'Card Token Mocking' do
85
85
  expect(card.exp_month).to eq(11)
86
86
  expect(card.exp_year).to eq(2019)
87
87
  end
88
+
89
+ it "generates a card token created from customer" do
90
+ card_token = Stripe::Token.create({
91
+ card: {
92
+ number: "1111222233334444",
93
+ exp_month: 11,
94
+ exp_year: 2019
95
+ }
96
+ })
97
+
98
+ cus = Stripe::Customer.create()
99
+ cus.card = card_token.id
100
+ cus.save
101
+
102
+ card_token = Stripe::Token.create({
103
+ customer: cus.id
104
+ })
105
+
106
+ expect(card_token.object).to eq("token")
107
+ end
108
+
109
+ it "throws an error if neither card nor customer are provided", :live => true do
110
+ expect { Stripe::Token.create }.to raise_error(
111
+ Stripe::InvalidRequestError, /must supply either a card, customer/
112
+ )
113
+ end
88
114
  end
89
115
 
90
116
  end