devise 1.1.9 → 1.2.rc

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise might be problematic. Click here for more details.

Files changed (121) hide show
  1. data/CHANGELOG.rdoc +34 -26
  2. data/README.rdoc +134 -100
  3. data/app/controllers/devise/confirmations_controller.rb +1 -1
  4. data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
  5. data/app/controllers/devise/passwords_controller.rb +1 -1
  6. data/app/controllers/devise/registrations_controller.rb +59 -6
  7. data/app/controllers/devise/sessions_controller.rb +3 -2
  8. data/app/controllers/devise/unlocks_controller.rb +1 -1
  9. data/app/helpers/devise_helper.rb +4 -2
  10. data/app/mailers/devise/mailer.rb +27 -10
  11. data/app/views/devise/confirmations/new.html.erb +1 -1
  12. data/app/views/devise/passwords/edit.html.erb +2 -2
  13. data/app/views/devise/passwords/new.html.erb +1 -1
  14. data/app/views/devise/registrations/edit.html.erb +1 -1
  15. data/app/views/devise/registrations/new.html.erb +1 -1
  16. data/app/views/devise/sessions/new.html.erb +1 -1
  17. data/app/views/devise/shared/_links.erb +6 -0
  18. data/app/views/devise/unlocks/new.html.erb +1 -1
  19. data/config/locales/en.yml +9 -2
  20. data/lib/devise.rb +116 -58
  21. data/lib/devise/controllers/helpers.rb +103 -107
  22. data/lib/devise/controllers/internal_helpers.rb +23 -7
  23. data/lib/devise/controllers/scoped_views.rb +4 -6
  24. data/lib/devise/controllers/url_helpers.rb +3 -5
  25. data/lib/devise/encryptors/base.rb +1 -1
  26. data/lib/devise/encryptors/restful_authentication_sha1.rb +4 -4
  27. data/lib/devise/failure_app.rb +29 -21
  28. data/lib/devise/hooks/forgetable.rb +2 -1
  29. data/lib/devise/hooks/rememberable.rb +11 -9
  30. data/lib/devise/mapping.rb +12 -5
  31. data/lib/devise/models.rb +0 -14
  32. data/lib/devise/models/authenticatable.rb +40 -30
  33. data/lib/devise/models/confirmable.rb +11 -15
  34. data/lib/devise/models/database_authenticatable.rb +23 -35
  35. data/lib/devise/models/encryptable.rb +65 -0
  36. data/lib/devise/models/lockable.rb +8 -7
  37. data/lib/devise/models/omniauthable.rb +23 -0
  38. data/lib/devise/models/recoverable.rb +5 -3
  39. data/lib/devise/models/registerable.rb +13 -0
  40. data/lib/devise/models/rememberable.rb +38 -30
  41. data/lib/devise/models/timeoutable.rb +20 -3
  42. data/lib/devise/models/token_authenticatable.rb +19 -7
  43. data/lib/devise/models/validatable.rb +16 -4
  44. data/lib/devise/modules.rb +15 -8
  45. data/lib/devise/omniauth.rb +47 -0
  46. data/lib/devise/omniauth/config.rb +30 -0
  47. data/lib/devise/omniauth/test_helpers.rb +57 -0
  48. data/lib/devise/omniauth/url_helpers.rb +29 -0
  49. data/lib/devise/orm/active_record.rb +2 -0
  50. data/lib/devise/orm/mongoid.rb +4 -2
  51. data/lib/devise/rails.rb +26 -46
  52. data/lib/devise/rails/routes.rb +64 -20
  53. data/lib/devise/rails/warden_compat.rb +18 -20
  54. data/lib/devise/schema.rb +13 -14
  55. data/lib/devise/strategies/authenticatable.rb +33 -7
  56. data/lib/devise/strategies/database_authenticatable.rb +1 -1
  57. data/lib/devise/strategies/rememberable.rb +1 -1
  58. data/lib/devise/strategies/token_authenticatable.rb +6 -2
  59. data/lib/devise/test_helpers.rb +11 -1
  60. data/lib/devise/version.rb +1 -1
  61. data/lib/generators/active_record/templates/migration.rb +1 -0
  62. data/lib/generators/devise/orm_helpers.rb +3 -2
  63. data/lib/generators/templates/devise.rb +70 -39
  64. data/test/controllers/helpers_test.rb +43 -67
  65. data/test/controllers/internal_helpers_test.rb +29 -8
  66. data/test/controllers/url_helpers_test.rb +2 -1
  67. data/test/failure_app_test.rb +56 -21
  68. data/test/generators/generators_test_helper.rb +4 -0
  69. data/test/generators/install_generator_test.rb +14 -0
  70. data/test/generators/views_generator_test.rb +37 -0
  71. data/test/integration/authenticatable_test.rb +147 -62
  72. data/test/integration/database_authenticatable_test.rb +22 -0
  73. data/test/integration/http_authenticatable_test.rb +12 -2
  74. data/test/integration/omniauthable_test.rb +107 -0
  75. data/test/integration/recoverable_test.rb +39 -20
  76. data/test/integration/registerable_test.rb +30 -4
  77. data/test/integration/rememberable_test.rb +57 -34
  78. data/test/integration/timeoutable_test.rb +10 -1
  79. data/test/integration/token_authenticatable_test.rb +12 -17
  80. data/test/mailers/confirmation_instructions_test.rb +4 -0
  81. data/test/mailers/reset_password_instructions_test.rb +4 -0
  82. data/test/mailers/unlock_instructions_test.rb +4 -0
  83. data/test/mapping_test.rb +37 -3
  84. data/test/models/confirmable_test.rb +3 -3
  85. data/test/models/database_authenticatable_test.rb +14 -71
  86. data/test/models/encryptable_test.rb +65 -0
  87. data/test/models/lockable_test.rb +17 -1
  88. data/test/models/recoverable_test.rb +17 -0
  89. data/test/models/rememberable_test.rb +186 -125
  90. data/test/models/token_authenticatable_test.rb +1 -13
  91. data/test/models_test.rb +5 -5
  92. data/test/omniauth/url_helpers_test.rb +47 -0
  93. data/test/rails_app/app/active_record/admin.rb +4 -1
  94. data/test/rails_app/app/active_record/user.rb +5 -4
  95. data/test/rails_app/app/controllers/{sessions_controller.rb → admins/sessions_controller.rb} +1 -1
  96. data/test/rails_app/app/controllers/home_controller.rb +9 -0
  97. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +7 -0
  98. data/test/rails_app/app/mongoid/admin.rb +4 -1
  99. data/test/rails_app/app/mongoid/shim.rb +16 -3
  100. data/test/rails_app/app/mongoid/user.rb +5 -5
  101. data/test/rails_app/config/initializers/devise.rb +52 -28
  102. data/test/rails_app/config/routes.rb +14 -6
  103. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +21 -17
  104. data/test/rails_app/db/schema.rb +17 -51
  105. data/test/rails_app/lib/shared_admin.rb +9 -0
  106. data/test/rails_app/lib/shared_user.rb +23 -0
  107. data/test/routes_test.rb +42 -9
  108. data/test/support/integration.rb +3 -3
  109. data/test/support/webrat/integrations/rails.rb +7 -0
  110. data/test/test_helper.rb +2 -0
  111. data/test/test_helpers_test.rb +29 -0
  112. metadata +60 -30
  113. data/Gemfile +0 -27
  114. data/Gemfile.lock +0 -115
  115. data/Rakefile +0 -55
  116. data/TODO +0 -3
  117. data/lib/devise/encryptors/bcrypt.rb +0 -19
  118. data/lib/generators/devise_install_generator.rb +0 -4
  119. data/lib/generators/devise_views_generator.rb +0 -4
  120. data/test/indifferent_hash.rb +0 -33
  121. data/test/support/test_silencer.rb +0 -5
