devise 1.1.rc1 → 1.1.rc2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of devise might be problematic. Click here for more details.
- data/CHANGELOG.rdoc +63 -27
- data/Gemfile +15 -13
- data/README.rdoc +63 -51
- data/Rakefile +3 -2
- data/TODO +1 -0
- data/app/controllers/devise/registrations_controller.rb +1 -1
- data/app/mailers/devise/mailer.rb +43 -43
- data/app/views/devise/confirmations/new.html.erb +2 -2
- data/app/views/devise/passwords/edit.html.erb +4 -4
- data/app/views/devise/passwords/new.html.erb +2 -2
- data/app/views/devise/registrations/edit.html.erb +8 -8
- data/app/views/devise/registrations/new.html.erb +6 -6
- data/app/views/devise/sessions/new.html.erb +4 -4
- data/app/views/devise/unlocks/new.html.erb +2 -2
- data/config/locales/en.yml +7 -4
- data/lib/devise.rb +33 -6
- data/lib/devise/controllers/helpers.rb +38 -2
- data/lib/devise/encryptors/authlogic_sha512.rb +0 -2
- data/lib/devise/encryptors/bcrypt.rb +0 -2
- data/lib/devise/encryptors/clearance_sha1.rb +0 -2
- data/lib/devise/encryptors/sha1.rb +6 -8
- data/lib/devise/encryptors/sha512.rb +6 -8
- data/lib/devise/failure_app.rb +3 -2
- data/lib/devise/hooks/activatable.rb +4 -1
- data/lib/devise/hooks/forgetable.rb +4 -3
- data/lib/devise/hooks/rememberable.rb +6 -2
- data/lib/devise/hooks/timeoutable.rb +6 -2
- data/lib/devise/mapping.rb +7 -8
- data/lib/devise/models.rb +0 -34
- data/lib/devise/models/authenticatable.rb +29 -3
- data/lib/devise/models/confirmable.rb +3 -2
- data/lib/devise/models/database_authenticatable.rb +4 -2
- data/lib/devise/models/lockable.rb +1 -1
- data/lib/devise/models/recoverable.rb +1 -1
- data/lib/devise/models/rememberable.rb +9 -1
- data/lib/devise/orm/active_record.rb +4 -6
- data/lib/devise/orm/data_mapper.rb +5 -7
- data/lib/devise/orm/mongoid.rb +2 -13
- data/lib/devise/path_checker.rb +13 -0
- data/lib/devise/rails.rb +45 -18
- data/lib/devise/rails/routes.rb +24 -7
- data/lib/devise/schema.rb +23 -19
- data/lib/devise/strategies/authenticatable.rb +20 -4
- data/lib/devise/strategies/database_authenticatable.rb +1 -1
- data/lib/devise/strategies/token_authenticatable.rb +2 -2
- data/lib/devise/test_helpers.rb +2 -1
- data/lib/devise/version.rb +1 -1
- data/lib/generators/devise/devise/devise_generator.rb +86 -0
- data/lib/generators/devise/{templates → devise/templates}/migration.rb +2 -2
- data/lib/generators/devise/install/install_generator.rb +24 -0
- data/lib/generators/{devise_install → devise/install}/templates/README +1 -1
- data/lib/generators/{devise_install → devise/install}/templates/devise.rb +37 -18
- data/lib/generators/devise/views/views_generator.rb +63 -0
- data/lib/generators/devise_generator.rb +2 -0
- data/lib/generators/devise_install_generator.rb +4 -0
- data/lib/generators/devise_views_generator.rb +4 -0
- data/test/controllers/helpers_test.rb +15 -0
- data/test/devise_test.rb +1 -0
- data/test/failure_app_test.rb +25 -10
- data/test/integration/authenticatable_test.rb +279 -0
- data/test/integration/database_authenticatable_test.rb +2 -262
- data/test/integration/http_authenticatable_test.rb +7 -9
- data/test/integration/registerable_test.rb +14 -2
- data/test/integration/rememberable_test.rb +15 -3
- data/test/integration/timeoutable_test.rb +12 -0
- data/test/integration/token_authenticatable_test.rb +2 -3
- data/test/mailers/confirmation_instructions_test.rb +2 -2
- data/test/mailers/reset_password_instructions_test.rb +2 -2
- data/test/mailers/unlock_instructions_test.rb +3 -3
- data/test/models/confirmable_test.rb +16 -0
- data/test/models/database_authenticatable_test.rb +20 -20
- data/test/models/lockable_test.rb +1 -1
- data/test/models/rememberable_test.rb +4 -4
- data/test/orm/data_mapper.rb +9 -0
- data/test/rails_app/app/active_record/shim.rb +2 -0
- data/test/rails_app/app/controllers/application_controller.rb +1 -0
- data/test/rails_app/app/controllers/home_controller.rb +3 -0
- data/test/rails_app/app/controllers/users_controller.rb +2 -0
- data/test/rails_app/app/data_mapper/shim.rb +2 -0
- data/test/rails_app/app/data_mapper/user.rb +4 -5
- data/test/rails_app/app/mongoid/admin.rb +1 -10
- data/test/rails_app/app/mongoid/shim.rb +16 -0
- data/test/rails_app/app/mongoid/user.rb +1 -12
- data/test/rails_app/config/application.rb +2 -0
- data/test/rails_app/config/initializers/devise.rb +2 -2
- data/test/rails_app/config/routes.rb +6 -1
- data/test/routes_test.rb +2 -2
- data/test/test_helper.rb +0 -4
- data/test/test_helpers_test.rb +1 -0
- metadata +113 -12
- data/lib/generators/devise/devise_generator.rb +0 -67
- data/lib/generators/devise_install/devise_install_generator.rb +0 -25
- data/lib/generators/devise_views/devise_views_generator.rb +0 -62
@@ -53,6 +53,13 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
|
53
53
|
@controller.signed_in?(:my_scope)
|
54
54
|
end
|
55
55
|
|
56
|
+
test 'proxy anybody_signed_in? to signed_in?' do
|
57
|
+
Devise.mappings.keys.each { |scope| # :user, :admin, :manager
|
58
|
+
@controller.expects(:signed_in?).with(scope)
|
59
|
+
}
|
60
|
+
@controller.anybody_signed_in?
|
61
|
+
end
|
62
|
+
|
56
63
|
test 'proxy current_admin to authenticate with admin scope' do
|
57
64
|
@mock_warden.expects(:authenticate).with(:scope => :admin)
|
58
65
|
@controller.current_admin
|
@@ -145,6 +152,14 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
|
145
152
|
assert_equal admin_root_path, @controller.after_sign_in_path_for(:admin)
|
146
153
|
end
|
147
154
|
|
155
|
+
test 'after update path defaults to root path if none by was specified for the given scope' do
|
156
|
+
assert_equal root_path, @controller.after_update_path_for(:user)
|
157
|
+
end
|
158
|
+
|
159
|
+
test 'after update path defaults to the scoped root path' do
|
160
|
+
assert_equal admin_root_path, @controller.after_update_path_for(:admin)
|
161
|
+
end
|
162
|
+
|
148
163
|
test 'after sign out path defaults to the root path' do
|
149
164
|
assert_equal root_path, @controller.after_sign_out_path_for(:admin)
|
150
165
|
assert_equal root_path, @controller.after_sign_out_path_for(:user)
|
data/test/devise_test.rb
CHANGED
data/test/failure_app_test.rb
CHANGED
@@ -8,11 +8,12 @@ class FailureTest < ActiveSupport::TestCase
|
|
8
8
|
|
9
9
|
def call_failure(env_params={})
|
10
10
|
env = {
|
11
|
-
'warden.options' => { :scope => :user },
|
12
11
|
'REQUEST_URI' => 'http://test.host/',
|
13
12
|
'HTTP_HOST' => 'test.host',
|
14
13
|
'REQUEST_METHOD' => 'GET',
|
14
|
+
'warden.options' => { :scope => :user },
|
15
15
|
'rack.session' => {},
|
16
|
+
'action_dispatch.request.formats' => Array(env_params.delete('formats') || :html),
|
16
17
|
'rack.input' => "",
|
17
18
|
'warden' => OpenStruct.new(:message => nil)
|
18
19
|
}.merge!(env_params)
|
@@ -21,11 +22,6 @@ class FailureTest < ActiveSupport::TestCase
|
|
21
22
|
@request = ActionDispatch::Request.new(env)
|
22
23
|
end
|
23
24
|
|
24
|
-
def call_failure_with_http(env_params={})
|
25
|
-
env = { "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("foo:bar")}" }
|
26
|
-
call_failure(env_params.merge!(env))
|
27
|
-
end
|
28
|
-
|
29
25
|
context 'When redirecting' do
|
30
26
|
test 'return 302 status' do
|
31
27
|
call_failure
|
@@ -61,22 +57,41 @@ class FailureTest < ActiveSupport::TestCase
|
|
61
57
|
assert_match /redirected/, @response.last.body
|
62
58
|
assert_match /users\/sign_in/, @response.last.body
|
63
59
|
end
|
60
|
+
|
61
|
+
test 'works for any navigational format' do
|
62
|
+
swap Devise, :navigational_formats => [:xml] do
|
63
|
+
call_failure('formats' => :xml)
|
64
|
+
assert_equal 302, @response.first
|
65
|
+
end
|
66
|
+
end
|
64
67
|
end
|
65
68
|
|
66
69
|
context 'For HTTP request' do
|
67
70
|
test 'return 401 status' do
|
68
|
-
|
71
|
+
call_failure('formats' => :xml)
|
69
72
|
assert_equal 401, @response.first
|
70
73
|
end
|
71
74
|
|
72
75
|
test 'return WWW-authenticate headers' do
|
73
|
-
|
76
|
+
call_failure('formats' => :xml)
|
74
77
|
assert_equal 'Basic realm="Application"', @response.second["WWW-Authenticate"]
|
75
78
|
end
|
76
79
|
|
77
80
|
test 'uses the proxy failure message as response body' do
|
78
|
-
|
79
|
-
|
81
|
+
call_failure('formats' => :xml, 'warden' => OpenStruct.new(:message => :invalid))
|
82
|
+
assert_match '<error>Invalid email or password.</error>', @response.third.body
|
83
|
+
end
|
84
|
+
|
85
|
+
test 'works for any non navigational format' do
|
86
|
+
swap Devise, :navigational_formats => [] do
|
87
|
+
call_failure('formats' => :html)
|
88
|
+
assert_equal 401, @response.first
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
test 'works for xml http requests' do
|
93
|
+
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
94
|
+
assert_equal 401, @response.first
|
80
95
|
end
|
81
96
|
end
|
82
97
|
|
@@ -0,0 +1,279 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AuthenticationSanityTest < ActionController::IntegrationTest
|
4
|
+
test 'home should be accessible without sign in' do
|
5
|
+
visit '/'
|
6
|
+
assert_response :success
|
7
|
+
assert_template 'home/index'
|
8
|
+
end
|
9
|
+
|
10
|
+
test 'sign in as user should not authenticate admin scope' do
|
11
|
+
sign_in_as_user
|
12
|
+
|
13
|
+
assert warden.authenticated?(:user)
|
14
|
+
assert_not warden.authenticated?(:admin)
|
15
|
+
end
|
16
|
+
|
17
|
+
test 'sign in as admin should not authenticate user scope' do
|
18
|
+
sign_in_as_admin
|
19
|
+
|
20
|
+
assert warden.authenticated?(:admin)
|
21
|
+
assert_not warden.authenticated?(:user)
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'sign in as both user and admin at same time' do
|
25
|
+
sign_in_as_user
|
26
|
+
sign_in_as_admin
|
27
|
+
|
28
|
+
assert warden.authenticated?(:user)
|
29
|
+
assert warden.authenticated?(:admin)
|
30
|
+
end
|
31
|
+
|
32
|
+
test 'sign out as user should not touch admin authentication' do
|
33
|
+
sign_in_as_user
|
34
|
+
sign_in_as_admin
|
35
|
+
|
36
|
+
get destroy_user_session_path
|
37
|
+
assert_not warden.authenticated?(:user)
|
38
|
+
assert warden.authenticated?(:admin)
|
39
|
+
end
|
40
|
+
|
41
|
+
test 'sign out as admin should not touch user authentication' do
|
42
|
+
sign_in_as_user
|
43
|
+
sign_in_as_admin
|
44
|
+
|
45
|
+
get destroy_admin_session_path
|
46
|
+
assert_not warden.authenticated?(:admin)
|
47
|
+
assert warden.authenticated?(:user)
|
48
|
+
end
|
49
|
+
|
50
|
+
test 'not signed in as admin should not be able to access admins actions' do
|
51
|
+
get admins_path
|
52
|
+
|
53
|
+
assert_redirected_to new_admin_session_path
|
54
|
+
assert_not warden.authenticated?(:admin)
|
55
|
+
end
|
56
|
+
|
57
|
+
test 'not signed in as admin should not be able to access private route restricted to admins' do
|
58
|
+
get private_path
|
59
|
+
|
60
|
+
assert_redirected_to new_admin_session_path
|
61
|
+
assert_not warden.authenticated?(:admin)
|
62
|
+
end
|
63
|
+
|
64
|
+
test 'signed in as user should not be able to access private route restricted to admins' do
|
65
|
+
sign_in_as_user
|
66
|
+
assert warden.authenticated?(:user)
|
67
|
+
assert_not warden.authenticated?(:admin)
|
68
|
+
|
69
|
+
get private_path
|
70
|
+
assert_redirected_to new_admin_session_path
|
71
|
+
end
|
72
|
+
|
73
|
+
test 'signed in as admin should be able to access private route restricted to admins' do
|
74
|
+
sign_in_as_admin
|
75
|
+
assert warden.authenticated?(:admin)
|
76
|
+
assert_not warden.authenticated?(:user)
|
77
|
+
|
78
|
+
get private_path
|
79
|
+
|
80
|
+
assert_response :success
|
81
|
+
assert_template 'home/private'
|
82
|
+
assert_contain 'Private!'
|
83
|
+
end
|
84
|
+
|
85
|
+
test 'signed in as user should not be able to access admins actions' do
|
86
|
+
sign_in_as_user
|
87
|
+
assert warden.authenticated?(:user)
|
88
|
+
assert_not warden.authenticated?(:admin)
|
89
|
+
|
90
|
+
get admins_path
|
91
|
+
assert_redirected_to new_admin_session_path
|
92
|
+
end
|
93
|
+
|
94
|
+
test 'signed in as admin should be able to access admin actions' do
|
95
|
+
sign_in_as_admin
|
96
|
+
assert warden.authenticated?(:admin)
|
97
|
+
assert_not warden.authenticated?(:user)
|
98
|
+
|
99
|
+
get admins_path
|
100
|
+
|
101
|
+
assert_response :success
|
102
|
+
assert_template 'admins/index'
|
103
|
+
assert_contain 'Welcome Admin'
|
104
|
+
end
|
105
|
+
|
106
|
+
test 'authenticated admin should not be able to sign as admin again' do
|
107
|
+
sign_in_as_admin
|
108
|
+
get new_admin_session_path
|
109
|
+
|
110
|
+
assert_response :redirect
|
111
|
+
assert_redirected_to admin_root_path
|
112
|
+
assert warden.authenticated?(:admin)
|
113
|
+
end
|
114
|
+
|
115
|
+
test 'authenticated admin should be able to sign out' do
|
116
|
+
sign_in_as_admin
|
117
|
+
assert warden.authenticated?(:admin)
|
118
|
+
|
119
|
+
get destroy_admin_session_path
|
120
|
+
assert_response :redirect
|
121
|
+
assert_redirected_to root_path
|
122
|
+
|
123
|
+
get root_path
|
124
|
+
assert_contain 'Signed out successfully'
|
125
|
+
assert_not warden.authenticated?(:admin)
|
126
|
+
end
|
127
|
+
|
128
|
+
test 'unauthenticated admin does not set message on sign out' do
|
129
|
+
get destroy_admin_session_path
|
130
|
+
assert_response :redirect
|
131
|
+
assert_redirected_to root_path
|
132
|
+
|
133
|
+
get root_path
|
134
|
+
assert_not_contain 'Signed out successfully'
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class AuthenticationRedirectTest < ActionController::IntegrationTest
|
139
|
+
test 'redirect from warden shows sign in or sign up message' do
|
140
|
+
get admins_path
|
141
|
+
|
142
|
+
warden_path = new_admin_session_path
|
143
|
+
assert_redirected_to warden_path
|
144
|
+
|
145
|
+
get warden_path
|
146
|
+
assert_contain 'You need to sign in or sign up before continuing.'
|
147
|
+
end
|
148
|
+
|
149
|
+
test 'redirect to default url if no other was configured' do
|
150
|
+
sign_in_as_user
|
151
|
+
assert_template 'home/index'
|
152
|
+
assert_nil session[:"user_return_to"]
|
153
|
+
end
|
154
|
+
|
155
|
+
test 'redirect to requested url after sign in' do
|
156
|
+
get users_path
|
157
|
+
assert_redirected_to new_user_session_path
|
158
|
+
assert_equal users_path, session[:"user_return_to"]
|
159
|
+
|
160
|
+
follow_redirect!
|
161
|
+
sign_in_as_user :visit => false
|
162
|
+
|
163
|
+
assert_template 'users/index'
|
164
|
+
assert_nil session[:"user_return_to"]
|
165
|
+
end
|
166
|
+
|
167
|
+
test 'redirect to last requested url overwriting the stored return_to option' do
|
168
|
+
get expire_user_path(create_user)
|
169
|
+
assert_redirected_to new_user_session_path
|
170
|
+
assert_equal expire_user_path(create_user), session[:"user_return_to"]
|
171
|
+
|
172
|
+
get users_path
|
173
|
+
assert_redirected_to new_user_session_path
|
174
|
+
assert_equal users_path, session[:"user_return_to"]
|
175
|
+
|
176
|
+
follow_redirect!
|
177
|
+
sign_in_as_user :visit => false
|
178
|
+
|
179
|
+
assert_template 'users/index'
|
180
|
+
assert_nil session[:"user_return_to"]
|
181
|
+
end
|
182
|
+
|
183
|
+
test 'xml http requests does not store urls for redirect' do
|
184
|
+
get users_path, {}, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
|
185
|
+
assert_equal 401, response.status
|
186
|
+
assert_nil session[:"user_return_to"]
|
187
|
+
end
|
188
|
+
|
189
|
+
test 'redirect to configured home path for a given scope after sign in' do
|
190
|
+
sign_in_as_admin
|
191
|
+
assert_equal "/admin_area/home", @request.path
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
class AuthenticationSessionTest < ActionController::IntegrationTest
|
196
|
+
test 'destroyed account is signed out' do
|
197
|
+
sign_in_as_user
|
198
|
+
get '/users'
|
199
|
+
|
200
|
+
User.destroy_all
|
201
|
+
get '/users'
|
202
|
+
assert_redirected_to new_user_session_path
|
203
|
+
end
|
204
|
+
|
205
|
+
test 'allows session to be set by a given scope' do
|
206
|
+
sign_in_as_user
|
207
|
+
get '/users'
|
208
|
+
assert_equal "Cart", @controller.user_session[:cart]
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
class AuthenticationWithScopesTest < ActionController::IntegrationTest
|
213
|
+
test 'renders the scoped view if turned on and view is available' do
|
214
|
+
swap Devise, :scoped_views => true do
|
215
|
+
assert_raise Webrat::NotFoundError do
|
216
|
+
sign_in_as_user
|
217
|
+
end
|
218
|
+
assert_match /Special user view/, response.body
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
test 'renders the scoped view if turned on in an specific controller' do
|
223
|
+
begin
|
224
|
+
Devise::SessionsController.scoped_views = true
|
225
|
+
assert_raise Webrat::NotFoundError do
|
226
|
+
sign_in_as_user
|
227
|
+
end
|
228
|
+
|
229
|
+
assert_match /Special user view/, response.body
|
230
|
+
assert !Devise::PasswordsController.scoped_views?
|
231
|
+
ensure
|
232
|
+
Devise::SessionsController.send :remove_instance_variable, :@scoped_views
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
test 'does not render the scoped view if turned off' do
|
237
|
+
swap Devise, :scoped_views => false do
|
238
|
+
assert_nothing_raised do
|
239
|
+
sign_in_as_user
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
test 'does not render the scoped view if not available' do
|
245
|
+
swap Devise, :scoped_views => true do
|
246
|
+
assert_nothing_raised do
|
247
|
+
sign_in_as_admin
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
test 'uses the mapping from the default scope if specified' do
|
253
|
+
swap Devise, :use_default_scope => true do
|
254
|
+
get '/sign_in'
|
255
|
+
assert_response :ok
|
256
|
+
assert_contain 'Sign in'
|
257
|
+
end
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
class AuthenticationOthersTest < ActionController::IntegrationTest
|
262
|
+
test 'uses the custom controller with the custom controller view' do
|
263
|
+
get '/admin_area/sign_in'
|
264
|
+
assert_contain 'Sign in'
|
265
|
+
assert_contain 'Welcome to "sessions" controller!'
|
266
|
+
assert_contain 'Welcome to "sessions/new" view!'
|
267
|
+
end
|
268
|
+
|
269
|
+
test 'render 404 on roles without routes' do
|
270
|
+
get '/admin_area/password/new'
|
271
|
+
assert_equal 404, response.status
|
272
|
+
end
|
273
|
+
|
274
|
+
test 'render 404 on roles without mapping' do
|
275
|
+
assert_raise AbstractController::ActionNotFound do
|
276
|
+
get '/sign_in'
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
@@ -1,113 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class
|
4
|
-
test 'home should be accessible without sign in' do
|
5
|
-
visit '/'
|
6
|
-
assert_response :success
|
7
|
-
assert_template 'home/index'
|
8
|
-
end
|
9
|
-
|
10
|
-
test 'sign in as user should not authenticate admin scope' do
|
11
|
-
sign_in_as_user
|
12
|
-
|
13
|
-
assert warden.authenticated?(:user)
|
14
|
-
assert_not warden.authenticated?(:admin)
|
15
|
-
end
|
16
|
-
|
17
|
-
test 'sign in as admin should not authenticate user scope' do
|
18
|
-
sign_in_as_admin
|
19
|
-
|
20
|
-
assert warden.authenticated?(:admin)
|
21
|
-
assert_not warden.authenticated?(:user)
|
22
|
-
end
|
23
|
-
|
24
|
-
test 'sign in as both user and admin at same time' do
|
25
|
-
sign_in_as_user
|
26
|
-
sign_in_as_admin
|
27
|
-
|
28
|
-
assert warden.authenticated?(:user)
|
29
|
-
assert warden.authenticated?(:admin)
|
30
|
-
end
|
31
|
-
|
32
|
-
test 'sign out as user should not touch admin authentication' do
|
33
|
-
sign_in_as_user
|
34
|
-
sign_in_as_admin
|
35
|
-
|
36
|
-
get destroy_user_session_path
|
37
|
-
assert_not warden.authenticated?(:user)
|
38
|
-
assert warden.authenticated?(:admin)
|
39
|
-
end
|
40
|
-
|
41
|
-
test 'sign out as admin should not touch user authentication' do
|
42
|
-
sign_in_as_user
|
43
|
-
sign_in_as_admin
|
44
|
-
|
45
|
-
get destroy_admin_session_path
|
46
|
-
assert_not warden.authenticated?(:admin)
|
47
|
-
assert warden.authenticated?(:user)
|
48
|
-
end
|
49
|
-
|
50
|
-
test 'not signed in as admin should not be able to access admins actions' do
|
51
|
-
get admins_path
|
52
|
-
|
53
|
-
assert_redirected_to new_admin_session_path
|
54
|
-
assert_not warden.authenticated?(:admin)
|
55
|
-
end
|
56
|
-
|
57
|
-
test 'signed in as user should not be able to access admins actions' do
|
58
|
-
sign_in_as_user
|
59
|
-
assert warden.authenticated?(:user)
|
60
|
-
assert_not warden.authenticated?(:admin)
|
61
|
-
|
62
|
-
get admins_path
|
63
|
-
assert_redirected_to new_admin_session_path
|
64
|
-
end
|
65
|
-
|
66
|
-
test 'signed in as admin should be able to access admin actions' do
|
67
|
-
sign_in_as_admin
|
68
|
-
assert warden.authenticated?(:admin)
|
69
|
-
assert_not warden.authenticated?(:user)
|
70
|
-
|
71
|
-
get admins_path
|
72
|
-
|
73
|
-
assert_response :success
|
74
|
-
assert_template 'admins/index'
|
75
|
-
assert_contain 'Welcome Admin'
|
76
|
-
end
|
77
|
-
|
78
|
-
test 'authenticated admin should not be able to sign as admin again' do
|
79
|
-
sign_in_as_admin
|
80
|
-
get new_admin_session_path
|
81
|
-
|
82
|
-
assert_response :redirect
|
83
|
-
assert_redirected_to admin_root_path
|
84
|
-
assert warden.authenticated?(:admin)
|
85
|
-
end
|
86
|
-
|
87
|
-
test 'authenticated admin should be able to sign out' do
|
88
|
-
sign_in_as_admin
|
89
|
-
assert warden.authenticated?(:admin)
|
90
|
-
|
91
|
-
get destroy_admin_session_path
|
92
|
-
assert_response :redirect
|
93
|
-
assert_redirected_to root_path
|
94
|
-
|
95
|
-
get root_path
|
96
|
-
assert_contain 'Signed out successfully'
|
97
|
-
assert_not warden.authenticated?(:admin)
|
98
|
-
end
|
99
|
-
|
100
|
-
test 'unauthenticated admin does not set message on sign out' do
|
101
|
-
get destroy_admin_session_path
|
102
|
-
assert_response :redirect
|
103
|
-
assert_redirected_to root_path
|
104
|
-
|
105
|
-
get root_path
|
106
|
-
assert_not_contain 'Signed out successfully'
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
class AuthenticationTest < ActionController::IntegrationTest
|
3
|
+
class DatabaseAuthenticationTest < ActionController::IntegrationTest
|
111
4
|
test 'sign in should not authenticate if not using proper authentication keys' do
|
112
5
|
swap Devise, :authentication_keys => [:username] do
|
113
6
|
sign_in_as_user
|
@@ -142,157 +35,4 @@ class AuthenticationTest < ActionController::IntegrationTest
|
|
142
35
|
assert_contain 'Invalid credentials'
|
143
36
|
end
|
144
37
|
end
|
145
|
-
|
146
|
-
test 'redirect from warden shows sign in or sign up message' do
|
147
|
-
get admins_path
|
148
|
-
|
149
|
-
warden_path = new_admin_session_path
|
150
|
-
assert_redirected_to warden_path
|
151
|
-
|
152
|
-
get warden_path
|
153
|
-
assert_contain 'You need to sign in or sign up before continuing.'
|
154
|
-
end
|
155
|
-
|
156
|
-
test 'redirect to default url if no other was configured' do
|
157
|
-
sign_in_as_user
|
158
|
-
|
159
|
-
assert_template 'home/index'
|
160
|
-
assert_nil session[:"user_return_to"]
|
161
|
-
end
|
162
|
-
|
163
|
-
test 'redirect to requested url after sign in' do
|
164
|
-
get users_path
|
165
|
-
assert_redirected_to new_user_session_path
|
166
|
-
assert_equal users_path, session[:"user_return_to"]
|
167
|
-
|
168
|
-
follow_redirect!
|
169
|
-
sign_in_as_user :visit => false
|
170
|
-
|
171
|
-
assert_template 'users/index'
|
172
|
-
assert_nil session[:"user_return_to"]
|
173
|
-
end
|
174
|
-
|
175
|
-
test 'redirect to last requested url overwriting the stored return_to option' do
|
176
|
-
get expire_user_path(create_user)
|
177
|
-
assert_redirected_to new_user_session_path
|
178
|
-
assert_equal expire_user_path(create_user), session[:"user_return_to"]
|
179
|
-
|
180
|
-
get users_path
|
181
|
-
assert_redirected_to new_user_session_path
|
182
|
-
assert_equal users_path, session[:"user_return_to"]
|
183
|
-
|
184
|
-
follow_redirect!
|
185
|
-
sign_in_as_user :visit => false
|
186
|
-
|
187
|
-
assert_template 'users/index'
|
188
|
-
assert_nil session[:"user_return_to"]
|
189
|
-
end
|
190
|
-
|
191
|
-
test 'redirect to configured home path for a given scope after sign in' do
|
192
|
-
sign_in_as_admin
|
193
|
-
assert_equal "/admin_area/home", @request.path
|
194
|
-
end
|
195
|
-
|
196
|
-
test 'destroyed account is signed out' do
|
197
|
-
sign_in_as_user
|
198
|
-
get '/users'
|
199
|
-
|
200
|
-
User.destroy_all
|
201
|
-
get '/users'
|
202
|
-
assert_redirected_to new_user_session_path
|
203
|
-
end
|
204
|
-
|
205
|
-
test 'allows session to be set by a given scope' do
|
206
|
-
sign_in_as_user
|
207
|
-
get '/users'
|
208
|
-
assert_equal "Cart", @controller.user_session[:cart]
|
209
|
-
end
|
210
|
-
|
211
|
-
# Scoped views
|
212
|
-
test 'renders the scoped view if turned on and view is available' do
|
213
|
-
swap Devise, :scoped_views => true do
|
214
|
-
assert_raise Webrat::NotFoundError do
|
215
|
-
sign_in_as_user
|
216
|
-
end
|
217
|
-
assert_match /Special user view/, response.body
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
test 'renders the scoped view if turned on in an specific controller' do
|
222
|
-
begin
|
223
|
-
Devise::SessionsController.scoped_views = true
|
224
|
-
assert_raise Webrat::NotFoundError do
|
225
|
-
sign_in_as_user
|
226
|
-
end
|
227
|
-
|
228
|
-
assert_match /Special user view/, response.body
|
229
|
-
assert !Devise::PasswordsController.scoped_views?
|
230
|
-
ensure
|
231
|
-
Devise::SessionsController.send :remove_instance_variable, :@scoped_views
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
test 'does not render the scoped view if turned off' do
|
236
|
-
swap Devise, :scoped_views => false do
|
237
|
-
assert_nothing_raised do
|
238
|
-
sign_in_as_user
|
239
|
-
end
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
test 'does not render the scoped view if not available' do
|
244
|
-
swap Devise, :scoped_views => true do
|
245
|
-
assert_nothing_raised do
|
246
|
-
sign_in_as_admin
|
247
|
-
end
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
# Default scope
|
252
|
-
test 'uses the mapping from the default scope if specified' do
|
253
|
-
swap Devise, :use_default_scope => true do
|
254
|
-
get '/sign_in'
|
255
|
-
assert_response :ok
|
256
|
-
assert_contain 'Sign in'
|
257
|
-
end
|
258
|
-
end
|
259
|
-
|
260
|
-
# Custom controller
|
261
|
-
test 'uses the custom controller with the custom controller view' do
|
262
|
-
get '/admin_area/sign_in'
|
263
|
-
assert_contain 'Sign in'
|
264
|
-
assert_contain 'Welcome to "sessions" controller!'
|
265
|
-
assert_contain 'Welcome to "sessions/new" view!'
|
266
|
-
end
|
267
|
-
|
268
|
-
# Custom strategy invoking custom!
|
269
|
-
test 'custom strategy invoking custom on sign up bevahes as expected' do
|
270
|
-
Warden::Strategies.add(:custom) do
|
271
|
-
def authenticate!
|
272
|
-
custom!([401, {"Content-Type" => "text/html"}, ["Custom strategy"]])
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
begin
|
277
|
-
Devise.warden_config.default_strategies(:scope => :user).unshift(:custom)
|
278
|
-
sign_in_as_user
|
279
|
-
assert_equal 401, status
|
280
|
-
assert_contain 'Custom strategy'
|
281
|
-
ensure
|
282
|
-
Devise.warden_config.default_strategies(:scope => :user).shift
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
# Access
|
287
|
-
test 'render 404 on roles without routes' do
|
288
|
-
assert_raise ActionController::RoutingError do
|
289
|
-
get '/admin_area/password/new'
|
290
|
-
end
|
291
|
-
end
|
292
|
-
|
293
|
-
test 'render 404 on roles without mapping' do
|
294
|
-
assert_raise AbstractController::ActionNotFound do
|
295
|
-
get '/sign_in'
|
296
|
-
end
|
297
|
-
end
|
298
|
-
end
|
38
|
+
end
|