stripe-ruby-mock 1.10.1.1 → 1.10.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  In your gemfile:
10
10
 
11
- gem 'stripe-ruby-mock', '>= 1.10.1.1'
11
+ gem 'stripe-ruby-mock', '~> 1.10.2'
12
12
 
13
13
  ## Features
14
14
 
data/lib/stripe_mock.rb CHANGED
@@ -29,6 +29,10 @@ require 'stripe_mock/api/webhooks'
29
29
  require 'stripe_mock/api/strict'
30
30
  require 'stripe_mock/api/debug'
31
31
 
32
+ require 'stripe_mock/request_handlers/helpers/card_helpers.rb'
33
+ require 'stripe_mock/request_handlers/helpers/subscription_helpers.rb'
34
+ require 'stripe_mock/request_handlers/helpers/token_helpers.rb'
35
+
32
36
  require 'stripe_mock/request_handlers/charges.rb'
33
37
  require 'stripe_mock/request_handlers/cards.rb'
34
38
  require 'stripe_mock/request_handlers/customers.rb'
@@ -19,9 +19,9 @@ module StripeMock
19
19
  json.delete(:id)
20
20
 
21
21
  if @state == 'local'
22
- event_data = instance.generate_event(json)
22
+ event_data = instance.generate_webhook_event(json)
23
23
  elsif @state == 'remote'
24
- event_data = client.generate_event(json)
24
+ event_data = client.generate_webhook_event(json)
25
25
  else
26
26
  raise UnstartedStateError
27
27
  end
@@ -56,8 +56,8 @@ module StripeMock
56
56
  timeout_wrap { @pipe.generate_card_token(card_params) }
57
57
  end
58
58
 
59
- def generate_event(event_data)
60
- timeout_wrap { Stripe::Util.symbolize_names @pipe.generate_event(event_data) }
59
+ def generate_webhook_event(event_data)
60
+ timeout_wrap { Stripe::Util.symbolize_names @pipe.generate_webhook_event(event_data) }
61
61
  end
62
62
 
63
63
  def clear_server_data
@@ -146,6 +146,7 @@ module StripeMock
146
146
  #FIXME nested overrides would be better than hardcoding plan_id
147
147
  def self.mock_subscription(params={})
148
148
  StripeMock::Util.rmerge({
149
+ :current_period_start => 1308595038,
149
150
  :current_period_end => 1308681468,
150
151
  :status => "trialing",
151
152
  :plan => {
@@ -155,7 +156,6 @@ module StripeMock
155
156
  :object => "plan",
156
157
  :id => '__test_plan_id__'
157
158
  },
158
- :current_period_start => 1308595038,
159
159
  :cancel_at_period_end => false,
160
160
  :canceled_at => nil,
161
161
  :ended_at => nil,
@@ -168,43 +168,74 @@ module StripeMock
168
168
  }, params)
169
169
  end
170
170
 
