devise 0.1.0

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 (74) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +220 -0
  3. data/Rakefile +45 -0
  4. data/TODO +37 -0
  5. data/app/controllers/confirmations_controller.rb +32 -0
  6. data/app/controllers/passwords_controller.rb +38 -0
  7. data/app/controllers/sessions_controller.rb +35 -0
  8. data/app/models/notifier.rb +47 -0
  9. data/app/views/confirmations/new.html.erb +16 -0
  10. data/app/views/notifier/confirmation_instructions.html.erb +5 -0
  11. data/app/views/notifier/reset_password_instructions.html.erb +8 -0
  12. data/app/views/passwords/edit.html.erb +20 -0
  13. data/app/views/passwords/new.html.erb +16 -0
  14. data/app/views/sessions/new.html.erb +23 -0
  15. data/config/locales/en.yml +16 -0
  16. data/init.rb +2 -0
  17. data/lib/devise.rb +48 -0
  18. data/lib/devise/active_record.rb +86 -0
  19. data/lib/devise/controllers/filters.rb +109 -0
  20. data/lib/devise/controllers/helpers.rb +91 -0
  21. data/lib/devise/controllers/url_helpers.rb +47 -0
  22. data/lib/devise/hooks/rememberable.rb +24 -0
  23. data/lib/devise/mapping.rb +95 -0
  24. data/lib/devise/migrations.rb +50 -0
  25. data/lib/devise/models/authenticable.rb +98 -0
  26. data/lib/devise/models/confirmable.rb +125 -0
  27. data/lib/devise/models/recoverable.rb +88 -0
  28. data/lib/devise/models/rememberable.rb +71 -0
  29. data/lib/devise/models/validatable.rb +36 -0
  30. data/lib/devise/routes.rb +95 -0
  31. data/lib/devise/strategies/authenticable.rb +45 -0
  32. data/lib/devise/strategies/base.rb +24 -0
  33. data/lib/devise/strategies/rememberable.rb +33 -0
  34. data/lib/devise/version.rb +3 -0
  35. data/lib/devise/warden.rb +64 -0
  36. data/test/active_record_test.rb +96 -0
  37. data/test/controllers/filters_test.rb +97 -0
  38. data/test/controllers/helpers_test.rb +40 -0
  39. data/test/controllers/url_helpers_test.rb +47 -0
  40. data/test/integration/authenticable_test.rb +191 -0
  41. data/test/integration/confirmable_test.rb +60 -0
  42. data/test/integration/recoverable_test.rb +131 -0
  43. data/test/integration/rememberable_test.rb +56 -0
  44. data/test/mailers/confirmation_instructions_test.rb +59 -0
  45. data/test/mailers/reset_password_instructions_test.rb +62 -0
  46. data/test/mapping_test.rb +71 -0
  47. data/test/models/authenticable_test.rb +138 -0
  48. data/test/models/confirmable_test.rb +206 -0
  49. data/test/models/recoverable_test.rb +145 -0
  50. data/test/models/rememberable_test.rb +68 -0
  51. data/test/models/validatable_test.rb +99 -0
  52. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  53. data/test/rails_app/app/controllers/application_controller.rb +10 -0
  54. data/test/rails_app/app/controllers/home_controller.rb +4 -0
  55. data/test/rails_app/app/controllers/users_controller.rb +7 -0
  56. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  57. data/test/rails_app/app/models/account.rb +3 -0
  58. data/test/rails_app/app/models/admin.rb +3 -0
  59. data/test/rails_app/app/models/organizer.rb +3 -0
  60. data/test/rails_app/app/models/user.rb +3 -0
  61. data/test/rails_app/config/boot.rb +110 -0
  62. data/test/rails_app/config/environment.rb +41 -0
  63. data/test/rails_app/config/environments/development.rb +17 -0
  64. data/test/rails_app/config/environments/production.rb +28 -0
  65. data/test/rails_app/config/environments/test.rb +28 -0
  66. data/test/rails_app/config/initializers/new_rails_defaults.rb +21 -0
  67. data/test/rails_app/config/initializers/session_store.rb +15 -0
  68. data/test/rails_app/config/routes.rb +18 -0
  69. data/test/routes_test.rb +75 -0
  70. data/test/support/assertions_helper.rb +22 -0
  71. data/test/support/integration_tests_helper.rb +66 -0
  72. data/test/support/model_tests_helper.rb +40 -0
  73. data/test/test_helper.rb +39 -0
  74. metadata +136 -0
