stripe-ruby-mock 1.10.1.7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -47,9 +47,7 @@ shared_examples 'Plan API' do
47
47
 
48
48
 
49
49
  it "retrieves a stripe plan" do
50
- original = Stripe::Plan.create({
51
- amount: 1331
52
- })
50
+ original = stripe_helper.create_plan(amount: 1331)
53
51
  plan = Stripe::Plan.retrieve(original.id)
54
52
 
55
53
  expect(plan.id).to eq(original.id)
@@ -58,7 +56,7 @@ shared_examples 'Plan API' do
58
56
 
59
57
 
60
58
  it "updates a stripe plan" do
61
- Stripe::Plan.create(id: 'super_member', amount: 111)
59
+ stripe_helper.create_plan(id: 'super_member', amount: 111)
62
60
 
63
61
  plan = Stripe::Plan.retrieve('super_member')
64
62
  expect(plan.amount).to eq(111)
@@ -79,7 +77,7 @@ shared_examples 'Plan API' do
79
77
  end
80
78
 
81
79
  it "deletes a stripe plan" do
82
- Stripe::Plan.create(id: 'super_member', amount: 111)
80
+ stripe_helper.create_plan(id: 'super_member', amount: 111)
83
81
 
84
82
  plan = Stripe::Plan.retrieve('super_member')
85
83
  expect(plan).to_not be_nil
@@ -94,29 +92,42 @@ shared_examples 'Plan API' do
94
92
  end
95
93
 
96
94
  it "retrieves all plans" do
97
- Stripe::Plan.create({ id: 'Plan One', amount: 54321 })
98
- Stripe::Plan.create({ id: 'Plan Two', amount: 98765 })
95
+ stripe_helper.create_plan(id: 'Plan One', amount: 54321)
96
+ stripe_helper.create_plan(id: 'Plan Two', amount: 98765)
99
97
 
100
98
  all = Stripe::Plan.all
101
99
  expect(all.length).to eq(2)
102
- all.map(&:id).should include('Plan One', 'Plan Two')
103
- all.map(&:amount).should include(54321, 98765)
100
+ expect(all.map &:id).to include('Plan One', 'Plan Two')
101
+ expect(all.map &:amount).to include(54321, 98765)
104
102
  end
105
103
 
106
104
 
107
- context "With strict mode toggled off" do
105
+ describe "Validation", :live => true do
106
+ let(:params) { stripe_helper.create_plan_params }
107
+ let(:subject) { Stripe::Plan.create(params) }
108
108
 
109
- before { StripeMock.toggle_strict(false) }
109
+ describe "Required Parameters" do
110
+ after do
111
+ params.delete(@name)
112
+ expect { subject }.to raise_error(Stripe::InvalidRequestError, "Missing required param: #{@name}")
113
+ end
110
114
 
111
- it "can retrieve a stripe plan with an id that doesn't exist" do
112
- plan = Stripe::Plan.retrieve('test_charge_x')
115
+ it("requires a name") { @name = :name }
116
+ it("requires an amount") { @name = :amount }
117
+ it("requires a currency") { @name = :currency }
118
+ it("requires an interval") { @name = :interval }
119
+ end
120
+
121
+ describe "Uniqueness" do
113
122
 
114
- expect(plan.id).to eq('test_charge_x')
115
- expect(plan.amount).to_not be_nil
116
- expect(plan.name).to_not be_nil
123
+ it "validates for uniqueness" do
124
+ stripe_helper.delete_plan(params[:id])
117
125
 
118
- expect(plan.currency).to_not be_nil
119
- expect(plan.interval).to_not be_nil
126
+ Stripe::Plan.create(params)
127
+ expect {
128
+ Stripe::Plan.create(params)
129
+ }.to raise_error(Stripe::InvalidRequestError, "Plan already exists.")
130
+ end
120
131
  end
121
132
  end
122
133
 
@@ -2,52 +2,87 @@ require 'spec_helper'
2
2
 
3
3
  shared_examples 'Recipient API' do
4
4
 
