gonow-devise 1.2.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. data/CHANGELOG.rdoc +506 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +335 -0
  4. data/app/controllers/devise/confirmations_controller.rb +33 -0
  5. data/app/controllers/devise/oauth_callbacks_controller.rb +4 -0
  6. data/app/controllers/devise/passwords_controller.rb +41 -0
  7. data/app/controllers/devise/registrations_controller.rb +109 -0
  8. data/app/controllers/devise/sessions_controller.rb +24 -0
  9. data/app/controllers/devise/unlocks_controller.rb +34 -0
  10. data/app/helpers/devise_helper.rb +19 -0
  11. data/app/mailers/devise/mailer.rb +88 -0
  12. data/app/views/devise/confirmations/new.html.erb +12 -0
  13. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  14. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  15. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  16. data/app/views/devise/passwords/edit.html.erb +16 -0
  17. data/app/views/devise/passwords/new.html.erb +12 -0
  18. data/app/views/devise/registrations/edit.html.erb +25 -0
  19. data/app/views/devise/registrations/new.html.erb +18 -0
  20. data/app/views/devise/sessions/new.html.erb +17 -0
  21. data/app/views/devise/shared/_links.erb +25 -0
  22. data/app/views/devise/unlocks/new.html.erb +12 -0
  23. data/config/locales/en.yml +46 -0
  24. data/lib/devise.rb +372 -0
  25. data/lib/devise/controllers/helpers.rb +228 -0
  26. data/lib/devise/controllers/internal_helpers.rb +113 -0
  27. data/lib/devise/controllers/scoped_views.rb +33 -0
  28. data/lib/devise/controllers/url_helpers.rb +39 -0
  29. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  30. data/lib/devise/encryptors/base.rb +20 -0
  31. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  32. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  33. data/lib/devise/encryptors/sha1.rb +25 -0
  34. data/lib/devise/encryptors/sha512.rb +25 -0
  35. data/lib/devise/failure_app.rb +126 -0
  36. data/lib/devise/hooks/activatable.rb +11 -0
  37. data/lib/devise/hooks/forgetable.rb +12 -0
  38. data/lib/devise/hooks/rememberable.rb +45 -0
  39. data/lib/devise/hooks/timeoutable.rb +22 -0
  40. data/lib/devise/hooks/trackable.rb +9 -0
  41. data/lib/devise/mapping.rb +105 -0
  42. data/lib/devise/models.rb +66 -0
  43. data/lib/devise/models/authenticatable.rb +143 -0
  44. data/lib/devise/models/confirmable.rb +160 -0
  45. data/lib/devise/models/database_authenticatable.rb +94 -0
  46. data/lib/devise/models/encryptable.rb +65 -0
  47. data/lib/devise/models/lockable.rb +168 -0
  48. data/lib/devise/models/oauthable.rb +49 -0
  49. data/lib/devise/models/recoverable.rb +83 -0
  50. data/lib/devise/models/registerable.rb +21 -0
  51. data/lib/devise/models/rememberable.rb +122 -0
  52. data/lib/devise/models/timeoutable.rb +43 -0
  53. data/lib/devise/models/token_authenticatable.rb +72 -0
  54. data/lib/devise/models/trackable.rb +30 -0
  55. data/lib/devise/models/validatable.rb +64 -0
  56. data/lib/devise/modules.rb +30 -0
  57. data/lib/devise/oauth.rb +41 -0
  58. data/lib/devise/oauth/config.rb +33 -0
  59. data/lib/devise/oauth/helpers.rb +18 -0
  60. data/lib/devise/oauth/internal_helpers.rb +182 -0
  61. data/lib/devise/oauth/test_helpers.rb +29 -0
  62. data/lib/devise/oauth/url_helpers.rb +35 -0
  63. data/lib/devise/orm/active_record.rb +38 -0
  64. data/lib/devise/orm/mongoid.rb +31 -0
  65. data/lib/devise/path_checker.rb +18 -0
  66. data/lib/devise/rails.rb +68 -0
  67. data/lib/devise/rails/routes.rb +260 -0
  68. data/lib/devise/rails/warden_compat.rb +41 -0
  69. data/lib/devise/schema.rb +96 -0
  70. data/lib/devise/strategies/authenticatable.rb +150 -0
  71. data/lib/devise/strategies/base.rb +15 -0
  72. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  73. data/lib/devise/strategies/rememberable.rb +51 -0
  74. data/lib/devise/strategies/token_authenticatable.rb +53 -0
  75. data/lib/devise/test_helpers.rb +100 -0
  76. data/lib/devise/version.rb +3 -0
  77. data/lib/generators/active_record/devise_generator.rb +28 -0
  78. data/lib/generators/active_record/templates/migration.rb +30 -0
  79. data/lib/generators/devise/devise_generator.rb +17 -0
  80. data/lib/generators/devise/install_generator.rb +24 -0
  81. data/lib/generators/devise/orm_helpers.rb +24 -0
  82. data/lib/generators/devise/views_generator.rb +63 -0
  83. data/lib/generators/mongoid/devise_generator.rb +17 -0
  84. data/lib/generators/templates/README +25 -0
  85. data/lib/generators/templates/devise.rb +168 -0
  86. data/test/controllers/helpers_test.rb +205 -0
  87. data/test/controllers/internal_helpers_test.rb +56 -0
  88. data/test/controllers/url_helpers_test.rb +59 -0
  89. data/test/devise_test.rb +65 -0
  90. data/test/encryptors_test.rb +30 -0
  91. data/test/failure_app_test.rb +148 -0
  92. data/test/generators/generators_test_helper.rb +4 -0
  93. data/test/generators/install_generator_test.rb +14 -0
  94. data/test/generators/views_generator_test.rb +37 -0
  95. data/test/integration/authenticatable_test.rb +424 -0
  96. data/test/integration/confirmable_test.rb +104 -0
  97. data/test/integration/database_authenticatable_test.rb +38 -0
  98. data/test/integration/http_authenticatable_test.rb +64 -0
  99. data/test/integration/lockable_test.rb +109 -0
  100. data/test/integration/oauthable_test.rb +244 -0
  101. data/test/integration/recoverable_test.rb +134 -0
  102. data/test/integration/registerable_test.rb +180 -0
  103. data/test/integration/rememberable_test.rb +179 -0
  104. data/test/integration/timeoutable_test.rb +89 -0
  105. data/test/integration/token_authenticatable_test.rb +99 -0
  106. data/test/integration/trackable_test.rb +64 -0
  107. data/test/mailers/confirmation_instructions_test.rb +84 -0
  108. data/test/mailers/reset_password_instructions_test.rb +72 -0
  109. data/test/mailers/unlock_instructions_test.rb +66 -0
  110. data/test/mapping_test.rb +95 -0
  111. data/test/models/confirmable_test.rb +221 -0
  112. data/test/models/database_authenticatable_test.rb +82 -0
  113. data/test/models/encryptable_test.rb +65 -0
  114. data/test/models/lockable_test.rb +204 -0
  115. data/test/models/oauthable_test.rb +21 -0
  116. data/test/models/recoverable_test.rb +155 -0
  117. data/test/models/rememberable_test.rb +271 -0
  118. data/test/models/timeoutable_test.rb +28 -0
  119. data/test/models/token_authenticatable_test.rb +37 -0
  120. data/test/models/trackable_test.rb +5 -0
  121. data/test/models/validatable_test.rb +99 -0
  122. data/test/models_test.rb +77 -0
  123. data/test/oauth/config_test.rb +44 -0
  124. data/test/oauth/url_helpers_test.rb +47 -0
  125. data/test/orm/active_record.rb +9 -0
  126. data/test/orm/mongoid.rb +10 -0
  127. data/test/rails_app/app/active_record/admin.rb +6 -0
  128. data/test/rails_app/app/active_record/shim.rb +2 -0
  129. data/test/rails_app/app/active_record/user.rb +8 -0
  130. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  131. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  132. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  133. data/test/rails_app/app/controllers/home_controller.rb +12 -0
  134. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  135. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  136. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  137. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  138. data/test/rails_app/app/mongoid/admin.rb +9 -0
  139. data/test/rails_app/app/mongoid/shim.rb +24 -0
  140. data/test/rails_app/app/mongoid/user.rb +10 -0
  141. data/test/rails_app/config/application.rb +35 -0
  142. data/test/rails_app/config/boot.rb +13 -0
  143. data/test/rails_app/config/environment.rb +5 -0
  144. data/test/rails_app/config/environments/development.rb +19 -0
  145. data/test/rails_app/config/environments/production.rb +33 -0
  146. data/test/rails_app/config/environments/test.rb +33 -0
  147. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  148. data/test/rails_app/config/initializers/devise.rb +172 -0
  149. data/test/rails_app/config/initializers/inflections.rb +2 -0
  150. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  151. data/test/rails_app/config/routes.rb +54 -0
  152. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +31 -0
  153. data/test/rails_app/db/schema.rb +52 -0
  154. data/test/rails_app/lib/shared_admin.rb +9 -0
  155. data/test/rails_app/lib/shared_user.rb +48 -0
  156. data/test/routes_test.rb +189 -0
  157. data/test/support/assertions.rb +24 -0
  158. data/test/support/helpers.rb +60 -0
  159. data/test/support/integration.rb +88 -0
  160. data/test/support/webrat/integrations/rails.rb +24 -0
  161. data/test/test_helper.rb +23 -0
  162. data/test/test_helpers_test.rb +101 -0
  163. metadata +357 -0