171
- def self.mock_invoice(params={})
171
+ def self.mock_invoice(lines, params={})
172
+ in_id = params[:id] || "test_in_default"
173
+ lines << Data.mock_line_item() if lines.empty?
172
174
  {
173
- :id => 'in_test_invoice',
174
- :object => 'invoice',
175
- :livemode => false,
176
- :amount_due => 1000,
177
- :attempt_count => 0,
178
- :attempted => false,
179
- :closed => false,
180
- :currency => 'usd',
181
- :customer => 'c_test_customer',
182
- :date => 1349738950,
183
- :lines => {
184
- "invoiceitems" => [
185
- {
186
- :id => 'ii_test_invoice_item',
187
- :object => '',
188
- :livemode => false,
189
- :amount => 1000,
190
- :currency => 'usd',
191
- :customer => 'c_test_customer',
192
- :date => 1349738950,
193
- :description => "A Test Invoice Item",
194
- :invoice => 'in_test_invoice'
195
- },
196
- ],
175
+ id: 'in_test_invoice',
176
+ date: 1349738950,
177
+ period_end: 1349738950,
178
+ period_start: 1349738950,
179
+ lines: {
180
+ object: "list",
181
+ count: lines.count,
182
+ url: "/v1/invoices/#{in_id}/lines",
183
+ data: lines
184
+ },
185
+ subtotal: lines.map {|line| line[:amount]}.reduce(0, :+),
186
+ total: lines.map {|line| line[:amount]}.reduce(0, :+),
187
+ customer: "test_customer",
188
+ object: 'invoice',
189
+ attempted: false,
190
+ closed: false,
191
+ paid: false,
192
+ livemode: false,
193
+ attempt_count: 0,
194
+ amount_due: lines.map {|line| line[:amount]}.reduce(0, :+),
195
+ currency: 'usd',
196
+ starting_balance: 0,
197
+ ending_balance: nil,
198
+ next_payment_attempt: 1349825350,
199
+ charge: nil,
200
+ discount: nil,
201
+ subscription: nil
202
+ }.merge(params)
203
+ end
204
+
205
+ def self.mock_line_item(params = {})
206
+ {
207
+ id: "ii_test",
208
+ object: "line_item",
209
+ type: "invoiceitem",
210
+ livemode: false,
211
+ amount: 1000,
212
+ currency: "usd",
213
+ proration: false,
214
+ period: {
215
+ start: 1349738920,
216
+ end: 1349738920
197
217
  },
198
- :paid => false,
199
- :period_end => 1349738950,
200
- :period_start => 1349738950,
201
- :starting_balance => 0,
202
- :subtotal => 1000,
203
- :total => 1000,
204
- :charge => nil,
205
- :discount => nil,
206
- :ending_balance => nil,
207
- :next_payemnt_attempt => 1349825350,
218
+ quantity: nil,
219
+ plan: nil,
220
+ description: "Test invoice item",
221
+ metadata: {}
222
+ }.merge(params)
223
+ end
224
+
225
+ def self.mock_invoice_item(params = {})
226
+ {
227
+ id: "ii_test",
228
+ object: "invoiceitem",
229
+ date: 1349738920,
230
+ amount: 1099,
231
+ livemode: false,
232
+ proration: false,
233
+ currency: "usd",
234
+ customer: "cus_test",
235
+ description: "invoice item desc",
236
+ metadata: {},
237
+ invoice: nil,
238
+ subscription: nil
208
239
  }.merge(params)
209
240
  end
210
241
 
@@ -1,6 +1,8 @@
1
1
  module StripeMock
2
2
  class Instance
3
3
 
4
+ include StripeMock::RequestHandlers::Helpers
5
+
4
6
  # Handlers are ordered by priority
5
7
  @@handlers = []
6
8
 
@@ -80,79 +82,11 @@ module StripeMock
80
82
  end
81
83
  end
82
84
 
83
- def generate_bank_token(bank_params)
84
- token = new_id 'btok'
85
- @bank_tokens[token] = Data.mock_bank_account bank_params
86
- token
87
- end
88
-
89
- def generate_card_token(card_params)
90
- token = new_id 'tok'
91
- card_params[:id] = new_id 'cc'
92
- @card_tokens[token] = Data.mock_card symbolize_names(card_params)
93
- token
94
- end
95
-
96
- def generate_event(event_data)
85
+ def generate_webhook_event(event_data)
97
86
  event_data[:id] ||= new_id 'evt'
98
87
  @events[ event_data[:id] ] = symbolize_names(event_data)
99
88
  end
100
89
 
101
- def get_bank_by_token(token)
102
- if token.nil? || @bank_tokens[token].nil?
103
- Data.mock_bank_account
104
- else
105
- @bank_tokens.delete(token)
106
- end
107
- end
108
-
109
- def get_card_by_token(token)
110
- if token.nil? || @card_tokens[token].nil?
111
- Data.mock_card :id => new_id('cc')
112
- else
113
- @card_tokens.delete(token)
114
- end
115
- end
116
-
117
- def get_customer_card(customer, token)
118
- customer[:cards][:data].find{|cc| cc[:id] == token }
119
- end
120
-
121
- def add_card_to_customer(card, cus)
122
- card[:customer] = cus[:id]
123
-
124
- if cus[:cards][:count] == 0
125
- cus[:cards][:count] += 1
126
- else
127
- cus[:cards][:data].delete_if {|card| card[:id] == cus[:default_card]}
128
- end
129
-
130
- cus[:cards][:data] << card
131
-
132
- card
133
- end
134
-
135
- def get_customer_subscription(customer, sub_id)
136
- customer[:subscriptions][:data].find{|sub| sub[:id] == sub_id }
137
- end
138
-
139
- def add_subscription_to_customer(plan, cus)
140
- params = { id: new_id('su'), plan: plan, customer: cus[:id] }
141
-
142
- if plan[:trial_period_days].nil?
143
- params.merge!({status: 'active', trial_start: nil, trial_end: nil})
144
- else
145
- params.merge!({status: 'trialing', trial_start: Time.now.to_i, trial_end: (Time.now + plan[:trial_period_days]).to_i })
146
- end
147
-
148
- subscription = Data.mock_subscription params
149
-
150
- cus[:subscriptions] = Data.mock_subscriptions_array(url: "/v1/customers/#{cus[:id]}/subscriptions") unless cus[:subscriptions]
151
- cus[:subscriptions][:count] = (cus[:subscriptions][:count] ? cus[:subscriptions][:count]+1 : 1 )
152
- cus[:subscriptions][:data] << subscription
153
- subscription
154
- end
155
-
156
90
  private
157
91
 
158
92
  def assert_existance(type, id, obj, message=nil)
@@ -0,0 +1,25 @@
1
+ module StripeMock
2
+ module RequestHandlers
3
+ module Helpers
4
+
5
+ def get_customer_card(customer, token)
6
+ customer[:cards][:data].find{|cc| cc[:id] == token }
7
+ end
8
+
9
+ def add_card_to_customer(card, cus)
10
+ card[:customer] = cus[:id]
11
+
12
+ if cus[:cards][:count] == 0
13
+ cus[:cards][:count] += 1
14
+ else
15
+ cus[:cards][:data].delete_if {|card| card[:id] == cus[:default_card]}
16
+ end
17
+
18
+ cus[:cards][:data] << card
19
+
20
+ card
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,44 @@
1
+ module StripeMock
2
+ module RequestHandlers
3
+ module Helpers
4
+
5
+ def get_customer_subscription(customer, sub_id)
6
+ customer[:subscriptions][:data].find{|sub| sub[:id] == sub_id }
7
+ end
8
+
9
+ def add_subscription_to_customer(plan, cus, start_time = nil)
10
+ start_time ||= Time.now.utc.to_i
11
+ params = { id: new_id('su'), plan: plan, customer: cus[:id], current_period_start: start_time, current_period_end: get_ending_time(start_time, plan) }
12
+
13
+ if plan[:trial_period_days].nil?
14
+ params.merge!({status: 'active', trial_start: nil, trial_end: nil})
15
+ else
16
+ params.merge!({status: 'trialing', trial_start: Time.now.utc.to_i, trial_end: (Time.now.utc.to_i + plan[:trial_period_days]*86400) })
17
+ end
18
+
19
+ subscription = Data.mock_subscription params
20
+
21
+ cus[:subscriptions] = Data.mock_subscriptions_array(url: "/v1/customers/#{cus[:id]}/subscriptions") unless cus[:subscriptions]
22
+ cus[:subscriptions][:count] = (cus[:subscriptions][:count] ? cus[:subscriptions][:count]+1 : 1 )
23
+ cus[:subscriptions][:data] << subscription
24
+ subscription
25
+ end
26
+
27
+ # intervals variable is set to 1 when calculating current_period_end from current_period_start & plan
28
+ # intervals variable is set to 2 when calculating Stripe::Invoice.upcoming end from current_period_start & plan
29
+ def get_ending_time(start_time, plan, intervals = 1)
30
+ case plan[:interval]
31
+ when "week"
32
+ start_time + (604800 * (plan[:interval_count] || 1) * intervals)
33
+ when "month"
34
+ (Time.at(start_time).to_datetime >> ((plan[:interval_count] || 1) * intervals)).to_time.to_i
35
+ when "year"
36
+ (Time.at(start_time).to_datetime >> (12 * intervals)).to_time.to_i # max period is 1 year
37
+ else
38
+ start_time
39
+ end
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ module StripeMock
2
+ module RequestHandlers
3
+ module Helpers
4
+
5
+ def generate_bank_token(bank_params)
6
+ token = new_id 'btok'
7
+ @bank_tokens[token] = Data.mock_bank_account bank_params
8
+ token
9
+ end
10
+
11
+ def generate_card_token(card_params)
12
+ token = new_id 'tok'
13
+ card_params[:id] = new_id 'cc'
14
+ @card_tokens[token] = Data.mock_card symbolize_names(card_params)
15
+ token
16
+ end
17
+
18
+ def get_bank_by_token(token)
19
+ if token.nil? || @bank_tokens[token].nil?
20
+ Data.mock_bank_account
21
+ else
22
+ @bank_tokens.delete(token)
23
+ end
24
+ end
25
+
26
+ def get_card_by_token(token)
27
+ if token.nil? || @card_tokens[token].nil?
28
+ Data.mock_card :id => new_id('cc')
29
+ else
30
+ @card_tokens.delete(token)
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -7,7 +7,7 @@ module StripeMock
7
7
  end
8
8
 
9
9
  def new_invoice_item(route, method_url, params, headers)
10
- Data.mock_invoice(params)
10
+ Data.mock_invoice_item(params)
11
11
  end
12
12
 
13
13
  end
@@ -4,6 +4,7 @@ module StripeMock
4
4
 
5
5
  def Invoices.included(klass)
6
6
  klass.add_handler 'post /v1/invoices', :new_invoice
7
+ klass.add_handler 'get /v1/invoices/upcoming', :upcoming_invoice
7
8
  klass.add_handler 'get /v1/invoices/(.*)', :get_invoice
8
9
  klass.add_handler 'get /v1/invoices', :list_invoices
9
10
  klass.add_handler 'post /v1/invoices/(.*)/pay', :pay_invoice
@@ -11,7 +12,8 @@ module StripeMock
11
12
 
12
13
  def new_invoice(route, method_url, params, headers)
13
14
  id = new_id('in')
14
- invoices[id] = Data.mock_invoice(params.merge :id => id)
15
+ invoice_item = Data.mock_line_item()
16
+ invoices[id] = Data.mock_invoice([invoice_item], params.merge(:id => id))
15
17
  end
16
18
 
17
19
  def list_invoices(route, method_url, params, headers)
@@ -30,16 +32,51 @@ module StripeMock
30
32
  def get_invoice(route, method_url, params, headers)
31
33
  route =~ method_url
32
34
  assert_existance :invoice, $1, invoices[$1]
33
- invoices[$1] ||= Data.mock_invoice(:id => $1)
35
+ invoices[$1] ||= Data.mock_invoice([], :id => $1)
34
36
  end
35
-
37
+
36
38
  def pay_invoice(route, method_url, params, headers)
37
39
  route =~ method_url
38
40
  assert_existance :invoice, $1, invoices[$1]
39
- invoices[$1] ||= Data.mock_invoice(:id => $1)
41
+ invoices[$1] ||= Data.mock_invoice([], :id => $1)
40
42
  invoices[$1].merge!(:paid => true, :attempted => true, :charge => 'ch_1fD6uiR9FAA2zc')
41
43
  end
42
44
 
45
+ def upcoming_invoice(route, method_url, params, headers)
46
+ route =~ method_url
47
+ raise Stripe::InvalidRequestError.new('Missing required param: customer', nil, 400) if params[:customer].nil?
48
+
49
+ customer = customers[params[:customer]]
50
+ assert_existance :customer, params[:customer], customer
51
+ customer ||= Data.mock_customer([], :id => params[:customer])
52
+
53
+ raise Stripe::InvalidRequestError.new("No upcoming invoices for customer: #{customer[:id]}", nil, 404) if customer[:subscriptions][:data].length == 0
54
+
55
+ most_recent = customer[:subscriptions][:data].min_by { |sub| sub[:current_period_end] }
56
+ invoice_item = get_mock_subscription_line_item(most_recent)
57
+
58
+ Data.mock_invoice([invoice_item],
59
+ subscription: most_recent[:id],
60
+ period_start: most_recent[:current_period_start],
61
+ period_end: most_recent[:current_period_end],
62
+ next_payment_attempt: most_recent[:current_period_end] + 3600 )
63
+ end
64
+
65
+ private
66
+
67
+ def get_mock_subscription_line_item(subscription)
68
+ Data.mock_line_item(
69
+ id: subscription[:id],
70
+ type: "subscription",
71
+ plan: subscription[:plan],
72
+ amount: subscription[:plan][:amount],
73
+ quantity: 1,
74
+ period: {
75
+ start: subscription[:current_period_end],
76
+ end: get_ending_time(subscription[:current_period_start], subscription[:plan], 2)
77
+ })
78
+ end
79
+
43
80
  end
44
81
  end
45
82
  end
@@ -22,7 +22,7 @@ module StripeMock
22
22
  # Ensure customer has card to charge if plan has no trial and is not free
23
23
  verify_card_present(customer, plan)
24
24
 
25
- subscription = add_subscription_to_customer(plan, customer)
25
+ subscription = add_subscription_to_customer(plan, customer, params[:current_period_start])
26
26
 
27
27
  # oddly, subscription returned from 'create_subscription' does not expand plan
28
28
  subscription.merge(plan: params[:plan])
@@ -95,13 +95,13 @@ module StripeMock
95
95
  subscription = get_customer_subscription(customer, $2)
96
96
  assert_existance :subscription, $2, subscription
97
97
 
98
- cancel_params = { canceled_at: Time.now.to_i }
98
+ cancel_params = { canceled_at: Time.now.utc.to_i }
99
99
  if params[:at_period_end] == true
100
100
  cancel_params[:cancel_at_period_end] = true
101
101
  else
102
102
  cancel_params[:status] = "canceled"
103
103
  cancel_params[:cancel_at_period_end] = false
104
- cancel_params[:ended_at] = Time.now.to_i
104
+ cancel_params[:ended_at] = Time.now.utc.to_i
105
105
  end
106
106
 
107
107
  subscription.merge!(cancel_params)
@@ -55,8 +55,8 @@ module StripeMock
55
55
  @instance.generate_bank_token(recipient_params)
56
56
  end
57
57
 
58
- def generate_event(event_data)
59
- @instance.generate_event(event_data)
58
+ def generate_webhook_event(event_data)
59
+ @instance.generate_webhook_event(event_data)
60
60
  end
61
61
 
62
62
  def debug?; @instance.debug; end
@@ -1,4 +1,4 @@
1
1
  module StripeMock
2
2
  # stripe-ruby-mock version
3
- VERSION = "1.10.1.1"
3
+ VERSION = "1.10.1.2"
4
4
  end
@@ -56,15 +56,157 @@ shared_examples 'Invoice API' do
56
56
  @invoice = Stripe::Invoice.create
57
57
  @invoice.pay
58
58
  end
59
-
59
+
60
60
  it 'updates attempted and paid flags' do
61
61
  expect(@invoice.attempted).to be_true
62
62
  expect(@invoice.paid).to be_true
63
63
  end
64
-
64
+
65
65
  it 'sets the charge attribute' do
66
66
  expect(@invoice.charge).to be_a String
67
- expect(@invoice.charge.length).to be > 0
67
+ expect(@invoice.charge.length).to be > 0
68
+ end
69
+ end
70
+
71
+ context "retrieving upcoming invoice" do
72
+ before do
73
+ @customer = Stripe::Customer.create(email: 'johnny@appleseed.com', card: 'some_card_token')
74
+ end
75
+
76
+ it 'fails without parameters' do
77
+ expect { Stripe::Invoice.upcoming() }.to raise_error {|e|
78
+ expect(e).to be_a(ArgumentError) }
79
+ end
80
+
81
+ it 'fails without a valid customer in strict mode' do
82
+ expect { Stripe::Invoice.upcoming(customer: 'whatever') }.to raise_error {|e|
83
+ expect(e).to be_a(Stripe::InvalidRequestError)
84
+ expect(e.message).to eq('No such customer: whatever') }
85
+ end
86
+
87
+ it 'fails without a customer parameter' do
88
+ expect { Stripe::Invoice.upcoming(gazebo: 'raindance') }.to raise_error {|e|
89
+ expect(e).to be_a(Stripe::InvalidRequestError)
90
+ expect(e.http_status).to eq(400)
91
+ expect(e.message).to eq('Missing required param: customer') }
92
+ end
93
+
94
+ it 'fails without a subscription' do
95
+ expect { Stripe::Invoice.upcoming(customer: @customer.id) }.to raise_error {|e|
96
+ expect(e).to be_a(Stripe::InvalidRequestError)
97
+ expect(e.http_status).to eq(404)
98
+ expect(e.message).to eq("No upcoming invoices for customer: #{@customer.id}") }
99
+ end
100
+
101
+ it 'works when customer has a subscription' do
102
+ @plan = Stripe::Plan.create()
103
+ @subscription = @customer.subscriptions.create(plan: @plan.id)
104
+ @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
105
+
106
+ expect(@upcoming).to be_a Stripe::Invoice
107
+ expect(@upcoming.total).to eq(@upcoming.lines.data[0].amount)
108
+ expect(@upcoming.period_end).to eq(@upcoming.lines.data[0].period.start)
109
+ expect(Time.at(@upcoming.period_start).to_datetime >> 1).to eq(Time.at(@upcoming.period_end).to_datetime) # +1 month
110
+ expect(Time.at(@upcoming.period_end).to_datetime >> 1).to eq(Time.at(@upcoming.lines.data[0].period.end).to_datetime) # +1 month
111
+ expect(@upcoming.next_payment_attempt).to eq(@upcoming.period_end + 3600) # +1 hour
112
+ expect(@upcoming.subscription).to eq(@subscription.id)
113
+ end
114
+
115
+ it 'sets the start and end of billing periods correctly when plan has an interval_count' do
116
+ @oddplan = Stripe::Plan.create(interval: "week", interval_count: 11)
117
+ @subscription = @customer.subscriptions.create(plan: @oddplan.id)
118
+ @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
119
+
120
+ expect(@upcoming.period_start + 6652800).to eq(@upcoming.period_end) # 6652800 = +11 weeks
121
+ expect(@upcoming.period_end).to eq(@upcoming.lines.data[0].period.start)
122
+ expect(@upcoming.period_end + 6652800).to eq(@upcoming.lines.data[0].period.end) # 6652800 = +11 weeks
123
+ expect(@upcoming.next_payment_attempt).to eq(@upcoming.period_end + 3600) # +1 hour
124
+ end
125
+
126
+ it 'chooses the most recent of multiple subscriptions' do
127
+ @shortplan = Stripe::Plan.create(interval: "week") # 1 week sub
128
+ @plainplan = Stripe::Plan.create() # 1 month sub
129
+ @longplan = Stripe::Plan.create(interval: "year") # 1 year sub
130
+
131
+ @plainsub = @customer.subscriptions.create(plan: @plainplan.id)
132
+ @shortsub = @customer.subscriptions.create(plan: @shortplan.id)
133
+ @longsub = @customer.subscriptions.create(plan: @longplan.id)
134
+
135
+ @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
136
+
137
+ expect(@upcoming.period_start + 604800).to eq(@upcoming.period_end) # 604800 = 1 week
138
+ expect(@upcoming.period_end + 604800).to eq(@upcoming.lines.data[0].period.end) # 604800 = 1 week
139
+ expect(@upcoming.subscription).to eq(@shortsub.id)
68
140
  end
141
+
142
+ context 'calculates month and year offsets correctly' do
143
+
144
+ it 'for one month plan on the 1st' do
145
+ @plan = Stripe::Plan.create()
146
+ @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2014,1,1,12).to_i)
147
+ @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
148
+
149
+ expect(Time.at(@upcoming.period_start)).to eq(Time.utc(2014,1,1,12))
150
+ expect(Time.at(@upcoming.period_end)).to eq(Time.utc(2014,2,1,12))
151
+ expect(Time.at(@upcoming.lines.data[0].period.start)).to eq(Time.utc(2014,2,1,12))
152
+ expect(Time.at(@upcoming.lines.data[0].period.end)).to eq(Time.utc(2014,3,1,12))
153
+ end
154
+
155
+ it 'for one year plan on the 1st' do
156
+ @plan = Stripe::Plan.create(interval: "year")
157
+ @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2012,1,1,12).to_i)
158
+ @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
159
+
160
+ expect(Time.at(@upcoming.period_start)).to eq(Time.utc(2012,1,1,12))
161
+ expect(Time.at(@upcoming.period_end)).to eq(Time.utc(2013,1,1,12))
162
+ expect(Time.at(@upcoming.lines.data[0].period.start)).to eq(Time.utc(2013,1,1,12))
163
+ expect(Time.at(@upcoming.lines.data[0].period.end)).to eq(Time.utc(2014,1,1,12))
164
+ end
165
+
166
+ it 'for one month plan on the 31st' do
167
+ @plan = Stripe::Plan.create()
168
+ @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2014,1,31,12).to_i)
169
+ @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
170
+
171
+ expect(Time.at(@upcoming.period_start)).to eq(Time.utc(2014,1,31,12))
172
+ expect(Time.at(@upcoming.period_end)).to eq(Time.utc(2014,2,28,12))
173
+ expect(Time.at(@upcoming.lines.data[0].period.start)).to eq(Time.utc(2014,2,28,12))
174
+ expect(Time.at(@upcoming.lines.data[0].period.end)).to eq(Time.utc(2014,3,31,12))
175
+ end
176
+
177
+ it 'for one year plan on feb. 29th' do
178
+ @plan = Stripe::Plan.create(interval: "year")
179
+ @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2012,2,29,12).to_i)
180
+ @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
181
+
182
+ expect(Time.at(@upcoming.period_start)).to eq(Time.utc(2012,2,29,12))
183
+ expect(Time.at(@upcoming.period_end)).to eq(Time.utc(2013,2,28,12))
184
+ expect(Time.at(@upcoming.lines.data[0].period.start)).to eq(Time.utc(2013,2,28,12))
185
+ expect(Time.at(@upcoming.lines.data[0].period.end)).to eq(Time.utc(2014,2,28,12))
186
+ end
187
+
188
+ it 'for two month plan on dec. 31st' do
189
+ @plan = Stripe::Plan.create(interval_count: 2)
190
+ @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2013,12,31,12).to_i)
191
+ @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
192
+
193
+ expect(Time.at(@upcoming.period_start)).to eq(Time.utc(2013,12,31,12))
194
+ expect(Time.at(@upcoming.period_end)).to eq(Time.utc(2014, 2,28,12))
195
+ expect(Time.at(@upcoming.lines.data[0].period.start)).to eq(Time.utc(2014, 2,28,12))
196
+ expect(Time.at(@upcoming.lines.data[0].period.end)).to eq(Time.utc(2014, 4,30,12))
197
+ end
198
+
199
+ it 'for three month plan on nov. 30th' do
200
+ @plan = Stripe::Plan.create(interval_count: 3)
201
+ @sub = @customer.subscriptions.create(plan: @plan.id, current_period_start: Time.utc(2013,11,30,12).to_i)
202
+ @upcoming = Stripe::Invoice.upcoming(customer: @customer.id)
203
+
204
+ expect(Time.at(@upcoming.period_start)).to eq(Time.utc(2013,11,30,12))
205
+ expect(Time.at(@upcoming.period_end)).to eq(Time.utc(2014, 2,28,12))
206
+ expect(Time.at(@upcoming.lines.data[0].period.start)).to eq(Time.utc(2014, 2,28,12))
207
+ expect(Time.at(@upcoming.lines.data[0].period.end)).to eq(Time.utc(2014, 5,30,12))
208
+ end
209
+ end
210
+
69
211
  end
