-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
store_credit_spec.rb
877 lines (683 loc) · 27.5 KB
/
store_credit_spec.rb
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
require 'spec_helper'
describe Spree::StoreCredit do
let(:currency) { "TEST" }
let(:store_credit) { build(:store_credit, store_credit_attrs) }
let(:store_credit_attrs) { {} }
describe "callbacks" do
subject { store_credit.save }
context "amount used is greater than zero" do
let(:store_credit) { create(:store_credit, amount: 100, amount_used: 1) }
subject { store_credit.destroy }
it 'can not delete the store credit' do
subject
expect(store_credit.reload).to eq store_credit
expect(store_credit.errors[:amount_used]).to include("is greater than zero. Can not delete store credit")
end
end
context "category is a non-expiring type" do
let!(:secondary_credit_type) { create(:secondary_credit_type) }
let(:store_credit) { build(:store_credit, credit_type: nil) }
before do
allow(store_credit.category).to receive(:non_expiring?).and_return(true)
end
it "sets the credit type to non-expiring" do
subject
expect(store_credit.credit_type.name).to eq secondary_credit_type.name
end
end
context "category is an expiring type" do
before do
allow(store_credit.category).to receive(:non_expiring?).and_return(false)
end
it "sets the credit type to non-expiring" do
subject
expect(store_credit.credit_type.name).to eq "Expiring"
end
end
context "the type is set" do
let!(:secondary_credit_type) { create(:secondary_credit_type) }
let(:store_credit) { build(:store_credit, credit_type: secondary_credit_type) }
before do
allow(store_credit.category).to receive(:non_expiring?).and_return(false)
end
it "doesn't overwrite the type" do
expect{ subject }.to_not change{ store_credit.credit_type }
end
end
end
describe "validations" do
describe "used amount should not be greater than the credited amount" do
context "the used amount is defined" do
let(:invalid_store_credit) { build(:store_credit, amount: 100, amount_used: 150) }
it "should not be valid" do
expect(invalid_store_credit).not_to be_valid
end
it "should set the correct error message" do
invalid_store_credit.valid?
expect(invalid_store_credit.errors.full_messages).to include("Amount used cannot be greater than the credited amount")
end
end
context "the used amount is not defined yet" do
let(:store_credit) { build(:store_credit, amount: 100) }
it "should be valid" do
expect(store_credit).to be_valid
end
end
end
describe "amount used less than or equal to amount" do
subject { build(:store_credit, amount_used: 101.0, amount: 100.0) }
it "is not valid" do
expect(subject).not_to be_valid
end
it "adds an error message about the invalid amount used" do
subject.valid?
expect(subject.errors[:amount_used]).to include("cannot be greater than the credited amount")
end
end
describe "amount authorized less than or equal to amount" do
subject { build(:store_credit, amount_authorized: 101.0, amount: 100.0) }
it "is not valid" do
expect(subject).not_to be_valid
end
it "adds an error message about the invalid authorized amount" do
subject.valid?
expect(subject.errors[:amount_authorized]).to include(" exceeds the available credit")
end
end
describe "editing category" do
let!(:store_credit) { create(:store_credit) }
let!(:test_category) { create(:store_credit_category, name: "Testing") }
subject { store_credit.update_attributes(category: test_category) }
it "returns false" do
expect(subject).to eq false
end
it "category doesn't change" do
expect { subject }.not_to change { store_credit.reload.category }
end
it "adds an error message about not being able to edit the category" do
subject
expect(store_credit.errors[:category]).to include("cannot be modified")
end
end
end
describe "#display_amount" do
it "returns a Spree::Money instance" do
expect(store_credit.display_amount).to be_instance_of(Spree::Money)
end
end
describe "#display_amount_used" do
it "returns a Spree::Money instance" do
expect(store_credit.display_amount_used).to be_instance_of(Spree::Money)
end
end
describe "#display_amount_authorized" do
it "returns a Spree::Money instance" do
expect(store_credit.display_amount_authorized).to be_instance_of(Spree::Money)
end
end
describe "#amount_remaining" do
context "invalidated" do
before { allow(store_credit).to receive(:invalidated?) { true } }
it { expect(store_credit.amount_remaining).to eq 0.0 }
end
context "the amount_used is not defined" do
context "the authorized amount is not defined" do
it "returns the credited amount" do
expect(store_credit.amount_remaining).to eq store_credit.amount
end
end
context "the authorized amount is defined" do
let(:authorized_amount) { 15.00 }
before { store_credit.update_attributes(amount_authorized: authorized_amount) }
it "subtracts the authorized amount from the credited amount" do
expect(store_credit.amount_remaining).to eq(store_credit.amount - authorized_amount)
end
end
end
context "the amount_used is defined" do
let(:amount_used) { 10.0 }
before { store_credit.update_attributes(amount_used: amount_used) }
context "the authorized amount is not defined" do
it "subtracts the amount used from the credited amount" do
expect(store_credit.amount_remaining).to eq(store_credit.amount - amount_used)
end
end
context "the authorized amount is defined" do
let(:authorized_amount) { 15.00 }
before { store_credit.update_attributes(amount_authorized: authorized_amount) }
it "subtracts the amount used and the authorized amount from the credited amount" do
expect(store_credit.amount_remaining).to eq(store_credit.amount - amount_used - authorized_amount)
end
end
end
end
describe "#authorize" do
context "amount is valid" do
let(:authorization_amount) { 1.0 }
let(:added_authorization_amount) { 3.0 }
let(:originator) { nil }
context "amount has not been authorized yet" do
before { store_credit.update_attributes(amount_authorized: authorization_amount) }
it "returns true" do
expect(store_credit.authorize(store_credit.amount - authorization_amount, store_credit.currency)).to be_truthy
end
it "adds the new amount to authorized amount" do
store_credit.authorize(added_authorization_amount, store_credit.currency)
expect(store_credit.reload.amount_authorized).to eq(authorization_amount + added_authorization_amount)
end
context "originator is present" do
let(:originator) { create(:user) } # won't actually be a user. just giving it a valid model here
subject { store_credit.authorize(added_authorization_amount, store_credit.currency, action_originator: originator) }
it "records the originator" do
expect { subject }.to change { Spree::StoreCreditEvent.count }.by(1)
expect(Spree::StoreCreditEvent.last.originator).to eq originator
end
end
end
context "authorization has already happened" do
let!(:auth_event) { create(:store_credit_auth_event, store_credit: store_credit) }
before { store_credit.update_attributes(amount_authorized: store_credit.amount) }
it "returns true" do
expect(store_credit.authorize(store_credit.amount, store_credit.currency, action_authorization_code: auth_event.authorization_code)).to be true
end
end
end
context "amount is invalid" do
it "returns false" do
expect(store_credit.authorize(store_credit.amount * 2, store_credit.currency)).to be false
end
end
end
describe "#validate_authorization" do
context "insufficient funds" do
subject { store_credit.validate_authorization(store_credit.amount * 2, store_credit.currency) }
it "returns false" do
expect(subject).to be false
end
it "adds an error to the model" do
subject
expect(store_credit.errors.full_messages).to include("Store credit amount remaining is not sufficient")
end
end
context "currency mismatch" do
subject { store_credit.validate_authorization(store_credit.amount, "EUR") }
it "returns false" do
expect(subject).to be false
end
it "adds an error to the model" do
subject
expect(store_credit.errors.full_messages).to include("Store credit currency does not match order currency")
end
end
context "valid authorization" do
subject { store_credit.validate_authorization(store_credit.amount, store_credit.currency) }
it "returns true" do
expect(subject).to be true
end
end
context 'troublesome floats' do
# 8.21.to_d < 8.21 => true
let(:store_credit_attrs) { { amount: 8.21 } }
subject { store_credit.validate_authorization(store_credit_attrs[:amount], store_credit.currency) }
it { is_expected.to be_truthy }
end
end
describe "#capture" do
let(:authorized_amount) { 10.00 }
let(:auth_code) { "23-SC-20140602164814476128" }
before do
@original_authed_amount = store_credit.amount_authorized
@auth_code = store_credit.authorize(authorized_amount, store_credit.currency)
end
context "insufficient funds" do
subject { store_credit.capture(authorized_amount * 2, @auth_code, store_credit.currency) }
it "returns false" do
expect(subject).to be false
end
it "adds an error to the model" do
subject
expect(store_credit.errors.full_messages).to include("Unable to capture more than authorized amount")
end
it "does not update the store credit model" do
expect { subject }.to_not change { store_credit }
end
end
context "currency mismatch" do
subject { store_credit.capture(authorized_amount, @auth_code, "EUR") }
it "returns false" do
expect(subject).to be false
end
it "adds an error to the model" do
subject
expect(store_credit.errors.full_messages).to include("Store credit currency does not match order currency")
end
it "does not update the store credit model" do
expect { subject }.to_not change { store_credit }
end
end
context "valid capture" do
let(:remaining_authorized_amount) { 1 }
let(:originator) { nil }
subject { store_credit.capture(authorized_amount - remaining_authorized_amount, @auth_code, store_credit.currency, action_originator: originator) }
it "returns true" do
expect(subject).to be_truthy
end
it "updates the authorized amount to the difference between the store credits total authed amount and the authorized amount for this event" do
subject
expect(store_credit.reload.amount_authorized).to eq(@original_authed_amount)
end
it "updates the used amount to the current used amount plus the captured amount" do
subject
expect(store_credit.reload.amount_used).to eq authorized_amount - remaining_authorized_amount
end
context "originator is present" do
let(:originator) { create(:user) } # won't actually be a user. just giving it a valid model here
it "records the originator" do
expect { subject }.to change { Spree::StoreCreditEvent.count }.by(1)
expect(Spree::StoreCreditEvent.last.originator).to eq originator
end
end
end
end
describe "#void" do
let(:auth_code) { "1-SC-20141111111111" }
let(:store_credit) { create(:store_credit, amount_used: 150.0) }
let(:originator) { nil }
subject do
store_credit.void(auth_code, action_originator: originator)
end
context "no event found for auth_code" do
it "returns false" do
expect(subject).to be false
end
it "adds an error to the model" do
subject
expect(store_credit.errors.full_messages).to include("Unable to void code: #{auth_code}")
end
end
context "capture event found for auth_code" do
let(:captured_amount) { 10.0 }
let!(:capture_event) {
create(:store_credit_auth_event,
action: Spree::StoreCredit::CAPTURE_ACTION,
authorization_code: auth_code,
amount: captured_amount,
store_credit: store_credit)
}
it "returns false" do
expect(subject).to be false
end
it "does not change the amount used on the store credit" do
expect { subject }.to_not change{ store_credit.amount_used.to_f }
end
end
context "auth event found for auth_code" do
let(:auth_event) { create(:store_credit_auth_event) }
let(:authorized_amount) { 10.0 }
let!(:auth_event) {
create(:store_credit_auth_event,
authorization_code: auth_code,
amount: authorized_amount,
store_credit: store_credit)
}
it "returns true" do
expect(subject).to be true
end
it "returns the capture amount to the store credit" do
expect { subject }.to change{ store_credit.amount_authorized.to_f }.by(-authorized_amount)
end
context "originator is present" do
let(:originator) { create(:user) } # won't actually be a user. just giving it a valid model here
it "records the originator" do
expect { subject }.to change { Spree::StoreCreditEvent.count }.by(1)
expect(Spree::StoreCreditEvent.last.originator).to eq originator
end
end
end
end
describe "#credit" do
let(:event_auth_code) { "1-SC-20141111111111" }
let(:amount_used) { 10.0 }
let(:store_credit) { create(:store_credit, amount_used: amount_used) }
let!(:capture_event) {
create(:store_credit_auth_event,
action: Spree::StoreCredit::CAPTURE_ACTION,
authorization_code: event_auth_code,
amount: captured_amount,
store_credit: store_credit)
}
let(:originator) { nil }
subject { store_credit.credit(credit_amount, auth_code, currency, action_originator: originator) }
context "currency does not match" do
let(:currency) { "AUD" }
let(:credit_amount) { 5.0 }
let(:captured_amount) { 100.0 }
let(:auth_code) { event_auth_code }
it "returns false" do
expect(subject).to be false
end
it "adds an error message about the currency mismatch" do
subject
expect(store_credit.errors.full_messages).to include("Store credit currency does not match order currency")
end
end
context "unable to find capture event" do
let(:currency) { "USD" }
let(:credit_amount) { 5.0 }
let(:captured_amount) { 100.0 }
let(:auth_code) { "UNKNOWN_CODE" }
it "returns false" do
expect(subject).to be false
end
it "adds an error message about the currency mismatch" do
subject
expect(store_credit.errors.full_messages).to include("Unable to credit code: #{auth_code}")
end
end
context "amount is more than what is captured" do
let(:currency) { "USD" }
let(:credit_amount) { 100.0 }
let(:captured_amount) { 5.0 }
let(:auth_code) { event_auth_code }
it "returns false" do
expect(subject).to be false
end
it "adds an error message about the currency mismatch" do
subject
expect(store_credit.errors.full_messages).to include("Unable to credit code: #{auth_code}")
end
end
context "amount is successfully credited" do
let(:currency) { "USD" }
let(:credit_amount) { 5.0 }
let(:captured_amount) { 100.0 }
let(:auth_code) { event_auth_code }
context "credit_to_new_allocation is set" do
before { Spree::Config[:credit_to_new_allocation] = true }
it "returns true" do
expect(subject).to be true
end
it "creates a new store credit record" do
expect { subject }.to change { Spree::StoreCredit.count }.by(1)
end
it "does not create a new store credit event on the parent store credit" do
expect { subject }.to_not change { store_credit.store_credit_events.count }
end
context "credits the passed amount to a new store credit record" do
before do
subject
@new_store_credit = Spree::StoreCredit.last
end
it "does not set the amount used on hte originating store credit" do
expect(store_credit.reload.amount_used).to eq amount_used
end
it "sets the correct amount on the new store credit" do
expect(@new_store_credit.amount).to eq credit_amount
end
[:user_id, :category_id, :created_by_id, :currency, :type_id].each do |attr|
it "sets attribute #{attr} inherited from the originating store credit" do
expect(@new_store_credit.send(attr)).to eq store_credit.send(attr)
end
end
it "sets a memo" do
expect(@new_store_credit.memo).to eq "This is a credit from store credit ID #{store_credit.id}"
end
end
context "originator is present" do
let(:originator) { create(:user) } # won't actually be a user. just giving it a valid model here
it "records the originator" do
expect { subject }.to change { Spree::StoreCreditEvent.count }.by(1)
expect(Spree::StoreCreditEvent.last.originator).to eq originator
end
end
end
context "credit_to_new_allocation is not set" do
it "returns true" do
expect(subject).to be true
end
it "credits the passed amount to the store credit amount used" do
subject
expect(store_credit.reload.amount_used).to eq(amount_used - credit_amount)
end
it "creates a new store credit event" do
expect { subject }.to change { store_credit.store_credit_events.count }.by(1)
end
end
end
end
describe "#amount_used" do
context "amount used is not defined" do
subject { Spree::StoreCredit.new }
it "returns zero" do
expect(subject.amount_used).to be_zero
end
end
context "amount used is defined" do
let(:amount_used) { 100.0 }
subject { create(:store_credit, amount_used: amount_used) }
it "returns the attribute value" do
expect(subject.amount_used).to eq amount_used
end
end
end
describe "#amount_authorized" do
context "amount authorized is not defined" do
subject { Spree::StoreCredit.new }
it "returns zero" do
expect(subject.amount_authorized).to be_zero
end
end
context "amount authorized is defined" do
let(:amount_authorized) { 100.0 }
subject { create(:store_credit, amount_authorized: amount_authorized) }
it "returns the attribute value" do
expect(subject.amount_authorized).to eq amount_authorized
end
end
end
describe "#can_capture?" do
let(:store_credit) { create(:store_credit) }
let(:payment) { create(:payment, state: payment_state) }
subject { store_credit.can_capture?(payment) }
context "pending payment" do
let(:payment_state) { 'pending' }
it "returns true" do
expect(subject).to be true
end
end
context "checkout payment" do
let(:payment_state) { 'checkout' }
it "returns true" do
expect(subject).to be true
end
end
context "void payment" do
let(:payment_state) { Spree::StoreCredit::VOID_ACTION }
it "returns false" do
expect(subject).to be false
end
end
context "invalid payment" do
let(:payment_state) { 'invalid' }
it "returns false" do
expect(subject).to be false
end
end
context "complete payment" do
let(:payment_state) { 'completed' }
it "returns false" do
expect(subject).to be false
end
end
end
describe "#can_void?" do
let(:store_credit) { create(:store_credit) }
let(:payment) { create(:payment, state: payment_state) }
subject { store_credit.can_void?(payment) }
context "pending payment" do
let(:payment_state) { 'pending' }
it "returns true" do
expect(subject).to be true
end
end
context "checkout payment" do
let(:payment_state) { 'checkout' }
it "returns false" do
expect(subject).to be false
end
end
context "void payment" do
let(:payment_state) { Spree::StoreCredit::VOID_ACTION }
it "returns false" do
expect(subject).to be false
end
end
context "invalid payment" do
let(:payment_state) { 'invalid' }
it "returns false" do
expect(subject).to be false
end
end
context "complete payment" do
let(:payment_state) { 'completed' }
it "returns false" do
expect(subject).to be false
end
end
end
describe "#can_credit?" do
let(:store_credit) { create(:store_credit) }
let(:payment) { create(:payment, state: payment_state) }
subject { store_credit.can_credit?(payment) }
context "payment is not completed" do
let(:payment_state) { "pending" }
it "returns false" do
expect(subject).to be false
end
end
context "payment is completed" do
let(:payment_state) { "completed" }
context "credit is owed on the order" do
before { allow(payment.order).to receive_messages(payment_state: 'credit_owed') }
context "payment doesn't have allowed credit" do
before { allow(payment).to receive_messages(credit_allowed: 0.0) }
it "returns false" do
expect(subject).to be false
end
end
context "payment has allowed credit" do
before { allow(payment).to receive_messages(credit_allowed: 5.0) }
it "returns true" do
expect(subject).to be true
end
end
end
end
describe "#store_events" do
context "create" do
context "user has one store credit" do
let(:store_credit_amount) { 100.0 }
subject { create(:store_credit, amount: store_credit_amount) }
it "creates a store credit event" do
expect { subject }.to change { Spree::StoreCreditEvent.count }.by(1)
end
it "makes the store credit event an allocation event" do
expect(subject.store_credit_events.first.action).to eq Spree::StoreCredit::ALLOCATION_ACTION
end
it "saves the user's total store credit in the event" do
expect(subject.store_credit_events.first.user_total_amount).to eq store_credit_amount
end
end
context "user has multiple store credits" do
let(:store_credit_amount) { 100.0 }
let(:additional_store_credit_amount) { 200.0 }
let(:user) { create(:user) }
let!(:store_credit) { create(:store_credit, user: user, amount: store_credit_amount) }
subject { create(:store_credit, user: user, amount: additional_store_credit_amount) }
it "saves the user's total store credit in the event" do
expect(subject.store_credit_events.first.user_total_amount).to eq(store_credit_amount + additional_store_credit_amount)
end
end
context "an action is specified" do
it "creates an event with the set action" do
store_credit = build(:store_credit)
store_credit.action = Spree::StoreCredit::VOID_ACTION
store_credit.action_authorization_code = "1-SC-TEST"
expect { store_credit.save! }.to change { Spree::StoreCreditEvent.where(action: Spree::StoreCredit::VOID_ACTION).count }.by(1)
end
end
end
end
end
describe "#update_amount" do
let(:invalidation_user) { create(:user) }
let(:invalidation_reason) { create(:store_credit_update_reason) }
subject { store_credit.update_amount(amount, invalidation_reason, invalidation_user) }
context "amount is valid" do
let(:amount) { 10.0 }
before { store_credit.update_attributes!(amount: 30.0) }
it "returns true" do
expect(subject).to eq true
end
it "creates an adjustment store credit event" do
expect { subject }.to change { store_credit.store_credit_events.where(action: Spree::StoreCredit::ADJUSTMENT_ACTION).count }.from(0).to(1)
end
it "sets the adjustment amount on the store credit event correctly" do
subject
expect(store_credit.store_credit_events.find_by(action: Spree::StoreCredit::ADJUSTMENT_ACTION).amount).to eq -20
end
it "sets the originator on the store credit event correctly" do
subject
expect(store_credit.store_credit_events.find_by(action: Spree::StoreCredit::ADJUSTMENT_ACTION).originator).to eq invalidation_user
end
end
context "amount is invalid" do
let(:amount) { -10.0 }
it "returns false" do
expect(subject).to eq false
end
it "doesn't create an adjustment store credit event" do
expect { subject }.to_not change { store_credit.store_credit_events.where(action: Spree::StoreCredit::ADJUSTMENT_ACTION).count }
end
end
end
describe "#invalidate" do
let(:invalidation_user) { create(:user) }
let(:invalidation_reason) { create(:store_credit_update_reason) }
before do
store_credit.save!
end
subject { store_credit.invalidate(invalidation_reason, invalidation_user) }
it "sets the invalidated_at field to the current time" do
invalidated_at = Time.current
Timecop.freeze(invalidated_at) do
subject
expect(store_credit.invalidated_at).to eq invalidated_at
end
end
context "there is an uncaptured authorization" do
before { store_credit.authorize(5.0, "USD") }
it "prevents invalidation" do
expect { subject }.to_not change { store_credit.reload.invalidated_at }
expect(store_credit.errors[:invalidated_at].join).to match /uncaptured authorization/
end
end
context "there is a captured authorization" do
before do
auth_code = store_credit.authorize(5.0, "USD")
store_credit.capture(5.0, auth_code, "USD")
end
it "can invalidate the rest of the store credit" do
expect { subject }.to change { store_credit.reload.invalidated_at }
expect(store_credit.errors).to be_blank
end
it "creates a store credit event for the invalidation" do
expect { subject }.to change { store_credit.store_credit_events.where(action: Spree::StoreCredit::INVALIDATE_ACTION).count }.from(0).to(1)
end
it "assigns the originator as the user that is performing the invalidation" do
subject
expect(store_credit.store_credit_events.find_by(action: Spree::StoreCredit::INVALIDATE_ACTION).originator).to eq invalidation_user
end
end
end
end