stripe-ruby-mock 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/stripe_mock.rb +3 -1
  4. data/lib/stripe_mock/data.rb +14 -5
  5. data/lib/stripe_mock/data/list.rb +64 -0
  6. data/lib/stripe_mock/instance.rb +1 -1
  7. data/lib/stripe_mock/request_handlers/cards.rb +21 -38
  8. data/lib/stripe_mock/request_handlers/charges.rb +4 -4
  9. data/lib/stripe_mock/request_handlers/coupons.rb +3 -3
  10. data/lib/stripe_mock/request_handlers/customers.rb +5 -5
  11. data/lib/stripe_mock/request_handlers/events.rb +1 -1
  12. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +36 -1
  13. data/lib/stripe_mock/request_handlers/invoice_items.rb +4 -4
  14. data/lib/stripe_mock/request_handlers/invoices.rb +7 -7
  15. data/lib/stripe_mock/request_handlers/plans.rb +4 -4
  16. data/lib/stripe_mock/request_handlers/recipients.rb +2 -2
  17. data/lib/stripe_mock/request_handlers/subscriptions.rb +10 -10
  18. data/lib/stripe_mock/request_handlers/tokens.rb +4 -4
  19. data/lib/stripe_mock/test_strategies/base.rb +2 -3
  20. data/lib/stripe_mock/util.rb +11 -0
  21. data/lib/stripe_mock/version.rb +1 -1
  22. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +1 -0
  23. data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +1 -0
  24. data/lib/stripe_mock/webhook_fixtures/customer.card.created.json +1 -0
  25. data/lib/stripe_mock/webhook_fixtures/customer.card.deleted.json +1 -0
  26. data/lib/stripe_mock/webhook_fixtures/customer.card.updated.json +1 -0
  27. data/lib/stripe_mock/webhook_fixtures/customer.created.json +1 -0
  28. data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +2 -1
  29. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +1 -0
  30. data/spec/list_spec.rb +123 -0
  31. data/spec/shared_stripe_examples/card_examples.rb +87 -9
  32. data/spec/shared_stripe_examples/charge_examples.rb +12 -6
  33. data/spec/shared_stripe_examples/coupon_examples.rb +1 -1
  34. data/spec/shared_stripe_examples/customer_examples.rb +1 -1
  35. data/spec/shared_stripe_examples/invoice_examples.rb +8 -2
  36. data/spec/shared_stripe_examples/invoice_item_examples.rb +1 -1
  37. data/spec/shared_stripe_examples/plan_examples.rb +1 -1
  38. data/spec/util_spec.rb +69 -35
  39. metadata +5 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d4f15152fa1de80be81c36c7d32ce3c95368c35
4
- data.tar.gz: a784112eabc78dfdbff8ccfacf76a010b3b388e5
3
+ metadata.gz: b72e2ccbae6fb810bf3d2a73fc6ddeb072badc6b
4
+ data.tar.gz: 1022933805ff7d31061036afb39884abcbd5a048
5
5
  SHA512:
6
- metadata.gz: d1e1ada3e91652ecec3d26f12906e0ccd86d630a5d035241c1fd2b2f3cb3f706566c2b6a3fdefe484d671f7987d09554a5f66fdfd65ffee1d72dd8d490ea4722
7
- data.tar.gz: beb89639c55692a25db58d0efca70e6c69487b37aba4c8ec87027e745bb620ed442629302b34e3e1015da52270b2b103a70f6684ad11aea9f6685fb970c19cd6
6
+ metadata.gz: 88d880ba38c32c1df49c39921a05770b1545232469ae771e9979a3b8a0ad65cfa7db8e7ad22dbcdbe376b5ebcaa035d45f5eac623531d0269a1492cf38778537
7
+ data.tar.gz: 5c96162f36106a8869c093def7b0de6f020f57b17b10a514e1994bc324565a3c12d43a0b59ecd73392985b6878ffca67831a76ee7abac5d3f8ccba3e5b3fb18a
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  In your gemfile:
10
10
 
11
- gem 'stripe-ruby-mock', '~> 2.0.0'
11
+ gem 'stripe-ruby-mock', '~> 2.0.2', :require => 'stripe_mock'
12
12
 
13
13
  ## Features
