epopia-stripe-ruby-mock 2.5.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. checksums.yaml +7 -0
  2. data/.env +2 -0
  3. data/.gitignore +8 -0
  4. data/.rspec +1 -0
  5. data/.travis.yml +28 -0
  6. data/Gemfile +12 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +413 -0
  9. data/Rakefile +14 -0
  10. data/bin/stripe-mock-server +19 -0
  11. data/lib/stripe_mock.rb +95 -0
  12. data/lib/stripe_mock/api/account_balance.rb +14 -0
  13. data/lib/stripe_mock/api/bank_tokens.rb +13 -0
  14. data/lib/stripe_mock/api/card_tokens.rb +13 -0
  15. data/lib/stripe_mock/api/client.rb +41 -0
  16. data/lib/stripe_mock/api/conversion_rate.rb +14 -0
  17. data/lib/stripe_mock/api/debug.rb +11 -0
  18. data/lib/stripe_mock/api/errors.rb +65 -0
  19. data/lib/stripe_mock/api/global_id_prefix.rb +22 -0
  20. data/lib/stripe_mock/api/instance.rb +38 -0
  21. data/lib/stripe_mock/api/live.rb +15 -0
  22. data/lib/stripe_mock/api/server.rb +39 -0
  23. data/lib/stripe_mock/api/test_helpers.rb +24 -0
  24. data/lib/stripe_mock/api/webhooks.rb +88 -0
  25. data/lib/stripe_mock/client.rb +127 -0
  26. data/lib/stripe_mock/data.rb +1193 -0
  27. data/lib/stripe_mock/data/list.rb +73 -0
  28. data/lib/stripe_mock/error_queue.rb +27 -0
  29. data/lib/stripe_mock/errors/closed_client_connection_error.rb +9 -0
  30. data/lib/stripe_mock/errors/server_timeout_error.rb +12 -0
  31. data/lib/stripe_mock/errors/stripe_mock_error.rb +15 -0
  32. data/lib/stripe_mock/errors/uninitialized_instance_error.rb +9 -0
  33. data/lib/stripe_mock/errors/unstarted_state_error.rb +9 -0
  34. data/lib/stripe_mock/errors/unsupported_request_error.rb +4 -0
  35. data/lib/stripe_mock/instance.rb +237 -0
  36. data/lib/stripe_mock/request_handlers/accounts.rb +86 -0
  37. data/lib/stripe_mock/request_handlers/balance.rb +17 -0
  38. data/lib/stripe_mock/request_handlers/balance_transactions.rb +37 -0
  39. data/lib/stripe_mock/request_handlers/cards.rb +35 -0
  40. data/lib/stripe_mock/request_handlers/charges.rb +177 -0
  41. data/lib/stripe_mock/request_handlers/country_spec.rb +22 -0
  42. data/lib/stripe_mock/request_handlers/coupons.rb +35 -0
  43. data/lib/stripe_mock/request_handlers/customers.rb +137 -0
  44. data/lib/stripe_mock/request_handlers/disputes.rb +35 -0
  45. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
  46. data/lib/stripe_mock/request_handlers/events.rb +21 -0
  47. data/lib/stripe_mock/request_handlers/external_accounts.rb +55 -0
  48. data/lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb +14 -0
  49. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +127 -0
  50. data/lib/stripe_mock/request_handlers/helpers/charge_helpers.rb +16 -0
  51. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +17 -0
  52. data/lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb +49 -0
  53. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +119 -0
  54. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +44 -0
  55. data/lib/stripe_mock/request_handlers/invoice_items.rb +45 -0
  56. data/lib/stripe_mock/request_handlers/invoices.rb +177 -0
  57. data/lib/stripe_mock/request_handlers/orders.rb +80 -0
  58. data/lib/stripe_mock/request_handlers/payment_intents.rb +203 -0
  59. data/lib/stripe_mock/request_handlers/payment_methods.rb +112 -0
  60. data/lib/stripe_mock/request_handlers/payouts.rb +32 -0
  61. data/lib/stripe_mock/request_handlers/plans.rb +42 -0
  62. data/lib/stripe_mock/request_handlers/products.rb +43 -0
  63. data/lib/stripe_mock/request_handlers/recipients.rb +60 -0
  64. data/lib/stripe_mock/request_handlers/refunds.rb +91 -0
  65. data/lib/stripe_mock/request_handlers/sources.rb +55 -0
  66. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  67. data/lib/stripe_mock/request_handlers/subscriptions.rb +296 -0
  68. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  69. data/lib/stripe_mock/request_handlers/tokens.rb +75 -0
  70. data/lib/stripe_mock/request_handlers/transfers.rb +65 -0
  71. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +32 -0
  72. data/lib/stripe_mock/server.rb +93 -0
  73. data/lib/stripe_mock/test_strategies/base.rb +81 -0
  74. data/lib/stripe_mock/test_strategies/live.rb +40 -0
  75. data/lib/stripe_mock/test_strategies/mock.rb +27 -0
  76. data/lib/stripe_mock/util.rb +44 -0
  77. data/lib/stripe_mock/version.rb +4 -0
  78. data/lib/stripe_mock/webhook_fixtures/account.application.deauthorized.json +12 -0
  79. data/lib/stripe_mock/webhook_fixtures/account.external_account.created.json +27 -0
  80. data/lib/stripe_mock/webhook_fixtures/account.external_account.deleted.json +27 -0
  81. data/lib/stripe_mock/webhook_fixtures/account.external_account.updated.json +27 -0
  82. data/lib/stripe_mock/webhook_fixtures/account.updated.json +26 -0
  83. data/lib/stripe_mock/webhook_fixtures/balance.available.json +25 -0
  84. data/lib/stripe_mock/webhook_fixtures/charge.dispute.closed.json +22 -0
  85. data/lib/stripe_mock/webhook_fixtures/charge.dispute.created.json +22 -0
  86. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  87. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  88. data/lib/stripe_mock/webhook_fixtures/charge.dispute.updated.json +25 -0
  89. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +56 -0
  90. data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +69 -0
  91. data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +55 -0
  92. data/lib/stripe_mock/webhook_fixtures/charge.updated.json +58 -0
  93. data/lib/stripe_mock/webhook_fixtures/coupon.created.json +23 -0
  94. data/lib/stripe_mock/webhook_fixtures/coupon.deleted.json +23 -0
  95. data/lib/stripe_mock/webhook_fixtures/customer.created.json +54 -0
  96. data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +42 -0
  97. data/lib/stripe_mock/webhook_fixtures/customer.discount.created.json +28 -0
  98. data/lib/stripe_mock/webhook_fixtures/customer.discount.deleted.json +28 -0
  99. data/lib/stripe_mock/webhook_fixtures/customer.discount.updated.json +43 -0
  100. data/lib/stripe_mock/webhook_fixtures/customer.source.created.json +32 -0
  101. data/lib/stripe_mock/webhook_fixtures/customer.source.deleted.json +32 -0
  102. data/lib/stripe_mock/webhook_fixtures/customer.source.updated.json +36 -0
  103. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +66 -0
  104. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +65 -0
  105. data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +65 -0
  106. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +78 -0
  107. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +57 -0
  108. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +71 -0
  109. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +105 -0
  110. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +112 -0
  111. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +74 -0
  112. data/lib/stripe_mock/webhook_fixtures/invoiceitem.created.json +21 -0
  113. data/lib/stripe_mock/webhook_fixtures/invoiceitem.deleted.json +21 -0
  114. data/lib/stripe_mock/webhook_fixtures/invoiceitem.updated.json +24 -0
  115. data/lib/stripe_mock/webhook_fixtures/plan.created.json +20 -0
  116. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +20 -0
  117. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +23 -0
  118. data/lib/stripe_mock/webhook_fixtures/transfer.created.json +89 -0
  119. data/lib/stripe_mock/webhook_fixtures/transfer.failed.json +89 -0
  120. data/lib/stripe_mock/webhook_fixtures/transfer.paid.json +89 -0
  121. data/lib/stripe_mock/webhook_fixtures/transfer.updated.json +92 -0
  122. data/lib/trollop.rb +782 -0
  123. data/spec/_dummy/webhooks/dummy.event.json +6 -0
  124. data/spec/api/instance_spec.rb +30 -0
  125. data/spec/fixtures/create_refund.yml +126 -0
  126. data/spec/fixtures/stripe_webhooks/account.updated.json +7 -0
  127. data/spec/fixtures/stripe_webhooks/custom.account.updated.json +5 -0
  128. data/spec/instance_spec.rb +100 -0
  129. data/spec/integration_examples/charge_token_examples.rb +51 -0
  130. data/spec/integration_examples/customer_card_examples.rb +42 -0
  131. data/spec/integration_examples/prepare_error_examples.rb +38 -0
  132. data/spec/list_spec.rb +140 -0
  133. data/spec/readme_spec.rb +75 -0
  134. data/spec/server_spec.rb +139 -0
  135. data/spec/shared_stripe_examples/account_examples.rb +96 -0
  136. data/spec/shared_stripe_examples/balance_examples.rb +11 -0
  137. data/spec/shared_stripe_examples/balance_transaction_examples.rb +63 -0
  138. data/spec/shared_stripe_examples/bank_examples.rb +229 -0
  139. data/spec/shared_stripe_examples/bank_token_examples.rb +59 -0
  140. data/spec/shared_stripe_examples/card_examples.rb +307 -0
  141. data/spec/shared_stripe_examples/card_token_examples.rb +185 -0
  142. data/spec/shared_stripe_examples/charge_examples.rb +510 -0
  143. data/spec/shared_stripe_examples/country_specs_examples.rb +18 -0
  144. data/spec/shared_stripe_examples/coupon_examples.rb +85 -0
  145. data/spec/shared_stripe_examples/customer_examples.rb +453 -0
  146. data/spec/shared_stripe_examples/dispute_examples.rb +98 -0
  147. data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
  148. data/spec/shared_stripe_examples/error_mock_examples.rb +162 -0
  149. data/spec/shared_stripe_examples/external_account_examples.rb +170 -0
  150. data/spec/shared_stripe_examples/extra_features_examples.rb +36 -0
  151. data/spec/shared_stripe_examples/invoice_examples.rb +524 -0
  152. data/spec/shared_stripe_examples/invoice_item_examples.rb +69 -0
  153. data/spec/shared_stripe_examples/payment_intent_examples.rb +131 -0
  154. data/spec/shared_stripe_examples/payment_method_examples.rb +175 -0
  155. data/spec/shared_stripe_examples/payout_examples.rb +68 -0
  156. data/spec/shared_stripe_examples/plan_examples.rb +194 -0
  157. data/spec/shared_stripe_examples/product_example.rb +65 -0
  158. data/spec/shared_stripe_examples/recipient_examples.rb +118 -0
  159. data/spec/shared_stripe_examples/refund_examples.rb +472 -0
  160. data/spec/shared_stripe_examples/subscription_examples.rb +1148 -0
  161. data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
  162. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  163. data/spec/shared_stripe_examples/transfer_examples.rb +130 -0
  164. data/spec/shared_stripe_examples/validation_examples.rb +19 -0
  165. data/spec/shared_stripe_examples/webhook_event_examples.rb +261 -0
  166. data/spec/spec_helper.rb +58 -0
  167. data/spec/stripe_mock_spec.rb +123 -0
  168. data/spec/support/stripe_examples.rb +42 -0
  169. data/spec/util_spec.rb +121 -0
  170. data/stripe-ruby-mock.gemspec +27 -0
  171. metadata +344 -0
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Bank Account Token Mocking' do
4
+
5
+ it "generates a bank token with default values" do
6
+ bank_token = StripeMock.generate_bank_token
7
+ tokens = test_data_source(:bank_tokens)
8
+ expect(tokens[bank_token]).to_not be_nil
9
+ expect(tokens[bank_token][:bank_name]).to eq("STRIPEMOCK TEST BANK")
10
+ expect(tokens[bank_token][:last4]).to eq("6789")
11
+ end
12
+
13
+ it "generates a bank token with an associated account in memory" do
14
+ bank_token = StripeMock.generate_bank_token(
15
+ :bank_name => "Memory Bank",
16
+ :last4 => "7171"
17
+ )
18
+ tokens = test_data_source(:bank_tokens)
19
+ expect(tokens[bank_token]).to_not be_nil
20
+ expect(tokens[bank_token][:bank_name]).to eq("Memory Bank")
21
+ expect(tokens[bank_token][:last4]).to eq("7171")
22
+ end
23
+
24
+ it "creates a token whose id begins with test_btok" do
25
+ bank_token = StripeMock.generate_bank_token({
26
+ :last4 => "1212"
27
+ })
28
+ expect(bank_token).to match /^test_btok/
29
+ end
30
+
31
+ it "assigns the generated bank account to a new recipient" do
32
+ bank_token = StripeMock.generate_bank_token(
33
+ :bank_name => "Bank Token Mocking",
34
+ :last4 => "7777"
35
+ )
36
+
37
+ recipient = Stripe::Recipient.create({
38
+ name: "Fred Flinstone",
39
+ type: "individual",
40
+ email: 'blah@domain.co',
41
+ bank_account: bank_token
42
+ })
43
+ expect(recipient.active_account.last4).to eq("7777")
44
+ expect(recipient.active_account.bank_name).to eq("Bank Token Mocking")
45
+ end
46
+
47
+ it "retrieves a created token" do
48
+ bank_token = StripeMock.generate_bank_token(
49
+ :bank_name => "Cha-ching Banking",
50
+ :last4 => "3939"
51
+ )
52
+ token = Stripe::Token.retrieve(bank_token)
53
+
54
+ expect(token.id).to eq(bank_token)
55
+ expect(token.bank_account.last4).to eq("3939")
56
+ expect(token.bank_account.bank_name).to eq("Cha-ching Banking")
57
+ end
58
+
59
+ end
@@ -0,0 +1,307 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Card API' do
4
+
5
+ it 'creates/returns a card when using customer.sources.create given a card token' do
6
+ customer = Stripe::Customer.create(id: 'test_customer_sub')
7
+ card_token = stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
8
+ card = customer.sources.create(source: card_token)
9
+
10
+ expect(card.customer).to eq('test_customer_sub')
11
+ expect(card.last4).to eq("1123")
12
+ expect(card.exp_month).to eq(11)
13
+ expect(card.exp_year).to eq(2099)
14
+
15
+ customer = Stripe::Customer.retrieve('test_customer_sub')
16
+ expect(customer.sources.count).to eq(1)
17
+ card = customer.sources.data.first
18
+ expect(card.customer).to eq('test_customer_sub')
19
+ expect(card.last4).to eq("1123")
20
+ expect(card.exp_month).to eq(11)
21
+ expect(card.exp_year).to eq(2099)
22
+ end
23
+
24
+ it 'creates/returns a card when using recipient.cards.create given a card token', skip: 'Stripe has deprecated Recipients' do
25
+ params = {
26
+ id: 'test_recipient_sub',
27
+ name: 'MyRec',
28
+ type: 'individual'
29
+ }
30
+
31
+ recipient = Stripe::Recipient.create(params)
32
+ card_token = stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
33
+ card = recipient.cards.create(card: card_token)
34
+
35
+ expect(card.recipient).to eq('test_recipient_sub')
36
+ expect(card.last4).to eq("1123")
37
+ expect(card.exp_month).to eq(11)
38
+ expect(card.exp_year).to eq(2099)
39
+
40
+ recipient = Stripe::Recipient.retrieve('test_recipient_sub')
41
+ expect(recipient.cards.count).to eq(1)
42
+ card = recipient.cards.data.first
43
+ expect(card.recipient).to eq('test_recipient_sub')
44
+ expect(card.last4).to eq("1123")
45
+ expect(card.exp_month).to eq(11)
46
+ expect(card.exp_year).to eq(2099)
47
+ end
48
+
49
+ it 'creates/returns a card when using customer.sources.create given card params' do
50
+ customer = Stripe::Customer.create(id: 'test_customer_sub')
51
+ card = customer.sources.create(card: {
52
+ number: '4242424242424242',
53
+ exp_month: '11',
54
+ exp_year: '3031',
55
+ cvc: '123'
56
+ })
57
+
58
+ expect(card.customer).to eq('test_customer_sub')
59
+ expect(card.last4).to eq("4242")
60
+ expect(card.exp_month).to eq(11)
61
+ expect(card.exp_year).to eq(3031)
62
+
63
+ customer = Stripe::Customer.retrieve('test_customer_sub')
64
+ expect(customer.sources.count).to eq(1)
65
+ card = customer.sources.data.first
66
+ expect(card.customer).to eq('test_customer_sub')
67
+ expect(card.last4).to eq("4242")
68
+ expect(card.exp_month).to eq(11)
69
+ expect(card.exp_year).to eq(3031)
70
+ end
71
+
72
+ it 'creates/returns a card when using recipient.cards.create given card params', skip: 'Stripe has deprecated Recipients' do
73
+ params = {
74
+ id: 'test_recipient_sub',
75
+ name: 'MyRec',
76
+ type: 'individual'
77
+ }
78
+ recipient = Stripe::Recipient.create(params)
79
+ card = recipient.cards.create(card: {
80
+ number: '4000056655665556',
81
+ exp_month: '11',
82
+ exp_year: '3031',
83
+ cvc: '123'
84
+ })
85
+
86
+ expect(card.recipient).to eq('test_recipient_sub')
87
+ expect(card.last4).to eq("5556")
88
+ expect(card.exp_month).to eq(11)
89
+ expect(card.exp_year).to eq(3031)
90
+
91
+ recipient = Stripe::Recipient.retrieve('test_recipient_sub')
92
+ expect(recipient.cards.count).to eq(1)
93
+ card = recipient.cards.data.first
94
+ expect(card.recipient).to eq('test_recipient_sub')
95
+ expect(card.last4).to eq("5556")
96
+ expect(card.exp_month).to eq(11)
97
+ expect(card.exp_year).to eq(3031)
98
+ end
99
+
100
+ it "creates a single card with a generated card token", :live => true do
101
+ customer = Stripe::Customer.create
102
+ expect(customer.sources.count).to eq 0
103
+
104
+ customer.sources.create :source => stripe_helper.generate_card_token
105
+ # Yes, stripe-ruby does not actually add the new card to the customer instance
106
+ expect(customer.sources.count).to eq 0
107
+
108
+ customer2 = Stripe::Customer.retrieve(customer.id)
109
+ expect(customer2.sources.count).to eq 1
110
+ expect(customer2.default_source).to eq customer2.sources.first.id
111
+ end
112
+
113
+ it 'create does not change the customers default card if already set' do
114
+ customer = Stripe::Customer.create(id: 'test_customer_sub', default_source: "test_cc_original")
115
+ card_token = stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
116
+ card = customer.sources.create(source: card_token)
117
+
118
+ customer = Stripe::Customer.retrieve('test_customer_sub')
119
+ expect(customer.default_source).to eq("test_cc_original")
120
+ end
121
+
122
+ it 'create updates the customers default card if not set' do
123
+ customer = Stripe::Customer.create(id: 'test_customer_sub')
124
+ card_token = stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
125
+ card = customer.sources.create(source: card_token)
126
+
127
+ customer = Stripe::Customer.retrieve('test_customer_sub')
128
+ expect(customer.default_source).to_not be_nil
129
+ end
130
+
131
+ describe "retrieval and deletion with customers" do
132
+ let!(:customer) { Stripe::Customer.create(id: 'test_customer_sub') }
133
+ let!(:card_token) { stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099) }
134
+ let!(:card) { customer.sources.create(source: card_token) }
135
+
136
+ it "can retrieve all customer's cards" do
137
+ retrieved = customer.sources.all
138
+ expect(retrieved.count).to eq(1)
139
+ end
140
+
141
+ it "retrieves a customers card" do
142
+ retrieved = customer.sources.retrieve(card.id)
143
+ expect(retrieved.to_s).to eq(card.to_s)
144
+ end
145
+
146
+ it "retrieves a customer's card after re-fetching the customer" do
147
+ retrieved = Stripe::Customer.retrieve(customer.id).sources.retrieve(card.id)
148
+ expect(retrieved.id).to eq card.id
149
+ end
150
+
151
+ it "deletes a customers card" do
152
+ card.delete
153
+ retrieved_cus = Stripe::Customer.retrieve(customer.id)
154
+ expect(retrieved_cus.sources.data).to be_empty
155
+ end
156
+
157
+ it "deletes a customers card then set the default_card to nil" do
158
+ card.delete
159
+ retrieved_cus = Stripe::Customer.retrieve(customer.id)
160
+ expect(retrieved_cus.default_source).to be_nil
161
+ end
162
+
163
+ it "updates the default card if deleted" do
164
+ card.delete
165
+ retrieved_cus = Stripe::Customer.retrieve(customer.id)
166
+ expect(retrieved_cus.default_source).to be_nil
167
+ end
168
+
169
+ it 'updates total_count if deleted' do
170
+ card.delete
171
+ sources = Stripe::Customer.retrieve(customer.id).sources
172
+
173
+ expect(sources.total_count).to eq 0
174
+ end
175
+
176
+ context "deletion when the user has two cards" do
177
+ let!(:card_token_2) { stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099) }
178
+ let!(:card_2) { customer.sources.create(source: card_token_2) }
179
+
180
+ it "has just one card anymore" do
181
+ card.delete
182
+ retrieved_cus = Stripe::Customer.retrieve(customer.id)
183
+ expect(retrieved_cus.sources.data.count).to eq 1
184
+ expect(retrieved_cus.sources.data.first.id).to eq card_2.id
185
+ end
186
+
187
+ it "sets the default_card id to the last card remaining id" do
188
+ card.delete
189
+ retrieved_cus = Stripe::Customer.retrieve(customer.id)
190
+ expect(retrieved_cus.default_source).to eq card_2.id
191
+ end
192
+ end
193
+ end
194
+
195
+ describe "retrieval and deletion with recipients", :live => true, skip: 'Stripe has deprecated Recipients' do
196
+ let!(:recipient) { Stripe::Recipient.create(name: 'Test Recipient', type: 'individual') }
197
+ let!(:card_token) { stripe_helper.generate_card_token(number: "4000056655665556") }
198
+ let!(:card) { recipient.cards.create(card: card_token) }
199
+
200
+ it "can retrieve all recipient's cards" do
201
+ retrieved = recipient.cards.all
202
+ expect(retrieved.count).to eq(1)
203
+ end
204
+
205
+ it "deletes a recipient card" do
206
+ card.delete
207
+ retrieved_cus = Stripe::Recipient.retrieve(recipient.id)
208
+ expect(retrieved_cus.cards.data).to be_empty
209
+ end
210
+
211
+ it "deletes a recipient card then set the default_card to nil" do
212
+ card.delete
213
+ retrieved_cus = Stripe::Recipient.retrieve(recipient.id)
214
+ expect(retrieved_cus.default_card).to be_nil
215
+ end
216
+
217
+ context "deletion when the recipient has two cards" do
218
+ let!(:card_token_2) { stripe_helper.generate_card_token(number: "5200828282828210") }
219
+ let!(:card_2) { recipient.cards.create(card: card_token_2) }
220
+
221
+ it "has just one card anymore" do
222
+ card.delete
223
+ retrieved_rec = Stripe::Recipient.retrieve(recipient.id)
224
+ expect(retrieved_rec.cards.data.count).to eq 1
225
+ expect(retrieved_rec.cards.data.first.id).to eq card_2.id
226
+ end
227
+
228
+ it "sets the default_card id to the last card remaining id" do
229
+ card.delete
230
+ retrieved_rec = Stripe::Recipient.retrieve(recipient.id)
231
+ expect(retrieved_rec.default_card).to eq card_2.id
232
+ end
233
+ end
234
+ end
235
+
236
+ describe "Errors", :live => true do
237
+ it "throws an error when the customer does not have the retrieving card id" do
238
+ customer = Stripe::Customer.create
239
+ card_id = "card_123"
240
+ expect { customer.sources.retrieve(card_id) }.to raise_error {|e|
241
+ expect(e).to be_a Stripe::InvalidRequestError
242
+ expect(e.message).to match /no.*source/i
243
+ expect(e.message).to include card_id
244
+ expect(e.param).to eq 'id'
245
+ expect(e.http_status).to eq 404
246
+ }
247
+ end
248
+ end
249
+
250
+ context "update card" do
251
+ let!(:customer) { Stripe::Customer.create(id: 'test_customer_sub') }
252
+ let!(:card_token) { stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099) }
253
+ let!(:card) { customer.sources.create(source: card_token) }
254
+
255
+ it "updates the card" do
256
+ exp_month = 10
257
+ exp_year = 2098
258
+
259
+ card.exp_month = exp_month
260
+ card.exp_year = exp_year
261
+ card.save
262
+
263
+ retrieved = customer.sources.retrieve(card.id)
264
+
265
+ expect(retrieved.exp_month).to eq(exp_month)
266
+ expect(retrieved.exp_year).to eq(exp_year)
267
+ end
268
+ end
269
+
270
+ context "retrieve multiple cards" do
271
+
272
+ it "retrieves a list of multiple cards" do
273
+ customer = Stripe::Customer.create(id: 'test_customer_card')
274
+
275
+ card_token = stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
276
+ card1 = customer.sources.create(source: card_token)
277
+ card_token = stripe_helper.generate_card_token(last4: "1124", exp_month: 12, exp_year: 2098)
278
+ card2 = customer.sources.create(source: card_token)
279
+
280
+ customer = Stripe::Customer.retrieve('test_customer_card')
281
+
282
+ list = customer.sources.all
283
+
284
+ expect(list.object).to eq("list")
285
+ expect(list.count).to eq(2)
286
+ expect(list.data.length).to eq(2)
287
+
288
+ expect(list.data.first.object).to eq("card")
289
+ expect(list.data.first.to_hash).to eq(card1.to_hash)
290
+
291
+ expect(list.data.last.object).to eq("card")
292
+ expect(list.data.last.to_hash).to eq(card2.to_hash)
293
+ end
294
+
295
+ it "retrieves an empty list if there's no subscriptions" do
296
+ Stripe::Customer.create(id: 'no_cards')
297
+ customer = Stripe::Customer.retrieve('no_cards')
298
+
299
+ list = customer.sources.all
300
+
301
+ expect(list.object).to eq("list")
302
+ expect(list.count).to eq(0)
303
+ expect(list.data.length).to eq(0)
304
+ end
305
+ end
306
+
307
+ end
@@ -0,0 +1,185 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Card Token Mocking' do
4
+
5
+ describe 'Direct Token Creation' do
6
+
7
+ it "generates and reads a card token with default values" do
8
+ card_token = StripeMock.generate_card_token
9
+
10
+ charge = Stripe::Charge.create(amount: 500, currency: 'usd', source: card_token)
11
+ card = charge.source
12
+ expect(card.last4).to eq("4242")
13
+ expect(card.exp_month).to eq(4)
14
+ expect(card.exp_year).to eq(2016)
15
+ expect(card.tokenization_method).to eq(nil)
16
+ end
17
+
18
+ it "generates and reads a card token for create charge" do
19
+ card_token = StripeMock.generate_card_token(last4: "2244", exp_month: 33, exp_year: 2255)
20
+
21
+ charge = Stripe::Charge.create(amount: 500, currency: 'usd', source: card_token)
22
+ card = charge.source
23
+ expect(card.last4).to eq("2244")
24
+ expect(card.exp_month).to eq(33)
25
+ expect(card.exp_year).to eq(2255)
26
+ end
27
+
28
+ it "generates and reads a card token for create customer" do
29
+ card_token = StripeMock.generate_card_token(last4: "9191", exp_month: 99, exp_year: 3005)
30
+
31
+ cus = Stripe::Customer.create(source: card_token)
32
+ card = cus.sources.data.first
33
+ expect(card.last4).to eq("9191")
34
+ expect(card.exp_month).to eq(99)
35
+ expect(card.exp_year).to eq(3005)
36
+ end
37
+
38
+ it "generates and reads a card token for update customer" do
39
+ card_token = StripeMock.generate_card_token(last4: "1133", exp_month: 11, exp_year: 2099)
40
+
41
+ cus = Stripe::Customer.create
42
+ cus.source = card_token
43
+ cus.save
44
+
45
+ card = cus.sources.data.first
46
+ expect(card.last4).to eq("1133")
47
+ expect(card.exp_month).to eq(11)
48
+ expect(card.exp_year).to eq(2099)
49
+ end
50
+
51
+ it "retrieves a created token" do
52
+ card_token = StripeMock.generate_card_token(last4: "2323", exp_month: 33, exp_year: 2222)
53
+ token = Stripe::Token.retrieve(card_token)
54
+
55
+ expect(token.id).to eq(card_token)
56
+ expect(token.card.last4).to eq("2323")
57
+ expect(token.card.exp_month).to eq(33)
58
+ expect(token.card.exp_year).to eq(2222)
59
+ end
60
+ end
61
+
62
+ describe 'Stripe::Token' do
63
+
64
+ it "generates and reads a card token for create customer" do
65
+
66
+ card_token = Stripe::Token.create({
67
+ card: {
68
+ number: "4222222222222222",
69
+ exp_month: 9,
70
+ exp_year: 2017
71
+ }
72
+ })
73
+
74
+ cus = Stripe::Customer.create(source: card_token.id)
75
+ card = cus.sources.data.first
76
+ expect(card.last4).to eq("2222")
77
+ expect(card.exp_month).to eq(9)
78
+ expect(card.exp_year).to eq(2017)
79
+ end
80
+
81
+ it "generates and reads a card token for update customer" do
82
+ card_token = Stripe::Token.create({
83
+ card: {
84
+ number: "1111222233334444",
85
+ exp_month: 11,
86
+ exp_year: 2019
87
+ }
88
+ })
89
+
90
+ cus = Stripe::Customer.create
91
+ cus.source = card_token.id
92
+ cus.save
93
+
94
+ card = cus.sources.data.first
95
+ expect(card.last4).to eq("4444")
96
+ expect(card.exp_month).to eq(11)
97
+ expect(card.exp_year).to eq(2019)
98
+ end
99
+
100
+ it "generates a card token created from customer" do
101
+ card_token = Stripe::Token.create({
102
+ card: {
103
+ number: "1111222233334444",
104
+ exp_month: 11,
105
+ exp_year: 2019
106
+ }
107
+ })
108
+
109
+ cus = Stripe::Customer.create()
110
+ cus.source = card_token.id
111
+ cus.save
112
+
113
+ card_token = Stripe::Token.create({
114
+ customer: cus.id
115
+ })
116
+
117
+ expect(card_token.object).to eq("token")
118
+ end
119
+
120
+ it "generates a card token from another card" do
121
+ token = StripeMock.generate_card_token(last4: "2244", exp_month: 33, exp_year: 2255)
122
+
123
+ cus1 = Stripe::Customer.create()
124
+ cus1.source = token
125
+ cus1.save
126
+
127
+ card1 = cus1.sources.data.first
128
+ expect(card1.last4).to eq("2244")
129
+ expect(card1.exp_month).to eq(33)
130
+ expect(card1.exp_year).to eq(2255)
131
+
132
+ card_token = Stripe::Token.create({
133
+ customer: cus1.id,
134
+ card: card1.id
135
+ })
136
+
137
+ cus2 = Stripe::Customer.create()
138
+ cus2.source = card_token.id
139
+ cus2.save
140
+
141
+ card2 = cus2.sources.data.first
142
+ expect(card2.last4).to eq("2244")
143
+ expect(card2.exp_month).to eq(33)
144
+ expect(card2.exp_year).to eq(2255)
145
+ end
146
+
147
+ it 'generates a card token from another card', oauth: true do
148
+ token = Stripe::Token.create(
149
+ card: {
150
+ exp_month: 10,
151
+ exp_year: 2016,
152
+ number: '4242424242424242'
153
+ }
154
+ )
155
+
156
+ cus1 = Stripe::Customer.create(source: token.id)
157
+
158
+ card1 = cus1.sources.data.first
159
+ expect(card1.last4).to eq('4242')
160
+ expect(card1.exp_month).to eq(10)
161
+ expect(card1.exp_year).to eq(2016)
162
+
163
+ card_token = Stripe::Token.create(
164
+ {
165
+ customer: cus1.id,
166
+ card: card1.id
167
+ },
168
+ ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN'])
169
+
170
+ cus2 = Stripe::Customer.create({ source: card_token.id }, ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN'])
171
+
172
+ card2 = cus2.sources.data.first
173
+ expect(card2.last4).to eq('4242')
174
+ expect(card2.exp_month).to eq(10)
175
+ expect(card2.exp_year).to eq(2016)
176
+ end
177
+
178
+ it "throws an error if neither card nor customer are provided", :live => true do
179
+ expect { Stripe::Token.create }.to raise_error(
180
+ Stripe::InvalidRequestError, /must supply either a card, customer/
181
+ )
182
+ end
183
+ end
184
+
185
+ end