devise 0.6.1 → 0.6.2

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.

@@ -0,0 +1,64 @@
1
+ require 'test/test_helper'
2
+
3
+ class TrackableHooksTest < ActionController::IntegrationTest
4
+
5
+ test "current and last sign in timestamps are updated on each sign in" do
6
+ user = create_user
7
+ assert_nil user.current_sign_in_at
8
+ assert_nil user.last_sign_in_at
9
+
10
+ sign_in_as_user
11
+ user.reload
12
+
13
+ assert_kind_of Time, user.current_sign_in_at
14
+ assert_kind_of Time, user.last_sign_in_at
15
+
16
+ assert_equal user.current_sign_in_at, user.last_sign_in_at
17
+ assert user.current_sign_in_at >= user.created_at
18
+
19
+ visit destroy_user_session_path
20
+ new_time = 2.seconds.from_now
21
+ Time.stubs(:now).returns(new_time)
22
+
23
+ sign_in_as_user
24
+ user.reload
25
+ assert user.current_sign_in_at > user.last_sign_in_at
26
+ end
27
+
28
+ test "current and last sign in remote ip are updated on each sign in" do
29
+ user = create_user
30
+ assert_nil user.current_sign_in_ip
31
+ assert_nil user.last_sign_in_ip
32
+
33
+ sign_in_as_user
34
+ user.reload
35
+
36
+ assert_equal "127.0.0.1", user.current_sign_in_ip
37
+ assert_equal "127.0.0.1", user.last_sign_in_ip
38
+ end
39
+
40
+ test "increase sign in count" do
41
+ user = create_user
42
+ assert_nil user.sign_in_count
43
+
44
+ sign_in_as_user
45
+ user.reload
46
+ assert_equal 1, user.sign_in_count
47
+
48
+ visit destroy_user_session_path
49
+ sign_in_as_user
50
+ user.reload
51
+ assert_equal 2, user.sign_in_count
52
+ end
53
+
54
+ test "does not update anything if user is signed out along the way" do
55
+ swap Devise, :confirm_within => 0 do
56
+ user = create_user(:confirm => false)
57
+ sign_in_as_user
58
+
59
+ user.reload
60
+ assert_nil user.current_sign_in_at
61
+ assert_nil user.last_sign_in_at
62
+ end
63
+ end
64
+ end
@@ -13,11 +13,10 @@ class AuthenticatableTest < ActiveSupport::TestCase
13
13
  assert user.respond_to?(:password_confirmation)
14
14
  end
15
15
 
16
- test 'should generate salt while setting password' do
17
- assert_present new_user.password_salt
18
- assert_present new_user(:password => nil).password_salt
19
- assert_present new_user(:password => '').password_salt
20
- assert_present create_user.password_salt
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
21
20
  end
22
21
 
23
22
  test 'should not change password salt when updating' do
@@ -33,20 +32,14 @@ class AuthenticatableTest < ActiveSupport::TestCase
33
32
  assert_equal 'friendly_token', new_user.password_salt
34
33
  end
35
34
 
36
- test 'should never generate the same salt for different users' do
37
- password_salts = []
38
- 10.times do
39
- salt = create_user.password_salt
40
- assert_not password_salts.include?(salt)
41
- password_salts << salt
42
- end
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
43
38
  end
44
39
 
45
- test 'should generate encrypted password while setting password' do
46
- assert_present new_user.encrypted_password
47
- assert_present new_user(:password => nil).encrypted_password
48
- assert_present new_user(:password => '').encrypted_password
49
- assert_present create_user.encrypted_password
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
50
43
  end
51
44
 
52
45
  test 'should encrypt password again if password has changed' do
@@ -57,31 +50,28 @@ class AuthenticatableTest < ActiveSupport::TestCase
57
50
  assert_not_equal encrypted_password, user.encrypted_password
58
51
  end
59
52
 
60
- test 'should fallback to Sha1 as default encryption' do
53
+ test 'should fallback to sha1 as default encryption' do
61
54
  user = new_user
62
55
  assert_equal encrypt_password(user), user.encrypted_password
63
56
  end
64
57
 
65
- test 'should fallback to devise pepper default configuring' do
58
+ test 'should fallback to devise pepper default configuration' do
66
59
  begin
67
60
  Devise.pepper = ''
68
61
  user = new_user
