-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathtwo_factor_authenticatable_spec.rb
More file actions
291 lines (218 loc) · 8.31 KB
/
Copy pathtwo_factor_authenticatable_spec.rb
File metadata and controls
291 lines (218 loc) · 8.31 KB
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
290
291
require 'spec_helper'
include AuthenticatedModelHelper
feature "User of two factor authentication" do
context 'sending two factor authentication code via SMS' do
shared_examples 'sends and authenticates code' do |user, type|
before do
user.reload
if type == 'encrypted'
allow(User).to receive(:has_one_time_password).with(encrypted: true)
end
end
it 'does not send an SMS before the user has signed in' do
expect(SMSProvider.messages).to be_empty
end
it 'sends code via SMS after sign in' do
visit new_user_session_path
complete_sign_in_form_for(user)
expect(page).to have_content 'Enter the code that was sent to you'
expect(SMSProvider.messages.size).to eq(1)
message = SMSProvider.last_message
expect(message.to).to eq(user.phone_number)
expect(message.body).to eq(user.reload.direct_otp)
end
it 'authenticates a valid OTP code' do
visit new_user_session_path
complete_sign_in_form_for(user)
expect(page).to have_content('You are signed in as Marissa')
fill_in 'code', with: SMSProvider.last_message.body
click_button 'Submit'
within('.flash.notice') do
expect(page).to have_content('Two factor authentication successful.')
end
expect(current_path).to eq root_path
end
end
it_behaves_like 'sends and authenticates code', create_user('not_encrypted')
it_behaves_like 'sends and authenticates code', create_user, 'encrypted'
end
scenario "must be logged in" do
visit user_two_factor_authentication_path
expect(page).to have_content("Welcome Home")
expect(page).to have_content("You are signed out")
end
context "when logged in" do
let(:user) { create_user }
background do
login_as user
end
scenario "is redirected to TFA when path requires authentication" do
visit dashboard_path + "?A=param%20a&B=param%20b"
expect(page).to_not have_content("Your Personal Dashboard")
fill_in "code", with: SMSProvider.last_message.body
click_button "Submit"
expect(page).to have_content("Your Personal Dashboard")
expect(page).to have_content("You are signed in as Marissa")
expect(page).to have_content("Param A is param a")
expect(page).to have_content("Param B is param b")
end
scenario "is redirected to TFA when path requires authentication and works with resent code" do
visit dashboard_path + "?A=param%20a&B=param%20b"
expect(page).to_not have_content("Your Personal Dashboard")
SMSProvider.messages.clear()
click_on "Resend Code"
fill_in "code", with: SMSProvider.last_message.body
click_button "Submit"
expect(page).to have_content("Your Personal Dashboard")
expect(page).to have_content("You are signed in as Marissa")
expect(page).to have_content("Param A is param a")
expect(page).to have_content("Param B is param b")
end
scenario "is locked out after max failed attempts" do
visit user_two_factor_authentication_path
max_attempts = User.max_login_attempts
max_attempts.times do
fill_in "code", with: "incorrect#{rand(100)}"
click_button "Submit"
within(".flash.alert") do
expect(page).to have_content("Attempt failed")
end
end
expect(page).to have_content("Access completely denied")
expect(page).to have_content("You are signed out")
end
scenario "cannot retry authentication after max attempts" do
user.update_attribute(:second_factor_attempts_count, User.max_login_attempts)
visit user_two_factor_authentication_path
expect(page).to have_content("Access completely denied")
expect(page).to have_content("You are signed out")
end
describe "rememberable TFA" do
before do
@original_remember_otp_session_for_seconds = User.remember_otp_session_for_seconds
User.remember_otp_session_for_seconds = 30.days
end
after do
User.remember_otp_session_for_seconds = @original_remember_otp_session_for_seconds
end
scenario "doesn't require TFA code again within 30 days" do
sms_sign_in
logout
login_as user
visit dashboard_path
expect(page).to have_content("Your Personal Dashboard")
expect(page).to have_content("You are signed in as Marissa")
end
scenario "requires TFA code again after 30 days" do
sms_sign_in
logout
Timecop.travel(30.days.from_now)
login_as user
visit dashboard_path
expect(page).to have_content("You are signed in as Marissa")
expect(page).to have_content("Enter the code that was sent to you")
end
scenario 'Sends OTP code by SMS' do
login_as user
SMSProvider.messages.clear()
visit dashboard_path
expect(SMSProvider.messages).not_to be_empty
end
scenario "Doesn't sends OTP code by SMS upon every request if so configured" do
login_as user
SMSProvider.messages.clear()
allow(Rails.application.config.devise).to receive(:skip_send_new_otp_in_after_set_user_for).and_return([:user])
visit dashboard_path
expect(SMSProvider.messages).to be_empty
end
scenario 'TFA should be different for different users' do
sms_sign_in
tfa_cookie1 = get_tfa_cookie()
logout
reset_session!
user2 = create_user()
login_as(user2)
sms_sign_in
tfa_cookie2 = get_tfa_cookie()
expect(tfa_cookie1).not_to eq tfa_cookie2
end
def sms_sign_in
SMSProvider.messages.clear()
visit user_two_factor_authentication_path
fill_in 'code', with: SMSProvider.last_message.body
click_button 'Submit'
end
def sms_sign_in_with_resent_code
visit user_two_factor_authentication_path
SMSProvider.messages.clear()
click_on 'Resend Code'
fill_in 'code', with: SMSProvider.last_message.body
click_button 'Submit'
end
scenario 'TFA should be unique for specific user' do
sms_sign_in
tfa_cookie1 = get_tfa_cookie()
logout
reset_session!
user2 = create_user()
set_tfa_cookie(tfa_cookie1)
login_as(user2)
visit dashboard_path
expect(page).to have_content("Enter the code that was sent to you")
end
scenario 'TFA should be unique for specific user with resent code' do
sms_sign_in_with_resent_code
tfa_cookie1 = get_tfa_cookie()
logout
reset_session!
user2 = create_user()
set_tfa_cookie(tfa_cookie1)
login_as(user2)
visit dashboard_path
expect(page).to have_content("Enter the code that was sent to you")
end
scenario 'Delete cookie when user logs out if enabled' do
user.class.delete_cookie_on_logout = true
login_as user
logout
login_as user
visit dashboard_path
expect(page).to have_content("Enter the code that was sent to you")
end
end
it 'sets the warden session need_two_factor_authentication key to true' do
session_hash = { 'need_two_factor_authentication' => true }
expect(page.get_rack_session_key('warden.user.user.session')).to eq session_hash
end
end
describe 'signing in' do
let(:user) { create_user }
let(:admin) { create_admin }
scenario 'user signs is' do
visit new_user_session_path
complete_sign_in_form_for(user)
expect(page).to have_content('Signed in successfully.')
end
scenario 'admin signs in' do
visit new_admin_session_path
complete_sign_in_form_for(admin)
expect(page).to have_content('Signed in successfully.')
end
end
describe 'signing out' do
let(:user) { create_user }
let(:admin) { create_admin }
scenario 'user signs out' do
visit new_user_session_path
complete_sign_in_form_for(user)
visit destroy_user_session_path
expect(page).to have_content('Signed out successfully.')
end
scenario 'admin signs out' do
visit new_admin_session_path
complete_sign_in_form_for(admin)
visit destroy_admin_session_path
expect(page).to have_content('Signed out successfully.')
end
end
end