14
14
 
data/lib/stripe_mock.rb CHANGED
@@ -5,10 +5,12 @@ require 'dante'
5
5
  require 'stripe'
6
6
 
7
7
  require 'stripe_mock/version'
8
- require 'stripe_mock/data'
9
8
  require 'stripe_mock/util'
10
9
  require 'stripe_mock/error_queue'
11
10
 
11
+ require 'stripe_mock/data'
12
+ require 'stripe_mock/data/list'
13
+
12
14
  require 'stripe_mock/errors/stripe_mock_error'
13
15
  require 'stripe_mock/errors/unsupported_request_error'
14
16
  require 'stripe_mock/errors/uninitialized_instance_error'
@@ -16,7 +16,7 @@ module StripeMock
16
16
  account_balance: 0,
17
17
  cards: {
18
18
  object: "list",
19
- count: cards.count,
19
+ total_count: cards.size,
20
20
  url: "/v1/customers/#{cus_id}/cards",
21
21
  data: cards
22
22
  },
@@ -49,6 +49,7 @@ module StripeMock
49
49
  last4: "4242",
50
50
  type: "Visa",
51
51
  brand: "Visa",
52
+ funding: "credit",
52
53
  exp_month: 12,
53
54
  exp_year: 2013,
54
55
  fingerprint: "3TQGpK9JoY1GgXPw",
@@ -107,12 +108,13 @@ module StripeMock
107
108
  end
108
109
 
109
110
  def self.mock_card(params={})
110
- {
111
+ StripeMock::Util.card_merge({
111
112
  id: "test_cc_default",
112
113
  object: "card",
113
114
  last4: "4242",
114
115
  type: "Visa",
115
116
  brand: "Visa",
117
+ funding: "credit",
116
118
  exp_month: 4,
117
119
  exp_year: 2016,
118
120
  fingerprint: "wXWJT135mEK107G8",
@@ -128,7 +130,7 @@ module StripeMock
128
130
  cvc_check: nil,
129
131
  address_line1_check: nil,
130
132
  address_zip_check: nil
131
- }.merge(params)
133
+ }, params)
132
134
  end
133
135
 
134
136
  def self.mock_bank_account(params={})
@@ -182,6 +184,7 @@ module StripeMock
182
184
  :trial_end => 1308681468,
183
185
  :customer => "c_test_customer",
184
186
  :quantity => 1,
187
+ :tax_percent => nil,
185
188
  :metadata => {}
186
189
  }, params)
187
190
  end
@@ -312,9 +315,9 @@ module StripeMock
312
315
  },
313
316
  cards: {
314
317
  object: "list",
315
- count: cards.count,
316
318
  url: "/v1/recipients/#{rp_id}/cards",
317
- data: cards
319
+ data: cards,
320
+ total_count: cards.count
318
321
  },
319
322
  default_card: nil
320
323
  }.merge(params)
@@ -341,6 +344,7 @@ module StripeMock
341
344
  :last4 => '2222',
342
345
  :type => 'Visa',
343
346
  :brand => 'Visa',
347
+ :funding => 'credit',
344
348
  :exp_month => 9,
345
349
  :exp_year => 2017,
346
350
  :fingerprint => 'JRRLXGh38NiYygM7',
@@ -436,5 +440,10 @@ module StripeMock
436
440
  :id => "di_test_coupon"
437
441
  }
438
442
  end
443
+
444
+ def self.mock_list_object(data, params = {})
445
+ list = StripeMock::Data::List.new(data, params)
446
+ list.to_h
447
+ end
439
448
  end
440
449
  end