@@ -1,6 +1,28 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class DatabaseAuthenticationTest < ActionController::IntegrationTest
4
+ test 'sign in with email of different case should succeed when email is in the list of case insensitive keys' do
5
+ create_user(:email => 'Foo@Bar.com')
6
+
7
+ sign_in_as_user do
8
+ fill_in 'email', :with => 'foo@bar.com'
9
+ end
10
+
11
+ assert warden.authenticated?(:user)
12
+ end
13
+
14
+ test 'sign in with email of different case should fail when email is NOT the list of case insensitive keys' do
15
+ swap Devise, :case_insensitive_keys => [] do
16
+ create_user(:email => 'Foo@Bar.com')
17
+
18
+ sign_in_as_user do
19
+ fill_in 'email', :with => 'foo@bar.com'
20
+ end
21
+
22
+ assert_not warden.authenticated?(:user)
23
+ end
24
+ end
25
+
4
26
  test 'sign in should not authenticate if not using proper authentication keys' do
5
27
  swap Devise, :authentication_keys => [:username] do
6
28
  sign_in_as_user
@@ -47,6 +47,16 @@ class HttpAuthenticationTest < ActionController::IntegrationTest
47
47
  end
48
48
  end
49
49
 
50
+ test 'sign in should authenticate with really long token' do
51
+ token = "token_containing_so_many_characters_that_the_base64_encoding_will_wrap"
52
+ user = create_user
53
+ user.update_attribute :authentication_token, token
54
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{token}:x")}"
55
+ assert_response :success
56
+ assert_match "<email>user@test.com</email>", response.body
57
+ assert warden.authenticated?(:user)
58
+ end
59
+
50
60
  private