5
- it "creates a stripe recipient with a default card" do
5
+ it "creates a stripe recipient with a default bank and card" do
6
6
  recipient = Stripe::Recipient.create({
7
7
  type: "corporation",
8
8
  name: "MyCo",
9
9
  email: "owner@myco.com",
10
- bank_account: 'void_bank_token'
10
+ bank_account: 'void_bank_token',
11
+ card: stripe_helper.generate_card_token
11
12
  })
12
13
  expect(recipient.id).to match /^test_rp/
14
+ expect(recipient.type).to eq('corporation')
13
15
  expect(recipient.name).to eq('MyCo')
14
16
  expect(recipient.email).to eq('owner@myco.com')
17
+ expect(recipient.default_card).to_not be_nil
15
18
 
16
19
  expect(recipient.active_account).to_not be_nil
17
20
  expect(recipient.active_account.bank_name).to_not be_nil
18
21
  expect(recipient.active_account.last4).to_not be_nil
22
+
23
+ expect(recipient.cards.count).to eq(1)
24
+ expect(recipient.cards.data.length).to eq(1)
25
+ expect(recipient.default_card).to_not be_nil
26
+ expect(recipient.default_card).to eq recipient.cards.data.first.id
27
+
28
+ expect { recipient.card }.to raise_error
29
+ end
30
+
31
+ it "creates a stripe recipient without a card" do
32
+ recipient = Stripe::Recipient.create({
33
+ type: "corporation",
34
+ name: "MyCo",
35
+ email: "cardless@appleseed.com"
36
+ })
37
+ expect(recipient.id).to match(/^test_rp/)
38
+ expect(recipient.type).to eq('corporation')
39
+ expect(recipient.name).to eq('MyCo')
40
+ expect(recipient.email).to eq('cardless@appleseed.com')
41
+
42
+ expect(recipient.cards.count).to eq(0)
43
+ expect(recipient.cards.data.length).to eq(0)
44
+ expect(recipient.default_card).to be_nil
19
45
  end
20
46
 
21
47
  it "stores a created stripe recipient in memory" do
22
48
  recipient = Stripe::Recipient.create({
23
49
  type: "individual",
24
50
  name: "Customer One",
25
- bank_account: 'bank_account_token_1'
51
+ bank_account: 'bank_account_token_1',
52
+ card: stripe_helper.generate_card_token
26
53
  })
27
54
  recipient2 = Stripe::Recipient.create({
28
55
  type: "individual",
29
56
  name: "Customer Two",
30
- bank_account: 'bank_account_token_1'
57
+ bank_account: 'bank_account_token_1',
58
+ card: stripe_helper.generate_card_token
31
59
  })
32
60
  data = test_data_source(:recipients)
33
61
  expect(data[recipient.id]).to_not be_nil
62
+ expect(data[recipient.id][:type]).to eq("individual")
34
63
  expect(data[recipient.id][:name]).to eq("Customer One")
64
+ expect(data[recipient.id][:default_card]).to_not be_nil
35
65
 
36
66
  expect(data[recipient2.id]).to_not be_nil
67
+ expect(data[recipient2.id][:type]).to eq("individual")
37
68
  expect(data[recipient2.id][:name]).to eq("Customer Two")
69
+ expect(data[recipient2.id][:default_card]).to_not be_nil
38
70
  end
39
71
 
40
72
  it "retrieves a stripe recipient" do
41
73
  original = Stripe::Recipient.create({
42
74
  type: "individual",
43
75
  name: "Bob",
44
- email: "bob@example.com"
76
+ email: "bob@example.com",
77
+ card: stripe_helper.generate_card_token
45
78
  })
46
79
  recipient = Stripe::Recipient.retrieve(original.id)
47
80
 
48
81
  expect(recipient.id).to eq(original.id)
82
+ expect(recipient.type).to eq(original.type)
49
83
  expect(recipient.name).to eq(original.name)
50
84
  expect(recipient.email).to eq(original.email)
85
+ expect(recipient.default_card).to_not be_nil
51
86
  end
52
87
 
53
88
  it "cannot retrieve a recipient that doesn't exist" do
@@ -58,5 +93,20 @@ shared_examples 'Recipient API' do
58
93
  }
59
94
  end
60
95
 