@@ -0,0 +1,64 @@
1
+ module StripeMock
2
+ module Data
3
+ class List
4
+ attr_reader :data, :limit, :offset, :starting_after
5
+
6
+ def initialize(data, options = {})
7
+ @data = Array(data.clone)
8
+ @limit = [[options[:limit] || 10, 100].min, 1].max # restrict @limit to 1..100
9
+ @starting_after = options[:starting_after]
10
+ end
11
+
12
+ def url
13
+ "/v1/#{object_types}"
14
+ end
15
+
16
+ def to_hash
17
+ { object: "list", data: data_page, url: url, has_more: has_more? }
18
+ end
19
+ alias_method :to_h, :to_hash
20
+
21
+ def has_more?
22
+ (offset + limit) < data.size
23
+ end
24
+
25
+ def method_missing(method_name, *args, &block)
26
+ hash = to_hash
27
+
28
+ if hash.keys.include?(method_name)
29
+ hash[method_name]
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ def respond_to?(method_name, priv = false)
36
+ to_hash.keys.include?(method_name) || super
37
+ end
38
+
39
+ private
40
+
41
+ def offset
42
+ if starting_after
43
+ if index = data.index { |datum| datum.id == starting_after }
44
+ index + 1
45
+ else
46
+ raise "No such object id: #{starting_after}"
47
+ end
48
+ else
49
+ 0
50
+ end
51
+ end
52
+
53
+ def data_page
54
+ data[offset, limit]
55
+ end
56
+
57
+ def object_types
58
+ if first_object = data[0]
59
+ "#{first_object.class.to_s.split('::')[-1].downcase}s"
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -95,7 +95,7 @@ module StripeMock
95
95
 
96
96
  private
97
97
 
98
- def assert_existance(type, id, obj, message=nil)
98
+ def assert_existence(type, id, obj, message=nil)
99
99
  if obj.nil?
100
100
  msg = message || "No such #{type}: #{id}"
101
101
  raise Stripe::InvalidRequestError.new(msg, type.to_s, 404)
@@ -9,78 +9,61 @@ module StripeMock
9
9
  klass.add_handler 'delete /v1/customers/(.*)/cards/(.*)', :delete_card
10
10
  klass.add_handler 'post /v1/customers/(.*)/cards/(.*)', :update_card
11
11
  klass.add_handler 'get /v1/recipients/(.*)/cards/(.*)', :retrieve_recipient_card
12
+ klass.add_handler 'post /v1/recipients/(.*)/cards', :create_recipient_card
13
+ klass.add_handler 'delete /v1/recipients/(.*)/cards/(.*)', :delete_recipient_card
12
14
  end
13
15
 
14
16
  def create_card(route, method_url, params, headers)
15
17
  route =~ method_url
16
- customer = assert_existance :customer, $1, customers[$1]
18
+ add_card_to(:customer, $1, params, customers)
19
+ end
17
20
 
18
- card = card_from_params(params[:card])
19
- add_card_to_object(:customer, card, customer)
21
+ def create_recipient_card(route, method_url, params, headers)
22
+ route =~ method_url
23
+ add_card_to(:recipient, $1, params, recipients)
20
24
  end
21
25
 
22
26
  def retrieve_cards(route, method_url, params, headers)
23
27
  route =~ method_url
24
- customer = assert_existance :customer, $1, customers[$1]
25
-
28
+ customer = assert_existence :customer, $1, customers[$1]
26
29
  cards = customer[:cards]
27
- cards[:count] = cards[:data].length
28
- cards
30
+
31
+ Data.mock_list_object(cards[:data])
29
32
  end
30
33
 
31
34
  def retrieve_card(route, method_url, params, headers)
32
35
  route =~ method_url
33
- customer = assert_existance :customer, $1, customers[$1]
36
+ customer = assert_existence :customer, $1, customers[$1]
34
37
 
35
- assert_existance :card, $2, get_card(customer, $2)
38
+ assert_existence :card, $2, get_card(customer, $2)
36
39
  end
37
40
 
38
41
  def retrieve_recipient_card(route, method_url, params, headers)
39
42
  route =~ method_url
40
- recipient = assert_existance :recipient, $1, recipients[$1]
43
+ recipient = assert_existence :recipient, $1, recipients[$1]
41
44
 
42
- assert_existance :card, $2, get_card(recipient, $2, "Recipient")
45
+ assert_existence :card, $2, get_card(recipient, $2, "Recipient")
43
46
  end
44
47
 
45
48
  def delete_card(route, method_url, params, headers)
46
49
  route =~ method_url
47
- customer = assert_existance :customer, $1, customers[$1]
48
-
49
- assert_existance :card, $2, get_card(customer, $2)
50
+ delete_card_from(:customer, $1, $2, customers)
51
+ end
50
52
 