51
61
 
52
62
  def sign_in_as_new_user_with_http(username="user@test.com", password="123456")
@@ -54,11 +64,11 @@ class HttpAuthenticationTest < ActionController::IntegrationTest
54
64
  get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
55
65
  user
56
66
  end
57
-
67
+
58
68
  # Sign in with oauth2 token. This is just to test that it isn't misinterpreted as basic authentication
59
69
  def add_oauth2_header
60
70
  user = create_user
61
71
  get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "OAuth #{ActiveSupport::Base64.encode64("#{user.email}:123456")}"
62
72
  end
63
73
 
64
- end
74
+ end
@@ -0,0 +1,107 @@
1
+ require 'test_helper'
2
+
3
+ class OmniauthableIntegrationTest < ActionController::IntegrationTest
4
+ FACEBOOK_INFO = {
5
+ :id => '12345',
6
+ :link => 'http://facebook.com/josevalim',
7
+ :email => 'user@example.com',
8
+ :first_name => 'Jose',
9
+ :last_name => 'Valim',
10
+ :website => 'http://blog.plataformatec.com.br'
11
+ }
12
+
13
+ ACCESS_TOKEN = {
14
+ :access_token => "plataformatec"
15
+ }
16
+
17
+ setup do
18
+ stub_facebook!
19
+ Devise::OmniAuth.short_circuit_authorizers!
20
+ end
21
+
22
+ teardown do
23
+ Devise::OmniAuth.unshort_circuit_authorizers!
24
+ Devise::OmniAuth.reset_stubs!
25
+ end
26
+
27
+ def stub_facebook!
28
+ Devise::OmniAuth.stub!(:facebook) do |b|
29
+ b.post('/oauth/access_token') { [200, {}, ACCESS_TOKEN.to_json] }
30
+ b.get('/me?access_token=plataformatec') { [200, {}, FACEBOOK_INFO.to_json] }
31
+ end
32
+ end
33
+
34
+ test "can access omniauth.auth in the env hash" do
35
+ visit "/users/sign_in"
36
+ click_link "Sign in with Facebook"
37
+
38
+ json = ActiveSupport::JSON.decode(response.body)
39
+
40
+ assert_equal "12345", json["uid"]
41
+ assert_equal "facebook", json["provider"]
42
+ assert_equal "josevalim", json["user_info"]["nickname"]
43
+ assert_equal FACEBOOK_INFO, json["extra"]["user_hash"].symbolize_keys
44
+ assert_equal "plataformatec", json["credentials"]["token"]
45
+ end
46
+
47
+ test "cleans up session on sign up" do
48
+ assert_no_difference "User.count" do
49
+ visit "/users/sign_in"
50
+ click_link "Sign in with Facebook"
51
+ end
52
+
53
+ assert session["devise.facebook_data"]
54
+
55
+ assert_difference "User.count" do
56
+ visit "/users/sign_up"
57
+ fill_in "Password", :with => "123456"
58
+ fill_in "Password confirmation", :with => "123456"
59
+ click_button "Sign up"
60
+ end
61
+
62
+ assert_current_url "/"
63
+ assert_contain "You have signed up successfully."
64
+ assert_contain "Hello User user@example.com"
65
+ assert_not session["devise.facebook_data"]
66
+ end
67
+
68
+ test "cleans up session on cancel" do
69
+ assert_no_difference "User.count" do
70
+ visit "/users/sign_in"
71
+ click_link "Sign in with Facebook"
72
+ end
73
+
74
+ assert session["devise.facebook_data"]
75
+ visit "/users/cancel"
76
+ assert !session["devise.facebook_data"]
77
+ end
78
+
79
+ test "cleans up session on sign in" do
80
+ assert_no_difference "User.count" do
81
+ visit "/users/sign_in"
82
+ click_link "Sign in with Facebook"
83
+ end
84
+
85
+ assert session["devise.facebook_data"]
86
+ user = sign_in_as_user
87
+ assert !session["devise.facebook_data"]
88
+ end
89
+
90
+ test "handles callback error parameter according to the specification" do
91
+ visit "/users/auth/facebook/callback?error=access_denied"
92
+ assert_current_url "/users/sign_in"
93
+ assert_contain 'Could not authorize you from Facebook because "Access denied".'
94
+ end
95
+
96
+ test "handles other exceptions from omniauth" do
97
+ Devise::OmniAuth.stub!(:facebook) do |b|
98
+ b.post('/oauth/access_token') { [401, {}, {}.to_json] }
99
+ end
100
+
101
+ visit "/users/sign_in"
102
+ click_link "Sign in with facebook"
103
+
104
+ assert_current_url "/users/sign_in"
105
+ assert_contain 'Could not authorize you from Facebook because "Invalid credentials".'
106
+ end
107
+ end
@@ -9,9 +9,7 @@ class PasswordTest < ActionController::IntegrationTest
9
9
 
