forked from heartcombo/devise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeoutable_test.rb
150 lines (119 loc) · 3.73 KB
/
timeoutable_test.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
require 'test_helper'
class SessionTimeoutTest < ActionDispatch::IntegrationTest
def last_request_at
@controller.user_session['last_request_at']
end
test 'set last request at in user session after each request' do
sign_in_as_user
old_last_request = last_request_at
assert_not_nil last_request_at
get users_path
assert_not_nil last_request_at
assert_not_equal old_last_request, last_request_at
end
test 'set last request at in user session after each request is skipped if tracking is disabled' do
sign_in_as_user
old_last_request = last_request_at
assert_not_nil last_request_at
get users_path, {}, 'devise.skip_trackable' => true
assert_equal old_last_request, last_request_at
end
test 'does not time out user session before default limit time' do
sign_in_as_user
assert_response :success
assert warden.authenticated?(:user)
get users_path
assert_response :success
assert warden.authenticated?(:user)
end
test 'time out user session after default limit time' do
user = sign_in_as_user
get expire_user_path(user)
assert_not_nil last_request_at
get users_path
assert_redirected_to users_path
assert_not warden.authenticated?(:user)
end
test 'time out user session after deault limit time and redirect to latest get request' do
user = sign_in_as_user
visit edit_form_user_path(user)
click_button 'Update'
sign_in_as_user
assert_equal edit_form_user_url(user), current_url
end
test 'time out is not triggered on sign out' do
user = sign_in_as_user
get expire_user_path(user)
get destroy_user_session_path
assert_response :redirect
assert_redirected_to root_path
follow_redirect!
assert_contain 'Signed out successfully'
end
test 'time out is not triggered on sign in' do
user = sign_in_as_user
get expire_user_path(user)
post "/users/sign_in", :email => user.email, :password => "123456"
assert_response :redirect
follow_redirect!
assert_contain 'You are signed in'
end
test 'admin does not explode on time out' do
admin = sign_in_as_admin
get expire_admin_path(admin)
Admin.send :define_method, :reset_authentication_token! do
nil
end
begin
get admins_path
assert_redirected_to admins_path
assert_not warden.authenticated?(:admin)
ensure
Admin.send(:remove_method, :reset_authentication_token!)
end
end
test 'user configured timeout limit' do
swap Devise, :timeout_in => 8.minutes do
user = sign_in_as_user
get users_path
assert_not_nil last_request_at
assert_response :success
assert warden.authenticated?(:user)
get expire_user_path(user)
get users_path
assert_redirected_to users_path
assert_not warden.authenticated?(:user)
end
end
test 'error message with i18n' do
store_translations :en, :devise => {
:failure => { :user => { :timeout => 'Session expired!' } }
} do
user = sign_in_as_user
get expire_user_path(user)
get root_path
follow_redirect!
assert_contain 'Session expired!'
end
end
test 'error message with i18n with double redirect' do
store_translations :en, :devise => {
:failure => { :user => { :timeout => 'Session expired!' } }
} do
user = sign_in_as_user
get expire_user_path(user)
get users_path
follow_redirect!
follow_redirect!
assert_contain 'Session expired!'
end
end
test 'time out not triggered if remembered' do
user = sign_in_as_user :remember_me => true
get expire_user_path(user)
assert_not_nil last_request_at
get users_path
assert_response :success
assert warden.authenticated?(:user)
end
end