96
+ describe "Errors", :live => true do
97
+ it "throws an error when the customer does not have the retrieving card id" do
98
+ recipient = Stripe::Recipient.create(:name => "Bob Bobber", :type => "individual")
99
+ card_id = "card_123"
100
+ expect { recipient.cards.retrieve(card_id) }.to raise_error {|e|
101
+ expect(e).to be_a Stripe::InvalidRequestError
102
+ expect(e.message).to include "Recipient", recipient.id, "does not have", card_id
103
+ expect(e.param).to eq 'card'
104
+ expect(e.http_status).to eq 404
105
+ }
106
+ end
107
+
108
+ it "throws an error when the name does not have both first and last"
109
+ end
110
+
61
111
  end
62
112
 
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Refund API' do
4
+
5
+ it "refunds a stripe charge item" do
6
+ charge = Stripe::Charge.create(
7
+ amount: 999,
8
+ currency: 'USD',
9
+ card: stripe_helper.generate_card_token,
10
+ description: 'card charge'
11
+ )
12
+
13
+ charge = charge.refund(amount: 999)
14
+
15
+ expect(charge.refunded).to eq(true)
16
+ expect(charge.refunds.data.first.amount).to eq(999)
17
+ expect(charge.amount_refunded).to eq(999)
18
+ end
19
+
20
+ it "creates a stripe refund with the charge ID", :live => true do
21
+ charge = Stripe::Charge.create(
22
+ amount: 999,
23
+ currency: 'USD',
24
+ card: stripe_helper.generate_card_token,
25
+ description: 'card charge'
26
+ )
27
+ refund = charge.refund
28
+
29
+ expect(charge.id).to match(/^(test_)?ch/)
30
+ expect(refund.id).to eq(charge.id)
31
+ end
32
+
33
+ it "creates a stripe refund with a refund ID" do
34
+ charge = Stripe::Charge.create(
35
+ amount: 999,
36
+ currency: 'USD',
37
+ card: stripe_helper.generate_card_token,
38
+ description: 'card charge'
39
+ )
40
+ refund = charge.refund
41
+
42
+ expect(refund.refunds.data.count).to eq 1
43
+ expect(refund.refunds.data.first.id).to match(/^test_re/)
44
+ end
45
+
46
+ it "creates a stripe refund with a different balance transaction than the charge" do
47
+ charge = Stripe::Charge.create(
48
+ amount: 999,
49
+ currency: 'USD',
50
+ card: stripe_helper.generate_card_token,
51
+ description: 'card charge'
52
+ )
53
+ refund = charge.refund
54
+
55
+ expect(charge.balance_transaction).not_to eq(refund.refunds.data.first.balance_transaction)
56
+ end
57
+
58
+ it "creates a refund off a charge", :live => true do
59
+ original = Stripe::Charge.create(amount: 555, currency: 'USD', card: stripe_helper.generate_card_token)
60
+
61
+ charge = Stripe::Charge.retrieve(original.id)
62
+
63
+ refund = charge.refunds.create(amount: 555)
64
+ expect(refund.amount).to eq 555
65
+ expect(refund.charge).to eq charge.id
66
+ end
67
+
68
+ it "handles multiple refunds", :live => true do
69
+ original = Stripe::Charge.create(amount: 1100, currency: 'USD', card: stripe_helper.generate_card_token)
70
+
71
+ charge = Stripe::Charge.retrieve(original.id)
72
+
73
+ refund_1 = charge.refunds.create(amount: 300)
74
+ expect(refund_1.amount).to eq 300
75
+ expect(refund_1.charge).to eq charge.id
76
+
77
+ refund_2 = charge.refunds.create(amount: 400)
78
+ expect(refund_2.amount).to eq 400
79
+ expect(refund_2.charge).to eq charge.id
80
+
81
+ expect(charge.refunds.count).to eq 0
82
+ expect(charge.refunds.total_count).to eq 0
83
+ expect(charge.amount_refunded).to eq 0
84
+
85
+ charge = Stripe::Charge.retrieve(original.id)
86
+ expect(charge.refunds.count).to eq 2
87
+ expect(charge.refunds.total_count).to eq 2
88
+ expect(charge.amount_refunded).to eq 700
89
+ end
90
+ end