51
- card = { id: $2, deleted: true }
52
- customer[:cards][:data].reject!{|cc|
53
- cc[:id] == card[:id]
54
- }
55
- customer[:default_card] = customer[:cards][:data].count > 0 ? customer[:cards][:data].first[:id] : nil
56
- card
53
+ def delete_recipient_card(route, method_url, params, headers)
54
+ route =~ method_url
55
+ delete_card_from(:recipient, $1, $2, recipients)
57
56
  end
58
57
 
59
58
  def update_card(route, method_url, params, headers)
60
59
  route =~ method_url
61
- customer = assert_existance :customer, $1, customers[$1]
60
+ customer = assert_existence :customer, $1, customers[$1]
62
61
 
63
- card = assert_existance :card, $2, get_card(customer, $2)
62
+ card = assert_existence :card, $2, get_card(customer, $2)
64
63
  card.merge!(params)
65
64
  card
66
65
  end
67
66
 
68
- private
69
-
70
- def validate_card(card)
71
- [:exp_month, :exp_year].each do |field|
72
- card[field] = card[field].to_i
73
- end
74
- card
75
- end
76
-
77
- def card_from_params(attrs_or_token)
78
- if attrs_or_token.is_a? Hash
79
- attrs_or_token = generate_card_token(attrs_or_token)
80
- end
81
- card = get_card_by_token(attrs_or_token)
82
- validate_card(card)
83
- end
84
67
  end
85
68
  end
86
69
  end
@@ -32,7 +32,7 @@ module StripeMock
32
32
 
33
33
  def get_charges(route, method_url, params, headers)
34
34
  params[:offset] ||= 0
35
- params[:count] ||= 10
35
+ params[:limit] ||= 10
36
36
 
37
37
  clone = charges.clone
38
38
 
@@ -40,17 +40,17 @@ module StripeMock
40
40
  clone.delete_if { |k,v| v[:customer] != params[:customer] }
41
41
  end
42
42
 
43
- clone.values[params[:offset], params[:count]]
43
+ Data.mock_list_object(clone.values, params)
44
44
  end
45
45
 
46
46
  def get_charge(route, method_url, params, headers)
47
47
  route =~ method_url
48
- assert_existance :charge, $1, charges[$1]
48
+ assert_existence :charge, $1, charges[$1]
49
49
  end
50
50
 
51
51
  def capture_charge(route, method_url, params, headers)
52
52
  route =~ method_url
53
- charge = assert_existance :charge, $1, charges[$1]
53
+ charge = assert_existence :charge, $1, charges[$1]
54
54
 
55
55
  if params[:amount]
