loyal_devise 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/.travis.yml +15 -0
- data/CHANGELOG.rdoc +881 -0
- data/CONTRIBUTING.md +12 -0
- data/Gemfile +31 -0
- data/Gemfile.lock +154 -0
- data/MIT-LICENSE +20 -0
- data/README.md +388 -0
- data/Rakefile +34 -0
- data/app/controllers/devise/confirmations_controller.rb +44 -0
- data/app/controllers/devise/omniauth_callbacks_controller.rb +31 -0
- data/app/controllers/devise/passwords_controller.rb +57 -0
- data/app/controllers/devise/registrations_controller.rb +120 -0
- data/app/controllers/devise/sessions_controller.rb +51 -0
- data/app/controllers/devise/unlocks_controller.rb +45 -0
- data/app/controllers/devise_controller.rb +193 -0
- data/app/helpers/devise_helper.rb +26 -0
- data/app/mailers/devise/mailer.rb +16 -0
- data/app/views/devise/_links.erb +3 -0
- data/app/views/devise/confirmations/new.html.erb +12 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/devise/passwords/edit.html.erb +16 -0
- data/app/views/devise/passwords/new.html.erb +12 -0
- data/app/views/devise/registrations/edit.html.erb +25 -0
- data/app/views/devise/registrations/new.html.erb +18 -0
- data/app/views/devise/sessions/new.html.erb +17 -0
- data/app/views/devise/shared/_links.erb +25 -0
- data/app/views/devise/unlocks/new.html.erb +12 -0
- data/config/locales/en.yml +59 -0
- data/devise.gemspec +26 -0
- data/gemfiles/Gemfile.rails-3.1.x +35 -0
- data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
- data/lib/devise/controllers/helpers.rb +273 -0
- data/lib/devise/controllers/rememberable.rb +53 -0
- data/lib/devise/controllers/scoped_views.rb +18 -0
- data/lib/devise/controllers/url_helpers.rb +68 -0
- data/lib/devise/delegator.rb +17 -0
- data/lib/devise/failure_app.rb +188 -0
- data/lib/devise/hooks/activatable.rb +12 -0
- data/lib/devise/hooks/forgetable.rb +10 -0
- data/lib/devise/hooks/lockable.rb +8 -0
- data/lib/devise/hooks/rememberable.rb +7 -0
- data/lib/devise/hooks/timeoutable.rb +26 -0
- data/lib/devise/hooks/trackable.rb +10 -0
- data/lib/devise/mailers/helpers.rb +92 -0
- data/lib/devise/mapping.rb +173 -0
- data/lib/devise/models/authenticatable.rb +269 -0
- data/lib/devise/models/confirmable.rb +271 -0
- data/lib/devise/models/database_authenticatable.rb +127 -0
- data/lib/devise/models/lockable.rb +194 -0
- data/lib/devise/models/omniauthable.rb +28 -0
- data/lib/devise/models/recoverable.rb +141 -0
- data/lib/devise/models/registerable.rb +26 -0
- data/lib/devise/models/rememberable.rb +126 -0
- data/lib/devise/models/timeoutable.rb +50 -0
- data/lib/devise/models/token_authenticatable.rb +90 -0
- data/lib/devise/models/trackable.rb +36 -0
- data/lib/devise/models/validatable.rb +67 -0
- data/lib/devise/models.rb +129 -0
- data/lib/devise/modules.rb +30 -0
- data/lib/devise/omniauth/config.rb +46 -0
- data/lib/devise/omniauth/url_helpers.rb +19 -0
- data/lib/devise/omniauth.rb +29 -0
- data/lib/devise/orm/active_record.rb +4 -0
- data/lib/devise/orm/mongoid.rb +4 -0
- data/lib/devise/param_filter.rb +42 -0
- data/lib/devise/rails/routes.rb +447 -0
- data/lib/devise/rails/warden_compat.rb +44 -0
- data/lib/devise/rails.rb +55 -0
- data/lib/devise/strategies/authenticatable.rb +177 -0
- data/lib/devise/strategies/base.rb +21 -0
- data/lib/devise/strategies/database_authenticatable.rb +21 -0
- data/lib/devise/strategies/rememberable.rb +56 -0
- data/lib/devise/strategies/token_authenticatable.rb +57 -0
- data/lib/devise/test_helpers.rb +132 -0
- data/lib/devise/time_inflector.rb +15 -0
- data/lib/devise/version.rb +4 -0
- data/lib/devise.rb +445 -0
- data/lib/generators/active_record/devise_generator.rb +80 -0
- data/lib/generators/active_record/templates/migration.rb +20 -0
- data/lib/generators/active_record/templates/migration_existing.rb +27 -0
- data/lib/generators/devise/devise_generator.rb +25 -0
- data/lib/generators/devise/install_generator.rb +25 -0
- data/lib/generators/devise/orm_helpers.rb +33 -0
- data/lib/generators/devise/views_generator.rb +117 -0
- data/lib/generators/mongoid/devise_generator.rb +58 -0
- data/lib/generators/templates/README +35 -0
- data/lib/generators/templates/devise.rb +241 -0
- data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
- data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
- data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
- data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
- data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
- data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
- data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
- data/test/controllers/custom_strategy_test.rb +63 -0
- data/test/controllers/helpers_test.rb +254 -0
- data/test/controllers/internal_helpers_test.rb +111 -0
- data/test/controllers/sessions_controller_test.rb +58 -0
- data/test/controllers/url_helpers_test.rb +60 -0
- data/test/delegator_test.rb +20 -0
- data/test/devise_test.rb +73 -0
- data/test/failure_app_test.rb +222 -0
- data/test/generators/active_record_generator_test.rb +76 -0
- data/test/generators/devise_generator_test.rb +40 -0
- data/test/generators/install_generator_test.rb +14 -0
- data/test/generators/mongoid_generator_test.rb +24 -0
- data/test/generators/views_generator_test.rb +53 -0
- data/test/helpers/devise_helper_test.rb +52 -0
- data/test/indifferent_hash.rb +34 -0
- data/test/integration/authenticatable_test.rb +634 -0
- data/test/integration/confirmable_test.rb +299 -0
- data/test/integration/database_authenticatable_test.rb +83 -0
- data/test/integration/http_authenticatable_test.rb +98 -0
- data/test/integration/lockable_test.rb +243 -0
- data/test/integration/omniauthable_test.rb +134 -0
- data/test/integration/recoverable_test.rb +307 -0
- data/test/integration/registerable_test.rb +346 -0
- data/test/integration/rememberable_test.rb +159 -0
- data/test/integration/timeoutable_test.rb +141 -0
- data/test/integration/token_authenticatable_test.rb +162 -0
- data/test/integration/trackable_test.rb +93 -0
- data/test/mailers/confirmation_instructions_test.rb +103 -0
- data/test/mailers/reset_password_instructions_test.rb +84 -0
- data/test/mailers/unlock_instructions_test.rb +78 -0
- data/test/mapping_test.rb +128 -0
- data/test/models/authenticatable_test.rb +8 -0
- data/test/models/confirmable_test.rb +392 -0
- data/test/models/database_authenticatable_test.rb +190 -0
- data/test/models/lockable_test.rb +274 -0
- data/test/models/omniauthable_test.rb +8 -0
- data/test/models/recoverable_test.rb +206 -0
- data/test/models/registerable_test.rb +8 -0
- data/test/models/rememberable_test.rb +175 -0
- data/test/models/serializable_test.rb +49 -0
- data/test/models/timeoutable_test.rb +47 -0
- data/test/models/token_authenticatable_test.rb +56 -0
- data/test/models/trackable_test.rb +14 -0
- data/test/models/validatable_test.rb +117 -0
- data/test/models_test.rb +180 -0
- data/test/omniauth/config_test.rb +58 -0
- data/test/omniauth/url_helpers_test.rb +52 -0
- data/test/orm/active_record.rb +10 -0
- data/test/orm/mongoid.rb +15 -0
- data/test/rails_app/Rakefile +10 -0
- data/test/rails_app/app/active_record/admin.rb +7 -0
- data/test/rails_app/app/active_record/shim.rb +3 -0
- data/test/rails_app/app/active_record/user.rb +7 -0
- data/test/rails_app/app/controllers/admins/sessions_controller.rb +7 -0
- data/test/rails_app/app/controllers/admins_controller.rb +12 -0
- data/test/rails_app/app/controllers/application_controller.rb +9 -0
- data/test/rails_app/app/controllers/home_controller.rb +26 -0
- data/test/rails_app/app/controllers/publisher/registrations_controller.rb +3 -0
- data/test/rails_app/app/controllers/publisher/sessions_controller.rb +3 -0
- data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +15 -0
- data/test/rails_app/app/controllers/users_controller.rb +24 -0
- data/test/rails_app/app/helpers/application_helper.rb +4 -0
- data/test/rails_app/app/mailers/users/mailer.rb +9 -0
- data/test/rails_app/app/mongoid/admin.rb +28 -0
- data/test/rails_app/app/mongoid/shim.rb +25 -0
- data/test/rails_app/app/mongoid/user.rb +43 -0
- data/test/rails_app/app/views/admins/index.html.erb +1 -0
- data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
- data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/home/index.html.erb +1 -0
- data/test/rails_app/app/views/home/join.html.erb +1 -0
- data/test/rails_app/app/views/home/private.html.erb +1 -0
- data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/layouts/application.html.erb +24 -0
- data/test/rails_app/app/views/users/index.html.erb +1 -0
- data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
- data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
- data/test/rails_app/config/application.rb +42 -0
- data/test/rails_app/config/boot.rb +9 -0
- data/test/rails_app/config/database.yml +18 -0
- data/test/rails_app/config/environment.rb +6 -0
- data/test/rails_app/config/environments/development.rb +19 -0
- data/test/rails_app/config/environments/production.rb +34 -0
- data/test/rails_app/config/environments/test.rb +34 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +8 -0
- data/test/rails_app/config/initializers/devise.rb +179 -0
- data/test/rails_app/config/initializers/inflections.rb +3 -0
- data/test/rails_app/config/initializers/secret_token.rb +3 -0
- data/test/rails_app/config/routes.rb +101 -0
- data/test/rails_app/config.ru +4 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +75 -0
- data/test/rails_app/db/schema.rb +53 -0
- data/test/rails_app/lib/shared_admin.rb +15 -0
- data/test/rails_app/lib/shared_user.rb +27 -0
- data/test/rails_app/public/404.html +26 -0
- data/test/rails_app/public/422.html +26 -0
- data/test/rails_app/public/500.html +26 -0
- data/test/rails_app/public/favicon.ico +0 -0
- data/test/rails_app/script/rails +10 -0
- data/test/routes_test.rb +249 -0
- data/test/support/assertions.rb +41 -0
- data/test/support/helpers.rb +92 -0
- data/test/support/integration.rb +93 -0
- data/test/support/locale/en.yml +4 -0
- data/test/support/webrat/integrations/rails.rb +25 -0
- data/test/test_helper.rb +28 -0
- data/test/test_helpers_test.rb +152 -0
- metadata +407 -0
@@ -0,0 +1,254 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
class ControllerAuthenticatableTest < ActionController::TestCase
|
6
|
+
tests ApplicationController
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@mock_warden = OpenStruct.new
|
10
|
+
@controller.request.env['warden'] = @mock_warden
|
11
|
+
end
|
12
|
+
|
13
|
+
test 'provide access to warden instance' do
|
14
|
+
assert_equal @mock_warden, @controller.warden
|
15
|
+
end
|
16
|
+
|
17
|
+
test 'proxy signed_in?(scope) to authenticate?' do
|
18
|
+
@mock_warden.expects(:authenticate?).with(:scope => :my_scope)
|
19
|
+
@controller.signed_in?(:my_scope)
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'proxy signed_in?(nil) to authenticate?' do
|
23
|
+
Devise.mappings.keys.each do |scope| # :user, :admin, :manager
|
24
|
+
@mock_warden.expects(:authenticate?).with(:scope => scope)
|
25
|
+
end
|
26
|
+
@controller.signed_in?
|
27
|
+
end
|
28
|
+
|
29
|
+
test 'proxy current_user to authenticate with user scope' do
|
30
|
+
@mock_warden.expects(:authenticate).with(:scope => :user)
|
31
|
+
@controller.current_user
|
32
|
+
end
|
33
|
+
|
34
|
+
test 'proxy current_admin to authenticate with admin scope' do
|
35
|
+
@mock_warden.expects(:authenticate).with(:scope => :admin)
|
36
|
+
@controller.current_admin
|
37
|
+
end
|
38
|
+
|
39
|
+
test 'proxy current_publisher_account to authenticate with namespaced publisher account scope' do
|
40
|
+
@mock_warden.expects(:authenticate).with(:scope => :publisher_account)
|
41
|
+
@controller.current_publisher_account
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'proxy authenticate_user! to authenticate with user scope' do
|
45
|
+
@mock_warden.expects(:authenticate!).with(:scope => :user)
|
46
|
+
@controller.authenticate_user!
|
47
|
+
end
|
48
|
+
|
49
|
+
test 'proxy authenticate_user! options to authenticate with user scope' do
|
50
|
+
@mock_warden.expects(:authenticate!).with(:scope => :user, :recall => "foo")
|
51
|
+
@controller.authenticate_user!(:recall => "foo")
|
52
|
+
end
|
53
|
+
|
54
|
+
test 'proxy authenticate_admin! to authenticate with admin scope' do
|
55
|
+
@mock_warden.expects(:authenticate!).with(:scope => :admin)
|
56
|
+
@controller.authenticate_admin!
|
57
|
+
end
|
58
|
+
|
59
|
+
test 'proxy authenticate_publisher_account! to authenticate with namespaced publisher account scope' do
|
60
|
+
@mock_warden.expects(:authenticate!).with(:scope => :publisher_account)
|
61
|
+
@controller.authenticate_publisher_account!
|
62
|
+
end
|
63
|
+
|
64
|
+
test 'proxy user_signed_in? to authenticate with user scope' do
|
65
|
+
@mock_warden.expects(:authenticate).with(:scope => :user).returns("user")
|
66
|
+
assert @controller.user_signed_in?
|
67
|
+
end
|
68
|
+
|
69
|
+
test 'proxy admin_signed_in? to authenticatewith admin scope' do
|
70
|
+
@mock_warden.expects(:authenticate).with(:scope => :admin)
|
71
|
+
assert_not @controller.admin_signed_in?
|
72
|
+
end
|
73
|
+
|
74
|
+
test 'proxy publisher_account_signed_in? to authenticate with namespaced publisher account scope' do
|
75
|
+
@mock_warden.expects(:authenticate).with(:scope => :publisher_account)
|
76
|
+
@controller.publisher_account_signed_in?
|
77
|
+
end
|
78
|
+
|
79
|
+
test 'proxy user_session to session scope in warden' do
|
80
|
+
@mock_warden.expects(:authenticate).with(:scope => :user).returns(true)
|
81
|
+
@mock_warden.expects(:session).with(:user).returns({})
|
82
|
+
@controller.user_session
|
83
|
+
end
|
84
|
+
|
85
|
+
test 'proxy admin_session to session scope in warden' do
|
86
|
+
@mock_warden.expects(:authenticate).with(:scope => :admin).returns(true)
|
87
|
+
@mock_warden.expects(:session).with(:admin).returns({})
|
88
|
+
@controller.admin_session
|
89
|
+
end
|
90
|
+
|
91
|
+
test 'proxy publisher_account_session from namespaced scope to session scope in warden' do
|
92
|
+
@mock_warden.expects(:authenticate).with(:scope => :publisher_account).returns(true)
|
93
|
+
@mock_warden.expects(:session).with(:publisher_account).returns({})
|
94
|
+
@controller.publisher_account_session
|
95
|
+
end
|
96
|
+
|
97
|
+
test 'sign in proxy to set_user on warden' do
|
98
|
+
user = User.new
|
99
|
+
@mock_warden.expects(:user).returns(nil)
|
100
|
+
@mock_warden.expects(:set_user).with(user, :scope => :user).returns(true)
|
101
|
+
@controller.sign_in(:user, user)
|
102
|
+
end
|
103
|
+
|
104
|
+
test 'sign in accepts a resource as argument' do
|
105
|
+
user = User.new
|
106
|
+
@mock_warden.expects(:user).returns(nil)
|
107
|
+
@mock_warden.expects(:set_user).with(user, :scope => :user).returns(true)
|
108
|
+
@controller.sign_in(user)
|
109
|
+
end
|
110
|
+
|
111
|
+
test 'does not sign in again if the user is already in' do
|
112
|
+
user = User.new
|
113
|
+
@mock_warden.expects(:user).returns(user)
|
114
|
+
@mock_warden.expects(:set_user).never
|
115
|
+
assert @controller.sign_in(user)
|
116
|
+
end
|
117
|
+
|
118
|
+
test 'sign in again when the user is already in only if force is given' do
|
119
|
+
user = User.new
|
120
|
+
@mock_warden.expects(:user).returns(user)
|
121
|
+
@mock_warden.expects(:set_user).with(user, :scope => :user).returns(true)
|
122
|
+
@controller.sign_in(user, :force => true)
|
123
|
+
end
|
124
|
+
|
125
|
+
test 'sign in accepts bypass as option' do
|
126
|
+
user = User.new
|
127
|
+
@mock_warden.expects(:session_serializer).returns(serializer = mock())
|
128
|
+
serializer.expects(:store).with(user, :user)
|
129
|
+
@controller.sign_in(user, :bypass => true)
|
130
|
+
end
|
131
|
+
|
132
|
+
test 'sign out clears up any signed in user from all scopes' do
|
133
|
+
user = User.new
|
134
|
+
@mock_warden.expects(:user).times(Devise.mappings.size)
|
135
|
+
@mock_warden.expects(:logout).with().returns(true)
|
136
|
+
@controller.instance_variable_set(:@current_user, user)
|
137
|
+
@controller.instance_variable_set(:@current_admin, user)
|
138
|
+
@controller.sign_out
|
139
|
+
assert_equal nil, @controller.instance_variable_get(:@current_user)
|
140
|
+
assert_equal nil, @controller.instance_variable_get(:@current_admin)
|
141
|
+
end
|
142
|
+
|
143
|
+
test 'sign out logs out and clears up any signed in user by scope' do
|
144
|
+
user = User.new
|
145
|
+
@mock_warden.expects(:user).with(:scope => :user, :run_callbacks => false).returns(user)
|
146
|
+
@mock_warden.expects(:logout).with(:user).returns(true)
|
147
|
+
@mock_warden.expects(:clear_strategies_cache!).with(:scope => :user).returns(true)
|
148
|
+
@controller.instance_variable_set(:@current_user, user)
|
149
|
+
@controller.sign_out(:user)
|
150
|
+
assert_equal nil, @controller.instance_variable_get(:@current_user)
|
151
|
+
end
|
152
|
+
|
153
|
+
test 'sign out accepts a resource as argument' do
|
154
|
+
@mock_warden.expects(:user).with(:scope => :user, :run_callbacks => false).returns(true)
|
155
|
+
@mock_warden.expects(:logout).with(:user).returns(true)
|
156
|
+
@mock_warden.expects(:clear_strategies_cache!).with(:scope => :user).returns(true)
|
157
|
+
@controller.sign_out(User.new)
|
158
|
+
end
|
159
|
+
|
160
|
+
test 'sign out without args proxy to sign out all scopes' do
|
161
|
+
@mock_warden.expects(:user).times(Devise.mappings.size)
|
162
|
+
@mock_warden.expects(:logout).with().returns(true)
|
163
|
+
@mock_warden.expects(:clear_strategies_cache!).with().returns(true)
|
164
|
+
@controller.sign_out
|
165
|
+
end
|
166
|
+
|
167
|
+
test 'sign out everybody proxy to logout on warden' do
|
168
|
+
@mock_warden.expects(:user).times(Devise.mappings.size)
|
169
|
+
@mock_warden.expects(:logout).with().returns(true)
|
170
|
+
@controller.sign_out_all_scopes
|
171
|
+
end
|
172
|
+
|
173
|
+
test 'stored location for returns the location for a given scope' do
|
174
|
+
assert_nil @controller.stored_location_for(:user)
|
175
|
+
@controller.session[:"user_return_to"] = "/foo.bar"
|
176
|
+
assert_equal "/foo.bar", @controller.stored_location_for(:user)
|
177
|
+
end
|
178
|
+
|
179
|
+
test 'stored location for accepts a resource as argument' do
|
180
|
+
assert_nil @controller.stored_location_for(:user)
|
181
|
+
@controller.session[:"user_return_to"] = "/foo.bar"
|
182
|
+
assert_equal "/foo.bar", @controller.stored_location_for(User.new)
|
183
|
+
end
|
184
|
+
|
185
|
+
test 'stored location cleans information after reading' do
|
186
|
+
@controller.session[:"user_return_to"] = "/foo.bar"
|
187
|
+
assert_equal "/foo.bar", @controller.stored_location_for(:user)
|
188
|
+
assert_nil @controller.session[:"user_return_to"]
|
189
|
+
end
|
190
|
+
|
191
|
+
test 'after sign in path defaults to root path if none by was specified for the given scope' do
|
192
|
+
assert_equal root_path, @controller.after_sign_in_path_for(:user)
|
193
|
+
end
|
194
|
+
|
195
|
+
test 'after sign in path defaults to the scoped root path' do
|
196
|
+
assert_equal admin_root_path, @controller.after_sign_in_path_for(:admin)
|
197
|
+
end
|
198
|
+
|
199
|
+
test 'after sign out path defaults to the root path' do
|
200
|
+
assert_equal root_path, @controller.after_sign_out_path_for(:admin)
|
201
|
+
assert_equal root_path, @controller.after_sign_out_path_for(:user)
|
202
|
+
end
|
203
|
+
|
204
|
+
test 'sign in and redirect uses the stored location' do
|
205
|
+
user = User.new
|
206
|
+
@controller.session[:"user_return_to"] = "/foo.bar"
|
207
|
+
@mock_warden.expects(:user).with(:user).returns(nil)
|
208
|
+
@mock_warden.expects(:set_user).with(user, :scope => :user).returns(true)
|
209
|
+
@controller.expects(:redirect_to).with("/foo.bar")
|
210
|
+
@controller.sign_in_and_redirect(user)
|
211
|
+
end
|
212
|
+
|
213
|
+
test 'sign in and redirect uses the configured after sign in path' do
|
214
|
+
admin = Admin.new
|
215
|
+
@mock_warden.expects(:user).with(:admin).returns(nil)
|
216
|
+
@mock_warden.expects(:set_user).with(admin, :scope => :admin).returns(true)
|
217
|
+
@controller.expects(:redirect_to).with(admin_root_path)
|
218
|
+
@controller.sign_in_and_redirect(admin)
|
219
|
+
end
|
220
|
+
|
221
|
+
test 'sign in and redirect does not sign in again if user is already signed' do
|
222
|
+
admin = Admin.new
|
223
|
+
@mock_warden.expects(:user).with(:admin).returns(admin)
|
224
|
+
@mock_warden.expects(:set_user).never
|
225
|
+
@controller.expects(:redirect_to).with(admin_root_path)
|
226
|
+
@controller.sign_in_and_redirect(admin)
|
227
|
+
end
|
228
|
+
|
229
|
+
test 'sign out and redirect uses the configured after sign out path when signing out only the current scope' do
|
230
|
+
swap Devise, :sign_out_all_scopes => false do
|
231
|
+
@mock_warden.expects(:user).with(:scope => :admin, :run_callbacks => false).returns(true)
|
232
|
+
@mock_warden.expects(:logout).with(:admin).returns(true)
|
233
|
+
@mock_warden.expects(:clear_strategies_cache!).with(:scope => :admin).returns(true)
|
234
|
+
@controller.expects(:redirect_to).with(admin_root_path)
|
235
|
+
@controller.instance_eval "def after_sign_out_path_for(resource); admin_root_path; end"
|
236
|
+
@controller.sign_out_and_redirect(:admin)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
test 'sign out and redirect uses the configured after sign out path when signing out all scopes' do
|
241
|
+
swap Devise, :sign_out_all_scopes => true do
|
242
|
+
@mock_warden.expects(:user).times(Devise.mappings.size)
|
243
|
+
@mock_warden.expects(:logout).with().returns(true)
|
244
|
+
@mock_warden.expects(:clear_strategies_cache!).with().returns(true)
|
245
|
+
@controller.expects(:redirect_to).with(admin_root_path)
|
246
|
+
@controller.instance_eval "def after_sign_out_path_for(resource); admin_root_path; end"
|
247
|
+
@controller.sign_out_and_redirect(:admin)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
test 'is not a devise controller' do
|
252
|
+
assert_not @controller.devise_controller?
|
253
|
+
end
|
254
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class MyController < DeviseController
|
5
|
+
end
|
6
|
+
|
7
|
+
class HelpersTest < ActionController::TestCase
|
8
|
+
tests MyController
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@mock_warden = OpenStruct.new
|
12
|
+
@controller.request.env['warden'] = @mock_warden
|
13
|
+
@controller.request.env['devise.mapping'] = Devise.mappings[:user]
|
14
|
+
end
|
15
|
+
|
16
|
+
test 'get resource name from env' do
|
17
|
+
assert_equal :user, @controller.resource_name
|
18
|
+
end
|
19
|
+
|
20
|
+
test 'get resource class from env' do
|
21
|
+
assert_equal User, @controller.resource_class
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'get resource instance variable from env' do
|
25
|
+
@controller.instance_variable_set(:@user, user = User.new)
|
26
|
+
assert_equal user, @controller.resource
|
27
|
+
end
|
28
|
+
|
29
|
+
test 'set resource instance variable from env' do
|
30
|
+
user = @controller.send(:resource_class).new
|
31
|
+
@controller.send(:resource=, user)
|
32
|
+
|
33
|
+
assert_equal user, @controller.send(:resource)
|
34
|
+
assert_equal user, @controller.instance_variable_get(:@user)
|
35
|
+
end
|
36
|
+
|
37
|
+
test 'get resource params from request params using resource name as key' do
|
38
|
+
user_params = {'name' => 'Shirley Templar'}
|
39
|
+
@controller.stubs(:params).returns(HashWithIndifferentAccess.new({'user' => user_params}))
|
40
|
+
|
41
|
+
assert_equal user_params, @controller.resource_params
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'resources methods are not controller actions' do
|
45
|
+
assert @controller.class.action_methods.empty?
|
46
|
+
end
|
47
|
+
|
48
|
+
test 'require no authentication tests current mapping' do
|
49
|
+
@mock_warden.expects(:authenticate?).with(:rememberable, :token_authenticatable, :scope => :user).returns(true)
|
50
|
+
@mock_warden.expects(:user).with(:user).returns(User.new)
|
51
|
+
@controller.expects(:redirect_to).with(root_path)
|
52
|
+
@controller.send :require_no_authentication
|
53
|
+
end
|
54
|
+
|
55
|
+
test 'require no authentication only checks if already authenticated if no inputs strategies are available' do
|
56
|
+
Devise.mappings[:user].expects(:no_input_strategies).returns([])
|
57
|
+
@mock_warden.expects(:authenticate?).never
|
58
|
+
@mock_warden.expects(:authenticated?).with(:user).once.returns(true)
|
59
|
+
@mock_warden.expects(:user).with(:user).returns(User.new)
|
60
|
+
@controller.expects(:redirect_to).with(root_path)
|
61
|
+
@controller.send :require_no_authentication
|
62
|
+
end
|
63
|
+
|
64
|
+
test 'require no authentication sets a flash message' do
|
65
|
+
@mock_warden.expects(:authenticate?).with(:rememberable, :token_authenticatable, :scope => :user).returns(true)
|
66
|
+
@mock_warden.expects(:user).with(:user).returns(User.new)
|
67
|
+
@controller.expects(:redirect_to).with(root_path)
|
68
|
+
@controller.send :require_no_authentication
|
69
|
+
assert flash[:alert] == I18n.t("devise.failure.already_authenticated")
|
70
|
+
end
|
71
|
+
|
72
|
+
test 'signed in resource returns signed in resource for current scope' do
|
73
|
+
@mock_warden.expects(:authenticate).with(:scope => :user).returns(User.new)
|
74
|
+
assert_kind_of User, @controller.signed_in_resource
|
75
|
+
end
|
76
|
+
|
77
|
+
test 'is a devise controller' do
|
78
|
+
assert @controller.devise_controller?
|
79
|
+
end
|
80
|
+
|
81
|
+
test 'does not issue blank flash messages' do
|
82
|
+
I18n.stubs(:t).returns(' ')
|
83
|
+
@controller.send :set_flash_message, :notice, :send_instructions
|
84
|
+
assert flash[:notice].nil?
|
85
|
+
end
|
86
|
+
|
87
|
+
test 'issues non-blank flash messages normally' do
|
88
|
+
I18n.stubs(:t).returns('non-blank')
|
89
|
+
@controller.send :set_flash_message, :notice, :send_instructions
|
90
|
+
assert_equal 'non-blank', flash[:notice]
|
91
|
+
end
|
92
|
+
|
93
|
+
test 'uses custom i18n options' do
|
94
|
+
@controller.stubs(:devise_i18n_options).returns(:default => "devise custom options")
|
95
|
+
@controller.send :set_flash_message, :notice, :invalid_i18n_messagesend_instructions
|
96
|
+
assert_equal 'devise custom options', flash[:notice]
|
97
|
+
end
|
98
|
+
|
99
|
+
test 'allows custom i18n options to override resource_name' do
|
100
|
+
I18n.expects(:t).with("custom_resource_name.confirmed", anything)
|
101
|
+
@controller.stubs(:devise_i18n_options).returns(:resource_name => "custom_resource_name")
|
102
|
+
@controller.send :set_flash_message, :notice, :confirmed
|
103
|
+
end
|
104
|
+
|
105
|
+
test 'navigational_formats not returning a wild card' do
|
106
|
+
MyController.send(:public, :navigational_formats)
|
107
|
+
Devise.navigational_formats = [:"*/*", :html]
|
108
|
+
assert_not @controller.navigational_formats.include?(:"*/*")
|
109
|
+
MyController.send(:protected, :navigational_formats)
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class SessionsControllerTest < ActionController::TestCase
|
5
|
+
tests Devise::SessionsController
|
6
|
+
include Devise::TestHelpers
|
7
|
+
|
8
|
+
test "#create works even with scoped views" do
|
9
|
+
swap Devise, :scoped_views => true do
|
10
|
+
request.env["devise.mapping"] = Devise.mappings[:user]
|
11
|
+
post :create
|
12
|
+
assert_equal 200, @response.status
|
13
|
+
assert_template "users/sessions/new"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
test "#create doesn't raise exception after Warden authentication fails when TestHelpers included" do
|
18
|
+
request.env["devise.mapping"] = Devise.mappings[:user]
|
19
|
+
post :create, :user => {
|
20
|
+
:email => "nosuchuser@example.com",
|
21
|
+
:password => "wevdude"
|
22
|
+
}
|
23
|
+
assert_equal 200, @response.status
|
24
|
+
assert_template "devise/sessions/new"
|
25
|
+
end
|
26
|
+
|
27
|
+
test "#destroy doesn't set the flash if the requested format is not navigational" do
|
28
|
+
request.env["devise.mapping"] = Devise.mappings[:user]
|
29
|
+
user = create_user
|
30
|
+
user.confirm!
|
31
|
+
post :create, :format => 'json', :user => {
|
32
|
+
:email => user.email,
|
33
|
+
:password => user.password
|
34
|
+
}
|
35
|
+
|
36
|
+
delete :destroy, :format => 'json'
|
37
|
+
assert flash[:notice].blank?, "flash[:notice] should be blank, not #{flash[:notice].inspect}"
|
38
|
+
assert_equal 204, @response.status
|
39
|
+
end
|
40
|
+
|
41
|
+
if defined?(ActiveRecord) && ActiveRecord::Base.respond_to?(:mass_assignment_sanitizer)
|
42
|
+
test "#new doesn't raise mass-assignment exception even if sign-in key is attr_protected" do
|
43
|
+
request.env["devise.mapping"] = Devise.mappings[:user]
|
44
|
+
|
45
|
+
ActiveRecord::Base.mass_assignment_sanitizer = :strict
|
46
|
+
User.class_eval { attr_protected :email }
|
47
|
+
|
48
|
+
begin
|
49
|
+
assert_nothing_raised ActiveModel::MassAssignmentSecurity::Error do
|
50
|
+
get :new, :user => { :email => "allez viens!" }
|
51
|
+
end
|
52
|
+
ensure
|
53
|
+
ActiveRecord::Base.mass_assignment_sanitizer = :logger
|
54
|
+
User.class_eval { attr_accessible :email }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class RoutesTest < ActionController::TestCase
|
5
|
+
tests ApplicationController
|
6
|
+
|
7
|
+
def assert_path_and_url(name, prepend_path=nil)
|
8
|
+
@request.path = '/users/session'
|
9
|
+
prepend_path = "#{prepend_path}_" if prepend_path
|
10
|
+
|
11
|
+
# Resource param
|
12
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_path", :user),
|
13
|
+
send(:"#{prepend_path}user_#{name}_path")
|
14
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_url", :user),
|
15
|
+
send(:"#{prepend_path}user_#{name}_url")
|
16
|
+
|
17
|
+
# Default url params
|
18
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_path", :user, :param => 123),
|
19
|
+
send(:"#{prepend_path}user_#{name}_path", :param => 123)
|
20
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_url", :user, :param => 123),
|
21
|
+
send(:"#{prepend_path}user_#{name}_url", :param => 123)
|
22
|
+
|
23
|
+
@request.path = nil
|
24
|
+
# With an object
|
25
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_path", User.new),
|
26
|
+
send(:"#{prepend_path}user_#{name}_path")
|
27
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_url", User.new),
|
28
|
+
send(:"#{prepend_path}user_#{name}_url")
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
test 'should alias session to mapped user session' do
|
33
|
+
assert_path_and_url :session
|
34
|
+
assert_path_and_url :session, :new
|
35
|
+
assert_path_and_url :session, :destroy
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'should alias password to mapped user password' do
|
39
|
+
assert_path_and_url :password
|
40
|
+
assert_path_and_url :password, :new
|
41
|
+
assert_path_and_url :password, :edit
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'should alias confirmation to mapped user confirmation' do
|
45
|
+
assert_path_and_url :confirmation
|
46
|
+
assert_path_and_url :confirmation, :new
|
47
|
+
end
|
48
|
+
|
49
|
+
test 'should alias unlock to mapped user unlock' do
|
50
|
+
assert_path_and_url :unlock
|
51
|
+
assert_path_and_url :unlock, :new
|
52
|
+
end
|
53
|
+
|
54
|
+
test 'should alias registration to mapped user registration' do
|
55
|
+
assert_path_and_url :registration
|
56
|
+
assert_path_and_url :registration, :new
|
57
|
+
assert_path_and_url :registration, :edit
|
58
|
+
assert_path_and_url :registration, :cancel
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class DelegatorTest < ActiveSupport::TestCase
|
5
|
+
def delegator
|
6
|
+
Devise::Delegator.new
|
7
|
+
end
|
8
|
+
|
9
|
+
test 'failure_app returns default failure app if no warden options in env' do
|
10
|
+
assert_equal Devise::FailureApp, delegator.failure_app({})
|
11
|
+
end
|
12
|
+
|
13
|
+
test 'failure_app returns default failure app if no scope in warden options' do
|
14
|
+
assert_equal Devise::FailureApp, delegator.failure_app({"warden.options" => {}})
|
15
|
+
end
|
16
|
+
|
17
|
+
test 'failure_app returns associated failure app by scope in the given environment' do
|
18
|
+
assert_kind_of Proc, delegator.failure_app({"warden.options" => {:scope => "manager"}})
|
19
|
+
end
|
20
|
+
end
|
data/test/devise_test.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
module Devise
|
5
|
+
def self.yield_and_restore
|
6
|
+
@@warden_configured = nil
|
7
|
+
c, b = @@warden_config, @@warden_config_block
|
8
|
+
yield
|
9
|
+
ensure
|
10
|
+
@@warden_config, @@warden_config_block = c, b
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class DeviseTest < ActiveSupport::TestCase
|
15
|
+
test 'model options can be configured through Devise' do
|
16
|
+
swap Devise, :allow_unconfirmed_access_for => 113, :pepper => "foo" do
|
17
|
+
assert_equal 113, Devise.allow_unconfirmed_access_for
|
18
|
+
assert_equal "foo", Devise.pepper
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'setup block yields self' do
|
23
|
+
Devise.setup do |config|
|
24
|
+
assert_equal Devise, config
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
test 'stores warden configuration' do
|
29
|
+
assert_kind_of Devise::Delegator, Devise.warden_config.failure_app
|
30
|
+
assert_equal :user, Devise.warden_config.default_scope
|
31
|
+
end
|
32
|
+
|
33
|
+
test 'warden manager user configuration through a block' do
|
34
|
+
Devise.yield_and_restore do
|
35
|
+
@executed = false
|
36
|
+
Devise.warden do |config|
|
37
|
+
@executed = true
|
38
|
+
assert_kind_of Warden::Config, config
|
39
|
+
end
|
40
|
+
|
41
|
+
Devise.configure_warden!
|
42
|
+
assert @executed
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
test 'add new module using the helper method' do
|
47
|
+
assert_nothing_raised(Exception) { Devise.add_module(:coconut) }
|
48
|
+
assert_equal 1, Devise::ALL.select { |v| v == :coconut }.size
|
49
|
+
assert_not Devise::STRATEGIES.include?(:coconut)
|
50
|
+
assert_not defined?(Devise::Models::Coconut)
|
51
|
+
Devise::ALL.delete(:coconut)
|
52
|
+
|
53
|
+
assert_nothing_raised(Exception) { Devise.add_module(:banana, :strategy => :fruits) }
|
54
|
+
assert_equal :fruits, Devise::STRATEGIES[:banana]
|
55
|
+
Devise::ALL.delete(:banana)
|
56
|
+
Devise::STRATEGIES.delete(:banana)
|
57
|
+
|
58
|
+
assert_nothing_raised(Exception) { Devise.add_module(:kivi, :controller => :fruits) }
|
59
|
+
assert_equal :fruits, Devise::CONTROLLERS[:kivi]
|
60
|
+
Devise::ALL.delete(:kivi)
|
61
|
+
Devise::CONTROLLERS.delete(:kivi)
|
62
|
+
end
|
63
|
+
|
64
|
+
test 'should complain when comparing empty or different sized passes' do
|
65
|
+
[nil, ""].each do |empty|
|
66
|
+
assert_not Devise.secure_compare(empty, "something")
|
67
|
+
assert_not Devise.secure_compare("something", empty)
|
68
|
+
assert_not Devise.secure_compare(empty, empty)
|
69
|
+
end
|
70
|
+
assert_not Devise.secure_compare("size_1", "size_four")
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|