forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkout_spec.rb
196 lines (165 loc) · 6.76 KB
/
checkout_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
require 'spec_helper'
module Spree
describe 'Api Feature Specs', type: :request do
before { Spree::Api::Config[:requires_authentication] = false }
let!(:promotion) { FactoryBot.create(:promotion, :with_order_adjustment, code: 'foo', weighted_order_adjustment_amount: 10) }
let(:promotion_code) { promotion.codes.first }
let!(:store) { FactoryBot.create(:store) }
let(:bill_address) { FactoryBot.create(:address) }
let(:ship_address) { FactoryBot.create(:address) }
let(:variant_1) { FactoryBot.create(:variant, price: 100.00) }
let(:variant_2) { FactoryBot.create(:variant, price: 200.00) }
let(:payment_method) { FactoryBot.create(:check_payment_method) }
let!(:shipping_method) do
FactoryBot.create(:shipping_method).tap do |shipping_method|
shipping_method.zones.first.zone_members.create!(zoneable: ship_address.country)
shipping_method.calculator.set_preference(:amount, 10.0)
end
end
def parsed
JSON.parse(response.body)
end
def login
expect {
post '/api/users', params: {
user: {
email: "[email protected]",
password: "featurecheckoutuser"
}
}
}.to change { Spree.user_class.count }.by 1
expect(response).to have_http_status(:created)
@user = Spree.user_class.find(parsed['id'])
# copied from api testing helpers support since we can't really sign in
allow(Spree::LegacyUser).to receive(:find_by).with(hash_including(:spree_api_key)) { @user }
end
def create_order(order_params: {})
expect { post '/api/orders', params: order_params }.to change { Order.count }.by 1
expect(response).to have_http_status(:created)
@order = Order.find(parsed['id'])
expect(@order.email).to eq "[email protected]"
end
def update_order(order_params: {})
put "/api/orders/#{@order.number}", params: order_params
expect(response).to have_http_status(:ok)
end
def create_line_item(variant, quantity = 1)
expect {
post "/api/orders/#{@order.number}/line_items",
params: { line_item: { variant_id: variant.id, quantity: quantity } }
}.to change { @order.line_items.count }.by 1
expect(response).to have_http_status(:created)
end
def add_promotion(_promotion)
expect {
put "/api/orders/#{@order.number}/apply_coupon_code",
params: { coupon_code: promotion_code.value }
}.to change { @order.promotions.count }.by 1
expect(response).to have_http_status(:ok)
end
def add_address(address, billing: true)
address_type = billing ? :bill_address : :ship_address
# It seems we are missing an order-scoped address api endpoint since we need
# to use update here.
expect {
update_order(order_params: { order: { address_type => address.attributes.except('id') } })
}.to change { @order.reload.public_send(address_type) }.to address
end
def add_payment
expect {
post "/api/orders/#{@order.number}/payments",
params: { payment: { payment_method_id: payment_method.id } }
}.to change { @order.reload.payments.count }.by 1
expect(response).to have_http_status(:created)
expect(@order.payments.last.payment_method).to eq payment_method
end
def advance
put "/api/checkouts/#{@order.number}/advance"
expect(response).to have_http_status(:ok)
end
def complete
put "/api/checkouts/#{@order.number}/complete"
expect(response).to have_http_status(:ok)
end
def assert_order_expectations
@order.reload
expect(@order.state).to eq 'complete'
expect(@order.completed_at).to be_a ActiveSupport::TimeWithZone
expect(@order.item_total).to eq 600.00
expect(@order.total).to eq 600.00
expect(@order.adjustment_total).to eq(-10.00)
expect(@order.shipment_total).to eq 10.00
expect(@order.user).to eq @user
expect(@order.bill_address).to eq bill_address
expect(@order.ship_address).to eq ship_address
expect(@order.payments.length).to eq 1
expect(@order.line_items.any? { |li| li.variant == variant_1 && li.quantity == 2 }).to eq true
expect(@order.line_items.any? { |li| li.variant == variant_2 && li.quantity == 2 }).to eq true
expect(@order.promotions).to eq [promotion]
end
it "is able to checkout with individualized requests" do
login
create_order
create_line_item(variant_1, 2)
add_promotion(promotion)
create_line_item(variant_2, 2)
add_address(bill_address)
add_address(ship_address, billing: false)
add_payment
advance
complete
assert_order_expectations
end
it "is able to checkout with the create request" do
login
create_order(order_params: {
order: {
bill_address: bill_address.as_json.except('id'),
ship_address: ship_address.as_json.except('id'),
line_items: {
0 => { variant_id: variant_1.id, quantity: 2 },
1 => { variant_id: variant_2.id, quantity: 2 }
},
# Would like to do this, but the save process from the orders controller
# does not actually call what it needs to to apply this coupon code :(
# coupon_code: promotion.code,
# Would like to do this, but it puts the payment in a complete state,
# which the order does not like when transitioning from confirm to complete
# since it looks to process pending payments.
# payments: [ { payment_method: payment_method.name, state: "pending" } ],
}
})
add_promotion(promotion)
add_payment
advance
complete
assert_order_expectations
end
it "is able to checkout with the update request" do
login
create_order
update_order(order_params: {
order: {
bill_address: bill_address.as_json.except('id'),
ship_address: ship_address.as_json.except('id'),
line_items: {
0 => { variant_id: variant_1.id, quantity: 2 },
1 => { variant_id: variant_2.id, quantity: 2 }
},
# Would like to do this, but the save process from the orders controller
# does not actually call what it needs to to apply this coupon code :(
# coupon_code: promotion.code,
# Would like to do this, but it puts the payment in a complete state,
# which the order does not like when transitioning from confirm to complete
# since it looks to process pending payments.
# payments: [ { payment_method: payment_method.name, state: "pending" } ],
}
})
add_promotion(promotion)
add_payment
advance
complete
assert_order_expectations
end
end
end