69
62
  assert_equal encrypt_password(user), user.encrypted_password
70
63
  assert_not_equal encrypt_password(user, 'another_pepper'), user.encrypted_password
64
+
71
65
  Devise.pepper = 'new_pepper'
72
66
  user = new_user
73
67
  assert_equal encrypt_password(user, 'new_pepper'), user.encrypted_password
74
68
  assert_not_equal encrypt_password(user, 'another_pepper'), user.encrypted_password
75
- Devise.pepper = '123456'
76
- user = new_user
77
- assert_equal encrypt_password(user, '123456'), user.encrypted_password
78
- assert_not_equal encrypt_password(user, 'another_pepper'), user.encrypted_password
79
69
  ensure
80
70
  Devise.pepper = nil
81
71
  end
82
72
  end
83
73
 
84
- test 'should fallback to devise stretches default configuring' do
74
+ test 'should fallback to devise stretches default configuration' do
85
75
  swap Devise, :stretches => 1 do
86
76
  user = new_user
87
77
  assert_equal encrypt_password(user, nil, 1), user.encrypted_password
@@ -140,11 +130,6 @@ class AuthenticatableTest < ActiveSupport::TestCase
140
130
  assert_not_nil Admin.authenticate(:email => admin.email, :password => admin.password)
141
131
  end
142
132
 
143
- test 'should never authenticate an account' do
144
- account = Account.create!(valid_attributes)
145
- assert_nil Account.authenticate(:email => account.email, :password => account.password)
146
- end
147
-
148
133
  test 'should serialize user into session' do
149
134
  user = create_user
150
135
  assert_equal [User, user.id], User.serialize_into_session(user)
@@ -155,16 +140,16 @@ class AuthenticatableTest < ActiveSupport::TestCase
155
140
  assert_equal user.id, User.serialize_from_session([User, user.id]).id
156
141
  end
157
142
 
158
- test 'should not serialize another klass from session' do
143
+ test 'should serialize another klass from session if it is an ancestors' do
159
144
  user = create_user
160
- assert_raise RuntimeError, /ancestors/ do
161
- User.serialize_from_session([Admin, user.id])
162
- end
145
+ klass = Class.new(User)
146
+ assert_equal user.id, User.serialize_from_session([klass, user.id]).id
163
147
  end
164
148
 
165
- test 'should serialize another klass from session' do
149
+ test 'should not serialize another klass from session if not an ancestors' do
166
150
  user = create_user
167
- klass = Class.new(User)
168
- assert_equal user.id, User.serialize_from_session([klass, user.id]).id
151
+ assert_raise RuntimeError, /ancestors/ do
152
+ User.serialize_from_session([Admin, user.id])
153
+ end
169
154
  end
170
155
  end
@@ -22,14 +22,14 @@ class ConfirmableTest < ActiveSupport::TestCase
22
22
 
23
23
  test 'should never generate the same confirmation token for different users' do
24
24
  confirmation_tokens = []
25
- 10.times do
25
+ 3.times do
26
26
  token = create_user.confirmation_token
27
27
  assert !confirmation_tokens.include?(token)
28
28
  confirmation_tokens << token
29
29
  end
30
30
  end
31
31
 
32
- test 'should confirm a user updating confirmed at' do
32
+ test 'should confirm a user by updating confirmed at' do
33
33
  user = create_user
34
34
  assert_nil user.confirmed_at
35
35
  assert user.confirm!
@@ -51,37 +51,37 @@ class ConfirmableTest < ActiveSupport::TestCase
51
51
  assert user.confirmed?
52
52
  end
53
53
 
54
- test 'should not confirm a user already confirmed and add an error to email' do
54
+ test 'should not confirm a user already confirmed' do
55
55
  user = create_user
56
56
  assert user.confirm!
57
57
  assert_nil user.errors[:email]
58
+
58
59
  assert_not user.confirm!
59
- assert_not_nil user.errors[:email]
60
60
  assert_equal 'already confirmed', user.errors[:email]
61
61
  end
62
62
 
63
63
  test 'should find and confirm an user automatically' do
64
64
  user = create_user
65
65
  confirmed_user = User.confirm!(:confirmation_token => user.confirmation_token)
66
- assert_not_nil confirmed_user
67
66
  assert_equal confirmed_user, user
68
67
  assert user.reload.confirmed?
69
68
  end