@@ -0,0 +1,134 @@
1
+ require 'test_helper'
2
+
3
+ class PasswordTest < ActionController::IntegrationTest
4
+
5
+ def visit_new_password_path
6
+ visit new_user_session_path
7
+ click_link 'Forgot your password?'
8
+ end
9
+
10
+ def request_forgot_password(&block)
11
+ visit_new_password_path
12
+ assert_response :success
13
+ assert_not warden.authenticated?(:user)
14
+
15
+ fill_in 'email', :with => 'user@test.com'
16
+ yield if block_given?
17
+ click_button 'Send me reset password instructions'
18
+ end
19
+
20
+ def reset_password(options={}, &block)
21
+ visit edit_user_password_path(:reset_password_token => options[:reset_password_token]) unless options[:visit] == false
22
+ assert_response :success
23
+
24
+ fill_in 'New password', :with => '987654321'
25
+ fill_in 'Confirm new password', :with => '987654321'
26
+ yield if block_given?
27
+ click_button 'Change my password'
28
+ end
29
+
30
+ test 'authenticated user should not be able to visit forgot password page' do
31
+ sign_in_as_user
32
+ assert warden.authenticated?(:user)
33
+
34
+ get new_user_password_path
35
+
36
+ assert_response :redirect
37
+ assert_redirected_to root_path
38
+ end
39
+
40
+ test 'not authenticated user should be able to request a forgot password' do
41
+ create_user
42
+ request_forgot_password
43
+
44
+ assert_current_url '/users/sign_in'
45
+ assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
46
+ end
47
+
48
+ test 'not authenticated user with invalid email should receive an error message' do
49
+ request_forgot_password do
50
+ fill_in 'email', :with => 'invalid.test@test.com'
51
+ end
52
+
53
+ assert_response :success
54
+ assert_current_url '/users/password'
55
+ assert_have_selector "input[type=email][value='invalid.test@test.com']"
56
+ assert_contain 'not found'
57
+ end
58
+
59
+ test 'authenticated user should not be able to visit edit password page' do
60
+ sign_in_as_user
61
+ get edit_user_password_path
62
+ assert_response :redirect
63
+ assert_redirected_to root_path
64
+ assert warden.authenticated?(:user)
65
+ end
66
+
67
+ test 'not authenticated user with invalid reset password token should not be able to change his password' do
68
+ user = create_user
69
+ reset_password :reset_password_token => 'invalid_reset_password'
70
+
71
+ assert_response :success
72
+ assert_current_url '/users/password'
73
+ assert_have_selector '#error_explanation'
74
+ assert_contain /Reset password token(.*)invalid/
75
+ assert_not user.reload.valid_password?('987654321')
76
+ end
77
+
78
+ test 'not authenticated user with valid reset password token but invalid password should not be able to change his password' do
79
+ user = create_user
80
+ request_forgot_password
81
+ reset_password :reset_password_token => user.reload.reset_password_token do
82
+ fill_in 'Confirm new password', :with => 'other_password'
83
+ end
84
+
85
+ assert_response :success
86
+ assert_current_url '/users/password'
87
+ assert_have_selector '#error_explanation'
88
+ assert_contain 'Password doesn\'t match confirmation'
89
+ assert_not user.reload.valid_password?('987654321')
90
+ end
91
+
92
+ test 'not authenticated user with valid data should be able to change his password' do
93
+ user = create_user
94
+ request_forgot_password
95
+ reset_password :reset_password_token => user.reload.reset_password_token
96
+
97
+ assert_current_url '/'
98
+ assert_contain 'Your password was changed successfully.'
99
+ assert user.reload.valid_password?('987654321')
100
+ end
101
+
102
+ test 'after entering invalid data user should still be able to change his password' do
103
+ user = create_user
104
+ request_forgot_password
105
+ reset_password :reset_password_token => user.reload.reset_password_token do
106
+ fill_in 'Confirm new password', :with => 'other_password'
107
+ end
108
+ assert_response :success
109
+ assert_have_selector '#error_explanation'
110
+ assert_not user.reload.valid_password?('987654321')
111
+
112
+ reset_password :reset_password_token => user.reload.reset_password_token, :visit => false
113
+ assert_contain 'Your password was changed successfully.'
114
+ assert user.reload.valid_password?('987654321')
115
+ end
116
+
117
+ test 'sign in user automatically after changing it\'s password' do
118
+ user = create_user
119
+ request_forgot_password
120
+ reset_password :reset_password_token => user.reload.reset_password_token
121
+
122
+ assert warden.authenticated?(:user)
123
+ end
124
+
125
+ test 'does not sign in user automatically after changing it\'s password if it\'s not active' do
126
+ user = create_user(:confirm => false)
127
+ request_forgot_password
128
+ reset_password :reset_password_token => user.reload.reset_password_token
129
+
130
+ assert_equal new_user_session_path, @request.path
131
+ assert !warden.authenticated?(:user)
132
+ end
133
+
134
+ end
@@ -0,0 +1,180 @@
1
+ require 'test_helper'
2
+
3
+ class RegistrationTest < ActionController::IntegrationTest
4
+
5
+ test 'a guest admin should be able to sign in successfully' do
6
+ get new_admin_session_path
7
+ click_link 'Sign up'
8
+
9
+ assert_template 'registrations/new'
10
+
11
+ fill_in 'email', :with => 'new_user@test.com'
12
+ fill_in 'password', :with => 'new_user123'
13
+ fill_in 'password confirmation', :with => 'new_user123'
14
+ click_button 'Sign up'
15
+
16
+ assert_contain 'Welcome! You have signed up successfully.'
17
+ assert warden.authenticated?(:admin)
18
+
19
+ admin = Admin.last :order => "id"
20
+ assert_equal admin.email, 'new_user@test.com'
21
+ end
22
+
23
+ test 'a guest user should be able to sign up successfully and be blocked by confirmation' do
24
+ get new_user_registration_path
25
+
26
+ fill_in 'email', :with => 'new_user@test.com'
27
+ fill_in 'password', :with => 'new_user123'
28
+ fill_in 'password confirmation', :with => 'new_user123'
29
+ click_button 'Sign up'
30
+
31
+ assert_contain 'You have signed up successfully. However, we could not sign you in because your account is unconfirmed.'
32
+ assert_contain 'Sign in'
33
+ assert_not_contain 'You have to confirm your account before continuing'
34
+
35
+ assert_not warden.authenticated?(:user)
36
+
37
+ user = User.last :order => "id"
38
+ assert_equal user.email, 'new_user@test.com'
39
+ assert_not user.confirmed?
40
+ end
41
+
42
+ test 'a guest user cannot sign up with invalid information' do
43
+ get new_user_registration_path
44
+
45
+ fill_in 'email', :with => 'invalid_email'
46
+ fill_in 'password', :with => 'new_user123'
47
+ fill_in 'password confirmation', :with => 'new_user321'
48
+ click_button 'Sign up'
49
+
50
+ assert_template 'registrations/new'
51
+ assert_have_selector '#error_explanation'
52
+ assert_contain "Email is invalid"
53
+ assert_contain "Password doesn't match confirmation"
54
+ assert_contain "2 errors prohibited"
55
+ assert_nil User.first
56
+
57
+ assert_not warden.authenticated?(:user)
58
+ end
59
+
60
+ test 'a guest should not sign up with email/password that already exists' do
61
+ user = create_user
62
+ get new_user_registration_path
63
+
64
+ fill_in 'email', :with => 'user@test.com'
65
+ fill_in 'password', :with => '123456'
66
+ fill_in 'password confirmation', :with => '123456'
67
+ click_button 'Sign up'
68
+
69
+ assert_current_url '/users'
70
+ assert_contain(/Email.*already.*taken/)
71
+
72
+ assert_not warden.authenticated?(:user)
73
+ end
74
+
75
+ test 'a guest should not be able to change account' do
76
+ get edit_user_registration_path
77
+ assert_redirected_to new_user_session_path
78
+ follow_redirect!
79
+ assert_contain 'You need to sign in or sign up before continuing.'
80
+ end
81
+
82
+ test 'a signed in user should not be able to access sign up' do
83
+ sign_in_as_user
84
+ get new_user_registration_path
85
+ assert_redirected_to root_path
86
+ end
87
+
88
+ test 'a signed in user should be able to edit his account' do
89
+ sign_in_as_user
90
+ get edit_user_registration_path
91
+
92
+ fill_in 'email', :with => 'user.new@email.com'
93
+ fill_in 'current password', :with => '123456'
94
+ click_button 'Update'
95
+
96
+ assert_current_url '/'
97
+ assert_contain 'You updated your account successfully.'
98
+
99
+ assert_equal "user.new@email.com", User.first.email
100
+ end
101
+
102
+ test 'a signed in user should still be able to use the website after changing his password' do
103
+ sign_in_as_user
104
+ get edit_user_registration_path
105
+
106
+ fill_in 'password', :with => '12345678'
107
+ fill_in 'password confirmation', :with => '12345678'
108
+ fill_in 'current password', :with => '123456'
109
+ click_button 'Update'
110
+
111
+ assert_contain 'You updated your account successfully.'
112
+ get users_path
113
+ assert warden.authenticated?(:user)
114
+ end
115
+
116
+ test 'a signed in user should not change his current user with invalid password' do
117
+ sign_in_as_user
118
+ get edit_user_registration_path
119
+
120
+ fill_in 'email', :with => 'user.new@email.com'
121
+ fill_in 'current password', :with => 'invalid'
122
+ click_button 'Update'
123
+
124
+ assert_template 'registrations/edit'
125
+ assert_contain 'user@test.com'
126
+ assert_have_selector 'form input[value="user.new@email.com"]'
127
+
128
+ assert_equal "user@test.com", User.first.email
129
+ end
130
+
131
+ test 'a signed in user should be able to edit his password' do
132
+ sign_in_as_user
133
+ get edit_user_registration_path
134
+
135
+ fill_in 'password', :with => 'pas123'
136
+ fill_in 'password confirmation', :with => 'pas123'
137
+ fill_in 'current password', :with => '123456'
138
+ click_button 'Update'
139
+
140
+ assert_current_url '/'
141
+ assert_contain 'You updated your account successfully.'
142
+
143
+ assert User.first.valid_password?('pas123')
144
+ end
145
+
146
+ test 'a signed in user should not be able to edit his password with invalid confirmation' do
147
+ sign_in_as_user
148
+ get edit_user_registration_path
149
+
150
+ fill_in 'password', :with => 'pas123'
151
+ fill_in 'password confirmation', :with => ''
152
+ fill_in 'current password', :with => '123456'
153
+ click_button 'Update'
154
+
155
+ assert_contain "Password doesn't match confirmation"
156
+ assert_not User.first.valid_password?('pas123')
157
+ end
158
+
159
+ test 'a signed in user should be able to cancel his account' do
160
+ sign_in_as_user
161
+ get edit_user_registration_path
162
+
163
+ click_link "Cancel my account", :method => :delete
164
+ assert_contain "Bye! Your account was successfully cancelled. We hope to see you again soon."
165
+
166
+ assert User.all.empty?
167
+ end
168
+
169
+ test 'a user should be able to cancel sign up by deleting data in the session' do
170
+ get "/set"
171
+ assert_equal "something", @request.session["user_provider_oauth_token"]
172
+
173
+ get "/users/sign_up"
174
+ assert_equal "something", @request.session["user_provider_oauth_token"]
175
+
176
+ get "/users/cancel"
177
+ assert_nil @request.session["user_provider_oauth_token"]
178
+ assert_redirected_to new_user_registration_path
179
+ end
180
+ end
@@ -0,0 +1,179 @@
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
+ cookie.split(";").map(&:strip).grep(/^expires=/)
33
+ Time.parse($')
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
+ end
73
+
74
+ test 'does not extend remember period through sign in' do
75
+ swap Devise, :extend_remember_period => true, :remember_for => 1.year do
76
+ user = create_user
77
+ user.remember_me!
78
+
79
+ user.remember_created_at = old = 10.days.ago
80
+ user.save
81
+
82
+ sign_in_as_user :remember_me => true
83
+ user.reload
84
+
85
+ assert warden.user(:user) == user
86
+ assert_equal old.to_i, user.remember_created_at.to_i
87
+ end
88
+ end
89
+
90
+ test 'if both extend_remember_period and remember_across_browsers are true, sends the same token with a new expire date' do
91
+ swap Devise, :remember_across_browsers => true, :extend_remember_period => true, :remember_for => 1.year do
92
+ admin = create_admin_and_remember
93
+ token = admin.remember_token
94
+
95
+ admin.remember_created_at = old = 10.minutes.ago
96
+ admin.save!
97
+
98
+ get root_path
99
+ assert (cookie_expires("remember_admin_token") - 1.year) > (old + 5.minutes)
100
+ assert_equal token, signed_cookie("remember_admin_token").last
101
+ end
102
+ end
103
+
104
+ test 'if both extend_remember_period and remember_across_browsers are false, sends a new token with old expire date' do
105
+ swap Devise, :remember_across_browsers => false, :extend_remember_period => false, :remember_for => 1.year do
106
+ admin = create_admin_and_remember
107
+ token = admin.remember_token
108
+
109
+ admin.remember_created_at = old = 10.minutes.ago
110
+ admin.save!
111
+
112
+ get root_path
113
+ assert (cookie_expires("remember_admin_token") - 1.year) < (old + 5.minutes)
114
+ assert_not_equal token, signed_cookie("remember_admin_token").last
115
+ end
116
+ end
117
+
118
+ test 'do not remember other scopes' do
119
+ user = create_user_and_remember
120
+ get root_path
121
+ assert_response :success
122
+ assert warden.authenticated?(:user)
123
+ assert_not warden.authenticated?(:admin)
124
+ end
125
+
126
+ test 'do not remember with invalid token' do
127
+ user = create_user_and_remember('add')
128
+ get users_path
129
+ assert_not warden.authenticated?(:user)
130
+ assert_redirected_to new_user_session_path
131
+ end
132
+
133
+ test 'do not remember with expired token' do
134
+ user = create_user_and_remember
135
+ swap Devise, :remember_for => 0 do
136
+ get users_path
137
+ assert_not warden.authenticated?(:user)
138
+ assert_redirected_to new_user_session_path
139
+ end
140
+ end
141
+
142
+ test 'do not remember the user anymore after forget' do
143
+ user = create_user_and_remember
144
+ get users_path
145
+ assert warden.authenticated?(:user)
146
+
147
+ get destroy_user_session_path
148
+ assert_not warden.authenticated?(:user)
149
+ assert_nil warden.cookies['remember_user_token']
150
+
151
+ get users_path
152
+ assert_not warden.authenticated?(:user)
153
+ assert_nil warden.cookies['remember_user_token']
154
+ end
155
+
156
+ test 'do not remember the admin anymore after forget' do
157
+ admin = create_admin_and_remember
158
+ get root_path
159
+ assert warden.authenticated?(:admin)
160
+
161
+ get destroy_admin_session_path
162
+ assert_not warden.authenticated?(:admin)
163
+ assert_nil warden.cookies['remember_admin_token']
164
+
165
+ get root_path
166
+ assert_not warden.authenticated?(:admin)
167
+ assert_nil warden.cookies['remember_admin_token']
168
+ end
169
+
170
+ test 'changing user password expires remember me token' do
171
+ user = create_user_and_remember
172
+ user.password = "another_password"
173
+ user.password_confirmation = "another_password"
174
+ user.save!
175
+
176
+ get users_path
177
+ assert_not warden.authenticated?(:user)
178
+ end
179
+ end