-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
return_authorization_spec.rb
289 lines (232 loc) 路 10.2 KB
/
return_authorization_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
require 'spec_helper'
describe Spree::ReturnAuthorization, type: :model do
let(:order) { create(:shipped_order) }
let(:stock_location) { create(:stock_location) }
let(:rma_reason) { create(:return_reason) }
let(:inventory_unit_1) { order.inventory_units.first }
let(:variant) { order.variants.first }
let(:return_authorization) do
Spree::ReturnAuthorization.new(order: order,
stock_location_id: stock_location.id,
return_reason_id: rma_reason.id)
end
context "save" do
let(:order) { Spree::Order.create }
it "should be invalid when order has no inventory units" do
order.inventory_units.each(&:delete)
return_authorization.save
expect(return_authorization.errors[:order]).to eq(["has no shipped units"])
end
context "an inventory unit is already being exchanged" do
let(:order) { create(:shipped_order, line_items_count: 2) }
let!(:previous_exchange_return_item) { create(:exchange_return_item, inventory_unit: order.inventory_units.last) }
let(:return_item) { create(:return_item, inventory_unit: order.inventory_units.last) }
let(:return_authorization) { build(:return_authorization, order: order, return_items: [return_item]) }
it "should be invalid" do
return_authorization.save
expect(return_authorization.errors['base']).to include('Return items cannot be created for inventory units that are already awaiting exchange.')
end
end
context "an inventory unit is not being exchanged" do
let(:order) { create(:shipped_order, line_items_count: 2) }
let(:return_item) { create(:return_item, inventory_unit: order.inventory_units.last) }
let(:return_authorization) { build(:return_authorization, order: order, return_items: [return_item]) }
it "is valid" do
return_authorization.save
expect(return_authorization.errors['base'].size).to eq 0
end
end
context "expedited exchanges are configured" do
let(:order) { create(:shipped_order, line_items_count: 2) }
let(:exchange_return_item) { build(:exchange_return_item, inventory_unit: order.inventory_units.first) }
let(:return_item) { build(:return_item, inventory_unit: order.inventory_units.last) }
subject { create(:return_authorization, order: order, return_items: [exchange_return_item, return_item]) }
before do
@expediteted_exchanges_config = Spree::Config[:expedited_exchanges]
Spree::Config[:expedited_exchanges] = true
@pre_exchange_hooks = subject.class.pre_expedited_exchange_hooks
end
after do
Spree::Config[:expedited_exchanges] = @expediteted_exchanges_config
subject.class.pre_expedited_exchange_hooks = @pre_exchange_hooks
end
context "no items to exchange" do
subject { create(:return_authorization, order: order) }
it "does not create a reimbursement" do
expect{ subject.save }.to_not change { Spree::Reimbursement.count }
end
end
context "items to exchange" do
it "calls pre_expedited_exchange hooks with the return items to exchange" do
hook = double(:as_null_object)
expect(hook).to receive(:call).with [exchange_return_item]
subject.class.pre_expedited_exchange_hooks = [hook]
subject.save
end
it "attempts to accept all return items requiring exchange" do
expect(exchange_return_item).to receive :attempt_accept
expect(return_item).not_to receive :attempt_accept
subject.save
end
it "performs an exchange reimbursement for the exchange return items" do
subject.save
reimbursement = Spree::Reimbursement.last
expect(reimbursement.order).to eq subject.order
expect(reimbursement.return_items).to eq [exchange_return_item]
expect(exchange_return_item.reload.exchange_shipment).to be_present
end
context "the reimbursement fails" do
before do
allow_any_instance_of(Spree::Reimbursement).to receive(:save) { false }
allow_any_instance_of(Spree::Reimbursement).to receive(:errors) { double(full_messages: "foo") }
end
it "puts errors on the return authorization" do
subject.save
expect(subject.errors[:base]).to include "foo"
end
end
end
end
end
describe ".before_create" do
describe "#generate_number" do
context "number is assigned" do
let(:return_authorization) { Spree::ReturnAuthorization.new(number: '123') }
it "should return the assigned number" do
return_authorization.save
expect(return_authorization.number).to eq('123')
end
end
context "number is not assigned" do
let(:return_authorization) { Spree::ReturnAuthorization.new(number: nil) }
before { allow(return_authorization).to receive_messages valid?: true }
it "should assign number with random RA number" do
return_authorization.save
expect(return_authorization.number).to match(/RA\d{9}/)
end
end
end
end
context "#currency" do
before { allow(order).to receive(:currency) { "ABC" } }
it "returns the order currency" do
expect(return_authorization.currency).to eq("ABC")
end
end
describe "#pre_tax_total" do
let(:pre_tax_amount_1) { 15.0 }
let!(:return_item_1) { create(:return_item, return_authorization: return_authorization, pre_tax_amount: pre_tax_amount_1) }
let(:pre_tax_amount_2) { 50.0 }
let!(:return_item_2) { create(:return_item, return_authorization: return_authorization, pre_tax_amount: pre_tax_amount_2) }
let(:pre_tax_amount_3) { 5.0 }
let!(:return_item_3) { create(:return_item, return_authorization: return_authorization, pre_tax_amount: pre_tax_amount_3) }
subject { return_authorization.pre_tax_total }
it "sums it's associated return_item's pre-tax amounts" do
expect(subject).to eq(pre_tax_amount_1 + pre_tax_amount_2 + pre_tax_amount_3)
end
end
describe "#display_pre_tax_total" do
it "returns a Spree::Money" do
allow(return_authorization).to receive_messages(pre_tax_total: 21.22)
expect(return_authorization.display_pre_tax_total).to eq(Spree::Money.new(21.22))
end
end
context "can_receive?" do
before do
pending "TODO: get this method into our fork"
end
it "should allow_receive when inventory units assigned" do
allow(return_authorization).to receive_messages(inventory_units: [1, 2, 3])
expect(return_authorization.can_receive?).to be true
end
it "should not allow_receive with no inventory units" do
allow(return_authorization).to receive_messages(inventory_units: [])
expect(return_authorization.can_receive?).to be false
end
end
describe "#refundable_amount" do
let(:weighted_line_item_pre_tax_amount) { 5.0 }
let(:line_item_count) { return_authorization.order.line_items.count }
subject { return_authorization.refundable_amount }
before do
return_authorization.order.line_items.update_all(pre_tax_amount: weighted_line_item_pre_tax_amount)
return_authorization.order.update_attribute(:promo_total, promo_total)
end
context "no promotions" do
let(:promo_total) { 0.0 }
it "returns the pre-tax line item total" do
expect(subject).to eq(weighted_line_item_pre_tax_amount * line_item_count)
end
end
context "promotions" do
let(:promo_total) { -10.0 }
it "returns the pre-tax line item total minus the order level promotion value" do
expect(subject).to eq((weighted_line_item_pre_tax_amount * line_item_count) + promo_total)
end
end
end
describe "#customer_returned_items?" do
before do
allow_any_instance_of(Spree::Order).to receive_messages(return!: true)
end
subject { return_authorization.customer_returned_items? }
context "has associated customer returns" do
let(:customer_return) { create(:customer_return) }
let(:return_authorization) { customer_return.return_authorizations.first }
it "returns true" do
expect(subject).to eq true
end
end
context "does not have associated customer returns" do
let(:return_authorization) { create(:return_authorization) }
it "returns false" do
expect(subject).to eq false
end
end
end
describe 'cancel_return_items' do
let(:return_authorization) { create(:return_authorization, return_items: return_items) }
let(:return_items) { [return_item] }
let(:return_item) { create(:return_item) }
subject {
return_authorization.cancel!
}
it 'cancels the associated return items' do
subject
expect(return_item.reception_status).to eq 'cancelled'
end
context 'some return items cannot be cancelled' do
let(:return_items) { [return_item, return_item_2] }
let(:return_item_2) { create(:return_item, reception_status: 'received') }
it 'cancels those that can be cancelled' do
subject
expect(return_item.reception_status).to eq 'cancelled'
expect(return_item_2.reception_status).to eq 'received'
end
end
end
describe '#can_cancel?' do
subject { create(:return_authorization, return_items: return_items).can_cancel? }
let(:return_items) { [return_item_1, return_item_2] }
let(:return_item_1) { create(:return_item) }
let(:return_item_2) { create(:return_item) }
context 'all items can be cancelled' do
it 'returns true' do
expect(subject).to eq true
end
end
context 'at least one return item can be cancelled' do
let(:return_item_2) { create(:return_item, reception_status: 'received') }
it { is_expected.to eq true }
end
context 'no items can be cancelled' do
let(:return_item_1) { create(:return_item, reception_status: 'received') }
let(:return_item_2) { create(:return_item, reception_status: 'received') }
it { is_expected.to eq false }
end
context 'when return_authorization has no return_items' do
let(:return_items) { [] }
it { is_expected.to eq true }
end
end
end