-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
state_machine_spec.rb
206 lines (174 loc) · 5.84 KB
/
state_machine_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
require 'spec_helper'
describe Spree::Order do
let(:order) { Spree::Order.new }
before do
# Ensure state machine has been re-defined correctly
Spree::Order.define_state_machine!
# We don't care about this validation here
order.stub(:require_email)
end
context "#next!" do
context "when current state is confirm" do
before do
order.state = "confirm"
order.run_callbacks(:create)
order.stub :payment_required? => true
order.stub :process_payments!
order.stub :has_available_shipment
end
context "with unprocessed payments" do
before do
order.stub :has_unprocessed_payments? => true
end
it "should finalize order when transitioning to complete state" do
order.should_receive(:finalize!)
order.next!
end
context "when credit card payment fails" do
before do
order.stub(:process_payments!).and_raise(Spree::Core::GatewayError)
end
context "when not configured to allow failed payments" do
before do
Spree::Config.set :allow_checkout_on_gateway_error => false
end
it "should not complete the order" do
order.next
order.state.should == "confirm"
end
end
context "when configured to allow failed payments" do
before do
Spree::Config.set :allow_checkout_on_gateway_error => true
order.stub :finalize!
end
it "should complete the order" do
order.next!
order.state.should == "complete"
end
end
end
end
context "with no unprocessed payments" do
before do
order.stub :has_unprocessed_payments? => false
end
it "cannot transition to complete" do
order.next
order.state.should == "confirm"
end
end
end
context "when current state is address" do
before do
order.stub(:has_available_payment)
order.state = "address"
end
it "adjusts tax rates when transitioning to delivery" do
# Once because the record is being saved
# Twice because it is transitioning to the delivery state
Spree::TaxRate.should_receive(:adjust).twice
order.next!
end
end
context "when current state is delivery" do
before do
order.state = "delivery"
order.stub :total => 10.0
end
end
end
context "#can_cancel?" do
%w(pending backorder ready).each do |shipment_state|
it "should be true if shipment_state is #{shipment_state}" do
order.stub :completed? => true
order.shipment_state = shipment_state
order.can_cancel?.should be_true
end
end
(Spree::Shipment.state_machine.states.keys - %w(pending backorder ready)).each do |shipment_state|
it "should be false if shipment_state is #{shipment_state}" do
order.stub :completed? => true
order.shipment_state = shipment_state
order.can_cancel?.should be_false
end
end
end
context "#cancel" do
let!(:variant) { stub_model(Spree::Variant) }
let!(:inventory_units) { [stub_model(Spree::InventoryUnit, :variant => variant),
stub_model(Spree::InventoryUnit, :variant => variant) ]}
let!(:shipment) do
shipment = stub_model(Spree::Shipment)
shipment.stub :inventory_units => inventory_units
order.stub :shipments => [shipment]
shipment
end
before do
order.stub :line_items => [stub_model(Spree::LineItem, :variant => variant, :quantity => 2)]
order.line_items.stub :find_by_variant_id => order.line_items.first
order.stub :completed? => true
order.stub :allow_cancel? => true
end
it "should send a cancel email" do
# Stub methods that cause side-effects in this test
shipment.stub(:cancel!)
order.stub :has_available_shipment
order.stub :restock_items!
mail_message = mock "Mail::Message"
order_id = nil
Spree::OrderMailer.should_receive(:cancel_email) { |*args|
order_id = args[0]
mail_message
}
mail_message.should_receive :deliver
order.cancel!
order_id.should == order.id
end
context "restocking inventory" do
before do
shipment.stub(:ensure_correct_adjustment)
shipment.stub(:update_order)
Spree::OrderMailer.stub(:cancel_email).and_return(mail_message = stub)
mail_message.stub :deliver
order.stub :has_available_shipment
end
end
context "resets payment state" do
before do
# TODO: This is ugly :(
# Stubs methods that cause unwanted side effects in this test
Spree::OrderMailer.stub(:cancel_email).and_return(mail_message = stub)
mail_message.stub :deliver
order.stub :has_available_shipment
order.stub :restock_items!
shipment.stub(:cancel!)
end
context "without shipped items" do
it "should set payment state to 'credit owed'" do
order.cancel!
order.payment_state.should == 'credit_owed'
end
end
context "with shipped items" do
before do
order.stub :shipment_state => 'partial'
end
it "should not alter the payment state" do
order.cancel!
order.payment_state.should be_nil
end
end
end
end
# Another regression test for #729
context "#resume" do
before do
order.stub :email => "user@spreecommerce.com"
order.stub :state => "canceled"
order.stub :allow_resume? => true
# Stubs method that cause unwanted side effects in this test
order.stub :has_available_shipment
end
end
end