70
69
 
71
- test 'should return a new user with errors if no user exists while trying to confirm' do
70
+ test 'should return a new record with errors when a invalid token is given' do
72
71
  confirmed_user = User.confirm!(:confirmation_token => 'invalid_confirmation_token')
73
72
  assert confirmed_user.new_record?
73
+ assert_equal "is invalid", confirmed_user.errors[:confirmation_token]
74
74
  end
75
75
 
76
- test 'should return errors for a new user when trying to confirm' do
77
- confirmed_user = User.confirm!(:confirmation_token => 'invalid_confirmation_token')
78
- assert_not_nil confirmed_user.errors[:confirmation_token]
79
- assert_equal 'is invalid', confirmed_user.errors[:confirmation_token]
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_equal "can't be blank", confirmed_user.errors[:confirmation_token]
80
80
  end
81
81
 
82
82
  test 'should generate errors for a user email if user is already confirmed' do
83
83
  user = create_user
84
- user.confirm!
84
+ user.update_attribute(:confirmed_at, Time.now)
85
85
  confirmed_user = User.confirm!(:confirmation_token => user.confirmation_token)
86
86
  assert confirmed_user.confirmed?
87
87
  assert confirmed_user.errors[:email]
@@ -91,7 +91,6 @@ class ConfirmableTest < ActiveSupport::TestCase
91
91
  user = create_user
92
92
  user.confirm!
93
93
  authenticated_user = User.authenticate(:email => user.email, :password => user.password)
94
- assert_not_nil authenticated_user
95
94
  assert_equal authenticated_user, user
96
95
  end
97
96
 
@@ -112,13 +111,11 @@ class ConfirmableTest < ActiveSupport::TestCase
112
111
  test 'should find a user to send confirmation instructions' do
113
112
  user = create_user
114
113
  confirmation_user = User.send_confirmation_instructions(:email => user.email)
115
- assert_not_nil confirmation_user
116
114
  assert_equal confirmation_user, user
117
115
  end
118
116
 
119
117
  test 'should return a new user if no email was found' do
120
118
  confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
121
- assert_not_nil confirmation_user
122
119
  assert confirmation_user.new_record?
123
120
  end
124
121
 
@@ -173,65 +170,53 @@ class ConfirmableTest < ActiveSupport::TestCase
173
170
  user.confirm!
174
171
  assert_not user.reset_confirmation!
175
172
  assert user.confirmed?
176
- assert user.errors[:email].present?
177
173
  assert_equal 'already confirmed', user.errors[:email]
178
174
  end
179
175
 
180
176
  test 'confirm time should fallback to devise confirm in default configuration' do
181
- begin
182
- confirm_within = Devise.confirm_within
183
- Devise.confirm_within = 1.day
177
+ swap Devise, :confirm_within => 1.day do
184
178
  user = new_user
185
179
  user.confirmation_sent_at = 2.days.ago
186
180
  assert_not user.active?
181
+
187
182
  Devise.confirm_within = 3.days
188
183
  assert user.active?
189
- ensure
190
- Devise.confirm_within = confirm_within
191
184
  end
192
185
  end
193
186
 
194
187
  test 'should be active when confirmation sent at is not overpast' do
195
- Devise.confirm_within = 5.days
196
- user = create_user
197
- user.confirmation_sent_at = 4.days.ago
198
- assert user.active?
188
+ swap Devise, :confirm_within => 5.days do
189
+ Devise.confirm_within = 5.days
190
+ user = create_user
191
+
192
+ user.confirmation_sent_at = 4.days.ago
193
+ assert user.active?
194
+
195
+ user.confirmation_sent_at = 5.days.ago
196
+ assert_not user.active?
197
+ end
199
198
  end
200
199
 
201
200
  test 'should be active when already confirmed' do
202
201
  user = create_user
203
202
  assert_not user.confirmed?
204
203
  assert_not user.active?
204
+
205
205
  user.confirm!
206
206
  assert user.confirmed?
207
207
  assert user.active?
208
208
  end
209
209
 
210
- test 'should not be active when confirmation was sent within the limit' do
211
- Devise.confirm_within = 5.days
212
- user = create_user
213
- user.confirmation_sent_at = 5.days.ago
214
- assert_not user.active?
215
- end
216
-
217
- test 'should be active when confirm in is zero' do
210
+ test 'should not be active when confirm in is zero' do
218
211
  Devise.confirm_within = 0.days
