summaryrefslogtreecommitdiff
path: root/spec/models/solidus_subscriptions/subscription_spec.rb
blob: 145cc442e951a93b3491a2e6810d776728e17b7e (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SolidusSubscriptions::Subscription, type: :model do
  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) }

  it 'validates currency correctly' do
    expect(subject).to validate_inclusion_of(:currency).
      in_array(::Money::Currency.all.map(&:iso_code)).
      with_message('is not a valid currency code')
  end

  describe 'creating a subscription' do
    it 'tracks the creation' do
      stub_const('Spree::Event', class_spy(Spree::Event))

      subscription = create(:subscription)

      expect(Spree::Event).to have_received(:fire).with(
        'solidus_subscriptions.subscription_created',
        subscription: subscription,
      )
    end

    it 'generates a guest token' do
      subscription = create(:subscription)

      expect(subscription.guest_token).to be_present
    end

    it 'sets default config currency if not given' do
      subscription = create(:subscription, currency: nil)

      expect(subscription.currency).to eq(Spree::Config.currency)
    end
  end

  describe 'updating a subscription' do
    it 'tracks interval changes' do
      stub_const('Spree::Event', class_spy(Spree::Event))
      subscription = create(:subscription)

      subscription.update!(interval_length: subscription.interval_length + 1)

      expect(Spree::Event).to have_received(:fire).with(
        'solidus_subscriptions.subscription_frequency_changed',
        subscription: subscription,
      )
    end

    it 'tracks shipping address changes' do
      stub_const('Spree::Event', class_spy(Spree::Event))
      subscription = create(:subscription)

      subscription.update!(shipping_address: create(:address))

      expect(Spree::Event).to have_received(:fire).with(
        'solidus_subscriptions.subscription_shipping_address_changed',
        subscription: subscription,
      )
    end

    it 'tracks billing address changes' do
      stub_const('Spree::Event', class_spy(Spree::Event))
      subscription = create(:subscription)

      subscription.update!(billing_address: create(:address))

      expect(Spree::Event).to have_received(:fire).with(
        'solidus_subscriptions.subscription_billing_address_changed',
        subscription: subscription,
      )
    end

    it 'tracks payment method changes' do
      stub_const('Spree::Event', class_spy(Spree::Event))

      subscription = create(:subscription)
      subscription.update!(payment_source: create(:credit_card))

      expect(Spree::Event).to have_received(:fire).with(
        'solidus_subscriptions.subscription_payment_method_changed',
        subscription: subscription,
      )
    end
  end

  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 } }

    before do
      allow(SolidusSubscriptions.configuration).to receive(:minimum_cancellation_notice) { minimum_cancellation_notice }
    end

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

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

      it 'creates a subscription_canceled event' do
        subject
        expect(subscription.events.last).to have_attributes(event_type: 'subscription_canceled')
      end
    end

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

      it 'is pending cancelation' do
        subject
        expect(subscription).to be_pending_cancellation
      end

      it 'creates a subscription_canceled event' do
        subject
        expect(subscription.events.last).to have_attributes(event_type: 'subscription_canceled')
      end
    end

    context 'when the minimum cancellation date is 0.days' do
      let(:actionable_date) { Date.current }
      let(:minimum_cancellation_notice) { 0.days }

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

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

    let(:total_skips) { 0 }
    let(:successive_skips) { 0 }
    let(:expected_date) { 2.months.from_now.to_date }

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

    before { stub_config(maximum_total_skips: 1) }

    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]).not_to be_empty
      end

      it 'does not create an event' do
        expect { subject }.not_to change(subscription.events, :count)
      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]).not_to be_empty
      end

      it 'does not create an event' do
        expect { subject }.not_to change(subscription.events, :count)
      end
    end

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

      it 'creates a subscription_skipped event' do
        subject
        expect(subscription.events.last).to have_attributes(event_type: 'subscription_skipped')
      end
    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 'when the subscription can be deactivated' do
      let(:attributes) do
        { end_date: Date.current.ago(2.days) }
      end

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

      it 'creates a subscription_deactivated event' do
        subject
        expect(subscription.events.last).to have_attributes(event_type: 'subscription_ended')
      end
    end

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

      it 'does not create an event' do
        expect { subject }.not_to change(subscription.events, :count)
      end
    end
  end

  describe '#activate' do
    context 'when the subscription can be activated' do
      it 'activates the subscription' do
        subscription = create(:subscription,
          actionable_date: Time.zone.today,
          end_date: Time.zone.yesterday,)
        subscription.deactivate!

        subscription.activate

        expect(subscription.state).to eq('active')
      end

      it 'creates a subscription_activated event' do
        subscription = create(:subscription,
          actionable_date: Time.zone.today,
          end_date: Time.zone.yesterday,)
        subscription.deactivate!

        subscription.activate

        expect(subscription.events.last).to have_attributes(event_type: 'subscription_activated')
      end
    end

    context 'when the subscription cannot be activated' do
      it 'returns false' do
        subscription = create(:subscription, actionable_date: Time.zone.today)

        expect(subscription.activate).to eq(false)
      end

      it 'does not create an event' do
        subscription = create(:subscription, actionable_date: Time.zone.today)

        expect {
          subscription.activate
        }.not_to change(subscription.events, :count)
      end
    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
    subject { described_class.actionable }

    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 }

    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).not_to include future_subscription
    end

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

    it "does not include canceled subscriptions" do
      expect(subject).not_to include canceled_subscription
    end
  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 'with successfull subscriptions' do
      let(:state) { :success }

      it { is_expected.to match_array success_subs }
    end

    context 'with failed subscriptions' do
      let(:state) { :failed }

      it { is_expected.to match_array failed_subs }
    end

    context 'with new subscriptions' do
      let(:state) { :pending }

      it { is_expected.to match_array new_subs }
    end

    context 'with 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_payment_source" 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_payment_source" 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 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 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 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 eq((4.days.ago + 1.month).to_date)
        end
      end
    end
  end

  describe '#failing_since' do
    context 'when the subscription is not failing' do
      it 'returns nil' do
        subscription = create(:subscription, installments: [
          create(:installment, details: [
            create(:installment_detail, success: false, created_at: '2020-11-11'),
            create(:installment_detail, success: false, created_at: '2020-11-12'),
            create(:installment_detail, success: true, created_at: '2020-11-13'),
          ]),
          create(:installment, details: [
            create(:installment_detail, success: false, created_at: '2020-11-24'),
            create(:installment_detail, success: true, created_at: '2020-11-25'),
          ]),
        ])

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

    context 'when the subscription is failing with a previous success' do
      it 'returns the date of the first failure' do
        subscription = create(:subscription, installments: [
          create(:installment, details: [
            create(:installment_detail, success: false, created_at: '2020-11-11'),
            create(:installment_detail, success: false, created_at: '2020-11-12'),
            create(:installment_detail, success: true, created_at: '2020-11-13'),
          ]),
          create(:installment, details: [
            create(:installment_detail, success: false, created_at: '2020-11-24'),
            create(:installment_detail, success: false, created_at: '2020-11-25'),
          ]),
          create(:installment, details: [
            create(:installment_detail, success: false, created_at: '2020-11-26'),
            create(:installment_detail, success: false, created_at: '2020-11-27'),
          ]),
        ])

        expect(subscription.failing_since).to eq(Time.zone.parse('2020-11-24'))
      end
    end

    context 'when the subscription is failing with no previous success' do
      it 'returns the date of the first failure' do
        subscription = create(:subscription, installments: [
          create(:installment, details: [
            create(:installment_detail, success: false, created_at: '2020-11-11'),
            create(:installment_detail, success: false, created_at: '2020-11-12'),
            create(:installment_detail, success: false, created_at: '2020-11-13'),
          ]),
          create(:installment, details: [
            create(:installment_detail, success: false, created_at: '2020-11-24'),
            create(:installment_detail, success: false, created_at: '2020-11-25'),
          ]),
          create(:installment, details: [
            create(:installment_detail, success: false, created_at: '2020-11-26'),
            create(:installment_detail, success: false, created_at: '2020-11-27'),
          ]),
        ])

        expect(subscription.failing_since).to eq(Time.zone.parse('2020-11-11'))
      end
    end
  end

  describe '#maximum_reprocessing_time_reached?' do
    context 'when maximum_reprocessing_time is not configured' do
      it 'returns false' do
        stub_config(maximum_reprocessing_time: 5.days)
        subscription = create(:subscription)

        expect(subscription.maximum_reprocessing_time_reached?).to eq(false)
      end
    end

    context 'when maximum_reprocessing_time is configured' do
      context 'when the subscription has been failing for too long' do
        it 'returns true' do
          stub_config(maximum_reprocessing_time: 15.days)

          subscription = create(:subscription, installments: [
            create(:installment, details: [
              create(:installment_detail, success: false, created_at: 20.days.ago),
              create(:installment_detail, success: false, created_at: 19.days.ago),
              create(:installment_detail, success: true, created_at: 18.days.ago),
            ]),
            create(:installment, details: [
              create(:installment_detail, success: false, created_at: 17.days.ago),
              create(:installment_detail, success: false, created_at: 16.days.ago),
            ]),
            create(:installment, details: [
              create(:installment_detail, success: false, created_at: 15.days.ago),
              create(:installment_detail, success: false, created_at: 14.days.ago),
            ]),
          ])

          expect(subscription.maximum_reprocessing_time_reached?).to eq(true)
        end
      end

      context 'when the subscription has not been failing for too long' do
        it 'returns false' do
          stub_config(maximum_reprocessing_time: 15.days)

          subscription = create(:subscription, installments: [
            create(:installment, details: [
              create(:installment_detail, success: false, created_at: 15.days.ago),
              create(:installment_detail, success: false, created_at: 14.days.ago),
              create(:installment_detail, success: true, created_at: 13.days.ago),
            ]),
            create(:installment, details: [
              create(:installment_detail, success: false, created_at: 12.days.ago),
              create(:installment_detail, success: false, created_at: 11.days.ago),
            ]),
            create(:installment, details: [
              create(:installment_detail, success: false, created_at: 10.days.ago),
              create(:installment_detail, success: false, created_at: 9.days.ago),
            ]),
          ])

          expect(subscription.maximum_reprocessing_time_reached?).to eq(false)
        end
      end

      context 'when the subscription is not failing' do
        it 'returns false' do
          stub_config(maximum_reprocessing_time: 2.days)

          subscription = create(:subscription, installments: [
            create(:installment, details: [
              create(:installment_detail, success: false, created_at: 15.days.ago),
              create(:installment_detail, success: false, created_at: 14.days.ago),
              create(:installment_detail, success: true, created_at: 13.days.ago),
            ]),
            create(:installment, details: [
              create(:installment_detail, success: false, created_at: 12.days.ago),
              create(:installment_detail, success: false, created_at: 11.days.ago),
            ]),
            create(:installment, details: [
              create(:installment_detail, success: false, created_at: 10.days.ago),
              create(:installment_detail, success: true, created_at: 9.days.ago),
            ]),
          ])

          expect(subscription.maximum_reprocessing_time_reached?).to eq(false)
        end
      end
    end
  end

  describe '#actionable?' do
    context 'when the actionable date is nil' do
      it 'is not actionable' do
        subscription = build_stubbed(:subscription, actionable_date: nil)

        expect(subscription).not_to be_actionable
      end
    end

    context 'when the actionable date is in the future' do
      it 'is not actionable' do
        subscription = build_stubbed(:subscription, actionable_date: Time.zone.today + 5.days)

        expect(subscription).not_to be_actionable
      end
    end

    context 'when the state is either canceled or inactive' do
      it 'is not actionable' do
        canceled_subscription = build_stubbed(:subscription, :canceled)
        inactive_subscription = build_stubbed(:subscription, :inactive)

        [canceled_subscription, inactive_subscription].each do |subscription|
          expect(subscription).not_to be_actionable
        end
      end
    end

    context 'when the active subscription actionable date is today or in the past' do
      it 'is actionable' do
        subscription = build_stubbed(:subscription, actionable_date: Time.zone.today)

        expect(subscription).to be_actionable
      end
    end
  end
end