af-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 +885 -0
- data/CONTRIBUTING.md +14 -0
- data/Gemfile +29 -0
- data/Gemfile.lock +155 -0
- data/MIT-LICENSE +20 -0
- data/README.md +394 -0
- data/Rakefile +34 -0
- data/app/controllers/devise/confirmations_controller.rb +43 -0
- data/app/controllers/devise/omniauth_callbacks_controller.rb +30 -0
- data/app/controllers/devise/passwords_controller.rb +65 -0
- data/app/controllers/devise/registrations_controller.rb +119 -0
- data/app/controllers/devise/sessions_controller.rb +50 -0
- data/app/controllers/devise/unlocks_controller.rb +44 -0
- data/app/controllers/devise_controller.rb +184 -0
- data/app/helpers/devise_helper.rb +25 -0
- data/app/mailers/devise/mailer.rb +15 -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 +25 -0
- data/gemfiles/Gemfile.rails-3.1.x +35 -0
- data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
- data/lib/devise.rb +444 -0
- data/lib/devise/controllers/helpers.rb +285 -0
- data/lib/devise/controllers/rememberable.rb +52 -0
- data/lib/devise/controllers/scoped_views.rb +17 -0
- data/lib/devise/controllers/url_helpers.rb +67 -0
- data/lib/devise/delegator.rb +16 -0
- data/lib/devise/failure_app.rb +187 -0
- data/lib/devise/hooks/activatable.rb +11 -0
- data/lib/devise/hooks/forgetable.rb +9 -0
- data/lib/devise/hooks/lockable.rb +7 -0
- data/lib/devise/hooks/rememberable.rb +6 -0
- data/lib/devise/hooks/timeoutable.rb +25 -0
- data/lib/devise/hooks/trackable.rb +9 -0
- data/lib/devise/mailers/helpers.rb +91 -0
- data/lib/devise/mapping.rb +172 -0
- data/lib/devise/models.rb +128 -0
- data/lib/devise/models/authenticatable.rb +268 -0
- data/lib/devise/models/confirmable.rb +270 -0
- data/lib/devise/models/database_authenticatable.rb +127 -0
- data/lib/devise/models/lockable.rb +193 -0
- data/lib/devise/models/omniauthable.rb +27 -0
- data/lib/devise/models/recoverable.rb +140 -0
- data/lib/devise/models/registerable.rb +25 -0
- data/lib/devise/models/rememberable.rb +125 -0
- data/lib/devise/models/timeoutable.rb +49 -0
- data/lib/devise/models/token_authenticatable.rb +89 -0
- data/lib/devise/models/trackable.rb +35 -0
- data/lib/devise/models/validatable.rb +66 -0
- data/lib/devise/modules.rb +29 -0
- data/lib/devise/omniauth.rb +28 -0
- data/lib/devise/omniauth/config.rb +45 -0
- data/lib/devise/omniauth/url_helpers.rb +18 -0
- data/lib/devise/orm/active_record.rb +3 -0
- data/lib/devise/orm/mongoid.rb +3 -0
- data/lib/devise/param_filter.rb +41 -0
- data/lib/devise/rails.rb +54 -0
- data/lib/devise/rails/routes.rb +446 -0
- data/lib/devise/rails/warden_compat.rb +43 -0
- data/lib/devise/strategies/authenticatable.rb +176 -0
- data/lib/devise/strategies/base.rb +20 -0
- data/lib/devise/strategies/database_authenticatable.rb +20 -0
- data/lib/devise/strategies/rememberable.rb +55 -0
- data/lib/devise/strategies/token_authenticatable.rb +56 -0
- data/lib/devise/test_helpers.rb +131 -0
- data/lib/devise/time_inflector.rb +14 -0
- data/lib/devise/version.rb +3 -0
- data/lib/generators/active_record/devise_generator.rb +79 -0
- data/lib/generators/active_record/templates/migration.rb +19 -0
- data/lib/generators/active_record/templates/migration_existing.rb +26 -0
- data/lib/generators/devise/devise_generator.rb +24 -0
- data/lib/generators/devise/install_generator.rb +24 -0
- data/lib/generators/devise/orm_helpers.rb +32 -0
- data/lib/generators/devise/views_generator.rb +116 -0
- data/lib/generators/mongoid/devise_generator.rb +57 -0
- data/lib/generators/templates/README +35 -0
- data/lib/generators/templates/devise.rb +240 -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 +62 -0
- data/test/controllers/helpers_test.rb +253 -0
- data/test/controllers/internal_helpers_test.rb +110 -0
- data/test/controllers/sessions_controller_test.rb +85 -0
- data/test/controllers/url_helpers_test.rb +59 -0
- data/test/delegator_test.rb +19 -0
- data/test/devise_test.rb +72 -0
- data/test/failure_app_test.rb +221 -0
- data/test/generators/active_record_generator_test.rb +75 -0
- data/test/generators/devise_generator_test.rb +39 -0
- data/test/generators/install_generator_test.rb +13 -0
- data/test/generators/mongoid_generator_test.rb +23 -0
- data/test/generators/views_generator_test.rb +52 -0
- data/test/helpers/devise_helper_test.rb +51 -0
- data/test/integration/authenticatable_test.rb +633 -0
- data/test/integration/confirmable_test.rb +298 -0
- data/test/integration/database_authenticatable_test.rb +82 -0
- data/test/integration/http_authenticatable_test.rb +97 -0
- data/test/integration/lockable_test.rb +242 -0
- data/test/integration/omniauthable_test.rb +133 -0
- data/test/integration/recoverable_test.rb +334 -0
- data/test/integration/registerable_test.rb +345 -0
- data/test/integration/rememberable_test.rb +158 -0
- data/test/integration/timeoutable_test.rb +140 -0
- data/test/integration/token_authenticatable_test.rb +161 -0
- data/test/integration/trackable_test.rb +92 -0
- data/test/mailers/confirmation_instructions_test.rb +102 -0
- data/test/mailers/reset_password_instructions_test.rb +83 -0
- data/test/mailers/unlock_instructions_test.rb +77 -0
- data/test/mapping_test.rb +127 -0
- data/test/models/authenticatable_test.rb +7 -0
- data/test/models/confirmable_test.rb +391 -0
- data/test/models/database_authenticatable_test.rb +196 -0
- data/test/models/lockable_test.rb +273 -0
- data/test/models/omniauthable_test.rb +7 -0
- data/test/models/recoverable_test.rb +205 -0
- data/test/models/registerable_test.rb +7 -0
- data/test/models/rememberable_test.rb +174 -0
- data/test/models/serializable_test.rb +49 -0
- data/test/models/timeoutable_test.rb +46 -0
- data/test/models/token_authenticatable_test.rb +55 -0
- data/test/models/trackable_test.rb +13 -0
- data/test/models/validatable_test.rb +117 -0
- data/test/models_test.rb +179 -0
- data/test/omniauth/config_test.rb +57 -0
- data/test/omniauth/url_helpers_test.rb +51 -0
- data/test/orm/active_record.rb +9 -0
- data/test/orm/mongoid.rb +13 -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 +6 -0
- data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
- data/test/rails_app/app/controllers/admins_controller.rb +11 -0
- data/test/rails_app/app/controllers/application_controller.rb +8 -0
- data/test/rails_app/app/controllers/home_controller.rb +25 -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 +14 -0
- data/test/rails_app/app/controllers/users_controller.rb +23 -0
- data/test/rails_app/app/helpers/application_helper.rb +3 -0
- data/test/rails_app/app/mailers/users/mailer.rb +8 -0
- data/test/rails_app/app/mongoid/admin.rb +29 -0
- data/test/rails_app/app/mongoid/shim.rb +24 -0
- data/test/rails_app/app/mongoid/user.rb +42 -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.ru +4 -0
- data/test/rails_app/config/application.rb +41 -0
- data/test/rails_app/config/boot.rb +8 -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 +18 -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 +178 -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 +100 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +74 -0
- data/test/rails_app/db/schema.rb +52 -0
- data/test/rails_app/lib/shared_admin.rb +14 -0
- data/test/rails_app/lib/shared_user.rb +26 -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 +248 -0
- data/test/support/assertions.rb +40 -0
- data/test/support/helpers.rb +91 -0
- data/test/support/integration.rb +92 -0
- data/test/support/locale/en.yml +4 -0
- data/test/support/webrat/integrations/rails.rb +24 -0
- data/test/test_helper.rb +27 -0
- data/test/test_helpers_test.rb +151 -0
- metadata +421 -0
@@ -0,0 +1,140 @@
|
|
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 'set last request at in user session after each request is skipped if tracking is disabled' do
|
20
|
+
sign_in_as_user
|
21
|
+
old_last_request = last_request_at
|
22
|
+
assert_not_nil last_request_at
|
23
|
+
|
24
|
+
get users_path, {}, 'devise.skip_trackable' => true
|
25
|
+
assert_equal old_last_request, last_request_at
|
26
|
+
end
|
27
|
+
|
28
|
+
test 'does not time out user session before default limit time' do
|
29
|
+
sign_in_as_user
|
30
|
+
assert_response :success
|
31
|
+
assert warden.authenticated?(:user)
|
32
|
+
|
33
|
+
get users_path
|
34
|
+
assert_response :success
|
35
|
+
assert warden.authenticated?(:user)
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'time out user session after default limit time' do
|
39
|
+
user = sign_in_as_user
|
40
|
+
get expire_user_path(user)
|
41
|
+
assert_not_nil last_request_at
|
42
|
+
|
43
|
+
get users_path
|
44
|
+
assert_redirected_to users_path
|
45
|
+
assert_not warden.authenticated?(:user)
|
46
|
+
end
|
47
|
+
|
48
|
+
test 'time out is not triggered on sign out' do
|
49
|
+
user = sign_in_as_user
|
50
|
+
get expire_user_path(user)
|
51
|
+
|
52
|
+
get destroy_user_session_path
|
53
|
+
|
54
|
+
assert_response :redirect
|
55
|
+
assert_redirected_to root_path
|
56
|
+
follow_redirect!
|
57
|
+
assert_contain 'Signed out successfully'
|
58
|
+
end
|
59
|
+
|
60
|
+
test 'time out is not triggered on sign in' do
|
61
|
+
user = sign_in_as_user
|
62
|
+
get expire_user_path(user)
|
63
|
+
|
64
|
+
post "/users/sign_in", :email => user.email, :password => "123456"
|
65
|
+
|
66
|
+
assert_response :redirect
|
67
|
+
follow_redirect!
|
68
|
+
assert_contain 'You are signed in'
|
69
|
+
end
|
70
|
+
|
71
|
+
test 'admin does not explode on time out' do
|
72
|
+
admin = sign_in_as_admin
|
73
|
+
get expire_admin_path(admin)
|
74
|
+
|
75
|
+
Admin.send :define_method, :reset_authentication_token! do
|
76
|
+
nil
|
77
|
+
end
|
78
|
+
|
79
|
+
begin
|
80
|
+
get admins_path
|
81
|
+
assert_redirected_to admins_path
|
82
|
+
assert_not warden.authenticated?(:admin)
|
83
|
+
ensure
|
84
|
+
Admin.send(:remove_method, :reset_authentication_token!)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
test 'user configured timeout limit' do
|
89
|
+
swap Devise, :timeout_in => 8.minutes do
|
90
|
+
user = sign_in_as_user
|
91
|
+
|
92
|
+
get users_path
|
93
|
+
assert_not_nil last_request_at
|
94
|
+
assert_response :success
|
95
|
+
assert warden.authenticated?(:user)
|
96
|
+
|
97
|
+
get expire_user_path(user)
|
98
|
+
get users_path
|
99
|
+
assert_redirected_to users_path
|
100
|
+
assert_not warden.authenticated?(:user)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
test 'error message with i18n' do
|
105
|
+
store_translations :en, :devise => {
|
106
|
+
:failure => { :user => { :timeout => 'Session expired!' } }
|
107
|
+
} do
|
108
|
+
user = sign_in_as_user
|
109
|
+
|
110
|
+
get expire_user_path(user)
|
111
|
+
get root_path
|
112
|
+
follow_redirect!
|
113
|
+
assert_contain 'Session expired!'
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
test 'error message with i18n with double redirect' do
|
118
|
+
store_translations :en, :devise => {
|
119
|
+
:failure => { :user => { :timeout => 'Session expired!' } }
|
120
|
+
} do
|
121
|
+
user = sign_in_as_user
|
122
|
+
|
123
|
+
get expire_user_path(user)
|
124
|
+
get users_path
|
125
|
+
follow_redirect!
|
126
|
+
follow_redirect!
|
127
|
+
assert_contain 'Session expired!'
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
test 'time out not triggered if remembered' do
|
132
|
+
user = sign_in_as_user :remember_me => true
|
133
|
+
get expire_user_path(user)
|
134
|
+
assert_not_nil last_request_at
|
135
|
+
|
136
|
+
get users_path
|
137
|
+
assert_response :success
|
138
|
+
assert warden.authenticated?(:user)
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,161 @@
|
|
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 and value through params, when params with the same key as scope exist' do
|
17
|
+
swap Devise, :token_authentication_key => :secret_token do
|
18
|
+
user = create_user_with_authentication_token
|
19
|
+
post exhibit_user_path(user), Devise.token_authentication_key => user.authentication_token, :user => { :some => "data" }
|
20
|
+
|
21
|
+
assert_response :success
|
22
|
+
assert_contain 'User is authenticated'
|
23
|
+
assert warden.authenticated?(:user)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
test 'authenticate with valid authentication token key but does not store if stateless' do
|
28
|
+
swap Devise, :token_authentication_key => :secret_token, :skip_session_storage => [:token_auth] do
|
29
|
+
sign_in_as_new_user_with_token
|
30
|
+
assert warden.authenticated?(:user)
|
31
|
+
|
32
|
+
get users_path
|
33
|
+
assert_redirected_to new_user_session_path
|
34
|
+
assert_not warden.authenticated?(:user)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'authenticate with valid authentication token key and value through http' do
|
39
|
+
swap Devise, :token_authentication_key => :secret_token do
|
40
|
+
sign_in_as_new_user_with_token(:http_auth => true)
|
41
|
+
|
42
|
+
assert_response :success
|
43
|
+
assert_match '<email>user@test.com</email>', response.body
|
44
|
+
assert warden.authenticated?(:user)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
test 'does authenticate with valid authentication token key and value through params if not configured' do
|
49
|
+
swap Devise, :token_authentication_key => :secret_token, :params_authenticatable => [:database] do
|
50
|
+
sign_in_as_new_user_with_token
|
51
|
+
|
52
|
+
assert_contain 'You need to sign in or sign up before continuing'
|
53
|
+
assert_contain 'Sign in'
|
54
|
+
assert_not warden.authenticated?(:user)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
test 'does authenticate with valid authentication token key and value through http if not configured' do
|
59
|
+
swap Devise, :token_authentication_key => :secret_token, :http_authenticatable => [:database] do
|
60
|
+
sign_in_as_new_user_with_token(:http_auth => true)
|
61
|
+
|
62
|
+
assert_response 401
|
63
|
+
assert_contain 'Invalid email or password.'
|
64
|
+
assert_not warden.authenticated?(:user)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
test 'does not authenticate with improper authentication token key' do
|
69
|
+
swap Devise, :token_authentication_key => :donald_duck_token do
|
70
|
+
sign_in_as_new_user_with_token(:auth_token_key => :secret_token)
|
71
|
+
assert_equal new_user_session_path, @request.path
|
72
|
+
|
73
|
+
assert_contain 'You need to sign in or sign up before continuing'
|
74
|
+
assert_contain 'Sign in'
|
75
|
+
assert_not warden.authenticated?(:user)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
test 'does not authenticate with improper authentication token value' do
|
80
|
+
store_translations :en, :devise => {:failure => {:invalid_token => 'LOL, that was not a single character correct.'}} do
|
81
|
+
sign_in_as_new_user_with_token(:auth_token => '*** INVALID TOKEN ***')
|
82
|
+
assert_equal new_user_session_path, @request.path
|
83
|
+
|
84
|
+
assert_contain 'LOL, that was not a single character correct.'
|
85
|
+
assert_contain 'Sign in'
|
86
|
+
assert_not warden.authenticated?(:user)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
test 'authenticate with valid authentication token key and do not store if stateless and timeoutable are enabled' do
|
91
|
+
swap Devise, :token_authentication_key => :secret_token, :skip_session_storage => [:token_auth], :timeout_in => (0.1).second do
|
92
|
+
user = sign_in_as_new_user_with_token
|
93
|
+
assert warden.authenticated?(:user)
|
94
|
+
|
95
|
+
# Expiring does not work because we are setting the session value when accessing it
|
96
|
+
sleep 0.3
|
97
|
+
|
98
|
+
get_users_path_as_existing_user(user)
|
99
|
+
assert warden.authenticated?(:user)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
test 'should reset token and not authenticate when expire_auth_token_on_timeout is set to true, timeoutable is enabled and we have a timed out session' do
|
104
|
+
swap Devise, :token_authentication_key => :secret_token, :expire_auth_token_on_timeout => true, :timeout_in => (-1).minute do
|
105
|
+
user = sign_in_as_new_user_with_token
|
106
|
+
assert warden.authenticated?(:user)
|
107
|
+
token = user.authentication_token
|
108
|
+
|
109
|
+
get_users_path_as_existing_user(user)
|
110
|
+
assert_not warden.authenticated?(:user)
|
111
|
+
user.reload
|
112
|
+
assert_not_equal token, user.authentication_token
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
test 'should not be subject to injection' do
|
117
|
+
swap Devise, :token_authentication_key => :secret_token do
|
118
|
+
user1 = create_user_with_authentication_token()
|
119
|
+
|
120
|
+
# Clean up user cache
|
121
|
+
@user = nil
|
122
|
+
|
123
|
+
user2 = create_user_with_authentication_token(:email => "another@test.com")
|
124
|
+
user2.update_attribute(:authentication_token, "ANOTHERTOKEN")
|
125
|
+
|
126
|
+
assert_not_equal user1, user2
|
127
|
+
visit users_path(Devise.token_authentication_key.to_s + '[$ne]' => user1.authentication_token)
|
128
|
+
assert_nil warden.user(:user)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
private
|
133
|
+
|
134
|
+
def sign_in_as_new_user_with_token(options = {})
|
135
|
+
user = options.delete(:user) || create_user_with_authentication_token(options)
|
136
|
+
|
137
|
+
options[:auth_token_key] ||= Devise.token_authentication_key
|
138
|
+
options[:auth_token] ||= user.authentication_token
|
139
|
+
|
140
|
+
if options[:http_auth]
|
141
|
+
header = "Basic #{Base64.encode64("#{VALID_AUTHENTICATION_TOKEN}:X")}"
|
142
|
+
get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => header
|
143
|
+
else
|
144
|
+
visit users_path(options[:auth_token_key].to_sym => options[:auth_token])
|
145
|
+
end
|
146
|
+
|
147
|
+
user
|
148
|
+
end
|
149
|
+
|
150
|
+
def create_user_with_authentication_token(options={})
|
151
|
+
user = create_user(options)
|
152
|
+
user.authentication_token = VALID_AUTHENTICATION_TOKEN
|
153
|
+
user.save
|
154
|
+
user
|
155
|
+
end
|
156
|
+
|
157
|
+
def get_users_path_as_existing_user(user)
|
158
|
+
sign_in_as_new_user_with_token(:user => user)
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
@@ -0,0 +1,92 @@
|
|
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 "current remote ip returns original ip behind a non transparent proxy" do
|
41
|
+
user = create_user
|
42
|
+
|
43
|
+
arbitrary_ip = '200.121.1.69'
|
44
|
+
sign_in_as_user do
|
45
|
+
header 'HTTP_X_FORWARDED_FOR', arbitrary_ip
|
46
|
+
end
|
47
|
+
user.reload
|
48
|
+
assert_equal arbitrary_ip, user.current_sign_in_ip
|
49
|
+
end
|
50
|
+
|
51
|
+
test "increase sign in count" do
|
52
|
+
user = create_user
|
53
|
+
assert_equal 0, user.sign_in_count
|
54
|
+
|
55
|
+
sign_in_as_user
|
56
|
+
user.reload
|
57
|
+
assert_equal 1, user.sign_in_count
|
58
|
+
|
59
|
+
visit destroy_user_session_path
|
60
|
+
sign_in_as_user
|
61
|
+
user.reload
|
62
|
+
assert_equal 2, user.sign_in_count
|
63
|
+
end
|
64
|
+
|
65
|
+
test "does not update anything if user has signed out along the way" do
|
66
|
+
swap Devise, :allow_unconfirmed_access_for => 0 do
|
67
|
+
user = create_user(:confirm => false)
|
68
|
+
sign_in_as_user
|
69
|
+
|
70
|
+
user.reload
|
71
|
+
assert_nil user.current_sign_in_at
|
72
|
+
assert_nil user.last_sign_in_at
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
test "do not track if devise.skip_trackable is set" do
|
77
|
+
user = create_user
|
78
|
+
sign_in_as_user do
|
79
|
+
header 'devise.skip_trackable', '1'
|
80
|
+
end
|
81
|
+
user.reload
|
82
|
+
assert_equal 0, user.sign_in_count
|
83
|
+
visit destroy_user_session_path
|
84
|
+
|
85
|
+
sign_in_as_user do
|
86
|
+
header 'devise.skip_trackable', false
|
87
|
+
end
|
88
|
+
user.reload
|
89
|
+
assert_equal 1, user.sign_in_count
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ConfirmationInstructionsTest < ActionMailer::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
setup_mailer
|
7
|
+
Devise.mailer = 'Devise::Mailer'
|
8
|
+
Devise.mailer_sender = 'test@example.com'
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
Devise.mailer = 'Devise::Mailer'
|
13
|
+
Devise.mailer_sender = 'please-change-me@config-initializers-devise.com'
|
14
|
+
end
|
15
|
+
|
16
|
+
def user
|
17
|
+
@user ||= create_user
|
18
|
+
end
|
19
|
+
|
20
|
+
def mail
|
21
|
+
@mail ||= begin
|
22
|
+
user
|
23
|
+
ActionMailer::Base.deliveries.first
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
test 'email sent after creating the user' do
|
28
|
+
assert_not_nil mail
|
29
|
+
end
|
30
|
+
|
31
|
+
test 'content type should be set to html' do
|
32
|
+
assert mail.content_type.include?('text/html')
|
33
|
+
end
|
34
|
+
|
35
|
+
test 'send confirmation instructions to the user email' do
|
36
|
+
mail
|
37
|
+
assert_equal [user.email], mail.to
|
38
|
+
end
|
39
|
+
|
40
|
+
test 'setup sender from configuration' do
|
41
|
+
assert_equal ['test@example.com'], mail.from
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'setup sender from custom mailer defaults' do
|
45
|
+
Devise.mailer = 'Users::Mailer'
|
46
|
+
assert_equal ['custom@example.com'], mail.from
|
47
|
+
end
|
48
|
+
|
49
|
+
test 'setup reply to as copy from sender' do
|
50
|
+
assert_equal ['test@example.com'], mail.reply_to
|
51
|
+
end
|
52
|
+
|
53
|
+
test 'setup reply to as different if set in defaults' do
|
54
|
+
Devise.mailer = 'Users::ReplyToMailer'
|
55
|
+
assert_equal ['custom@example.com'], mail.from
|
56
|
+
assert_equal ['custom_reply_to@example.com'], mail.reply_to
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
test 'setup subject from I18n' do
|
61
|
+
store_translations :en, :devise => { :mailer => { :confirmation_instructions => { :subject => 'Account Confirmation' } } } do
|
62
|
+
assert_equal 'Account Confirmation', mail.subject
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
test 'subject namespaced by model' do
|
67
|
+
store_translations :en, :devise => { :mailer => { :confirmation_instructions => { :user_subject => 'User Account Confirmation' } } } do
|
68
|
+
assert_equal 'User Account Confirmation', mail.subject
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
test 'body should have user info' do
|
73
|
+
assert_match /#{user.email}/, mail.body.encoded
|
74
|
+
end
|
75
|
+
|
76
|
+
test 'body should have link to confirm the account' do
|
77
|
+
host = ActionMailer::Base.default_url_options[:host]
|
78
|
+
confirmation_url_regexp = %r{<a href=\"http://#{host}/users/confirmation\?confirmation_token=#{user.confirmation_token}">}
|
79
|
+
assert_match confirmation_url_regexp, mail.body.encoded
|
80
|
+
end
|
81
|
+
|
82
|
+
test 'renders a scoped if scoped_views is set to true' do
|
83
|
+
swap Devise, :scoped_views => true do
|
84
|
+
assert_equal user.email, mail.body.decoded
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
test 'renders a scoped if scoped_views is set in the mailer class' do
|
89
|
+
begin
|
90
|
+
Devise::Mailer.scoped_views = true
|
91
|
+
assert_equal user.email, mail.body.decoded
|
92
|
+
ensure
|
93
|
+
Devise::Mailer.send :remove_instance_variable, :@scoped_views
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
test 'mailer sender accepts a proc' do
|
98
|
+
swap Devise, :mailer_sender => proc { "another@example.com" } do
|
99
|
+
assert_equal ['another@example.com'], mail.from
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|