219
212
  user = create_user
220
213
  user.confirmation_sent_at = Date.today
221
214
  assert_not user.active?
222
215
  end
223
216
 
224
- test 'should not be active when confirmation was sent before confirm in time' do
225
- Devise.confirm_within = 4.days
226
- user = create_user
227
- user.confirmation_sent_at = 5.days.ago
228
- assert_not user.active?
229
- end
230
-
231
217
  test 'should not be active without confirmation' do
232
218
  user = create_user
233
219
  user.update_attribute(:confirmation_sent_at, nil)
234
220
  assert_not user.reload.active?
235
221
  end
236
-
237
222
  end
@@ -8,7 +8,6 @@ class RecoverableTest < ActiveSupport::TestCase
8
8
 
9
9
  test 'should not generate reset password token after creating a record' do
10
10
  assert_nil new_user.reset_password_token
11
- assert_nil create_user.reset_password_token
12
11
  end
13
12
 
14
13
  test 'should regenerate reset password token each time' do
@@ -22,7 +21,7 @@ class RecoverableTest < ActiveSupport::TestCase
22
21
 
23
22
  test 'should never generate the same reset password token for different users' do
24
23
  reset_password_tokens = []
25
- 10.times do
24
+ 3.times do
26
25
  user = create_user
27
26
  user.send_reset_password_instructions
28
27
  token = user.reset_password_token
@@ -45,6 +44,7 @@ class RecoverableTest < ActiveSupport::TestCase
45
44
  test 'should clear reset password token while reseting the password' do
46
45
  user = create_user
47
46
  assert_nil user.reset_password_token
47
+
48
48
  user.send_reset_password_instructions
49
49
  assert_present user.reset_password_token
50
50
  assert user.reset_password!('123456789', '123456789')
@@ -77,64 +77,61 @@ class RecoverableTest < ActiveSupport::TestCase
77
77
  test 'should find a user to send instructions by email' do
78
78
  user = create_user
79
79
  reset_password_user = User.send_reset_password_instructions(:email => user.email)
80
- assert_not_nil reset_password_user
81
80
  assert_equal reset_password_user, user
82
81
  end
83
82
 
84
- test 'should return a new user if no email was found' do
83
+ test 'should return a new record with errors if user was not found by e-mail' do
85
84
  reset_password_user = User.send_reset_password_instructions(:email => "invalid@email.com")
86
- assert_not_nil reset_password_user
87
85
  assert reset_password_user.new_record?
88
- end
89
-
90
- test 'should add error to new user email if no email was found' do
91
- reset_password_user = User.send_reset_password_instructions(:email => "invalid@email.com")
92
- assert reset_password_user.errors[:email]
93
86
  assert_equal 'not found', reset_password_user.errors[:email]
94
87
  end
95
88
 
96
- test 'should reset reset password token before send the reset instructions email' do
89
+ test 'should reset reset_password_token before send the reset instructions email' do
97
90
  user = create_user
98
91
  token = user.reset_password_token
99
92
  reset_password_user = User.send_reset_password_instructions(:email => user.email)
100
93
  assert_not_equal token, user.reload.reset_password_token
101
94
  end
102
95
 
103
- test 'should send email instructions to the user reset it\'s password' do
96
+ test 'should send email instructions to the user reset his password' do
104
97
  user = create_user
105
98
  assert_email_sent do
106
99
  User.send_reset_password_instructions(:email => user.email)
107
100
  end
108
101
  end
109
102
 
110
- test 'should find a user to reset it\'s password based on reset_password_token' do
103
+ test 'should find a user to reset his password based on reset_password_token' do
111
104
  user = create_user
105
+ user.send :generate_reset_password_token!
106
+
112
107
  reset_password_user = User.reset_password!(:reset_password_token => user.reset_password_token)
113
- assert_not_nil reset_password_user
114
108
  assert_equal reset_password_user, user
115
109
  end
116
110
 
117
- test 'should return a new user when trying to reset it\'s password if no reset_password_token is found' do
111
+ test 'should a new record with errors if no reset_password_token is found' do
118
112
  reset_password_user = User.reset_password!(:reset_password_token => 'invalid_token')
119
- assert_not_nil reset_password_user
120
113
  assert reset_password_user.new_record?
