-
-
Notifications
You must be signed in to change notification settings - Fork 730
/
Copy pathvoucher_adjustments_spec.rb
281 lines (220 loc) · 9.32 KB
/
voucher_adjustments_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
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe VoucherAdjustmentsController, type: :request do
let(:user) { order.user }
let(:address) { create(:address) }
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
let(:order_cycle) { create(:order_cycle, distributors: [distributor]) }
let(:exchange) { order_cycle.exchanges.outgoing.first }
let!(:order) do
create(
:order_with_line_items,
line_items_count: 1,
distributor:,
order_cycle:,
bill_address: address,
ship_address: address
)
end
let(:shipping_method) { distributor.shipping_methods.first }
let(:voucher) { create(:voucher_flat_rate, code: 'some_code', enterprise: distributor) }
before do
order.update!(created_by: user)
order.select_shipping_method shipping_method.id
Orders::WorkflowService.new(order).advance_to_payment
sign_in user
end
describe "POST voucher_adjustments" do
let(:params) { { order: { voucher_code: voucher.code } } }
it "adds a voucher to the user's current order" do
expect {
post("/voucher_adjustments", params:)
}.to change { order.reload.voucher_adjustments.count }.by(1)
expect(response).to be_successful
end
context "when voucher doesn't exist" do
let(:params) { { order: { voucher_code: "non_voucher" } } }
it "returns 422 and an error message" do
post("/voucher_adjustments", params:)
expect(response).to be_unprocessable
expect(flash[:error]).to match "Voucher code Not found"
end
end
context "when adding fails" do
it "returns 422 and an error message" do
# Create a non valid adjustment
bad_adjustment = build(:adjustment, label: nil)
allow(voucher).to receive(:create_adjustment).and_return(bad_adjustment)
allow(Voucher).to receive(:find_by).and_return(voucher)
post("/voucher_adjustments", params:)
expect(response).to be_unprocessable
expect(flash[:error]).to match("Voucher code There was an error while adding the voucher")
end
end
context "when the order has a payment and payment feed" do
let(:payment_method) { create(:payment_method, calculator:) }
let(:calculator) { Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) }
before do
create(:payment, order:, payment_method:, amount: order.total)
end
it "removes existing payments" do
expect do
post "/voucher_adjustments", params:
end.to change { order.reload.payments.count }.from(1).to(0)
end
it "removes existing payment fees" do
expect do
post "/voucher_adjustments", params:
end.to change { order.reload.all_adjustments.payment_fee.count }.from(1).to(0)
end
end
context "with a VINE voucher", feature: :connected_apps do
let(:vine_voucher_validator) { instance_double(Vine::VoucherValidatorService) }
before do
allow(Vine::VoucherValidatorService).to receive(:new).and_return(vine_voucher_validator)
end
context "with a new voucher" do
let(:params) { { order: { voucher_code: vine_voucher_code } } }
let(:vine_voucher_code) { "PQ3187" }
context "with a valid voucher" do
it "verifies the voucher with VINE API" do
expect(vine_voucher_validator).to receive(:validate)
allow(vine_voucher_validator).to receive(:errors).and_return({})
post "/voucher_adjustments", params:
end
it "adds a voucher to the user's current order" do
vine_voucher = create(:vine_voucher, code: vine_voucher_code)
mock_vine_voucher_validator(voucher: vine_voucher)
post("/voucher_adjustments", params:)
expect(response).to be_successful
expect(order.reload.voucher_adjustments.length).to eq(1)
end
end
context "when coordinator is not connected to VINE" do
it "returns 422 and an error message" do
mock_vine_voucher_validator(voucher: nil)
post("/voucher_adjustments", params:)
expect(response).to be_unprocessable
expect(flash[:error]).to match "Voucher code Not found"
end
end
context "when there is an API error" do
it "returns 422 and an error message" do
mock_vine_voucher_validator(
voucher: nil,
errors: { vine_api: "There was an error communicating with the API" }
)
post("/voucher_adjustments", params:)
expect(response).to be_unprocessable
expect(flash[:error]).to match "There was an error while adding the voucher"
end
end
context "when the voucher doesn't exist" do
it "returns 422 and an error message" do
mock_vine_voucher_validator(voucher: nil,
errors: { not_found_voucher: "The voucher doesn't exist" })
post("/voucher_adjustments", params:)
expect(response).to be_unprocessable
expect(flash[:error]).to match "Voucher code Not found"
end
end
context "when the voucher is invalid voucher" do
it "returns 422 and an error message" do
mock_vine_voucher_validator(voucher: nil,
errors: { invalid_voucher: "The voucher is not valid" })
post("/voucher_adjustments", params:)
expect(response).to be_unprocessable
expect(flash[:error]).to match "There was an error while adding the voucher"
end
end
context "when creating a new voucher fails" do
it "returns 422 and an error message" do
vine_voucher = build(:vine_voucher, code: vine_voucher_code,
enterprise: distributor, amount: "")
mock_vine_voucher_validator(voucher: vine_voucher)
post("/voucher_adjustments", params:)
expect(response).to be_unprocessable
expect(flash[:error]).to match(
"There was an error while creating the voucher: Amount can't be blank and " \
"Amount is not a number"
)
end
end
end
context "with an existing voucher" do
let(:params) { { order: { voucher_code: vine_voucher_code } } }
let(:vine_voucher_code) { "PQ3187" }
it "verify the voucher with VINE API" do
expect(vine_voucher_validator).to receive(:validate)
allow(vine_voucher_validator).to receive(:errors).and_return({})
post "/voucher_adjustments", params:
end
it "adds a voucher to the user's current order" do
vine_voucher = create(:vine_voucher, code: vine_voucher_code,
enterprise: distributor)
mock_vine_voucher_validator(voucher: vine_voucher)
expect {
post("/voucher_adjustments", params:)
}.to change { order.reload.voucher_adjustments.count }.by(1)
expect(response).to be_successful
end
context "when updating the voucher fails" do
it "returns 422 and an error message" do
vine_voucher = build(:vine_voucher, code: vine_voucher_code,
enterprise: distributor, amount: "")
mock_vine_voucher_validator(voucher: vine_voucher)
post("/voucher_adjustments", params:)
expect(response).to be_unprocessable
expect(flash[:error]).to match(
"There was an error while creating the voucher: Amount can't be blank and " \
"Amount is not a number"
)
end
end
end
end
end
describe "DELETE voucher_adjustments/:id" do
let!(:adjustment) { voucher.create_adjustment(voucher.code, order) }
it "deletes the voucher adjustment" do
delete "/voucher_adjustments/#{adjustment.id}"
expect(order.voucher_adjustments.reload.length).to eq(0)
end
it "render a success response" do
delete "/voucher_adjustments/#{adjustment.id}"
expect(response).to be_successful
end
context "when adjustment doesn't exist" do
it "does nothing" do
delete "/voucher_adjustments/-1"
expect(order.voucher_adjustments.reload.length).to eq(1)
end
it "render a success response" do
delete "/voucher_adjustments/-1"
expect(response).to be_successful
end
end
context "when tax excluded from price" do
it "deletes all voucher adjustment" do
# Add a tax adjustment
adjustment_attributes = {
amount: 2.00,
originator: adjustment.originator,
order:,
label: "Tax #{adjustment.label}",
mandatory: false,
state: 'closed',
tax_category: nil,
included_tax: 0
}
order.adjustments.create(adjustment_attributes)
delete "/voucher_adjustments/#{adjustment.id}"
expect(order.voucher_adjustments.reload.length).to eq(0)
end
end
end
def mock_vine_voucher_validator(voucher:, errors: {})
allow(vine_voucher_validator).to receive(:validate).and_return(voucher)
allow(vine_voucher_validator).to receive(:errors).and_return(errors)
end
end