summaryrefslogtreecommitdiff
path: root/spec/models/solidus_subscriptions/subscription_spec.rb
blob: 7831a761c37cd49dfdbe5200f5639584a95ed9c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
require 'spec_helper'

RSpec.describe SolidusSubscriptions::Subscription, type: :model do
  it { is_expected.to have_many :installments }
  it { is_expected.to belong_to :user }
  it { is_expected.to belong_to :store }
  it { is_expected.to have_many :line_items }
  it { is_expected.to belong_to :shipping_address }

  it { is_expected.to validate_presence_of :user }
  it { is_expected.to validate_presence_of :skip_count }
  it { is_expected.to validate_presence_of :successive_skip_count }
  it { is_expected.to validate_numericality_of(:skip_count).is_greater_than_or_equal_to(0) }
  it { is_expected.to validate_numericality_of(:successive_skip_count).is_greater_than_or_equal_to(0) }

  it { is_expected.to accept_nested_attributes_for :line_items }

  describe '#cancel' do
    subject { subscription.cancel }

    let(:subscription) do
      create :subscription, :with_line_item, actionable_date: actionable_date
    end

    around { |e| Timecop.freeze { e.run } }

    context 'the subscription can be canceled' do
      let(:actionable_date) { 1.month.from_now }

      it 'is canceled' do
        subject
        expect(subscription.canceled?).to be_truthy
      end
    end

    context 'the subscription cannot be canceled' do
      let(:actionable_date) { Date.current }

      it 'is pending cancelation' do
        subject
        expect(subscription.pending_cancellation?).to be_truthy
      end
    end
  end

  describe '#skip' do
    subject { subscription.skip&.to_date }

    let(:total_skips) { 0 }
    let(:successive_skips) { 0 }
    let(:expected_date) { 1.month.from_now.to_date }

    let(:subscription) do
      create(
        :subscription,
        :with_line_item,
        skip_count: total_skips,
        successive_skip_count: successive_skips
      )
    end

    around do |e|
      successive_skip_limit = SolidusSubscriptions::Config.maximum_successive_skips
      total_skip_limit = SolidusSubscriptions::Config.maximum_total_skips

      SolidusSubscriptions::Config.maximum_successive_skips = 1
      SolidusSubscriptions::Config.maximum_total_skips = 1

      Timecop.freeze { e.run }

      SolidusSubscriptions::Config.maximum_successive_skips = successive_skip_limit
      SolidusSubscriptions::Config.maximum_total_skips = total_skip_limit
    end

    context 'when the successive skips have been exceeded' do
      let(:successive_skips) { 1 }
      it { is_expected.to be_falsy }

      it 'adds errors to the subscription' do
        subject
        expect(subscription.errors[:successive_skip_count]).to_not be_empty
      end
    end

    context 'when the total skips have been exceeded' do
      let(:total_skips) { 1 }
      it { is_expected.to be_falsy }

      it 'adds errors to the subscription' do
        subject
        expect(subscription.errors[:skip_count]).to_not be_empty
      end
    end

    context 'when the subscription can be skipped' do
      it { is_expected.to eq expected_date }
    end
  end

  describe '#deactivate' do
    subject { subscription.deactivate }

    let(:attributes) { {} }
    let(:subscription) do
      create :subscription, :actionable, :with_line_item, attributes do |s|
        s.installments = build_list(:installment, 2)
      end
    end

    context 'the subscription can be deactivated' do
      let(:attributes) do
        { end_date: Date.current.ago(2.days) }
      end

      it 'is inactive' do
        subject
        expect(subscription.inactive?).to be_truthy
      end
    end

    context 'the subscription cannot be deactivated' do
      it { is_expected.to be_falsy }
    end
  end

  describe '#next_actionable_date' do
    subject { subscription.next_actionable_date }

    context "when the subscription is active" do
      let(:expected_date) { Date.current + subscription.interval }
      let(:subscription) do
        build_stubbed(
          :subscription,
          :with_line_item,
          actionable_date: Date.current
        )
      end

      it { is_expected.to eq expected_date }
    end

    context "when the subscription is not active" do
      let(:subscription) { build_stubbed :subscription, :with_line_item, state: :canceled }
      it { is_expected.to be_nil }
    end
  end

  describe '#advance_actionable_date' do
    subject { subscription.advance_actionable_date }

    let(:expected_date) { Date.current + subscription.interval }
    let(:subscription) do
      build(
        :subscription,
        :with_line_item,
        actionable_date: Date.current
      )
    end

    it { is_expected.to eq expected_date }

    it 'updates the subscription with the new actionable date' do
      subject
      expect(subscription.reload).to have_attributes(
        actionable_date: expected_date
      )
    end
  end

  describe ".actionable" do
    let!(:past_subscription) { create :subscription, actionable_date: 2.days.ago }
    let!(:future_subscription) { create :subscription, actionable_date: 1.month.from_now }
    let!(:inactive_subscription) { create :subscription, state: "inactive", actionable_date: 7.days.ago }
    let!(:canceled_subscription) { create :subscription, state: "canceled", actionable_date: 4.days.ago }

    subject { described_class.actionable }

    it "returns subscriptions that have an actionable date in the past" do
      expect(subject).to include past_subscription
    end

    it "does not include future subscriptions" do
      expect(subject).to_not include future_subscription
    end

    it "does not include inactive subscriptions" do
      expect(subject).to_not include inactive_subscription
    end

    it "does not include canceled subscriptions" do
      expect(subject).to_not include canceled_subscription
    end
  end

  describe '#line_item_builder' do
    subject { subscription.line_item_builder }

    let(:subscription) { create :subscription, :with_line_item }
    let(:line_items) { subscription.line_items }

    it { is_expected.to be_a SolidusSubscriptions::LineItemBuilder }
    it { is_expected.to have_attributes(subscription_line_items: line_items) }
  end

  describe '#processing_state' do
    subject { subscription.processing_state }

    context 'when the subscription has never been processed' do
      let(:subscription) { build_stubbed :subscription }
      it { is_expected.to eq 'pending' }
    end

    context 'when the last processing attempt failed' do
      let(:subscription) do
        create(
          :subscription,
          installments: create_list(:installment, 1, :failed)
        )
      end

      it { is_expected.to eq 'failed' }
    end

    context 'when the last processing attempt succeeded' do
      let(:order) { create :completed_order_with_totals }

      let(:subscription) do
        create(
          :subscription,
          installments: create_list(
            :installment,
            1,
            :success,
            details: build_list(:installment_detail, 1, order: order, success: true)
          )
        )
      end

      it { is_expected.to eq 'success' }
    end
  end

  describe '.ransackable_scopes' do
    subject { described_class.ransackable_scopes }
    it { is_expected.to match_array [:in_processing_state] }
  end

  describe '.in_processing_state' do
    subject { described_class.in_processing_state(state) }

    let!(:new_subs) { create_list :subscription, 2 }
    let!(:failed_subs) { create_list(:installment, 2, :failed).map(&:subscription) }
    let!(:success_subs) { create_list(:installment, 2, :success).map(&:subscription) }

    context 'successfull subscriptions' do
      let(:state) { :success }
      it { is_expected.to match_array success_subs }
    end

    context 'failed subscriptions' do
      let(:state) { :failed }
      it { is_expected.to match_array failed_subs }
    end

    context 'new subscriptions' do
      let(:state) { :pending }
      it { is_expected.to match_array new_subs }
    end

    context 'unknown state' do
      let(:state) { :foo }

      it 'raises an error' do
        expect { subject }.to raise_error ArgumentError, /state must be one of/
      end
    end
  end

  describe '.processing_states' do
    subject { described_class.processing_states }
    it { is_expected.to match_array [:pending, :success, :failed] }
  end

  describe '#payment_source_to_use' do
    context 'when the subscription has a payment method without source' do
      it 'returns nil' do
        payment_method = create(:check_payment_method)

        subscription = create(:subscription, payment_method: payment_method)

        expect(subscription.payment_source_to_use).to eq(nil)
      end
    end

    context 'when the subscription has a payment method with a source' do
      it 'returns the source on the subscription' do
        user = create(:user)
        payment_method = create(:credit_card_payment_method)
        payment_source = create(:credit_card,
          payment_method: payment_method,
          gateway_customer_profile_id: 'BGS-123',
          user: user,
        )

        subscription = create(:subscription,
          user: user,
          payment_method: payment_method,
          payment_source: payment_source,
        )

        expect(subscription.payment_source_to_use).to eq(payment_source)
      end
    end

    context 'when the subscription has no payment method' do
      it "returns the default source from the user's wallet" do
        user = create(:user)
        payment_source = create(:credit_card, gateway_customer_profile_id: 'BGS-123', user: user)
        wallet_payment_source = user.wallet.add(payment_source)
        user.wallet.default_wallet_payment_source = wallet_payment_source

        subscription = create(:subscription, user: user)

        expect(subscription.payment_source_to_use).to eq(payment_source)
      end
    end
  end

  describe '#payment_method_to_use' do
    context 'when the subscription has a payment method without source' do
      it 'returns the payment method on the subscription' do
        payment_method = create(:check_payment_method)
        subscription = create(:subscription, payment_method: payment_method)

        expect(subscription.payment_method_to_use).to eq(payment_method)
      end
    end

    context 'when the subscription has a payment method with a source' do
      it 'returns the payment method on the subscription' do
        user = create(:user)
        payment_method = create(:credit_card_payment_method)
        payment_source = create(:credit_card,
          payment_method: payment_method,
          gateway_customer_profile_id: 'BGS-123',
          user: user,
        )

        subscription = create(:subscription,
          user: user,
          payment_method: payment_method,
          payment_source: payment_source,
        )

        expect(subscription.payment_method_to_use).to eq(payment_method)
      end
    end

    context 'when the subscription has no payment method' do
      it "returns the method from the default source in the user's wallet" do
        user = create(:user)
        payment_source = create(:credit_card, gateway_customer_profile_id: 'BGS-123', user: user)
        wallet_payment_source = user.wallet.add(payment_source)
        user.wallet.default_wallet_payment_source = wallet_payment_source

        subscription = create(:subscription, user: user)

        expect(subscription.payment_method_to_use).to eq(payment_source.payment_method)
      end
    end
  end

  describe '#billing_address_to_use' do
    context 'when the subscription has a billing address' do
      it 'returns the billing address on the subscription' do
        billing_address = create(:bill_address)

        subscription = create(:subscription, billing_address: billing_address)

        expect(subscription.billing_address_to_use).to eq(billing_address)
      end
    end

    context 'when the subscription has no billing address' do
      it 'returns the billing address on the user' do
        user = create(:user)
        billing_address = create(:bill_address)
        user.bill_address = billing_address

        subscription = create(:subscription, user: user)

        expect(subscription.billing_address_to_use).to eq(billing_address)
      end
    end
  end

  describe '#shipping_address_to_use' do
    context 'when the subscription has a shipping address' do
      it 'returns the shipping address on the subscription' do
        shipping_address = create(:ship_address)

        subscription = create(:subscription, shipping_address: shipping_address)

        expect(subscription.shipping_address_to_use).to eq(shipping_address)
      end
    end

    context 'when the subscription has no shipping address' do
      it 'returns the shipping address on the user' do
        user = create(:user)
        shipping_address = create(:ship_address)
        user.ship_address = shipping_address

        subscription = create(:subscription, user: user)

        expect(subscription.shipping_address_to_use).to eq(shipping_address)
      end
    end
  end

  describe "#update_actionable_date_if_interval_changed" do
    context "with installments" do
      context "when the last installment date would cause the interval to be in the past" do
        it "sets the actionable_date to the current day" do
          subscription = create(:subscription, actionable_date: Time.zone.parse('2016-08-22'))
          create(:installment, subscription: subscription, created_at: Time.zone.parse('2016-07-22'))

          subscription.update!(interval_length: 1, interval_units: 'month')

          expect(subscription.actionable_date.to_date).to eq(Time.zone.today)
        end
      end

      context "when the last installment date would cause the interval to be in the future" do
        it "sets the actionable_date to an interval from the last installment" do
          subscription = create(:subscription, actionable_date: Time.zone.parse('2016-08-22'))
          create(:installment, subscription: subscription, created_at: 4.days.ago)

          subscription.update!(interval_length: 1, interval_units: 'month')

          expect(subscription.actionable_date.to_date).to eq((4.days.ago + 1.month).to_date)
        end
      end
    end

    context "when there are no installments" do
      context "when the subscription creation date would cause the interval to be in the past" do
        it "sets the actionable_date to the current day" do
          subscription = create(:subscription, created_at: Time.zone.parse('2016-08-22'))

          subscription.update!(interval_length: 1, interval_units: 'month')

          expect(subscription.actionable_date.to_date).to eq(Time.zone.today)
        end
      end

      context "when the subscription creation date would cause the interval to be in the future" do
        it "sets the actionable_date to one interval past the subscription creation date" do
          subscription = create(:subscription, created_at: 4.days.ago)

          subscription.update!(interval_length: 1, interval_units: 'month')

          expect(subscription.actionable_date.to_date).to eq((4.days.ago + 1.month).to_date)
        end
      end
    end
  end
end