devise-jdguyot 1.2.rc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +10 -0
- data/CHANGELOG.rdoc +532 -0
- data/Gemfile +29 -0
- data/Gemfile.lock +152 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +353 -0
- data/Rakefile +36 -0
- data/TODO +4 -0
- data/app/controllers/devise/confirmations_controller.rb +33 -0
- data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
- data/app/controllers/devise/passwords_controller.rb +41 -0
- data/app/controllers/devise/registrations_controller.rb +110 -0
- data/app/controllers/devise/sessions_controller.rb +25 -0
- data/app/controllers/devise/unlocks_controller.rb +34 -0
- data/app/helpers/devise_helper.rb +19 -0
- data/app/mailers/devise/mailer.rb +88 -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 +46 -0
- data/devise.gemspec +25 -0
- data/lib/devise/controllers/helpers.rb +227 -0
- data/lib/devise/controllers/internal_helpers.rb +119 -0
- data/lib/devise/controllers/scoped_views.rb +33 -0
- data/lib/devise/controllers/url_helpers.rb +39 -0
- data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
- data/lib/devise/encryptors/base.rb +20 -0
- data/lib/devise/encryptors/clearance_sha1.rb +17 -0
- data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
- data/lib/devise/encryptors/sha1.rb +25 -0
- data/lib/devise/encryptors/sha512.rb +25 -0
- data/lib/devise/failure_app.rb +132 -0
- data/lib/devise/hooks/activatable.rb +11 -0
- data/lib/devise/hooks/forgetable.rb +12 -0
- data/lib/devise/hooks/rememberable.rb +48 -0
- data/lib/devise/hooks/timeoutable.rb +22 -0
- data/lib/devise/hooks/trackable.rb +9 -0
- data/lib/devise/mapping.rb +110 -0
- data/lib/devise/models/authenticatable.rb +146 -0
- data/lib/devise/models/confirmable.rb +160 -0
- data/lib/devise/models/database_authenticatable.rb +100 -0
- data/lib/devise/models/encryptable.rb +72 -0
- data/lib/devise/models/lockable.rb +169 -0
- data/lib/devise/models/omniauthable.rb +23 -0
- data/lib/devise/models/recoverable.rb +123 -0
- data/lib/devise/models/registerable.rb +21 -0
- data/lib/devise/models/rememberable.rb +130 -0
- data/lib/devise/models/timeoutable.rb +43 -0
- data/lib/devise/models/token_authenticatable.rb +72 -0
- data/lib/devise/models/trackable.rb +30 -0
- data/lib/devise/models/validatable.rb +65 -0
- data/lib/devise/models.rb +68 -0
- data/lib/devise/modules.rb +30 -0
- data/lib/devise/omniauth/config.rb +30 -0
- data/lib/devise/omniauth/test_helpers.rb +57 -0
- data/lib/devise/omniauth/url_helpers.rb +29 -0
- data/lib/devise/omniauth.rb +47 -0
- data/lib/devise/orm/active_record.rb +38 -0
- data/lib/devise/orm/mongoid.rb +31 -0
- data/lib/devise/path_checker.rb +18 -0
- data/lib/devise/rails/routes.rb +292 -0
- data/lib/devise/rails/warden_compat.rb +125 -0
- data/lib/devise/rails.rb +50 -0
- data/lib/devise/schema.rb +97 -0
- data/lib/devise/strategies/authenticatable.rb +150 -0
- data/lib/devise/strategies/base.rb +15 -0
- data/lib/devise/strategies/database_authenticatable.rb +21 -0
- data/lib/devise/strategies/rememberable.rb +51 -0
- data/lib/devise/strategies/token_authenticatable.rb +53 -0
- data/lib/devise/test_helpers.rb +100 -0
- data/lib/devise/version.rb +3 -0
- data/lib/devise.rb +381 -0
- data/lib/generators/active_record/devise_generator.rb +28 -0
- data/lib/generators/active_record/templates/migration.rb +31 -0
- data/lib/generators/devise/devise_generator.rb +17 -0
- data/lib/generators/devise/install_generator.rb +24 -0
- data/lib/generators/devise/orm_helpers.rb +23 -0
- data/lib/generators/devise/views_generator.rb +106 -0
- data/lib/generators/mongoid/devise_generator.rb +17 -0
- data/lib/generators/templates/README +25 -0
- data/lib/generators/templates/devise.rb +186 -0
- data/test/controllers/helpers_test.rb +237 -0
- data/test/controllers/internal_helpers_test.rb +72 -0
- data/test/controllers/url_helpers_test.rb +59 -0
- data/test/devise_test.rb +65 -0
- data/test/encryptors_test.rb +30 -0
- data/test/failure_app_test.rb +187 -0
- data/test/generators/active_record_generator_test.rb +24 -0
- data/test/generators/install_generator_test.rb +13 -0
- data/test/generators/mongoid_generator_test.rb +22 -0
- data/test/generators/views_generator_test.rb +35 -0
- data/test/indifferent_hash.rb +33 -0
- data/test/integration/authenticatable_test.rb +447 -0
- data/test/integration/confirmable_test.rb +104 -0
- data/test/integration/database_authenticatable_test.rb +60 -0
- data/test/integration/http_authenticatable_test.rb +74 -0
- data/test/integration/lockable_test.rb +109 -0
- data/test/integration/omniauthable_test.rb +107 -0
- data/test/integration/recoverable_test.rb +160 -0
- data/test/integration/registerable_test.rb +179 -0
- data/test/integration/rememberable_test.rb +180 -0
- data/test/integration/timeoutable_test.rb +89 -0
- data/test/integration/token_authenticatable_test.rb +99 -0
- data/test/integration/trackable_test.rb +64 -0
- data/test/mailers/confirmation_instructions_test.rb +84 -0
- data/test/mailers/reset_password_instructions_test.rb +72 -0
- data/test/mailers/unlock_instructions_test.rb +66 -0
- data/test/mapping_test.rb +119 -0
- data/test/models/confirmable_test.rb +221 -0
- data/test/models/database_authenticatable_test.rb +98 -0
- data/test/models/encryptable_test.rb +65 -0
- data/test/models/lockable_test.rb +204 -0
- data/test/models/recoverable_test.rb +190 -0
- data/test/models/rememberable_test.rb +279 -0
- data/test/models/timeoutable_test.rb +28 -0
- data/test/models/token_authenticatable_test.rb +37 -0
- data/test/models/trackable_test.rb +5 -0
- data/test/models/validatable_test.rb +99 -0
- data/test/models_test.rb +84 -0
- data/test/omniauth/url_helpers_test.rb +47 -0
- data/test/orm/active_record.rb +9 -0
- data/test/orm/mongoid.rb +11 -0
- data/test/rails_app/Rakefile +10 -0
- data/test/rails_app/app/active_record/admin.rb +6 -0
- data/test/rails_app/app/active_record/shim.rb +2 -0
- data/test/rails_app/app/active_record/user.rb +8 -0
- data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
- data/test/rails_app/app/controllers/admins_controller.rb +6 -0
- data/test/rails_app/app/controllers/application_controller.rb +8 -0
- data/test/rails_app/app/controllers/home_controller.rb +16 -0
- data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
- data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
- data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +7 -0
- data/test/rails_app/app/controllers/users_controller.rb +18 -0
- data/test/rails_app/app/helpers/application_helper.rb +3 -0
- data/test/rails_app/app/mongoid/admin.rb +9 -0
- data/test/rails_app/app/mongoid/shim.rb +29 -0
- data/test/rails_app/app/mongoid/user.rb +10 -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/index.html.erb +1 -0
- data/test/rails_app/app/views/home/private.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 +40 -0
- data/test/rails_app/config/boot.rb +13 -0
- data/test/rails_app/config/database.yml +18 -0
- data/test/rails_app/config/environment.rb +5 -0
- data/test/rails_app/config/environments/development.rb +19 -0
- data/test/rails_app/config/environments/production.rb +33 -0
- data/test/rails_app/config/environments/test.rb +33 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/test/rails_app/config/initializers/devise.rb +176 -0
- data/test/rails_app/config/initializers/inflections.rb +2 -0
- data/test/rails_app/config/initializers/secret_token.rb +2 -0
- data/test/rails_app/config/routes.rb +55 -0
- data/test/rails_app/config.ru +4 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +31 -0
- data/test/rails_app/db/schema.rb +52 -0
- data/test/rails_app/lib/shared_admin.rb +9 -0
- data/test/rails_app/lib/shared_user.rb +23 -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 +179 -0
- data/test/support/assertions.rb +24 -0
- data/test/support/helpers.rb +60 -0
- data/test/support/integration.rb +88 -0
- data/test/support/locale/en.yml +4 -0
- data/test/support/webrat/integrations/rails.rb +24 -0
- data/test/test_helper.rb +29 -0
- data/test/test_helpers_test.rb +118 -0
- metadata +388 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class RememberMeTest < ActionController::IntegrationTest
|
|
4
|
+
def create_user_and_remember(add_to_token='')
|
|
5
|
+
user = create_user
|
|
6
|
+
user.remember_me!
|
|
7
|
+
raw_cookie = User.serialize_into_cookie(user).tap { |a| a.last << add_to_token }
|
|
8
|
+
cookies['remember_user_token'] = generate_signed_cookie(raw_cookie)
|
|
9
|
+
user
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def create_admin_and_remember
|
|
13
|
+
admin = create_admin
|
|
14
|
+
admin.remember_me!
|
|
15
|
+
raw_cookie = Admin.serialize_into_cookie(admin)
|
|
16
|
+
cookies['remember_admin_token'] = generate_signed_cookie(raw_cookie)
|
|
17
|
+
admin
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def generate_signed_cookie(raw_cookie)
|
|
21
|
+
request = ActionDispatch::TestRequest.new
|
|
22
|
+
request.cookie_jar.signed['raw_cookie'] = raw_cookie
|
|
23
|
+
request.cookie_jar['raw_cookie']
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def signed_cookie(key)
|
|
27
|
+
controller.send(:cookies).signed[key]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def cookie_expires(key)
|
|
31
|
+
cookie = response.headers["Set-Cookie"].split("\n").grep(/^#{key}/).first
|
|
32
|
+
expires = cookie.split(";").map(&:strip).grep(/^expires=/).first
|
|
33
|
+
Time.parse(expires)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
test 'do not remember the user if he has not checked remember me option' do
|
|
37
|
+
user = sign_in_as_user
|
|
38
|
+
assert_nil request.cookies["remember_user_cookie"]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
test 'generate remember token after sign in' do
|
|
42
|
+
user = sign_in_as_user :remember_me => true
|
|
43
|
+
assert request.cookies["remember_user_token"]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
test 'generate remember token after sign in setting cookie options' do
|
|
47
|
+
# We test this by asserting the cookie is not sent after the redirect
|
|
48
|
+
# since we changed the domain. This is the only difference with the
|
|
49
|
+
# previous test.
|
|
50
|
+
swap Devise, :cookie_options => { :domain => "omg.somewhere.com" } do
|
|
51
|
+
user = sign_in_as_user :remember_me => true
|
|
52
|
+
assert_nil request.cookies["remember_user_token"]
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
test 'generate remember token after sign in setting session options' do
|
|
57
|
+
begin
|
|
58
|
+
Rails.configuration.session_options[:domain] = "omg.somewhere.com"
|
|
59
|
+
user = sign_in_as_user :remember_me => true
|
|
60
|
+
assert_nil request.cookies["remember_user_token"]
|
|
61
|
+
ensure
|
|
62
|
+
Rails.configuration.session_options.delete(:domain)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
test 'remember the user before sign in' do
|
|
67
|
+
user = create_user_and_remember
|
|
68
|
+
get users_path
|
|
69
|
+
assert_response :success
|
|
70
|
+
assert warden.authenticated?(:user)
|
|
71
|
+
assert warden.user(:user) == user
|
|
72
|
+
assert_match /remember_user_token[^\n]*HttpOnly\n/, response.headers["Set-Cookie"], "Expected Set-Cookie header in response to set HttpOnly flag on remember_user_token cookie."
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
test 'does not extend remember period through sign in' do
|
|
76
|
+
swap Devise, :extend_remember_period => true, :remember_for => 1.year do
|
|
77
|
+
user = create_user
|
|
78
|
+
user.remember_me!
|
|
79
|
+
|
|
80
|
+
user.remember_created_at = old = 10.days.ago
|
|
81
|
+
user.save
|
|
82
|
+
|
|
83
|
+
sign_in_as_user :remember_me => true
|
|
84
|
+
user.reload
|
|
85
|
+
|
|
86
|
+
assert warden.user(:user) == user
|
|
87
|
+
assert_equal old.to_i, user.remember_created_at.to_i
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
test 'if both extend_remember_period and remember_across_browsers are true, sends the same token with a new expire date' do
|
|
92
|
+
swap Devise, :remember_across_browsers => true, :extend_remember_period => true, :remember_for => 1.year do
|
|
93
|
+
admin = create_admin_and_remember
|
|
94
|
+
token = admin.remember_token
|
|
95
|
+
|
|
96
|
+
admin.remember_created_at = old = 10.minutes.ago
|
|
97
|
+
admin.save!
|
|
98
|
+
|
|
99
|
+
get root_path
|
|
100
|
+
assert (cookie_expires("remember_admin_token") - 1.year) > (old + 5.minutes)
|
|
101
|
+
assert_equal token, signed_cookie("remember_admin_token").last
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
test 'if both extend_remember_period and remember_across_browsers are false, sends a new token with old expire date' do
|
|
106
|
+
swap Devise, :remember_across_browsers => false, :extend_remember_period => false, :remember_for => 1.year do
|
|
107
|
+
admin = create_admin_and_remember
|
|
108
|
+
token = admin.remember_token
|
|
109
|
+
|
|
110
|
+
admin.remember_created_at = old = 10.minutes.ago
|
|
111
|
+
admin.save!
|
|
112
|
+
|
|
113
|
+
get root_path
|
|
114
|
+
assert (cookie_expires("remember_admin_token") - 1.year) < (old + 5.minutes)
|
|
115
|
+
assert_not_equal token, signed_cookie("remember_admin_token").last
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
test 'do not remember other scopes' do
|
|
120
|
+
user = create_user_and_remember
|
|
121
|
+
get root_path
|
|
122
|
+
assert_response :success
|
|
123
|
+
assert warden.authenticated?(:user)
|
|
124
|
+
assert_not warden.authenticated?(:admin)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
test 'do not remember with invalid token' do
|
|
128
|
+
user = create_user_and_remember('add')
|
|
129
|
+
get users_path
|
|
130
|
+
assert_not warden.authenticated?(:user)
|
|
131
|
+
assert_redirected_to new_user_session_path
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
test 'do not remember with expired token' do
|
|
135
|
+
user = create_user_and_remember
|
|
136
|
+
swap Devise, :remember_for => 0 do
|
|
137
|
+
get users_path
|
|
138
|
+
assert_not warden.authenticated?(:user)
|
|
139
|
+
assert_redirected_to new_user_session_path
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
test 'do not remember the user anymore after forget' do
|
|
144
|
+
user = create_user_and_remember
|
|
145
|
+
get users_path
|
|
146
|
+
assert warden.authenticated?(:user)
|
|
147
|
+
|
|
148
|
+
get destroy_user_session_path
|
|
149
|
+
assert_not warden.authenticated?(:user)
|
|
150
|
+
assert_nil warden.cookies['remember_user_token']
|
|
151
|
+
|
|
152
|
+
get users_path
|
|
153
|
+
assert_not warden.authenticated?(:user)
|
|
154
|
+
assert_nil warden.cookies['remember_user_token']
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
test 'do not remember the admin anymore after forget' do
|
|
158
|
+
admin = create_admin_and_remember
|
|
159
|
+
get root_path
|
|
160
|
+
assert warden.authenticated?(:admin)
|
|
161
|
+
|
|
162
|
+
get destroy_admin_session_path
|
|
163
|
+
assert_not warden.authenticated?(:admin)
|
|
164
|
+
assert_nil warden.cookies['remember_admin_token']
|
|
165
|
+
|
|
166
|
+
get root_path
|
|
167
|
+
assert_not warden.authenticated?(:admin)
|
|
168
|
+
assert_nil warden.cookies['remember_admin_token']
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
test 'changing user password expires remember me token' do
|
|
172
|
+
user = create_user_and_remember
|
|
173
|
+
user.password = "another_password"
|
|
174
|
+
user.password_confirmation = "another_password"
|
|
175
|
+
user.save!
|
|
176
|
+
|
|
177
|
+
get users_path
|
|
178
|
+
assert_not warden.authenticated?(:user)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class SessionTimeoutTest < ActionController::IntegrationTest
|
|
4
|
+
|
|
5
|
+
def last_request_at
|
|
6
|
+
@controller.user_session['last_request_at']
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
test 'set last request at in user session after each request' do
|
|
10
|
+
sign_in_as_user
|
|
11
|
+
old_last_request = last_request_at
|
|
12
|
+
assert_not_nil last_request_at
|
|
13
|
+
|
|
14
|
+
get users_path
|
|
15
|
+
assert_not_nil last_request_at
|
|
16
|
+
assert_not_equal old_last_request, last_request_at
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
test 'not time out user session before default limit time' do
|
|
20
|
+
sign_in_as_user
|
|
21
|
+
assert_response :success
|
|
22
|
+
assert warden.authenticated?(:user)
|
|
23
|
+
|
|
24
|
+
get users_path
|
|
25
|
+
assert_response :success
|
|
26
|
+
assert warden.authenticated?(:user)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
test 'time out user session after default limit time' do
|
|
30
|
+
user = sign_in_as_user
|
|
31
|
+
get expire_user_path(user)
|
|
32
|
+
assert_not_nil last_request_at
|
|
33
|
+
|
|
34
|
+
get users_path
|
|
35
|
+
assert_redirected_to new_user_session_path
|
|
36
|
+
assert_not warden.authenticated?(:user)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
test 'time out is not triggered on sign out' do
|
|
40
|
+
user = sign_in_as_user
|
|
41
|
+
get expire_user_path(user)
|
|
42
|
+
|
|
43
|
+
get destroy_user_session_path
|
|
44
|
+
assert_response :redirect
|
|
45
|
+
assert_redirected_to root_path
|
|
46
|
+
|
|
47
|
+
follow_redirect!
|
|
48
|
+
assert_contain 'Signed out successfully'
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
test 'user configured timeout limit' do
|
|
52
|
+
swap Devise, :timeout_in => 8.minutes do
|
|
53
|
+
user = sign_in_as_user
|
|
54
|
+
|
|
55
|
+
get users_path
|
|
56
|
+
assert_not_nil last_request_at
|
|
57
|
+
assert_response :success
|
|
58
|
+
assert warden.authenticated?(:user)
|
|
59
|
+
|
|
60
|
+
get expire_user_path(user)
|
|
61
|
+
get users_path
|
|
62
|
+
assert_redirected_to new_user_session_path
|
|
63
|
+
assert_not warden.authenticated?(:user)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
test 'error message with i18n' do
|
|
68
|
+
store_translations :en, :devise => {
|
|
69
|
+
:failure => { :user => { :timeout => 'Session expired!' } }
|
|
70
|
+
} do
|
|
71
|
+
user = sign_in_as_user
|
|
72
|
+
|
|
73
|
+
get expire_user_path(user)
|
|
74
|
+
get users_path
|
|
75
|
+
follow_redirect!
|
|
76
|
+
assert_contain 'Session expired!'
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
test 'time out not triggered if remembered' do
|
|
81
|
+
user = sign_in_as_user :remember_me => true
|
|
82
|
+
get expire_user_path(user)
|
|
83
|
+
assert_not_nil last_request_at
|
|
84
|
+
|
|
85
|
+
get users_path
|
|
86
|
+
assert_response :success
|
|
87
|
+
assert warden.authenticated?(:user)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class TokenAuthenticationTest < ActionController::IntegrationTest
|
|
4
|
+
|
|
5
|
+
test 'authenticate with valid authentication token key and value through params' do
|
|
6
|
+
swap Devise, :token_authentication_key => :secret_token do
|
|
7
|
+
sign_in_as_new_user_with_token
|
|
8
|
+
|
|
9
|
+
assert_response :success
|
|
10
|
+
assert_current_url "/users?secret_token=#{VALID_AUTHENTICATION_TOKEN}"
|
|
11
|
+
assert_contain 'Welcome'
|
|
12
|
+
assert warden.authenticated?(:user)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
test 'authenticate with valid authentication token key but does not store if stateless' do
|
|
17
|
+
swap Devise, :token_authentication_key => :secret_token, :stateless_token => true do
|
|
18
|
+
sign_in_as_new_user_with_token
|
|
19
|
+
assert warden.authenticated?(:user)
|
|
20
|
+
|
|
21
|
+
get users_path
|
|
22
|
+
assert_redirected_to new_user_session_path
|
|
23
|
+
assert_not warden.authenticated?(:user)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test 'authenticate with valid authentication token key and value through http' do
|
|
28
|
+
swap Devise, :token_authentication_key => :secret_token do
|
|
29
|
+
sign_in_as_new_user_with_token(:http_auth => true)
|
|
30
|
+
|
|
31
|
+
assert_response :success
|
|
32
|
+
assert_match '<email>user@test.com</email>', response.body
|
|
33
|
+
assert warden.authenticated?(:user)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
test 'does authenticate with valid authentication token key and value through params if not configured' do
|
|
38
|
+
swap Devise, :token_authentication_key => :secret_token, :params_authenticatable => [:database] do
|
|
39
|
+
sign_in_as_new_user_with_token
|
|
40
|
+
|
|
41
|
+
assert_contain 'You need to sign in or sign up before continuing'
|
|
42
|
+
assert_contain 'Sign in'
|
|
43
|
+
assert_not warden.authenticated?(:user)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
test 'does authenticate with valid authentication token key and value through http if not configured' do
|
|
48
|
+
swap Devise, :token_authentication_key => :secret_token, :http_authenticatable => [:database] do
|
|
49
|
+
sign_in_as_new_user_with_token(:http_auth => true)
|
|
50
|
+
|
|
51
|
+
assert_response 401
|
|
52
|
+
assert_contain 'Invalid email or password.'
|
|
53
|
+
assert_not warden.authenticated?(:user)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
test 'does not authenticate with improper authentication token key' do
|
|
58
|
+
swap Devise, :token_authentication_key => :donald_duck_token do
|
|
59
|
+
sign_in_as_new_user_with_token(:auth_token_key => :secret_token)
|
|
60
|
+
assert_equal new_user_session_path, @request.path
|
|
61
|
+
|
|
62
|
+
assert_contain 'You need to sign in or sign up before continuing'
|
|
63
|
+
assert_contain 'Sign in'
|
|
64
|
+
assert_not warden.authenticated?(:user)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
test 'does not authenticate with improper authentication token value' do
|
|
69
|
+
store_translations :en, :devise => {:failure => {:invalid_token => 'LOL, that was not a single character correct.'}} do
|
|
70
|
+
sign_in_as_new_user_with_token(:auth_token => '*** INVALID TOKEN ***')
|
|
71
|
+
assert_equal new_user_session_path, @request.path
|
|
72
|
+
|
|
73
|
+
assert_contain 'LOL, that was not a single character correct.'
|
|
74
|
+
assert_contain 'Sign in'
|
|
75
|
+
assert_not warden.authenticated?(:user)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def sign_in_as_new_user_with_token(options = {})
|
|
82
|
+
options[:auth_token_key] ||= Devise.token_authentication_key
|
|
83
|
+
options[:auth_token] ||= VALID_AUTHENTICATION_TOKEN
|
|
84
|
+
|
|
85
|
+
user = create_user(options)
|
|
86
|
+
user.authentication_token = VALID_AUTHENTICATION_TOKEN
|
|
87
|
+
user.save
|
|
88
|
+
|
|
89
|
+
if options[:http_auth]
|
|
90
|
+
header = "Basic #{ActiveSupport::Base64.encode64("#{VALID_AUTHENTICATION_TOKEN}:X")}"
|
|
91
|
+
get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => header
|
|
92
|
+
else
|
|
93
|
+
visit users_path(options[:auth_token_key].to_sym => options[:auth_token])
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
user
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class TrackableHooksTest < ActionController::IntegrationTest
|
|
4
|
+
|
|
5
|
+
test "current and last sign in timestamps are updated on each sign in" do
|
|
6
|
+
user = create_user
|
|
7
|
+
assert_nil user.current_sign_in_at
|
|
8
|
+
assert_nil user.last_sign_in_at
|
|
9
|
+
|
|
10
|
+
sign_in_as_user
|
|
11
|
+
user.reload
|
|
12
|
+
|
|
13
|
+
assert_kind_of Time, user.current_sign_in_at
|
|
14
|
+
assert_kind_of Time, user.last_sign_in_at
|
|
15
|
+
|
|
16
|
+
assert_equal user.current_sign_in_at, user.last_sign_in_at
|
|
17
|
+
assert user.current_sign_in_at >= user.created_at
|
|
18
|
+
|
|
19
|
+
visit destroy_user_session_path
|
|
20
|
+
new_time = 2.seconds.from_now
|
|
21
|
+
Time.stubs(:now).returns(new_time)
|
|
22
|
+
|
|
23
|
+
sign_in_as_user
|
|
24
|
+
user.reload
|
|
25
|
+
assert user.current_sign_in_at > user.last_sign_in_at
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
test "current and last sign in remote ip are updated on each sign in" do
|
|
29
|
+
user = create_user
|
|
30
|
+
assert_nil user.current_sign_in_ip
|
|
31
|
+
assert_nil user.last_sign_in_ip
|
|
32
|
+
|
|
33
|
+
sign_in_as_user
|
|
34
|
+
user.reload
|
|
35
|
+
|
|
36
|
+
assert_equal "127.0.0.1", user.current_sign_in_ip
|
|
37
|
+
assert_equal "127.0.0.1", user.last_sign_in_ip
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
test "increase sign in count" do
|
|
41
|
+
user = create_user
|
|
42
|
+
assert_equal 0, user.sign_in_count
|
|
43
|
+
|
|
44
|
+
sign_in_as_user
|
|
45
|
+
user.reload
|
|
46
|
+
assert_equal 1, user.sign_in_count
|
|
47
|
+
|
|
48
|
+
visit destroy_user_session_path
|
|
49
|
+
sign_in_as_user
|
|
50
|
+
user.reload
|
|
51
|
+
assert_equal 2, user.sign_in_count
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
test "does not update anything if user has signed out along the way" do
|
|
55
|
+
swap Devise, :confirm_within => 0 do
|
|
56
|
+
user = create_user(:confirm => false)
|
|
57
|
+
sign_in_as_user
|
|
58
|
+
|
|
59
|
+
user.reload
|
|
60
|
+
assert_nil user.current_sign_in_at
|
|
61
|
+
assert_nil user.last_sign_in_at
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class ConfirmationInstructionsTest < ActionMailer::TestCase
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
setup_mailer
|
|
7
|
+
Devise.mailer_sender = 'test@example.com'
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def user
|
|
11
|
+
@user ||= create_user
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def mail
|
|
15
|
+
@mail ||= begin
|
|
16
|
+
user
|
|
17
|
+
ActionMailer::Base.deliveries.first
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test 'email sent after creating the user' do
|
|
22
|
+
assert_not_nil mail
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
test 'content type should be set to html' do
|
|
26
|
+
assert mail.content_type.include?('text/html')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
test 'send confirmation instructions to the user email' do
|
|
30
|
+
mail
|
|
31
|
+
assert_equal [user.email], mail.to
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
test 'setup sender from configuration' do
|
|
35
|
+
assert_equal ['test@example.com'], mail.from
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
test 'setup reply to as copy from sender' do
|
|
39
|
+
assert_equal ['test@example.com'], mail.reply_to
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
test 'setup subject from I18n' do
|
|
43
|
+
store_translations :en, :devise => { :mailer => { :confirmation_instructions => { :subject => 'Account Confirmation' } } } do
|
|
44
|
+
assert_equal 'Account Confirmation', mail.subject
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
test 'subject namespaced by model' do
|
|
49
|
+
store_translations :en, :devise => { :mailer => { :confirmation_instructions => { :user_subject => 'User Account Confirmation' } } } do
|
|
50
|
+
assert_equal 'User Account Confirmation', mail.subject
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
test 'body should have user info' do
|
|
55
|
+
assert_match /#{user.email}/, mail.body.encoded
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
test 'body should have link to confirm the account' do
|
|
59
|
+
host = ActionMailer::Base.default_url_options[:host]
|
|
60
|
+
confirmation_url_regexp = %r{<a href=\"http://#{host}/users/confirmation\?confirmation_token=#{user.confirmation_token}">}
|
|
61
|
+
assert_match confirmation_url_regexp, mail.body.encoded
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
test 'renders a scoped if scoped_views is set to true' do
|
|
65
|
+
swap Devise, :scoped_views => true do
|
|
66
|
+
assert_equal user.email, mail.body.decoded
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
test 'renders a scoped if scoped_views is set in the mailer class' do
|
|
71
|
+
begin
|
|
72
|
+
Devise::Mailer.scoped_views = true
|
|
73
|
+
assert_equal user.email, mail.body.decoded
|
|
74
|
+
ensure
|
|
75
|
+
Devise::Mailer.send :remove_instance_variable, :@scoped_views
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
test 'mailer sender accepts a proc' do
|
|
80
|
+
swap Devise, :mailer_sender => proc { "another@example.com" } do
|
|
81
|
+
assert_equal ['another@example.com'], mail.from
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class ResetPasswordInstructionsTest < ActionMailer::TestCase
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
setup_mailer
|
|
7
|
+
Devise.mailer_sender = 'test@example.com'
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def user
|
|
11
|
+
@user ||= begin
|
|
12
|
+
user = create_user
|
|
13
|
+
user.send_reset_password_instructions
|
|
14
|
+
user
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def mail
|
|
19
|
+
@mail ||= begin
|
|
20
|
+
user
|
|
21
|
+
ActionMailer::Base.deliveries.last
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
test 'email sent after reseting the user password' do
|
|
26
|
+
assert_not_nil mail
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
test 'content type should be set to html' do
|
|
30
|
+
assert mail.content_type.include?('text/html')
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
test 'send confirmation instructions to the user email' do
|
|
34
|
+
assert_equal [user.email], mail.to
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
test 'setup sender from configuration' do
|
|
38
|
+
assert_equal ['test@example.com'], mail.from
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
test 'setup reply to as copy from sender' do
|
|
42
|
+
assert_equal ['test@example.com'], mail.reply_to
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
test 'setup subject from I18n' do
|
|
46
|
+
store_translations :en, :devise => { :mailer => { :reset_password_instructions => { :subject => 'Reset instructions' } } } do
|
|
47
|
+
assert_equal 'Reset instructions', mail.subject
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
test 'subject namespaced by model' do
|
|
52
|
+
store_translations :en, :devise => { :mailer => { :reset_password_instructions => { :user_subject => 'User Reset Instructions' } } } do
|
|
53
|
+
assert_equal 'User Reset Instructions', mail.subject
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
test 'body should have user info' do
|
|
58
|
+
assert_match(/#{user.email}/, mail.body.encoded)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
test 'body should have link to confirm the account' do
|
|
62
|
+
host = ActionMailer::Base.default_url_options[:host]
|
|
63
|
+
reset_url_regexp = %r{<a href=\"http://#{host}/users/password/edit\?reset_password_token=#{user.reset_password_token}">}
|
|
64
|
+
assert_match reset_url_regexp, mail.body.encoded
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
test 'mailer sender accepts a proc' do
|
|
68
|
+
swap Devise, :mailer_sender => proc { "another@example.com" } do
|
|
69
|
+
assert_equal ['another@example.com'], mail.from
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class UnlockInstructionsTest < ActionMailer::TestCase
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
setup_mailer
|
|
7
|
+
Devise.mailer_sender = 'test@example.com'
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def user
|
|
11
|
+
@user ||= begin
|
|
12
|
+
user = create_user
|
|
13
|
+
user.lock_access!
|
|
14
|
+
user
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def mail
|
|
19
|
+
@mail ||= begin
|
|
20
|
+
user
|
|
21
|
+
ActionMailer::Base.deliveries.last
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
test 'email sent after locking the user' do
|
|
26
|
+
assert_not_nil mail
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
test 'content type should be set to html' do
|
|
30
|
+
assert mail.content_type.include?('text/html')
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
test 'send unlock instructions to the user email' do
|
|
34
|
+
assert_equal [user.email], mail.to
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
test 'setup sender from configuration' do
|
|
38
|
+
assert_equal ['test@example.com'], mail.from
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
test 'setup reply to as copy from sender' do
|
|
42
|
+
assert_equal ['test@example.com'], mail.reply_to
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
test 'setup subject from I18n' do
|
|
46
|
+
store_translations :en, :devise => { :mailer => { :unlock_instructions => { :subject => 'Yo unlock instructions' } } } do
|
|
47
|
+
assert_equal 'Yo unlock instructions', mail.subject
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
test 'subject namespaced by model' do
|
|
52
|
+
store_translations :en, :devise => { :mailer => { :unlock_instructions => { :user_subject => 'User Unlock Instructions' } } } do
|
|
53
|
+
assert_equal 'User Unlock Instructions', mail.subject
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
test 'body should have user info' do
|
|
58
|
+
assert_match(/#{user.email}/, mail.body.encoded)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
test 'body should have link to unlock the account' do
|
|
62
|
+
host = ActionMailer::Base.default_url_options[:host]
|
|
63
|
+
unlock_url_regexp = %r{<a href=\"http://#{host}/users/unlock\?unlock_token=#{user.unlock_token}">}
|
|
64
|
+
assert_match unlock_url_regexp, mail.body.encoded
|
|
65
|
+
end
|
|
66
|
+
end
|