@@ -0,0 +1,56 @@
1
+ require 'test/test_helper'
2
+
3
+ class RememberMeTest < ActionController::IntegrationTest
4
+
5
+ def create_user_and_remember(add_to_token='')
6
+ user = create_user
7
+ user.remember_me!
8
+ cookies['remember_token'] = User.serialize_into_cookie(user) + add_to_token
9
+ user
10
+ end
11
+
12
+ test 'do not remember the user if he has not checked remember me option' do
13
+ user = sign_in_as_user
14
+
15
+ assert_nil user.reload.remember_token
16
+ end
17
+
18
+ test 'generate remember token after sign in' do
19
+ user = sign_in_as_user :remember_me => true
20
+
21
+ assert_not_nil user.reload.remember_token
22
+ end
23
+
24
+ test 'remember the user before sign in' do
25
+ user = create_user_and_remember
26
+ get users_path
27
+ assert_response :success
28
+ assert warden.authenticated?(:user)
29
+ assert warden.user(:user) == user
30
+ end
31
+
32
+ test 'do not remember with invalid token' do
33
+ user = create_user_and_remember('add')
34
+ get users_path
35
+ assert_response :success
36
+ assert_not warden.authenticated?(:user)
37
+ end
38
+
39
+ test 'forget the user before sign out' do
40
+ user = create_user_and_remember
41
+ get users_path
42
+ assert warden.authenticated?(:user)
43
+ get destroy_user_session_path
44
+ assert_not warden.authenticated?(:user)
45
+ assert_nil user.reload.remember_token
46
+ end
47
+
48
+ test 'do not remember the user anymore after forget' do
49
+ user = create_user_and_remember
50
+ get users_path
51
+ assert warden.authenticated?(:user)
52
+ get destroy_user_session_path
53
+ get users_path
54
+ assert_not warden.authenticated?(:user)
55
+ end
56
+ end
@@ -0,0 +1,59 @@
1
+ require 'test/test_helper'
2
+
3
+ class ConfirmationInstructionsTest < ActionMailer::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ Notifier.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_equal 'text/html', mail.content_type
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 subject from I18n' do
39
+ store_translations :en, :devise => { :notifier => { :confirmation_instructions => 'Account Confirmation' } } do
40
+ assert_equal 'Account Confirmation', mail.subject
41
+ end
42
+ end
43
+
44
+ test 'subject namespaced by model' do
45
+ store_translations :en, :devise => { :notifier => { :user => { :confirmation_instructions => 'User Account Confirmation' } } } do
46
+ assert_equal 'User Account Confirmation', mail.subject
47
+ end
48
+ end
49
+
50
+ test 'body should have user info' do
51
+ assert_match /#{user.email}/, mail.body
52
+ end
53
+
54
+ test 'body should have link to confirm the account' do
55
+ host = ActionMailer::Base.default_url_options[:host]
56
+ confirmation_url_regexp = %r{<a href=\"http://#{host}/users/confirmation\?confirmation_token=#{user.confirmation_token}">}
57
+ assert_match confirmation_url_regexp, mail.body
58
+ end
59
+ end
@@ -0,0 +1,62 @@
1
+ require 'test/test_helper'
2
+
3
+ class ResetPasswordInstructionsTest < ActionMailer::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ Notifier.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_equal 'text/html', mail.content_type
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 subject from I18n' do
42
+ store_translations :en, :devise => { :notifier => { :reset_password_instructions => 'Reset instructions' } } do
43
+ assert_equal 'Reset instructions', mail.subject
44
+ end
45
+ end
46
+
47
+ test 'subject namespaced by model' do
48
+ store_translations :en, :devise => { :notifier => { :user => { :reset_password_instructions => 'User Reset Instructions' } } } do
49
+ assert_equal 'User Reset Instructions', mail.subject
50
+ end
51
+ end
52
+
53
+ test 'body should have user info' do
54
+ assert_match /#{user.email}/, mail.body
55
+ end
56
+
57
+ test 'body should have link to confirm the account' do
58
+ host = ActionMailer::Base.default_url_options[:host]
59
+ reset_url_regexp = %r{<a href=\"http://#{host}/users/password/edit\?reset_password_token=#{user.reset_password_token}">}
60
+ assert_match reset_url_regexp, mail.body
61
+ end
62
+ end
@@ -0,0 +1,71 @@
1
+ require 'test/test_helper'
2
+
3
+ class MapTest < ActiveSupport::TestCase
4
+
5
+ test 'store options' do
6
+ mapping = Devise.mappings[:user]
7
+
8
+ assert_equal User, mapping.to
9
+ assert_equal User.devise_modules, mapping.for
10
+ assert_equal :users, mapping.as
11
+ end
12
+
13
+ test 'allows as to be given' do
14
+ assert_equal :admin_area, Devise.mappings[:admin].as
15
+ end
16
+
17
+ test 'allow custom singular to be given' do
18
+ assert_equal :organizers, Devise.mappings[:manager].as
19
+ end
20
+
21
+ test 'allows a controller depending on the mapping' do
22
+ mapping = Devise.mappings[:user]
23
+ assert mapping.allows?(:sessions)
24
+ assert mapping.allows?(:confirmations)
25
+ assert mapping.allows?(:passwords)
26
+
27
+ mapping = Devise.mappings[:admin]
28
+ assert mapping.allows?(:sessions)
29
+ assert_not mapping.allows?(:confirmations)
30
+ assert_not mapping.allows?(:passwords)
31
+ end
32
+
33
+ test 'return mapping by path' do
34
+ assert_nil Devise.find_mapping_by_path("/foo/bar")
35
+ assert_equal Devise.mappings[:user], Devise.find_mapping_by_path("/users/session")
36
+ end
37
+
38
+ test 'return mapping by customized path' do
39
+ assert_equal Devise.mappings[:admin], Devise.find_mapping_by_path("/admin_area/session")
40
+ end
41
+
42
+ test 'return default path names' do
43
+ mapping = Devise.mappings[:user]
44
+ assert_equal 'sign_in', mapping.path_names[:sign_in]
45
+ assert_equal 'sign_out', mapping.path_names[:sign_out]
46
+ assert_equal 'password', mapping.path_names[:password]
47
+ assert_equal 'confirmation', mapping.path_names[:confirmation]
48
+ end
49
+
50
+ test 'allow custom path names to be given' do
51
+ mapping = Devise.mappings[:account]
52
+ assert_equal 'login', mapping.path_names[:sign_in]
53
+ assert_equal 'logout', mapping.path_names[:sign_out]
54
+ assert_equal 'secret', mapping.path_names[:password]
55
+ assert_equal 'verification', mapping.path_names[:confirmation]
56
+ end
57
+
58
+ test 'magic predicates' do
59
+ mapping = Devise.mappings[:user]
60
+ assert mapping.authenticable?
61
+ assert mapping.confirmable?
62
+ assert mapping.recoverable?
63
+ assert mapping.rememberable?
64
+
65
+ mapping = Devise.mappings[:admin]
66
+ assert mapping.authenticable?
67
+ assert_not mapping.confirmable?
68
+ assert_not mapping.recoverable?
69
+ assert_not mapping.rememberable?
70
+ end
71
+ end
@@ -0,0 +1,138 @@
1
+ require 'test/test_helper'
2
+ require 'digest/sha1'
3
+
4
+ class AuthenticableTest < ActiveSupport::TestCase
5
+
6
+ def encrypt_password(user, pepper=nil, stretches=1)
7
+ user.class_eval { define_method(:stretches) { stretches } } if stretches
8
+ user.password = '123456'
9
+ ::Digest::SHA1.hexdigest("--#{user.password_salt}--#{pepper}--123456--#{pepper}--")
10
+ end
11
+
12
+ test 'should respond to password and password confirmation' do
13
+ user = new_user
14
+ assert user.respond_to?(:password)
15
+ assert user.respond_to?(:password_confirmation)
16
+ end
17
+
18
+ test 'should have email acessible' do
19
+ assert field_accessible?(:email)
20
+ end
21
+
22
+ test 'should have password acessible' do
23
+ assert field_accessible?(:password)
24
+ end
25
+
26
+ test 'should have password confirmation accessible' do
27
+ assert field_accessible?(:password_confirmation)
28
+ end
29
+
30
+ test 'should not have password salt accessible' do
31
+ assert_not field_accessible?(:password_salt)
32
+ end
33
+
34
+ test 'should not have encrypted password accessible' do
35
+ assert_not field_accessible?(:encrypted_password)
36
+ end
37
+
38
+ test 'should generate salt while setting password' do
39
+ assert_present new_user.password_salt
40
+ assert_present new_user(:password => nil).password_salt
41
+ assert_present new_user(:password => '').password_salt
42
+ assert_present create_user.password_salt
43
+ end
44
+
45
+ test 'should not change password salt when updating' do
46
+ user = create_user
47
+ salt = user.password_salt
48
+ user.expects(:password_salt=).never
49
+ user.save!
50
+ assert_equal salt, user.password_salt
51
+ end
52
+
53
+ test 'should generate a base64 hash using SecureRandom for password salt' do
54
+ ActiveSupport::SecureRandom.expects(:base64).with(15).returns('friendly_token')
55
+ assert_equal 'friendly_token', new_user.password_salt
56
+ end
57
+
58
+ test 'should never generate the same salt for different users' do
59
+ password_salts = []
60
+ 10.times do
61
+ salt = create_user.password_salt
62
+ assert_not password_salts.include?(salt)
63
+ password_salts << salt
64
+ end
65
+ end
66
+
67
+ test 'should generate encrypted password while setting password' do
68
+ assert_present new_user.encrypted_password
69
+ assert_present new_user(:password => nil).encrypted_password
70
+ assert_present new_user(:password => '').encrypted_password
71
+ assert_present create_user.encrypted_password
72
+ end
73
+
74
+ test 'should encrypt password again if password has changed' do
75
+ user = create_user
76
+ encrypted_password = user.encrypted_password
77
+ user.password = user.password_confirmation = 'new_password'
78
+ user.save!
79
+ assert_not_equal encrypted_password, user.encrypted_password
80
+ end
81
+
82
+ test 'should encrypt password using a sha1 hash' do
83
+ user = new_user
84
+ assert_equal encrypt_password(user), user.encrypted_password
85
+ end
86
+
87
+ test 'should fallback to devise pepper default configuring' do
88
+ begin
89
+ Devise.pepper = ''
90
+ user = new_user
91
+ assert_equal encrypt_password(user), user.encrypted_password
92
+ Devise.pepper = 'new_pepper'
93
+ user = new_user
94
+ assert_equal encrypt_password(user, 'new_pepper'), user.encrypted_password
95
+ Devise.pepper = '123456'
96
+ user = new_user
97
+ assert_equal encrypt_password(user, '123456'), user.encrypted_password
98
+ ensure
99
+ Devise.pepper = nil
100
+ end
101
+ end
102
+
103
+ test 'should fallback to devise stretches default configuring' do
104
+ begin
105
+ default_stretches = Devise.stretches
106
+ Devise.stretches = 1
107
+ user = new_user
108
+ assert_equal encrypt_password(user, nil, nil), user.encrypted_password
109
+ ensure
110
+ Devise.stretches = default_stretches
111
+ end
112
+ end
113
+
114
+ test 'should test for a valid password' do
115
+ user = create_user
116
+ assert user.valid_password?('123456')
117
+ assert_not user.valid_password?('654321')
118
+ end
119
+
120
+ test 'should authenticate a valid user with email and password and return it' do
121
+ user = create_user
122
+ User.any_instance.stubs(:confirmed?).returns(true)
123
+ authenticated_user = User.authenticate(:email => user.email, :password => user.password)
124
+ assert_equal authenticated_user, user
125
+ end
126
+
127
+ test 'should return nil when authenticating an invalid user by email' do
128
+ user = create_user
129
+ authenticated_user = User.authenticate(:email => 'another.email@email.com', :password => user.password)
130
+ assert_nil authenticated_user
131
+ end
132
+
133
+ test 'should return nil when authenticating an invalid user by password' do
134
+ user = create_user
135
+ authenticated_user = User.authenticate(:email => user.email, :password => 'another_password')
136
+ assert_nil authenticated_user
137
+ end
138
+ end
@@ -0,0 +1,206 @@
1
+ require 'test/test_helper'
2
+
3
+ class ConfirmableTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ end
8
+
9
+ test 'should not have confirmed at accessible' do
10
+ assert_not field_accessible?(:confirmed_at)
11
+ end
12
+
13
+ test 'should not have confirmation token accessible' do
14
+ assert_not field_accessible?(:confirmation_token)
15
+ end
16
+
17
+ test 'should not have confirmation sent at accessible' do
18
+ assert_not field_accessible?(:confirmation_sent_at)
19
+ end
20
+
21
+ test 'should generate confirmation token after creating a record' do
22
+ assert_nil new_user.confirmation_token
23
+ assert_not_nil create_user.confirmation_token
24
+ end
25
+
26
+ test 'should regenerate confirmation token each time' do
27
+ user = create_user
28
+ 3.times do
29
+ token = user.confirmation_token
30
+ user.reset_confirmation!
31
+ assert_not_equal token, user.confirmation_token
32
+ end
33
+ end
34
+
35
+ test 'should never generate the same confirmation token for different users' do
36
+ confirmation_tokens = []
37
+ 10.times do
38
+ token = create_user.confirmation_token
39
+ assert !confirmation_tokens.include?(token)
40
+ confirmation_tokens << token
41
+ end
42
+ end
43
+
44
+ test 'should confirm a user updating confirmed at' do
45
+ user = create_user
46
+ assert_nil user.confirmed_at
47
+ assert user.confirm!
48
+ assert_not_nil user.confirmed_at
49
+ end
50
+
51
+ test 'should clear confirmation token while confirming a user' do
52
+ user = create_user
53
+ assert_present user.confirmation_token
54
+ user.confirm!
55
+ assert_nil user.confirmation_token
56
+ end
57
+
58
+ test 'should verify whether a user is confirmed or not' do
59
+ assert_not new_user.confirmed?
60
+ user = create_user
61
+ assert_not user.confirmed?
62
+ user.confirm!
63
+ assert user.confirmed?
64
+ end
65
+
66
+ test 'should not confirm a user already confirmed and add an error to email' do
67
+ user = create_user
68
+ assert user.confirm!
69
+ assert_nil user.errors[:email]
70
+ assert_not user.confirm!
71
+ assert_not_nil user.errors[:email]
72
+ assert_equal 'already confirmed', user.errors[:email]
73
+ end
74
+
75
+ test 'should find and confirm an user automatically' do
76
+ user = create_user
77
+ confirmed_user = User.confirm!(:confirmation_token => user.confirmation_token)
78
+ assert_not_nil confirmed_user
79
+ assert_equal confirmed_user, user
80
+ assert user.reload.confirmed?
81
+ end
82
+
83
+ test 'should return a new user with errors if no user exists while trying to confirm' do
84
+ confirmed_user = User.confirm!(:confirmation_token => 'invalid_confirmation_token')
85
+ assert confirmed_user.new_record?
86
+ end
87
+
88
+ test 'should return errors for a new user when trying to confirm' do
89
+ confirmed_user = User.confirm!(:confirmation_token => 'invalid_confirmation_token')
90
+ assert_not_nil confirmed_user.errors[:confirmation_token]
91
+ assert_equal 'is invalid', confirmed_user.errors[:confirmation_token]
92
+ end
93
+
94
+ test 'should generate errors for a user email if user is already confirmed' do
95
+ user = create_user
96
+ user.confirm!
97
+ confirmed_user = User.confirm!(:confirmation_token => user.confirmation_token)
98
+ assert confirmed_user.confirmed?
99
+ assert confirmed_user.errors[:email]
100
+ end
101
+
102
+ test 'should authenticate a confirmed user' do
103
+ user = create_user
104
+ user.confirm!
105
+ authenticated_user = User.authenticate(:email => user.email, :password => user.password)
106
+ assert_not_nil authenticated_user
107
+ assert_equal authenticated_user, user
108
+ end
109
+
110
+ test 'should send confirmation instructions by email' do
111
+ assert_email_sent do
112
+ create_user
113
+ end
114
+ end
115
+
116
+ test 'should not send confirmation when trying to save an invalid user' do
117
+ assert_email_not_sent do
118
+ user = new_user
119
+ user.stubs(:valid?).returns(false)
120
+ user.save
121
+ end
122
+ end
123
+
124
+ test 'should find a user to send confirmation instructions' do
125
+ user = create_user
126
+ confirmation_user = User.send_confirmation_instructions(:email => user.email)
127
+ assert_not_nil confirmation_user
128
+ assert_equal confirmation_user, user
129
+ end
130
+
131
+ test 'should return a new user if no email was found' do
132
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
133
+ assert_not_nil confirmation_user
134
+ assert confirmation_user.new_record?
135
+ end
136
+
137
+ test 'should add error to new user email if no email was found' do
138
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
139
+ assert confirmation_user.errors[:email]
140
+ assert_equal 'not found', confirmation_user.errors[:email]
141
+ end
142
+
143
+ test 'should reset confirmation token before send the confirmation instructions email' do
144
+ user = create_user
145
+ token = user.confirmation_token
146
+ confirmation_user = User.send_confirmation_instructions(:email => user.email)
147
+ assert_not_equal token, user.reload.confirmation_token
148
+ end
149
+
150
+ test 'should reset confirmation status when sending the confirmation instructions' do
151
+ user = create_user
152
+ assert_not user.confirmed?
153
+ confirmation_user = User.send_confirmation_instructions(:email => user.email)
154
+ assert_not user.reload.confirmed?
155
+ end
156
+
157
+ test 'should send email instructions for the user confirm it\'s email' do
158
+ user = create_user
159
+ assert_email_sent do
160
+ User.send_confirmation_instructions(:email => user.email)
161
+ end
162
+ end
163
+
164
+ test 'should resend email instructions for the user reconfirming the email if it has changed' do
165
+ user = create_user
166
+ user.email = 'new_test@example.com'
167
+ assert_email_sent do
168
+ user.save!
169
+ end
170
+ end
171
+
172
+ test 'should not resend email instructions if the user is updated but the email is not' do
173
+ user = create_user
174
+ user.confirmed_at = Time.now
175
+ assert_email_not_sent do
176
+ user.save!
177
+ end
178
+ end
179
+
180
+ test 'should reset confirmation status when updating email' do
181
+ user = create_user
182
+ assert_not user.confirmed?
183
+ user.confirm!
184
+ assert user.confirmed?
185
+ user.email = 'new_test@example.com'
186
+ user.save!
187
+ assert_not user.reload.confirmed?
188
+ end
189
+
190
+ test 'should reset confirmation token when updating email' do
191
+ user = create_user
192
+ token = user.confirmation_token
193
+ user.email = 'new_test@example.com'
194
+ user.save!
195
+ assert_not_equal token, user.reload.confirmation_token
196
+ end
197
+
198
+ test 'should not be able to send instructions if the user is already confirmed' do
199
+ user = create_user
200
+ user.confirm!
201
+ assert_not user.reset_confirmation!
202
+ assert user.confirmed?
203
+ assert user.errors[:email].present?
204
+ assert_equal 'already confirmed', user.errors[:email]
205
+ end
206
+ end