10
10
  def request_forgot_password(&block)
11
11
  visit_new_password_path
12
-
13
12
  assert_response :success
14
- assert_template 'passwords/new'
15
13
  assert_not warden.authenticated?(:user)
16
14
 
17
15
  fill_in 'email', :with => 'user@test.com'
@@ -19,19 +17,42 @@ class PasswordTest < ActionController::IntegrationTest
19
17
  click_button 'Send me reset password instructions'
20
18
  end
21
19
 
22
- def reset_password(options={}, &block)
23
- unless options[:visit] == false
24
- visit edit_user_password_path(:reset_password_token => options[:reset_password_token])
25
- end
20
+ def reset_password(options={}, &block)
21
+ visit edit_user_password_path(:reset_password_token => options[:reset_password_token]) unless options[:visit] == false
26
22
  assert_response :success
27
- assert_template 'passwords/edit'
28
23
 
29
- fill_in 'Password', :with => '987654321'
30
- fill_in 'Password confirmation', :with => '987654321'
24
+ fill_in 'New password', :with => '987654321'
25
+ fill_in 'Confirm new password', :with => '987654321'
31
26
  yield if block_given?
32
27
  click_button 'Change my password'
33
28
  end
34
29
 
30
+ test 'reset password with email of different case should succeed when email is in the list of case insensitive keys' do
31
+ create_user(:email => 'Foo@Bar.com')
32
+
33
+ request_forgot_password do
34
+ fill_in 'email', :with => 'foo@bar.com'
35
+ end
36
+
37
+ assert_current_url '/users/sign_in'
38
+ assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
39
+ end
40
+
41
+ test 'reset password with email of different case should fail when email is NOT the list of case insensitive keys' do
42
+ swap Devise, :case_insensitive_keys => [] do
43
+ create_user(:email => 'Foo@Bar.com')
44
+
45
+ request_forgot_password do
46
+ fill_in 'email', :with => 'foo@bar.com'
47
+ end
48
+
49
+ assert_response :success
50
+ assert_current_url '/users/password'
51
+ assert_have_selector "input[type=email][value='foo@bar.com']"
52
+ assert_contain 'not found'
53
+ end
54
+ end
55
+
35
56
  test 'authenticated user should not be able to visit forgot password page' do
