forked from solidusio/solidus_auth_devise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout_controller_spec.rb
196 lines (163 loc) · 6.01 KB
/
checkout_controller_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
# frozen_string_literal: true
RSpec.describe Spree::CheckoutController, type: :controller do
let(:order) { create(:order_with_line_items, email: nil, user: nil, guest_token: token) }
let(:user) { build(:user, spree_api_key: 'fake') }
let(:token) { 'some_token' }
let(:cookie_token) { token }
before do
request.cookie_jar.signed[:guest_token] = cookie_token
allow(controller).to receive(:current_order) { order }
allow(order).to receive(:confirmation_required?) { true }
end
context '#edit' do
context 'when registration step enabled' do
context 'when authenticated as registered user' do
before { allow(controller).to receive(:spree_current_user) { user } }
it 'proceeds to the first checkout step' do
get :edit, params: { state: 'address' }
expect(response).to render_template :edit
end
end
context 'when not authenticated as guest' do
it 'redirects to registration step' do
get :edit, params: { state: 'address' }
expect(response).to redirect_to spree.checkout_registration_path
end
end
context 'when authenticated as guest' do
before { order.email = 'guest@solidus.io' }
it 'proceeds to the first checkout step' do
get :edit, params: { state: 'address' }
expect(response).to render_template :edit
end
context 'when guest checkout not allowed' do
before do
Spree::Config.set(allow_guest_checkout: false)
end
after do
Spree::Config.set(allow_guest_checkout: true)
end
it 'redirects to registration step' do
get :edit, params: { state: 'address' }
expect(response).to redirect_to spree.checkout_registration_path
end
end
end
end
context 'when registration step disabled' do
before do
Spree::Auth::Config.set(registration_step: false)
end
context 'when authenticated as registered' do
before { allow(controller).to receive(:spree_current_user) { user } }
it 'proceeds to the first checkout step' do
get :edit, params: { state: 'address' }
expect(response).to render_template :edit
end
end
context 'when authenticated as guest' do
it 'proceeds to the first checkout step' do
get :edit, params: { state: 'address' }
expect(response).to render_template :edit
end
end
end
end
context '#update' do
context 'when in the confirm state' do
before do
order.update(email: 'spree@example.com', state: 'confirm')
# So that the order can transition to complete successfully
allow(order).to receive(:payment_required?) { false }
end
context 'with a token' do
before { allow(order).to receive(:guest_token) { 'ABC' } }
it 'redirects to the tokenized order view' do
request.cookie_jar.signed[:guest_token] = 'ABC'
post :update, params: { state: 'confirm' }
expect(response).to redirect_to spree.token_order_path(order, 'ABC')
expect(flash.notice).to eq I18n.t('spree.order_processed_successfully')
end
end
context 'with a registered user' do
before do
allow(controller).to receive(:spree_current_user) { user }
allow(order).to receive(:user) { user }
allow(order).to receive(:guest_token) { nil }
end
it 'redirects to the standard order view' do
post :update, params: { state: 'confirm' }
expect(response).to redirect_to spree.order_path(order)
end
end
end
end
context '#registration' do
it 'does not check registration' do
expect(controller).not_to receive(:check_registration)
get :registration
end
it 'checks if the user is authorized for :edit' do
expect(controller).to receive(:authorize!).with(:edit, order, token)
request.cookie_jar.signed[:guest_token] = token
get :registration, params: {}
end
end
context '#update_registration' do
subject { put :update_registration, params: { order: { email: email } } }
let(:email) { 'foo@example.com' }
it 'does not check registration' do
expect(controller).not_to receive(:check_registration)
subject
end
it 'redirects to the checkout_path after saving' do
subject
expect(response).to redirect_to spree.checkout_path
end
# Regression test for https://github.com/solidusio/solidus/issues/1588
context 'order in address state' do
let(:order) do
create(
:order_with_line_items,
email: nil,
user: nil,
guest_token: token,
bill_address: nil,
ship_address: nil,
state: 'address'
)
end
# This may seem out of left field, but previously there was an issue
# where address would be built in a before filter and then would be saved
# when trying to update the email.
it "doesn't create addresses" do
expect {
subject
}.not_to change { Spree::Address.count }
expect(response).to redirect_to spree.checkout_path
end
end
context 'invalid email' do
let(:email) { 'invalid' }
it 'renders the registration view' do
subject
expect(flash[:registration_error]).to eq I18n.t(:email_is_invalid, scope: [:errors, :messages])
expect(response).to render_template :registration
end
end
context 'with wrong order token' do
let(:cookie_token) { 'lol_no_access' }
it 'redirects to login' do
put :update_registration, params: { order: { email: 'foo@example.com' } }
expect(response).to redirect_to(login_path)
end
end
context 'without order token' do
let(:cookie_token) { nil }
it 'redirects to login' do
put :update_registration, params: { order: { email: 'foo@example.com' } }
expect(response).to redirect_to(login_path)
end
end
end
end