114
+ assert_equal 'is invalid', reset_password_user.errors[:reset_password_token]
121
115
  end
122
116
 
123
- test 'should add error to new user email if no reset password token was found' do
124
- reset_password_user = User.reset_password!(:reset_password_token => "invalid_token")
125
- assert reset_password_user.errors[:reset_password_token]
126
- assert_equal 'is invalid', reset_password_user.errors[:reset_password_token]
117
+ test 'should a new record with errors if reset_password_token is blank' do
118
+ reset_password_user = User.reset_password!(:reset_password_token => '')
119
+ assert reset_password_user.new_record?
120
+ assert_equal "can't be blank", reset_password_user.errors[:reset_password_token]
127
121
  end
128
122
 
129
123
  test 'should reset successfully user password given the new password and confirmation' do
130
124
  user = create_user
131
125
  old_password = user.password
126
+ user.send :generate_reset_password_token!
127
+
132
128
  reset_password_user = User.reset_password!(
133
129
  :reset_password_token => user.reset_password_token,
134
130
  :password => 'new_password',
135
131
  :password_confirmation => 'new_password'
136
132
  )
137
133
  user.reload
134
+
138
135
  assert_not user.valid_password?(old_password)
139
136
  assert user.valid_password?('new_password')
140
137
  end
@@ -82,49 +82,48 @@ class RememberableTest < ActiveSupport::TestCase
82
82
  end
83
83
 
84
84
  test 'remember for should fallback to devise remember for default configuration' do
85
- begin
86
- remember_for = Devise.remember_for
85
+ swap Devise, :remember_for => 1.day do
87
86
  user = create_user
88
- Devise.remember_for = 1.day
89
87
  user.remember_me!
90
88
  assert_not user.remember_expired?
91
- Devise.remember_for = 0.days
92
- user.remember_me!
93
- assert user.remember_expired?
94
- ensure
95
- Devise.remember_for = remember_for
96
89
  end
97
90
  end
98
91
 
99
92
  test 'remember expires at should sum date of creation with remember for configuration' do
100
- Devise.remember_for = 3.days
101
- user = create_user
102
- user.remember_me!
103
- assert_equal 3.days.from_now.to_date, user.remember_expires_at.to_date
104
- Devise.remember_for = 5.days
105
- assert_equal 5.days.from_now.to_date, user.remember_expires_at.to_date
93
+ swap Devise, :remember_for => 3.days do
94
+ user = create_user
95
+ user.remember_me!
96
+ assert_equal 3.days.from_now.to_date, user.remember_expires_at.to_date
97
+
98
+ Devise.remember_for = 5.days
99
+ assert_equal 5.days.from_now.to_date, user.remember_expires_at.to_date
100
+ end
106
101
  end
107
102
 
108
103
  test 'remember should be expired if remember_for is zero' do
109
- Devise.remember_for = 0.days
110
- user = create_user
111
- user.remember_me!
112
- assert user.remember_expired?
104
+ swap Devise, :remember_for => 0.days do
105
+ Devise.remember_for = 0.days
106
+ user = create_user
107
+ user.remember_me!
108
+ assert user.remember_expired?
109
+ end
113
110
  end
114
111
 
115
112
  test 'remember should be expired if it was created before limit time' do
116
- Devise.remember_for = 1.day
117
- user = create_user
118
- user.remember_me!
119
- user.update_attribute(:remember_created_at, 2.days.ago)
120
- assert user.remember_expired?
113
+ swap Devise, :remember_for => 1.day do
114
+ user = create_user
115
+ user.remember_me!
116
+ user.update_attribute(:remember_created_at, 2.days.ago)
117
+ assert user.remember_expired?
118
+ end
121
119
  end
122
120
 
123
121
  test 'remember should not be expired if it was created whitin the limit time' do
124
- Devise.remember_for = 30.days
125
- user = create_user
126
- user.remember_me!
127
- user.update_attribute(:remember_created_at, 30.days.ago + 2.minutes)
128
- assert_not user.remember_expired?
122
+ swap Devise, :remember_for => 30.days do
123
+ user = create_user
124
+ user.remember_me!
125
+ user.update_attribute(:remember_created_at, 30.days.ago + 2.minutes)
126
+ assert_not user.remember_expired?
127
+ end
129
128
  end
130
129
  end