36
57
  sign_in_as_user
37
58
  assert warden.authenticated?(:user)
@@ -46,7 +67,7 @@ class PasswordTest < ActionController::IntegrationTest
46
67
  create_user
47
68
  request_forgot_password
48
69
 
49
- assert_template 'sessions/new'
70
+ assert_current_url '/users/sign_in'
50
71
  assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
51
72
  end
52
73
 
@@ -56,16 +77,14 @@ class PasswordTest < ActionController::IntegrationTest
56
77
  end
57
78
 
58
79
  assert_response :success
59
- assert_template 'passwords/new'
60
- assert_have_selector 'input[type=text][value=\'invalid.test@test.com\']'
61
- assert_contain 'Email not found'
80
+ assert_current_url '/users/password'
81
+ assert_have_selector "input[type=email][value='invalid.test@test.com']"
82
+ assert_contain 'not found'
62
83
  end
63
84
 
64
85
  test 'authenticated user should not be able to visit edit password page' do
65
86
  sign_in_as_user
66
-
67
87
  get edit_user_password_path
68
-
69
88
  assert_response :redirect
70
89
  assert_redirected_to root_path
71
90
  assert warden.authenticated?(:user)
@@ -76,7 +95,7 @@ class PasswordTest < ActionController::IntegrationTest
76
95
  reset_password :reset_password_token => 'invalid_reset_password'
77
96
 
78
97
  assert_response :success
79
- assert_template 'passwords/edit'
98
+ assert_current_url '/users/password'
80
99
  assert_have_selector '#error_explanation'
81
100
  assert_contain /Reset password token(.*)invalid/
82
101
  assert_not user.reload.valid_password?('987654321')
@@ -86,11 +105,11 @@ class PasswordTest < ActionController::IntegrationTest
86
105
  user = create_user
87
106
  request_forgot_password
88
107
  reset_password :reset_password_token => user.reload.reset_password_token do
89
- fill_in 'Password confirmation', :with => 'other_password'
108
+ fill_in 'Confirm new password', :with => 'other_password'
90
109
  end
91
110
 
92
111
  assert_response :success
93
- assert_template 'passwords/edit'
112
+ assert_current_url '/users/password'
94
113
  assert_have_selector '#error_explanation'
95
114
  assert_contain 'Password doesn\'t match confirmation'
96
115
  assert_not user.reload.valid_password?('987654321')
@@ -101,7 +120,7 @@ class PasswordTest < ActionController::IntegrationTest
101
120
  request_forgot_password
102
121
  reset_password :reset_password_token => user.reload.reset_password_token
103
122
 
104
- assert_template 'home/index'
123
+ assert_current_url '/'
105
124
  assert_contain 'Your password was changed successfully.'
106
125
  assert user.reload.valid_password?('987654321')
107
126
  end
@@ -110,7 +129,7 @@ class PasswordTest < ActionController::IntegrationTest
110
129
  user = create_user
111
130
  request_forgot_password
112
131
  reset_password :reset_password_token => user.reload.reset_password_token do
113
- fill_in 'Password confirmation', :with => 'other_password'
132
+ fill_in 'Confirm new password', :with => 'other_password'
114
133
  end
115
134
  assert_response :success
116
135
  assert_have_selector '#error_explanation'
@@ -13,7 +13,7 @@ class RegistrationTest < ActionController::IntegrationTest
13
13
  fill_in 'password confirmation', :with => 'new_user123'
14
14
  click_button 'Sign up'
15
15
 
16
- assert_contain 'You have signed up successfully.'
16
+ assert_contain 'Welcome! You have signed up successfully.'
17
17
  assert warden.authenticated?(:admin)
