mongoid-devise 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +333 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +260 -0
- data/Rakefile +53 -0
- data/TODO +2 -0
- data/app/controllers/confirmations_controller.rb +33 -0
- data/app/controllers/passwords_controller.rb +42 -0
- data/app/controllers/registrations_controller.rb +55 -0
- data/app/controllers/sessions_controller.rb +45 -0
- data/app/controllers/unlocks_controller.rb +33 -0
- data/app/models/devise_mailer.rb +68 -0
- data/app/views/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/passwords/edit.html.erb +16 -0
- data/app/views/passwords/new.html.erb +12 -0
- data/app/views/registrations/edit.html.erb +25 -0
- data/app/views/registrations/new.html.erb +17 -0
- data/app/views/sessions/new.html.erb +17 -0
- data/app/views/shared/_devise_links.erb +19 -0
- data/app/views/unlocks/new.html.erb +12 -0
- data/generators/devise/USAGE +5 -0
- data/generators/devise/devise_generator.rb +15 -0
- data/generators/devise/lib/route_devise.rb +32 -0
- data/generators/devise/templates/migration.rb +23 -0
- data/generators/devise/templates/model.rb +9 -0
- data/generators/devise_install/USAGE +3 -0
- data/generators/devise_install/devise_install_generator.rb +15 -0
- data/generators/devise_install/templates/README +18 -0
- data/generators/devise_install/templates/devise.rb +102 -0
- data/generators/devise_views/USAGE +3 -0
- data/generators/devise_views/devise_views_generator.rb +21 -0
- data/init.rb +2 -0
- data/lib/devise.rb +253 -0
- data/lib/devise/controllers/helpers.rb +200 -0
- data/lib/devise/controllers/internal_helpers.rb +129 -0
- data/lib/devise/controllers/url_helpers.rb +41 -0
- data/lib/devise/encryptors/authlogic_sha512.rb +21 -0
- data/lib/devise/encryptors/base.rb +20 -0
- data/lib/devise/encryptors/bcrypt.rb +21 -0
- data/lib/devise/encryptors/clearance_sha1.rb +19 -0
- data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
- data/lib/devise/encryptors/sha1.rb +27 -0
- data/lib/devise/encryptors/sha512.rb +27 -0
- data/lib/devise/failure_app.rb +65 -0
- data/lib/devise/hooks/activatable.rb +15 -0
- data/lib/devise/hooks/rememberable.rb +30 -0
- data/lib/devise/hooks/timeoutable.rb +18 -0
- data/lib/devise/hooks/trackable.rb +18 -0
- data/lib/devise/locales/en.yml +35 -0
- data/lib/devise/mapping.rb +131 -0
- data/lib/devise/models.rb +112 -0
- data/lib/devise/models/activatable.rb +16 -0
- data/lib/devise/models/authenticatable.rb +146 -0
- data/lib/devise/models/confirmable.rb +172 -0
- data/lib/devise/models/http_authenticatable.rb +21 -0
- data/lib/devise/models/lockable.rb +160 -0
- data/lib/devise/models/recoverable.rb +80 -0
- data/lib/devise/models/registerable.rb +8 -0
- data/lib/devise/models/rememberable.rb +94 -0
- data/lib/devise/models/timeoutable.rb +28 -0
- data/lib/devise/models/token_authenticatable.rb +89 -0
- data/lib/devise/models/trackable.rb +16 -0
- data/lib/devise/models/validatable.rb +48 -0
- data/lib/devise/orm/active_record.rb +41 -0
- data/lib/devise/orm/data_mapper.rb +83 -0
- data/lib/devise/orm/mongo_mapper.rb +51 -0
- data/lib/devise/orm/mongoid.rb +60 -0
- data/lib/devise/rails.rb +14 -0
- data/lib/devise/rails/routes.rb +125 -0
- data/lib/devise/rails/warden_compat.rb +25 -0
- data/lib/devise/schema.rb +65 -0
- data/lib/devise/strategies/authenticatable.rb +36 -0
- data/lib/devise/strategies/base.rb +16 -0
- data/lib/devise/strategies/http_authenticatable.rb +49 -0
- data/lib/devise/strategies/rememberable.rb +37 -0
- data/lib/devise/strategies/token_authenticatable.rb +37 -0
- data/lib/devise/test_helpers.rb +86 -0
- data/lib/devise/version.rb +3 -0
- data/test/controllers/helpers_test.rb +177 -0
- data/test/controllers/internal_helpers_test.rb +55 -0
- data/test/controllers/url_helpers_test.rb +47 -0
- data/test/devise_test.rb +69 -0
- data/test/encryptors_test.rb +31 -0
- data/test/failure_app_test.rb +44 -0
- data/test/integration/authenticatable_test.rb +271 -0
- data/test/integration/confirmable_test.rb +97 -0
- data/test/integration/http_authenticatable_test.rb +44 -0
- data/test/integration/lockable_test.rb +83 -0
- data/test/integration/recoverable_test.rb +141 -0
- data/test/integration/registerable_test.rb +130 -0
- data/test/integration/rememberable_test.rb +63 -0
- data/test/integration/timeoutable_test.rb +68 -0
- data/test/integration/token_authenticatable_test.rb +55 -0
- data/test/integration/trackable_test.rb +64 -0
- data/test/mailers/confirmation_instructions_test.rb +80 -0
- data/test/mailers/reset_password_instructions_test.rb +68 -0
- data/test/mailers/unlock_instructions_test.rb +62 -0
- data/test/mapping_test.rb +153 -0
- data/test/models/authenticatable_test.rb +180 -0
- data/test/models/confirmable_test.rb +228 -0
- data/test/models/lockable_test.rb +202 -0
- data/test/models/recoverable_test.rb +138 -0
- data/test/models/rememberable_test.rb +135 -0
- data/test/models/timeoutable_test.rb +28 -0
- data/test/models/token_authenticatable_test.rb +51 -0
- data/test/models/trackable_test.rb +5 -0
- data/test/models/validatable_test.rb +106 -0
- data/test/models_test.rb +56 -0
- data/test/orm/active_record.rb +31 -0
- data/test/orm/mongo_mapper.rb +20 -0
- data/test/orm/mongoid.rb +22 -0
- data/test/rails_app/app/active_record/admin.rb +7 -0
- data/test/rails_app/app/active_record/user.rb +7 -0
- data/test/rails_app/app/controllers/admins_controller.rb +6 -0
- data/test/rails_app/app/controllers/application_controller.rb +10 -0
- data/test/rails_app/app/controllers/home_controller.rb +4 -0
- data/test/rails_app/app/controllers/users_controller.rb +16 -0
- data/test/rails_app/app/helpers/application_helper.rb +3 -0
- data/test/rails_app/app/mongo_mapper/admin.rb +9 -0
- data/test/rails_app/app/mongo_mapper/user.rb +8 -0
- data/test/rails_app/app/mongoid/admin.rb +9 -0
- data/test/rails_app/app/mongoid/user.rb +8 -0
- data/test/rails_app/config/boot.rb +110 -0
- data/test/rails_app/config/environment.rb +42 -0
- data/test/rails_app/config/environments/development.rb +17 -0
- data/test/rails_app/config/environments/production.rb +28 -0
- data/test/rails_app/config/environments/test.rb +28 -0
- data/test/rails_app/config/initializers/devise.rb +79 -0
- data/test/rails_app/config/initializers/inflections.rb +2 -0
- data/test/rails_app/config/initializers/new_rails_defaults.rb +24 -0
- data/test/rails_app/config/initializers/session_store.rb +15 -0
- data/test/rails_app/config/routes.rb +21 -0
- data/test/routes_test.rb +110 -0
- data/test/support/assertions_helper.rb +37 -0
- data/test/support/integration_tests_helper.rb +71 -0
- data/test/support/test_silencer.rb +5 -0
- data/test/support/tests_helper.rb +39 -0
- data/test/test_helper.rb +21 -0
- data/test/test_helpers_test.rb +57 -0
- metadata +216 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
require 'test/test_helper'
|
|
2
|
+
require 'digest/sha1'
|
|
3
|
+
|
|
4
|
+
class AuthenticatableTest < ActiveSupport::TestCase
|
|
5
|
+
|
|
6
|
+
def encrypt_password(user, pepper=User.pepper, stretches=User.stretches, encryptor=::Devise::Encryptors::Sha1)
|
|
7
|
+
encryptor.digest('123456', stretches, user.password_salt, pepper)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
test 'should respond to password and password confirmation' do
|
|
11
|
+
user = new_user
|
|
12
|
+
assert user.respond_to?(:password)
|
|
13
|
+
assert user.respond_to?(:password_confirmation)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
test 'should generate encrypted password and salt while setting password' do
|
|
17
|
+
user = new_user
|
|
18
|
+
assert_present user.password_salt
|
|
19
|
+
assert_present user.encrypted_password
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
test 'should not change password salt when updating' do
|
|
23
|
+
user = create_user
|
|
24
|
+
salt = user.password_salt
|
|
25
|
+
user.expects(:password_salt=).never
|
|
26
|
+
user.save!
|
|
27
|
+
assert_equal salt, user.password_salt
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
test 'should generate a base64 hash using SecureRandom for password salt' do
|
|
31
|
+
ActiveSupport::SecureRandom.expects(:base64).with(15).returns('friendly_token')
|
|
32
|
+
assert_equal 'friendly_token', new_user.password_salt
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
test 'should not generate salt if password is blank' do
|
|
36
|
+
assert_blank new_user(:password => nil).password_salt
|
|
37
|
+
assert_blank new_user(:password => '').password_salt
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
test 'should not generate encrypted password if password is blank' do
|
|
41
|
+
assert_blank new_user(:password => nil).encrypted_password
|
|
42
|
+
assert_blank new_user(:password => '').encrypted_password
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
test 'should encrypt password again if password has changed' do
|
|
46
|
+
user = create_user
|
|
47
|
+
encrypted_password = user.encrypted_password
|
|
48
|
+
user.password = user.password_confirmation = 'new_password'
|
|
49
|
+
user.save!
|
|
50
|
+
assert_not_equal encrypted_password, user.encrypted_password
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
test 'should fallback to sha1 as default encryption' do
|
|
54
|
+
user = new_user
|
|
55
|
+
assert_equal encrypt_password(user), user.encrypted_password
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
test 'should fallback to devise pepper default configuration' do
|
|
59
|
+
begin
|
|
60
|
+
Devise.pepper = ''
|
|
61
|
+
user = new_user
|
|
62
|
+
assert_equal encrypt_password(user), user.encrypted_password
|
|
63
|
+
assert_not_equal encrypt_password(user, 'another_pepper'), user.encrypted_password
|
|
64
|
+
|
|
65
|
+
Devise.pepper = 'new_pepper'
|
|
66
|
+
user = new_user
|
|
67
|
+
assert_equal encrypt_password(user, 'new_pepper'), user.encrypted_password
|
|
68
|
+
assert_not_equal encrypt_password(user, 'another_pepper'), user.encrypted_password
|
|
69
|
+
ensure
|
|
70
|
+
Devise.pepper = nil
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
test 'should fallback to devise stretches default configuration' do
|
|
75
|
+
swap Devise, :stretches => 1 do
|
|
76
|
+
user = new_user
|
|
77
|
+
assert_equal encrypt_password(user, nil, 1), user.encrypted_password
|
|
78
|
+
assert_not_equal encrypt_password(user, nil, 2), user.encrypted_password
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
test 'should respect encryptor configuration' do
|
|
83
|
+
User.instance_variable_set(:@encryptor_class, nil)
|
|
84
|
+
|
|
85
|
+
swap Devise, :encryptor => :sha512 do
|
|
86
|
+
begin
|
|
87
|
+
user = create_user
|
|
88
|
+
assert_equal user.encrypted_password, encrypt_password(user, User.pepper, User.stretches, ::Devise::Encryptors::Sha512)
|
|
89
|
+
ensure
|
|
90
|
+
User.instance_variable_set(:@encryptor_class, nil)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
test 'should test for a valid password' do
|
|
96
|
+
user = create_user
|
|
97
|
+
assert user.valid_password?('123456')
|
|
98
|
+
assert_not user.valid_password?('654321')
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
test 'should authenticate a valid user with email and password and return it' do
|
|
102
|
+
user = create_user
|
|
103
|
+
User.any_instance.stubs(:confirmed?).returns(true)
|
|
104
|
+
authenticated_user = User.authenticate(:email => user.email, :password => user.password)
|
|
105
|
+
assert_equal authenticated_user, user
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
test 'should return nil when authenticating an invalid user by email' do
|
|
109
|
+
user = create_user
|
|
110
|
+
authenticated_user = User.authenticate(:email => 'another.email@email.com', :password => user.password)
|
|
111
|
+
assert_nil authenticated_user
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
test 'should return nil when authenticating an invalid user by password' do
|
|
115
|
+
user = create_user
|
|
116
|
+
authenticated_user = User.authenticate(:email => user.email, :password => 'another_password')
|
|
117
|
+
assert_nil authenticated_user
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
test 'should use authentication keys to retrieve users' do
|
|
121
|
+
swap Devise, :authentication_keys => [:username] do
|
|
122
|
+
user = create_user
|
|
123
|
+
assert_nil User.authenticate(:email => user.email, :password => user.password)
|
|
124
|
+
assert_not_nil User.authenticate(:username => user.username, :password => user.password)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
test 'should allow overwriting find for authentication conditions' do
|
|
129
|
+
admin = Admin.create!(valid_attributes)
|
|
130
|
+
assert_not_nil Admin.authenticate(:email => admin.email, :password => admin.password)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
test 'should respond to current password' do
|
|
134
|
+
assert new_user.respond_to?(:current_password)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
test 'should update password with valid current password' do
|
|
138
|
+
user = create_user
|
|
139
|
+
assert user.update_with_password(:current_password => '123456',
|
|
140
|
+
:password => 'pass321', :password_confirmation => 'pass321')
|
|
141
|
+
assert user.reload.valid_password?('pass321')
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
test 'should add an error to current password when it is invalid' do
|
|
145
|
+
user = create_user
|
|
146
|
+
assert_not user.update_with_password(:current_password => 'other',
|
|
147
|
+
:password => 'pass321', :password_confirmation => 'pass321')
|
|
148
|
+
assert user.reload.valid_password?('123456')
|
|
149
|
+
assert_match /invalid/, user.errors[:current_password]
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
test 'should add an error to current password when it is blank' do
|
|
153
|
+
user = create_user
|
|
154
|
+
assert_not user.update_with_password(:password => 'pass321',
|
|
155
|
+
:password_confirmation => 'pass321')
|
|
156
|
+
assert user.reload.valid_password?('123456')
|
|
157
|
+
assert_match /blank/, user.errors[:current_password]
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
test 'should ignore password and its confirmation if they are blank' do
|
|
161
|
+
user = create_user
|
|
162
|
+
assert user.update_with_password(:current_password => '123456', :email => "new@email.com")
|
|
163
|
+
assert_equal "new@email.com", user.email
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
test 'should not update password with invalid confirmation' do
|
|
167
|
+
user = create_user
|
|
168
|
+
assert_not user.update_with_password(:current_password => '123456',
|
|
169
|
+
:password => 'pass321', :password_confirmation => 'other')
|
|
170
|
+
assert user.reload.valid_password?('123456')
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
test 'should clean up password fields on failure' do
|
|
174
|
+
user = create_user
|
|
175
|
+
assert_not user.update_with_password(:current_password => '123456',
|
|
176
|
+
:password => 'pass321', :password_confirmation => 'other')
|
|
177
|
+
assert user.password.blank?
|
|
178
|
+
assert user.password_confirmation.blank?
|
|
179
|
+
end
|
|
180
|
+
end
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
require 'test/test_helper'
|
|
2
|
+
|
|
3
|
+
class ConfirmableTest < ActiveSupport::TestCase
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
setup_mailer
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
test 'should generate confirmation token after creating a record' do
|
|
10
|
+
assert_nil new_user.confirmation_token
|
|
11
|
+
assert_not_nil create_user.confirmation_token
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
test 'should regenerate confirmation token each time' do
|
|
15
|
+
user = create_user
|
|
16
|
+
3.times do
|
|
17
|
+
token = user.confirmation_token
|
|
18
|
+
user.resend_confirmation!
|
|
19
|
+
assert_not_equal token, user.confirmation_token
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test 'should never generate the same confirmation token for different users' do
|
|
24
|
+
confirmation_tokens = []
|
|
25
|
+
3.times do
|
|
26
|
+
token = create_user.confirmation_token
|
|
27
|
+
assert !confirmation_tokens.include?(token)
|
|
28
|
+
confirmation_tokens << token
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
test 'should confirm a user by updating confirmed at' do
|
|
33
|
+
user = create_user
|
|
34
|
+
assert_nil user.confirmed_at
|
|
35
|
+
assert user.confirm!
|
|
36
|
+
assert_not_nil user.confirmed_at
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
test 'should clear confirmation token while confirming a user' do
|
|
40
|
+
user = create_user
|
|
41
|
+
assert_present user.confirmation_token
|
|
42
|
+
user.confirm!
|
|
43
|
+
assert_nil user.confirmation_token
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
test 'should verify whether a user is confirmed or not' do
|
|
47
|
+
assert_not new_user.confirmed?
|
|
48
|
+
user = create_user
|
|
49
|
+
assert_not user.confirmed?
|
|
50
|
+
user.confirm!
|
|
51
|
+
assert user.confirmed?
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
test 'should not confirm a user already confirmed' do
|
|
55
|
+
user = create_user
|
|
56
|
+
assert user.confirm!
|
|
57
|
+
assert_nil user.errors[:email]
|
|
58
|
+
|
|
59
|
+
assert_not user.confirm!
|
|
60
|
+
assert_match /already confirmed/, user.errors[:email]
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
test 'should find and confirm an user automatically' do
|
|
64
|
+
user = create_user
|
|
65
|
+
confirmed_user = User.confirm!(:confirmation_token => user.confirmation_token)
|
|
66
|
+
assert_equal confirmed_user, user.reload
|
|
67
|
+
assert user.confirmed?
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
test 'should return a new record with errors when a invalid token is given' do
|
|
71
|
+
confirmed_user = User.confirm!(:confirmation_token => 'invalid_confirmation_token')
|
|
72
|
+
assert confirmed_user.new_record?
|
|
73
|
+
assert_match /invalid/, confirmed_user.errors[:confirmation_token]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
test 'should return a new record with errors when a blank token is given' do
|
|
77
|
+
confirmed_user = User.confirm!(:confirmation_token => '')
|
|
78
|
+
assert confirmed_user.new_record?
|
|
79
|
+
assert_match /blank/, confirmed_user.errors[:confirmation_token]
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
test 'should generate errors for a user email if user is already confirmed' do
|
|
83
|
+
user = create_user
|
|
84
|
+
user.confirmed_at = Time.now
|
|
85
|
+
user.save
|
|
86
|
+
confirmed_user = User.confirm!(:confirmation_token => user.confirmation_token)
|
|
87
|
+
assert confirmed_user.confirmed?
|
|
88
|
+
assert confirmed_user.errors[:email]
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
test 'should authenticate a confirmed user' do
|
|
92
|
+
user = create_user
|
|
93
|
+
user.confirm!
|
|
94
|
+
authenticated_user = User.authenticate(:email => user.email, :password => user.password)
|
|
95
|
+
assert_equal authenticated_user, user
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
test 'should send confirmation instructions by email' do
|
|
99
|
+
assert_email_sent do
|
|
100
|
+
create_user
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
test 'should not send confirmation when trying to save an invalid user' do
|
|
105
|
+
assert_email_not_sent do
|
|
106
|
+
user = new_user
|
|
107
|
+
user.stubs(:valid?).returns(false)
|
|
108
|
+
user.save
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
test 'should not generate a new token neither send e-mail if skip_confirmation! is invoked' do
|
|
113
|
+
user = new_user
|
|
114
|
+
user.skip_confirmation!
|
|
115
|
+
|
|
116
|
+
assert_email_not_sent do
|
|
117
|
+
user.save!
|
|
118
|
+
assert_nil user.confirmation_token
|
|
119
|
+
assert_not_nil user.confirmed_at
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
test 'should find a user to send confirmation instructions' do
|
|
124
|
+
user = create_user
|
|
125
|
+
confirmation_user = User.send_confirmation_instructions(:email => user.email)
|
|
126
|
+
assert_equal confirmation_user, user.reload
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
test 'should return a new user if no email was found' do
|
|
130
|
+
confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
|
|
131
|
+
assert confirmation_user.new_record?
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
test 'should add error to new user email if no email was found' do
|
|
135
|
+
confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
|
|
136
|
+
assert confirmation_user.errors[:email]
|
|
137
|
+
assert_equal 'not found', confirmation_user.errors[:email]
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
test 'should generate a confirmation token before send the confirmation instructions email' do
|
|
141
|
+
user = create_user
|
|
142
|
+
token = user.confirmation_token
|
|
143
|
+
confirmation_user = User.send_confirmation_instructions(:email => user.email)
|
|
144
|
+
assert_not_equal token, user.reload.confirmation_token
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
test 'should send email instructions for the user confirm it\'s email' do
|
|
148
|
+
user = create_user
|
|
149
|
+
assert_email_sent do
|
|
150
|
+
User.send_confirmation_instructions(:email => user.email)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
test 'should not resend email instructions if the user change his email' do
|
|
155
|
+
user = create_user
|
|
156
|
+
user.email = 'new_test@example.com'
|
|
157
|
+
assert_email_not_sent do
|
|
158
|
+
user.save!
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
test 'should not reset confirmation status or token when updating email' do
|
|
163
|
+
user = create_user
|
|
164
|
+
user.confirm!
|
|
165
|
+
user.email = 'new_test@example.com'
|
|
166
|
+
user.save!
|
|
167
|
+
|
|
168
|
+
user.reload
|
|
169
|
+
assert user.confirmed?
|
|
170
|
+
assert_nil user.confirmation_token
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
test 'should not be able to send instructions if the user is already confirmed' do
|
|
174
|
+
user = create_user
|
|
175
|
+
user.confirm!
|
|
176
|
+
assert_not user.resend_confirmation!
|
|
177
|
+
assert user.confirmed?
|
|
178
|
+
assert_equal 'already confirmed', user.errors[:email]
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
test 'confirm time should fallback to devise confirm in default configuration' do
|
|
182
|
+
swap Devise, :confirm_within => 1.day do
|
|
183
|
+
user = new_user
|
|
184
|
+
user.confirmation_sent_at = 2.days.ago
|
|
185
|
+
assert_not user.active?
|
|
186
|
+
|
|
187
|
+
Devise.confirm_within = 3.days
|
|
188
|
+
assert user.active?
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
test 'should be active when confirmation sent at is not overpast' do
|
|
193
|
+
swap Devise, :confirm_within => 5.days do
|
|
194
|
+
Devise.confirm_within = 5.days
|
|
195
|
+
user = create_user
|
|
196
|
+
|
|
197
|
+
user.confirmation_sent_at = 4.days.ago
|
|
198
|
+
assert user.active?
|
|
199
|
+
|
|
200
|
+
user.confirmation_sent_at = 5.days.ago
|
|
201
|
+
assert_not user.active?
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
test 'should be active when already confirmed' do
|
|
206
|
+
user = create_user
|
|
207
|
+
assert_not user.confirmed?
|
|
208
|
+
assert_not user.active?
|
|
209
|
+
|
|
210
|
+
user.confirm!
|
|
211
|
+
assert user.confirmed?
|
|
212
|
+
assert user.active?
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
test 'should not be active when confirm in is zero' do
|
|
216
|
+
Devise.confirm_within = 0.days
|
|
217
|
+
user = create_user
|
|
218
|
+
user.confirmation_sent_at = Date.today
|
|
219
|
+
assert_not user.active?
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
test 'should not be active without confirmation' do
|
|
223
|
+
user = create_user
|
|
224
|
+
user.confirmation_sent_at = nil
|
|
225
|
+
user.save
|
|
226
|
+
assert_not user.reload.active?
|
|
227
|
+
end
|
|
228
|
+
end
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
require 'test/test_helper'
|
|
2
|
+
|
|
3
|
+
class LockableTest < ActiveSupport::TestCase
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
setup_mailer
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
test "should increment failed attempts on unsuccessful authentication" do
|
|
10
|
+
user = create_user
|
|
11
|
+
assert_equal 0, user.failed_attempts
|
|
12
|
+
authenticated_user = User.authenticate(:email => user.email, :password => "anotherpassword")
|
|
13
|
+
assert_equal 1, user.reload.failed_attempts
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
test "should lock account base on maximum_attempts" do
|
|
17
|
+
user = create_user
|
|
18
|
+
attempts = Devise.maximum_attempts + 1
|
|
19
|
+
attempts.times { authenticated_user = User.authenticate(:email => user.email, :password => "anotherpassword") }
|
|
20
|
+
assert user.reload.locked?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test "should respect maximum attempts configuration" do
|
|
24
|
+
user = create_user
|
|
25
|
+
swap Devise, :maximum_attempts => 2 do
|
|
26
|
+
3.times { authenticated_user = User.authenticate(:email => user.email, :password => "anotherpassword") }
|
|
27
|
+
assert user.reload.locked?
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
test "should clear failed_attempts on successfull sign in" do
|
|
32
|
+
user = create_user
|
|
33
|
+
User.authenticate(:email => user.email, :password => "anotherpassword")
|
|
34
|
+
assert_equal 1, user.reload.failed_attempts
|
|
35
|
+
User.authenticate(:email => user.email, :password => "123456")
|
|
36
|
+
assert_equal 0, user.reload.failed_attempts
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
test "should verify wheter a user is locked or not" do
|
|
40
|
+
user = create_user
|
|
41
|
+
assert_not user.locked?
|
|
42
|
+
user.lock!
|
|
43
|
+
assert user.locked?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
test "active? should be the opposite of locked?" do
|
|
47
|
+
user = create_user
|
|
48
|
+
user.confirm!
|
|
49
|
+
assert user.active?
|
|
50
|
+
user.lock!
|
|
51
|
+
assert_not user.active?
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
test "should unlock an user by cleaning locked_at, falied_attempts and unlock_token" do
|
|
55
|
+
user = create_user
|
|
56
|
+
user.lock!
|
|
57
|
+
assert_not_nil user.reload.locked_at
|
|
58
|
+
assert_not_nil user.reload.unlock_token
|
|
59
|
+
user.unlock!
|
|
60
|
+
assert_nil user.reload.locked_at
|
|
61
|
+
assert_nil user.reload.unlock_token
|
|
62
|
+
assert 0, user.reload.failed_attempts
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
test 'should not unlock an unlocked user' do
|
|
66
|
+
user = create_user
|
|
67
|
+
assert_not user.unlock!
|
|
68
|
+
assert_match /not locked/, user.errors[:email]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
test "new user should not be locked and should have zero failed_attempts" do
|
|
72
|
+
assert_not new_user.locked?
|
|
73
|
+
assert_equal 0, create_user.failed_attempts
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
test "should unlock user after unlock_in period" do
|
|
77
|
+
swap Devise, :unlock_in => 3.hours do
|
|
78
|
+
user = new_user
|
|
79
|
+
user.locked_at = 2.hours.ago
|
|
80
|
+
assert user.locked?
|
|
81
|
+
|
|
82
|
+
Devise.unlock_in = 1.hour
|
|
83
|
+
assert_not user.locked?
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
test "should not unlock in 'unlock_in' if :time unlock strategy is not set" do
|
|
88
|
+
swap Devise, :unlock_strategy => :email do
|
|
89
|
+
user = new_user
|
|
90
|
+
user.locked_at = 2.hours.ago
|
|
91
|
+
assert user.locked?
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
test "should set unlock_token when locking" do
|
|
96
|
+
user = create_user
|
|
97
|
+
assert_nil user.unlock_token
|
|
98
|
+
user.lock!
|
|
99
|
+
assert_not_nil user.unlock_token
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
test 'should not regenerate unlock token if it already exists' do
|
|
103
|
+
user = create_user
|
|
104
|
+
user.lock!
|
|
105
|
+
3.times do
|
|
106
|
+
token = user.unlock_token
|
|
107
|
+
user.resend_unlock!
|
|
108
|
+
assert_equal token, user.unlock_token
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
test "should never generate the same unlock token for different users" do
|
|
113
|
+
unlock_tokens = []
|
|
114
|
+
3.times do
|
|
115
|
+
user = create_user
|
|
116
|
+
user.lock!
|
|
117
|
+
token = user.unlock_token
|
|
118
|
+
assert !unlock_tokens.include?(token)
|
|
119
|
+
unlock_tokens << token
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
test "should not generate unlock_token when :email is not an unlock strategy" do
|
|
124
|
+
swap Devise, :unlock_strategy => :time do
|
|
125
|
+
user = create_user
|
|
126
|
+
user.lock!
|
|
127
|
+
assert_nil user.unlock_token
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
test "should send email with unlock instructions when :email is an unlock strategy" do
|
|
132
|
+
swap Devise, :unlock_strategy => :email do
|
|
133
|
+
user = create_user
|
|
134
|
+
assert_email_sent do
|
|
135
|
+
user.lock!
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
test "should not send email with unlock instructions when :email is not an unlock strategy" do
|
|
141
|
+
swap Devise, :unlock_strategy => :time do
|
|
142
|
+
user = create_user
|
|
143
|
+
assert_email_not_sent do
|
|
144
|
+
user.lock!
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
test 'should find and unlock an user automatically' do
|
|
150
|
+
user = create_user
|
|
151
|
+
user.lock!
|
|
152
|
+
locked_user = User.unlock!(:unlock_token => user.unlock_token)
|
|
153
|
+
assert_equal locked_user, user.reload
|
|
154
|
+
assert_not user.reload.locked?
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
test 'should return a new record with errors when a invalid token is given' do
|
|
158
|
+
locked_user = User.unlock!(:unlock_token => 'invalid_token')
|
|
159
|
+
assert locked_user.new_record?
|
|
160
|
+
assert_match /invalid/, locked_user.errors[:unlock_token]
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
test 'should return a new record with errors when a blank token is given' do
|
|
164
|
+
locked_user = User.unlock!(:unlock_token => '')
|
|
165
|
+
assert locked_user.new_record?
|
|
166
|
+
assert_match /blank/, locked_user.errors[:unlock_token]
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
test 'should authenticate a unlocked user' do
|
|
170
|
+
user = create_user
|
|
171
|
+
user.lock!
|
|
172
|
+
user.unlock!
|
|
173
|
+
authenticated_user = User.authenticate(:email => user.email, :password => user.password)
|
|
174
|
+
assert_equal authenticated_user, user
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
test 'should find a user to send unlock instructions' do
|
|
178
|
+
user = create_user
|
|
179
|
+
user.lock!
|
|
180
|
+
unlock_user = User.send_unlock_instructions(:email => user.email)
|
|
181
|
+
assert_equal unlock_user, user
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
test 'should return a new user if no email was found' do
|
|
185
|
+
unlock_user = User.send_unlock_instructions(:email => "invalid@email.com")
|
|
186
|
+
assert unlock_user.new_record?
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
test 'should add error to new user email if no email was found' do
|
|
190
|
+
unlock_user = User.send_unlock_instructions(:email => "invalid@email.com")
|
|
191
|
+
assert unlock_user.errors[:email]
|
|
192
|
+
assert_equal 'not found', unlock_user.errors[:email]
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
test 'should not be able to send instructions if the user is not locked' do
|
|
196
|
+
user = create_user
|
|
197
|
+
assert_not user.resend_unlock!
|
|
198
|
+
assert_not user.locked?
|
|
199
|
+
assert_equal 'not locked', user.errors[:email]
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
end
|