56
56
  refund = Data.mock_refund(
@@ -16,16 +16,16 @@ module StripeMock
16
16
 
17
17
  def get_coupon(route, method_url, params, headers)
18
18
  route =~ method_url
19
- assert_existance :coupon, $1, coupons[$1]
19
+ assert_existence :coupon, $1, coupons[$1]
20
20
  end
21
21
 
22
22
  def delete_coupon(route, method_url, params, headers)
23
23
  route =~ method_url
24
- assert_existance :coupon, $1, coupons.delete($1)
24
+ assert_existence :coupon, $1, coupons.delete($1)
25
25
  end
26
26
 
27
27
  def list_coupons(route, method_url, params, headers)
28
- coupons.values
28
+ Data.mock_list_object(coupons.values, params)
29
29
  end
30
30
 
31
31
  end
@@ -23,7 +23,7 @@ module StripeMock
23
23
 
24
24
  if params[:plan]
25
25
  plan_id = params[:plan].to_s
26
- plan = assert_existance :plan, plan_id, plans[plan_id]
26
+ plan = assert_existence :plan, plan_id, plans[plan_id]
27
27
 
28
28
  if params[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
29
29
  raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
@@ -41,7 +41,7 @@ module StripeMock
41
41
 
42
42
  def update_customer(route, method_url, params, headers)
43
43
  route =~ method_url
44
- cus = assert_existance :customer, $1, customers[$1]
44
+ cus = assert_existence :customer, $1, customers[$1]
45
45
  cus.merge!(params)
46
46
 
47
47
  if params[:card]
@@ -55,7 +55,7 @@ module StripeMock
55
55
 
56
56
  def delete_customer(route, method_url, params, headers)
57
57
  route =~ method_url
58
- assert_existance :customer, $1, customers[$1]
58
+ assert_existence :customer, $1, customers[$1]
59
59
 
60
60
  customers[$1] = {
61
61
  id: customers[$1][:id],
@@ -65,11 +65,11 @@ module StripeMock
65
65
 
66
66
  def get_customer(route, method_url, params, headers)
67
67
  route =~ method_url
68
- assert_existance :customer, $1, customers[$1]
68
+ assert_existence :customer, $1, customers[$1]
69
69
  end
70
70
 
71
71
  def list_customers(route, method_url, params, headers)
72
- customers.values
72
+ Data.mock_list_object(customers.values, params)
73
73
  end
74
74
 
75
75
  end
@@ -8,7 +8,7 @@ module StripeMock
8
8
 
9
9
  def retrieve_event(route, method_url, params, headers)
10
10
  route =~ method_url
11
- assert_existance :event, $1, events[$1]
11
+ assert_existence :event, $1, events[$1]
12
12
  end
13
13
 
14
14
  end
@@ -18,7 +18,7 @@ module StripeMock
18
18
  object[:cards][:data].delete_if {|card| card[:id] == object[:default_card]}
19
19
  object[:default_card] = card[:id]
20
20
  else
21
- object[:cards][:count] += 1
21
+ object[:cards][:total_count] += 1
22
22
  end
23
23
 
24
24
  object[:default_card] = card[:id] unless object[:default_card]
@@ -27,6 +27,41 @@ module StripeMock
27
27
  card
28
28
  end
29
29
 
30
+ def delete_card_from(type, type_id, card_id, objects)
31
+ resource = assert_existence type, type_id, objects[type_id]
32
+
33
+ assert_existence :card, card_id, get_card(resource, card_id)
34
+
35
+ card = { id: card_id, deleted: true }
36
+ resource[:cards][:data].reject!{|cc|
37
+ cc[:id] == card[:id]
38
+ }
39
+ resource[:default_card] = resource[:cards][:data].count > 0 ? resource[:cards][:data].first[:id] : nil
40
+ card
41
+ end
42
+
43
+ def add_card_to(type, type_id, params, objects)
44
+ resource = assert_existence type, type_id, objects[type_id]
45
+
46
+ card = card_from_params(params[:card])
47
+ add_card_to_object(type, card, resource)
48
+ end
49
+
50
+ def validate_card(card)
51
+ [:exp_month, :exp_year].each do |field|
52
+ card[field] = card[field].to_i
53
+ end
54
+ card
55
+ end
56
+
57
+ def card_from_params(attrs_or_token)
58
+ if attrs_or_token.is_a? Hash
59
+ attrs_or_token = generate_card_token(attrs_or_token)
60
+ end
61
+ card = get_card_by_token(attrs_or_token)
62
+ validate_card(card)
63
+ end
64
+
30
65
  end
31
66
  end
32
67
  end
@@ -17,13 +17,13 @@ module StripeMock
17
17
 
18
18
  def update_invoice_item(route, method_url, params, headers)
19
19
  route =~ method_url
20
- list_item = assert_existance :list_item, $1, invoice_items[$1]
20
+ list_item = assert_existence :list_item, $1, invoice_items[$1]
21
21
  list_item.merge!(params)
22
22
  end
23
23
 
24
24
  def delete_invoice_item(route, method_url, params, headers)
25
25
  route =~ method_url
26
- assert_existance :list_item, $1, invoice_items[$1]
26
+ assert_existence :list_item, $1, invoice_items[$1]
27
27
 
28
28
  invoice_items[$1] = {
29
29
  id: invoice_items[$1][:id],
@@ -32,12 +32,12 @@ module StripeMock
32
32
  end
33
33
 
34
34
  def list_invoice_items(route, method_url, params, headers)
35
- invoice_items.values
35
+ Data.mock_list_object(invoice_items.values, params)
36
36
  end
37
37
 
38
38
  def get_invoice_item(route, method_url, params, headers)
39
39
  route =~ method_url
40
- assert_existance :invoice_item, $1, invoice_items[$1]
40
+ assert_existence :invoice_item, $1, invoice_items[$1]
41
41
  end
42
42
 
43
43
  end