18
18
 
19
19
  admin = Admin.last :order => "id"
@@ -28,8 +28,7 @@ class RegistrationTest < ActionController::IntegrationTest
28
28
  fill_in 'password confirmation', :with => 'new_user123'
29
29
  click_button 'Sign up'
30
30
 
31
- assert_contain 'You have signed up successfully'
32
- assert_contain 'Sign in'
31
+ assert_contain 'You have signed up successfully. However, we could not sign you in because your account is unconfirmed.'
33
32
  assert_not_contain 'You have to confirm your account before continuing'
34
33
 
35
34
  assert_not warden.authenticated?(:user)
@@ -51,6 +50,7 @@ class RegistrationTest < ActionController::IntegrationTest
51
50
  assert_have_selector '#error_explanation'
52
51
  assert_contain "Email is invalid"
53
52
  assert_contain "Password doesn't match confirmation"
53
+ assert_contain "2 errors prohibited"
54
54
  assert_nil User.first
55
55
 
56
56
  assert_not warden.authenticated?(:user)
@@ -66,7 +66,7 @@ class RegistrationTest < ActionController::IntegrationTest
66
66
  click_button 'Sign up'
67
67
 
68
68
  assert_current_url '/users'
69
- assert_contain(/Email .* already.*taken/)
69
+ assert_contain(/Email.*already.*taken/)
70
70
 
71
71
  assert_not warden.authenticated?(:user)
72
72
  end
@@ -98,6 +98,20 @@ class RegistrationTest < ActionController::IntegrationTest
98
98
  assert_equal "user.new@email.com", User.first.email
99
99
  end
100
100
 
101
+ test 'a signed in user should still be able to use the website after changing his password' do
102
+ sign_in_as_user
103
+ get edit_user_registration_path
104
+
105
+ fill_in 'password', :with => '12345678'
106
+ fill_in 'password confirmation', :with => '12345678'
107
+ fill_in 'current password', :with => '123456'
108
+ click_button 'Update'
109
+
110
+ assert_contain 'You updated your account successfully.'
111
+ get users_path
112
+ assert warden.authenticated?(:user)
113
+ end
114
+
101
115
  test 'a signed in user should not change his current user with invalid password' do
102
116
  sign_in_as_user
103
117
  get edit_user_registration_path
@@ -150,4 +164,16 @@ class RegistrationTest < ActionController::IntegrationTest
150
164
 
151
165
  assert User.all.empty?
152
166
  end
167
+
168
+ test 'a user should be able to cancel sign up by deleting data in the session' do
169
+ get "/set"
170
+ assert_equal "something", @request.session["devise.foo_bar"]
171
+
172
+ get "/users/sign_up"
173
+ assert_equal "something", @request.session["devise.foo_bar"]
174
+
175
+ get "/users/cancel"
176
+ assert_nil @request.session["devise.foo_bar"]
177
+ assert_redirected_to new_user_registration_path
178
+ end
153
179
  end
@@ -1,7 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class RememberMeTest < ActionController::IntegrationTest
4
-
5
4
  def create_user_and_remember(add_to_token='')
6
5
  user = create_user
7
6
  user.remember_me!
@@ -10,6 +9,14 @@ class RememberMeTest < ActionController::IntegrationTest
10
9
  user
11
10
  end
12
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
+
13
20
  def generate_signed_cookie(raw_cookie)
14
21
  request = ActionDispatch::TestRequest.new
15
22
  request.cookie_jar.signed['raw_cookie'] = raw_cookie
@@ -29,32 +36,30 @@ class RememberMeTest < ActionController::IntegrationTest
29
36
  test 'do not remember the user if he has not checked remember me option' do
30
37
  user = sign_in_as_user
31
38
  assert_nil request.cookies["remember_user_cookie"]
32
- assert_nil user.reload.remember_token
33
39
  end
34
40
 
35
41
  test 'generate remember token after sign in' do
36
42
  user = sign_in_as_user :remember_me => true
37
43
  assert request.cookies["remember_user_token"]