70
212
  end
@@ -84,6 +84,7 @@ shared_examples 'Customer Subscriptions' do
84
84
 
85
85
  expect(sub.object).to eq('subscription')
86
86
  expect(sub.plan).to eq('trial')
87
+ expect(sub.trial_end - sub.trial_start).to eq(14 * 86400)
87
88
 
88
89
  customer = Stripe::Customer.retrieve('cardless')
89
90
  expect(customer.subscriptions.data).to_not be_empty
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe-ruby-mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.1.1
4
+ version: 1.10.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-24 00:00:00.000000000 Z
12
+ date: 2014-03-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stripe
@@ -148,6 +148,9 @@ files:
148
148
  - lib/stripe_mock/request_handlers/coupons.rb
149
149
  - lib/stripe_mock/request_handlers/customers.rb
150
150
  - lib/stripe_mock/request_handlers/events.rb
151
+ - lib/stripe_mock/request_handlers/helpers/card_helpers.rb
152
+ - lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb
153
+ - lib/stripe_mock/request_handlers/helpers/token_helpers.rb
151
154
  - lib/stripe_mock/request_handlers/invoice_items.rb
152
155
  - lib/stripe_mock/request_handlers/invoices.rb
153
156
  - lib/stripe_mock/request_handlers/plans.rb