dcu-devise 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. data/CHANGELOG.rdoc +378 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +260 -0
  4. data/Rakefile +53 -0
  5. data/TODO +2 -0
  6. data/app/controllers/confirmations_controller.rb +33 -0
  7. data/app/controllers/passwords_controller.rb +41 -0
  8. data/app/controllers/registrations_controller.rb +53 -0
  9. data/app/controllers/sessions_controller.rb +44 -0
  10. data/app/controllers/unlocks_controller.rb +41 -0
  11. data/app/models/devise_mailer.rb +68 -0
  12. data/app/views/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/passwords/edit.html.erb +16 -0
  17. data/app/views/passwords/new.html.erb +12 -0
  18. data/app/views/registrations/edit.html.erb +25 -0
  19. data/app/views/registrations/new.html.erb +17 -0
  20. data/app/views/sessions/new.html.erb +17 -0
  21. data/app/views/shared/_devise_links.erb +19 -0
  22. data/app/views/unlocks/new.html.erb +12 -0
  23. data/generators/devise/USAGE +5 -0
  24. data/generators/devise/devise_generator.rb +15 -0
  25. data/generators/devise/lib/route_devise.rb +32 -0
  26. data/generators/devise/templates/migration.rb +23 -0
  27. data/generators/devise/templates/model.rb +9 -0
  28. data/generators/devise_install/USAGE +3 -0
  29. data/generators/devise_install/devise_install_generator.rb +15 -0
  30. data/generators/devise_install/templates/README +23 -0
  31. data/generators/devise_install/templates/devise.rb +105 -0
  32. data/generators/devise_views/USAGE +3 -0
  33. data/generators/devise_views/devise_views_generator.rb +21 -0
  34. data/lib/devise.rb +264 -0
  35. data/lib/devise/controllers/helpers.rb +200 -0
  36. data/lib/devise/controllers/internal_helpers.rb +129 -0
  37. data/lib/devise/controllers/url_helpers.rb +41 -0
  38. data/lib/devise/encryptors/authlogic_sha512.rb +21 -0
  39. data/lib/devise/encryptors/base.rb +20 -0
  40. data/lib/devise/encryptors/bcrypt.rb +21 -0
  41. data/lib/devise/encryptors/clearance_sha1.rb +19 -0
  42. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  43. data/lib/devise/encryptors/sha1.rb +27 -0
  44. data/lib/devise/encryptors/sha512.rb +27 -0
  45. data/lib/devise/failure_app.rb +65 -0
  46. data/lib/devise/hooks/activatable.rb +15 -0
  47. data/lib/devise/hooks/rememberable.rb +32 -0
  48. data/lib/devise/hooks/timeoutable.rb +18 -0
  49. data/lib/devise/hooks/trackable.rb +18 -0
  50. data/lib/devise/locales/en.yml +35 -0
  51. data/lib/devise/mapping.rb +128 -0
  52. data/lib/devise/models.rb +117 -0
  53. data/lib/devise/models/activatable.rb +16 -0
  54. data/lib/devise/models/confirmable.rb +162 -0
  55. data/lib/devise/models/database_authenticatable.rb +144 -0
  56. data/lib/devise/models/http_authenticatable.rb +21 -0
  57. data/lib/devise/models/lockable.rb +150 -0
  58. data/lib/devise/models/recoverable.rb +80 -0
  59. data/lib/devise/models/registerable.rb +8 -0
  60. data/lib/devise/models/rememberable.rb +92 -0
  61. data/lib/devise/models/timeoutable.rb +28 -0
  62. data/lib/devise/models/token_authenticatable.rb +89 -0
  63. data/lib/devise/models/trackable.rb +16 -0
  64. data/lib/devise/models/validatable.rb +39 -0
  65. data/lib/devise/orm/active_record.rb +41 -0
  66. data/lib/devise/orm/data_mapper.rb +83 -0
  67. data/lib/devise/orm/mongo_mapper.rb +47 -0
  68. data/lib/devise/rails.rb +14 -0
  69. data/lib/devise/rails/routes.rb +125 -0
  70. data/lib/devise/rails/warden_compat.rb +25 -0
  71. data/lib/devise/schema.rb +73 -0
  72. data/lib/devise/strategies/base.rb +16 -0
  73. data/lib/devise/strategies/database_authenticatable.rb +36 -0
  74. data/lib/devise/strategies/http_authenticatable.rb +59 -0
  75. data/lib/devise/strategies/rememberable.rb +37 -0
  76. data/lib/devise/strategies/token_authenticatable.rb +37 -0
  77. data/lib/devise/test_helpers.rb +90 -0
  78. data/lib/devise/version.rb +3 -0
  79. data/rails/init.rb +2 -0
  80. data/test/controllers/helpers_test.rb +177 -0
  81. data/test/controllers/internal_helpers_test.rb +55 -0
  82. data/test/controllers/url_helpers_test.rb +47 -0
  83. data/test/devise_test.rb +74 -0
  84. data/test/encryptors_test.rb +31 -0
  85. data/test/failure_app_test.rb +44 -0
  86. data/test/integration/authenticatable_test.rb +271 -0
  87. data/test/integration/confirmable_test.rb +97 -0
  88. data/test/integration/http_authenticatable_test.rb +52 -0
  89. data/test/integration/lockable_test.rb +102 -0
  90. data/test/integration/rack_middleware_test.rb +47 -0
  91. data/test/integration/recoverable_test.rb +141 -0
  92. data/test/integration/registerable_test.rb +144 -0
  93. data/test/integration/rememberable_test.rb +71 -0
  94. data/test/integration/timeoutable_test.rb +68 -0
  95. data/test/integration/token_authenticatable_test.rb +55 -0
  96. data/test/integration/trackable_test.rb +64 -0
  97. data/test/mailers/confirmation_instructions_test.rb +86 -0
  98. data/test/mailers/reset_password_instructions_test.rb +68 -0
  99. data/test/mailers/unlock_instructions_test.rb +62 -0
  100. data/test/mapping_test.rb +148 -0
  101. data/test/models/authenticatable_test.rb +180 -0
  102. data/test/models/confirmable_test.rb +212 -0
  103. data/test/models/lockable_test.rb +202 -0
  104. data/test/models/recoverable_test.rb +138 -0
  105. data/test/models/rememberable_test.rb +135 -0
  106. data/test/models/timeoutable_test.rb +28 -0
  107. data/test/models/token_authenticatable_test.rb +51 -0
  108. data/test/models/trackable_test.rb +5 -0
  109. data/test/models/validatable_test.rb +106 -0
  110. data/test/models_test.rb +70 -0
  111. data/test/orm/active_record.rb +31 -0
  112. data/test/orm/mongo_mapper.rb +20 -0
  113. data/test/rails_app/app/active_record/admin.rb +7 -0
  114. data/test/rails_app/app/active_record/user.rb +7 -0
  115. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  116. data/test/rails_app/app/controllers/application_controller.rb +12 -0
  117. data/test/rails_app/app/controllers/home_controller.rb +4 -0
  118. data/test/rails_app/app/controllers/users_controller.rb +16 -0
  119. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  120. data/test/rails_app/app/mongo_mapper/admin.rb +13 -0
  121. data/test/rails_app/app/mongo_mapper/user.rb +14 -0
  122. data/test/rails_app/config/boot.rb +110 -0
  123. data/test/rails_app/config/environment.rb +42 -0
  124. data/test/rails_app/config/environments/development.rb +17 -0
  125. data/test/rails_app/config/environments/production.rb +28 -0
  126. data/test/rails_app/config/environments/test.rb +28 -0
  127. data/test/rails_app/config/initializers/devise.rb +82 -0
  128. data/test/rails_app/config/initializers/inflections.rb +2 -0
  129. data/test/rails_app/config/initializers/new_rails_defaults.rb +24 -0
  130. data/test/rails_app/config/initializers/session_store.rb +15 -0
  131. data/test/rails_app/config/routes.rb +21 -0
  132. data/test/routes_test.rb +110 -0
  133. data/test/support/assertions_helper.rb +37 -0
  134. data/test/support/integration_tests_helper.rb +71 -0
  135. data/test/support/test_silencer.rb +5 -0
  136. data/test/support/tests_helper.rb +39 -0
  137. data/test/test_helper.rb +21 -0
  138. data/test/test_helpers_test.rb +57 -0
  139. metadata +213 -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,212 @@
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 never generate the same confirmation token for different users' do
15
+ confirmation_tokens = []
16
+ 3.times do
17
+ token = create_user.confirmation_token
18
+ assert !confirmation_tokens.include?(token)
19
+ confirmation_tokens << token
20
+ end
21
+ end
22
+
23
+ test 'should confirm a user by updating confirmed at' do
24
+ user = create_user
25
+ assert_nil user.confirmed_at
26
+ assert user.confirm!
27
+ assert_not_nil user.confirmed_at
28
+ end
29
+
30
+ test 'should clear confirmation token while confirming a user' do
31
+ user = create_user
32
+ assert_present user.confirmation_token
33
+ user.confirm!
34
+ assert_nil user.confirmation_token
35
+ end
36
+
37
+ test 'should verify whether a user is confirmed or not' do
38
+ assert_not new_user.confirmed?
39
+ user = create_user
40
+ assert_not user.confirmed?
41
+ user.confirm!
42
+ assert user.confirmed?
43
+ end
44
+
45
+ test 'should not confirm a user already confirmed' do
46
+ user = create_user
47
+ assert user.confirm!
48
+ assert_nil user.errors[:email]
49
+
50
+ assert_not user.confirm!
51
+ assert_match /already confirmed/, user.errors[:email]
52
+ end
53
+
54
+ test 'should find and confirm an user automatically' do
55
+ user = create_user
56
+ confirmed_user = User.confirm_by_token(user.confirmation_token)
57
+ assert_equal confirmed_user, user
58
+ assert user.reload.confirmed?
59
+ end
60
+
61
+ test 'should return a new record with errors when a invalid token is given' do
62
+ confirmed_user = User.confirm_by_token('invalid_confirmation_token')
63
+ assert confirmed_user.new_record?
64
+ assert_match /invalid/, confirmed_user.errors[:confirmation_token]
65
+ end
66
+
67
+ test 'should return a new record with errors when a blank token is given' do
68
+ confirmed_user = User.confirm_by_token('')
69
+ assert confirmed_user.new_record?
70
+ assert_match /blank/, confirmed_user.errors[:confirmation_token]
71
+ end
72
+
73
+ test 'should generate errors for a user email if user is already confirmed' do
74
+ user = create_user
75
+ user.confirmed_at = Time.now
76
+ user.save
77
+ confirmed_user = User.confirm_by_token(user.confirmation_token)
78
+ assert confirmed_user.confirmed?
79
+ assert confirmed_user.errors[:email]
80
+ end
81
+
82
+ test 'should authenticate a confirmed user' do
83
+ user = create_user
84
+ user.confirm!
85
+ authenticated_user = User.authenticate(:email => user.email, :password => user.password)
86
+ assert_equal authenticated_user, user
87
+ end
88
+
89
+ test 'should send confirmation instructions by email' do
90
+ assert_email_sent do
91
+ create_user
92
+ end
93
+ end
94
+
95
+ test 'should not send confirmation when trying to save an invalid user' do
96
+ assert_email_not_sent do
97
+ user = new_user
98
+ user.stubs(:valid?).returns(false)
99
+ user.save
100
+ end
101
+ end
102
+
103
+ test 'should not generate a new token neither send e-mail if skip_confirmation! is invoked' do
104
+ user = new_user
105
+ user.skip_confirmation!
106
+
107
+ assert_email_not_sent do
108
+ user.save!
109
+ assert_nil user.confirmation_token
110
+ assert_not_nil user.confirmed_at
111
+ end
112
+ end
113
+
114
+ test 'should find a user to send confirmation instructions' do
115
+ user = create_user
116
+ confirmation_user = User.send_confirmation_instructions(:email => user.email)
117
+ assert_equal confirmation_user, user
118
+ end
119
+
120
+ test 'should return a new user if no email was found' do
121
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
122
+ assert confirmation_user.new_record?
123
+ end
124
+
125
+ test 'should add error to new user email if no email was found' do
126
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
127
+ assert confirmation_user.errors[:email]
128
+ assert_equal 'not found', confirmation_user.errors[:email]
129
+ end
130
+
131
+ test 'should send email instructions for the user confirm it\'s email' do
132
+ user = create_user
133
+ assert_email_sent do
134
+ User.send_confirmation_instructions(:email => user.email)
135
+ end
136
+ end
137
+
138
+ test 'should not resend email instructions if the user change his email' do
139
+ user = create_user
140
+ user.email = 'new_test@example.com'
141
+ assert_email_not_sent do
142
+ user.save!
143
+ end
144
+ end
145
+
146
+ test 'should not reset confirmation status or token when updating email' do
147
+ user = create_user
148
+ user.confirm!
149
+ user.email = 'new_test@example.com'
150
+ user.save!
151
+
152
+ user.reload
153
+ assert user.confirmed?
154
+ assert_nil user.confirmation_token
155
+ end
156
+
157
+ test 'should not be able to send instructions if the user is already confirmed' do
158
+ user = create_user
159
+ user.confirm!
160
+ assert_not user.resend_confirmation_token
161
+ assert user.confirmed?
162
+ assert_equal 'already confirmed', user.errors[:email]
163
+ end
164
+
165
+ test 'confirm time should fallback to devise confirm in default configuration' do
166
+ swap Devise, :confirm_within => 1.day do
167
+ user = new_user
168
+ user.confirmation_sent_at = 2.days.ago
169
+ assert_not user.active?
170
+
171
+ Devise.confirm_within = 3.days
172
+ assert user.active?
173
+ end
174
+ end
175
+
176
+ test 'should be active when confirmation sent at is not overpast' do
177
+ swap Devise, :confirm_within => 5.days do
178
+ Devise.confirm_within = 5.days
179
+ user = create_user
180
+
181
+ user.confirmation_sent_at = 4.days.ago
182
+ assert user.active?
183
+
184
+ user.confirmation_sent_at = 5.days.ago
185
+ assert_not user.active?
186
+ end
187
+ end
188
+
189
+ test 'should be active when already confirmed' do
190
+ user = create_user
191
+ assert_not user.confirmed?
192
+ assert_not user.active?
193
+
194
+ user.confirm!
195
+ assert user.confirmed?
196
+ assert user.active?
197
+ end
198
+
199
+ test 'should not be active when confirm in is zero' do
200
+ Devise.confirm_within = 0.days
201
+ user = create_user
202
+ user.confirmation_sent_at = Date.today
203
+ assert_not user.reload.active?
204
+ end
205
+
206
+ test 'should not be active without confirmation' do
207
+ user = create_user
208
+ user.confirmation_sent_at = nil
209
+ user.save
210
+ assert_not user.reload.active?
211
+ end
212
+ 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.access_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.access_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 whether a user is locked or not" do
40
+ user = create_user
41
+ assert_not user.access_locked?
42
+ user.lock_access!
43
+ assert user.access_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_access!
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_access!
57
+ assert_not_nil user.reload.locked_at
58
+ assert_not_nil user.reload.unlock_token
59
+
60
+ user.unlock_access!
61
+ assert_nil user.reload.locked_at
62
+ assert_nil user.reload.unlock_token
63
+ assert 0, user.reload.failed_attempts
64
+ end
65
+
66
+ test "should not lock a locked account" do
67
+ user = create_user
68
+ user.lock_access!
69
+ assert_no_difference "ActionMailer::Base.deliveries.size" do
70
+ user.lock_access!
71
+ end
72
+ end
73
+
74
+ test 'should not unlock an unlocked user' do
75
+ user = create_user
76
+
77
+ assert_not user.unlock_access!
78
+ assert_match /not locked/, user.errors[:email]
79
+ end
80
+
81
+ test "new user should not be locked and should have zero failed_attempts" do
82
+ assert_not new_user.access_locked?
83
+ assert_equal 0, create_user.failed_attempts
84
+ end
85
+
86
+ test "should unlock user after unlock_in period" do
87
+ swap Devise, :unlock_in => 3.hours do
88
+ user = new_user
89
+ user.locked_at = 2.hours.ago
90
+ assert user.access_locked?
91
+
92
+ Devise.unlock_in = 1.hour
93
+ assert_not user.access_locked?
94
+ end
95
+ end
96
+
97
+ test "should not unlock in 'unlock_in' if :time unlock strategy is not set" do
98
+ swap Devise, :unlock_strategy => :email do
99
+ user = new_user
100
+ user.locked_at = 2.hours.ago
101
+ assert user.access_locked?
102
+ end
103
+ end
104
+
105
+ test "should set unlock_token when locking" do
106
+ user = create_user
107
+ assert_nil user.unlock_token
108
+ user.lock_access!
109
+ assert_not_nil user.unlock_token
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_access!
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_access!
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_access!
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_access!
145
+ end
146
+ end
147
+ end
148
+
149
+ test 'should find and unlock an user automatically' do
150
+ user = create_user
151
+ user.lock_access!
152
+ locked_user = User.unlock_access_by_token(user.unlock_token)
153
+ assert_equal locked_user, user
154
+ assert_not user.reload.access_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_access_by_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_access_by_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_access!
172
+ user.unlock_access!
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_access!
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_token
198
+ assert_not user.access_locked?
199
+ assert_equal 'not locked', user.errors[:email]
200
+ end
201
+
202
+ end