38
- assert user.reload.remember_token
39
44
  end
40
45
 
41
- test 'generate remember token after sign in setting cookie domain' do
46
+ test 'generate remember token after sign in setting cookie options' do
42
47
  # We test this by asserting the cookie is not sent after the redirect
43
48
  # since we changed the domain. This is the only difference with the
44
49
  # previous test.
45
- swap User, :cookie_domain => "omg.somewhere.com" do
50
+ swap Devise, :cookie_options => { :domain => "omg.somewhere.com" } do
46
51
  user = sign_in_as_user :remember_me => true
47
52
  assert_nil request.cookies["remember_user_token"]
48
53
  end
49
54
  end
50
55
 
51
- test 'cookies are destroyed on unverified requests' do
52
- swap ApplicationController, :allow_forgery_protection => true do
53
- user = create_user_and_remember
54
- get users_path
55
- assert warden.authenticated?(:user)
56
- post root_path, :authenticity_token => 'INVALID'
57
- assert_not warden.authenticated?(:user)
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)
58
63
  end
59
64
  end
60
65
 
@@ -84,29 +89,29 @@ class RememberMeTest < ActionController::IntegrationTest
84
89
 
85
90
  test 'if both extend_remember_period and remember_across_browsers are true, sends the same token with a new expire date' do
86
91
  swap Devise, :remember_across_browsers => true, :extend_remember_period => true, :remember_for => 1.year do
87
- user = create_user_and_remember
88
- token = user.remember_token
92
+ admin = create_admin_and_remember
93
+ token = admin.remember_token
89
94
 
90
- user.remember_created_at = old = 10.minutes.ago
91
- user.save!
95
+ admin.remember_created_at = old = 10.minutes.ago
96
+ admin.save!
92
97
 
93
- get users_path
94
- assert (cookie_expires("remember_user_token") - 1.year) > (old + 5.minutes)
95
- assert_equal token, signed_cookie("remember_user_token").last
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
96
101
  end
97
102
  end
98
103
 
99
104
  test 'if both extend_remember_period and remember_across_browsers are false, sends a new token with old expire date' do
100
105
  swap Devise, :remember_across_browsers => false, :extend_remember_period => false, :remember_for => 1.year do
101
- user = create_user_and_remember
102
- token = user.remember_token
106
+ admin = create_admin_and_remember
107
+ token = admin.remember_token
103
108
 
104
- user.remember_created_at = old = 10.minutes.ago
105
- user.save!
109
+ admin.remember_created_at = old = 10.minutes.ago
110
+ admin.save!
106
111
 
107
- get users_path
108
- assert (cookie_expires("remember_user_token") - 1.year) < (old + 5.minutes)
109
- assert_not_equal token, signed_cookie("remember_user_token").last
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
110
115
  end
111
116
  end
112
117
 
@@ -134,23 +139,41 @@ class RememberMeTest < ActionController::IntegrationTest
134
139
  end
135
140
  end
136
141
 
137
- test 'forget the user before sign out' do
142
+ test 'do not remember the user anymore after forget' do
138
143
  user = create_user_and_remember
139
144
  get users_path
140
145
  assert warden.authenticated?(:user)
146
+
141
147
  get destroy_user_session_path
142
148
  assert_not warden.authenticated?(:user)
143
- assert_nil user.reload.remember_token
149
+ assert_nil warden.cookies['remember_user_token']
150
+
151
+ get users_path
152
+ assert_not warden.authenticated?(:user)
144
153
  assert_nil warden.cookies['remember_user_token']
145
154
  end
146
155
 
147
- test 'do not remember the user anymore after forget' do
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
148
171
  user = create_user_and_remember
149
- get users_path
150
- assert warden.authenticated?(:user)
151
- get destroy_user_session_path
172
+ user.password = "another_password"
173
+ user.password_confirmation = "another_password"
174
+ user.save!
175
+
152
176
  get users_path
153
177
  assert_not warden.authenticated?(:user)
154
- assert_nil warden.cookies['remember_user_token']
155
178
  end
156
- end
179
+ end