shingara-devise 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/CHANGELOG.rdoc +119 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +253 -0
  4. data/Rakefile +45 -0
  5. data/TODO +5 -0
  6. data/app/controllers/confirmations_controller.rb +33 -0
  7. data/app/controllers/passwords_controller.rb +41 -0
  8. data/app/controllers/sessions_controller.rb +33 -0
  9. data/app/models/devise_mailer.rb +53 -0
  10. data/app/views/confirmations/new.html.erb +16 -0
  11. data/app/views/devise_mailer/confirmation_instructions.html.erb +5 -0
  12. data/app/views/devise_mailer/reset_password_instructions.html.erb +8 -0
  13. data/app/views/passwords/edit.html.erb +20 -0
  14. data/app/views/passwords/new.html.erb +16 -0
  15. data/app/views/sessions/new.html.erb +23 -0
  16. data/generators/devise/USAGE +5 -0
  17. data/generators/devise/devise_generator.rb +25 -0
  18. data/generators/devise/lib/route_devise.rb +32 -0
  19. data/generators/devise/templates/README +22 -0
  20. data/generators/devise/templates/migration.rb +20 -0
  21. data/generators/devise/templates/model.rb +5 -0
  22. data/generators/devise_install/USAGE +3 -0
  23. data/generators/devise_install/devise_install_generator.rb +9 -0
  24. data/generators/devise_install/templates/devise.rb +47 -0
  25. data/generators/devise_views/USAGE +3 -0
  26. data/generators/devise_views/devise_views_generator.rb +24 -0
  27. data/init.rb +2 -0
  28. data/lib/devise/controllers/filters.rb +111 -0
  29. data/lib/devise/controllers/helpers.rb +130 -0
  30. data/lib/devise/controllers/url_helpers.rb +49 -0
  31. data/lib/devise/encryptors/authlogic_sha512.rb +28 -0
  32. data/lib/devise/encryptors/clearance_sha1.rb +26 -0
  33. data/lib/devise/encryptors/restful_authentication_sha1.rb +29 -0
  34. data/lib/devise/encryptors/sha1.rb +34 -0
  35. data/lib/devise/encryptors/sha512.rb +34 -0
  36. data/lib/devise/failure.rb +36 -0
  37. data/lib/devise/hooks/confirmable.rb +11 -0
  38. data/lib/devise/hooks/rememberable.rb +27 -0
  39. data/lib/devise/locales/en.yml +18 -0
  40. data/lib/devise/mapping.rb +120 -0
  41. data/lib/devise/migrations.rb +57 -0
  42. data/lib/devise/models/authenticatable.rb +87 -0
  43. data/lib/devise/models/confirmable.rb +156 -0
  44. data/lib/devise/models/recoverable.rb +88 -0
  45. data/lib/devise/models/rememberable.rb +95 -0
  46. data/lib/devise/models/validatable.rb +36 -0
  47. data/lib/devise/models.rb +110 -0
  48. data/lib/devise/orm/mongo_mapper.rb +26 -0
  49. data/lib/devise/rails/routes.rb +109 -0
  50. data/lib/devise/rails/warden_compat.rb +26 -0
  51. data/lib/devise/rails.rb +17 -0
  52. data/lib/devise/strategies/authenticatable.rb +46 -0
  53. data/lib/devise/strategies/base.rb +24 -0
  54. data/lib/devise/strategies/rememberable.rb +35 -0
  55. data/lib/devise/version.rb +3 -0
  56. data/lib/devise/warden.rb +20 -0
  57. data/lib/devise.rb +130 -0
  58. data/test/controllers/filters_test.rb +103 -0
  59. data/test/controllers/helpers_test.rb +55 -0
  60. data/test/controllers/url_helpers_test.rb +47 -0
  61. data/test/devise_test.rb +72 -0
  62. data/test/encryptors_test.rb +28 -0
  63. data/test/failure_test.rb +34 -0
  64. data/test/integration/authenticatable_test.rb +195 -0
  65. data/test/integration/confirmable_test.rb +89 -0
  66. data/test/integration/recoverable_test.rb +131 -0
  67. data/test/integration/rememberable_test.rb +65 -0
  68. data/test/mailers/confirmation_instructions_test.rb +59 -0
  69. data/test/mailers/reset_password_instructions_test.rb +62 -0
  70. data/test/mapping_test.rb +101 -0
  71. data/test/models/authenticatable_test.rb +130 -0
  72. data/test/models/confirmable_test.rb +237 -0
  73. data/test/models/recoverable_test.rb +141 -0
  74. data/test/models/rememberable_test.rb +130 -0
  75. data/test/models/validatable_test.rb +99 -0
  76. data/test/models_test.rb +111 -0
  77. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  78. data/test/rails_app/app/controllers/application_controller.rb +10 -0
  79. data/test/rails_app/app/controllers/home_controller.rb +4 -0
  80. data/test/rails_app/app/controllers/users_controller.rb +7 -0
  81. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  82. data/test/rails_app/app/models/account.rb +3 -0
  83. data/test/rails_app/app/models/admin.rb +3 -0
  84. data/test/rails_app/app/models/organizer.rb +3 -0
  85. data/test/rails_app/app/models/user.rb +3 -0
  86. data/test/rails_app/config/boot.rb +110 -0
  87. data/test/rails_app/config/environment.rb +41 -0
  88. data/test/rails_app/config/environments/development.rb +17 -0
  89. data/test/rails_app/config/environments/production.rb +28 -0
  90. data/test/rails_app/config/environments/test.rb +28 -0
  91. data/test/rails_app/config/initializers/new_rails_defaults.rb +21 -0
  92. data/test/rails_app/config/initializers/session_store.rb +15 -0
  93. data/test/rails_app/config/routes.rb +18 -0
  94. data/test/routes_test.rb +79 -0
  95. data/test/support/assertions_helper.rb +22 -0
  96. data/test/support/integration_tests_helper.rb +66 -0
  97. data/test/support/model_tests_helper.rb +51 -0
  98. data/test/test_helper.rb +40 -0
  99. metadata +161 -0
@@ -0,0 +1,237 @@
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.reset_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
+ 10.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 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 and add an error to email' do
55
+ user = create_user
56
+ assert user.confirm!
57
+ assert_nil user.errors[:email]
58
+ assert_not user.confirm!
59
+ assert_not_nil user.errors[:email]
60
+ assert_equal '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_not_nil confirmed_user
67
+ assert_equal confirmed_user, user
68
+ assert user.reload.confirmed?
69
+ end
70
+
71
+ test 'should return a new user with errors if no user exists while trying to confirm' do
72
+ confirmed_user = User.confirm!(:confirmation_token => 'invalid_confirmation_token')
73
+ assert confirmed_user.new_record?
74
+ end
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]
80
+ end
81
+
82
+ test 'should generate errors for a user email if user is already confirmed' do
83
+ user = create_user
84
+ user.confirm!
85
+ confirmed_user = User.confirm!(:confirmation_token => user.confirmation_token)
86
+ assert confirmed_user.confirmed?
87
+ assert confirmed_user.errors[:email]
88
+ end
89
+
90
+ test 'should authenticate a confirmed user' do
91
+ user = create_user
92
+ user.confirm!
93
+ authenticated_user = User.authenticate(:email => user.email, :password => user.password)
94
+ assert_not_nil authenticated_user
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 find a user to send confirmation instructions' do
113
+ user = create_user
114
+ confirmation_user = User.send_confirmation_instructions(:email => user.email)
115
+ assert_not_nil confirmation_user
116
+ assert_equal confirmation_user, user
117
+ end
118
+
119
+ test 'should return a new user if no email was found' do
120
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
121
+ assert_not_nil confirmation_user
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 reset confirmation token before send the confirmation instructions email' do
132
+ user = create_user
133
+ token = user.confirmation_token
134
+ confirmation_user = User.send_confirmation_instructions(:email => user.email)
135
+ assert_not_equal token, user.reload.confirmation_token
136
+ end
137
+
138
+ test 'should reset confirmation status when sending the confirmation instructions' do
139
+ user = create_user
140
+ assert_not user.confirmed?
141
+ confirmation_user = User.send_confirmation_instructions(:email => user.email)
142
+ assert_not user.reload.confirmed?
143
+ end
144
+
145
+ test 'should send email instructions for the user confirm it\'s email' do
146
+ user = create_user
147
+ assert_email_sent do
148
+ User.send_confirmation_instructions(:email => user.email)
149
+ end
150
+ end
151
+
152
+ test 'should not resend email instructions if the user change his email' do
153
+ user = create_user
154
+ user.email = 'new_test@example.com'
155
+ assert_email_not_sent do
156
+ user.save!
157
+ end
158
+ end
159
+
160
+ test 'should not reset confirmation status or token when updating email' do
161
+ user = create_user
162
+ user.confirm!
163
+ user.email = 'new_test@example.com'
164
+ user.save!
165
+
166
+ user.reload
167
+ assert user.confirmed?
168
+ assert_nil user.confirmation_token
169
+ end
170
+
171
+ test 'should not be able to send instructions if the user is already confirmed' do
172
+ user = create_user
173
+ user.confirm!
174
+ assert_not user.reset_confirmation!
175
+ assert user.confirmed?
176
+ assert user.errors[:email].present?
177
+ assert_equal 'already confirmed', user.errors[:email]
178
+ end
179
+
180
+ 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
184
+ user = new_user
185
+ user.confirmation_sent_at = 2.days.ago
186
+ assert_not user.active?
187
+ Devise.confirm_within = 3.days
188
+ assert user.active?
189
+ ensure
190
+ Devise.confirm_within = confirm_within
191
+ end
192
+ end
193
+
194
+ 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?
199
+ end
200
+
201
+ test 'should be active when already confirmed' do
202
+ user = create_user
203
+ assert_not user.confirmed?
204
+ assert_not user.active?
205
+ user.confirm!
206
+ assert user.confirmed?
207
+ assert user.active?
208
+ end
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
218
+ Devise.confirm_within = 0.days
219
+ user = create_user
220
+ user.confirmation_sent_at = Date.today
221
+ assert_not user.active?
222
+ end
223
+
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
+ test 'should not be active without confirmation' do
232
+ user = create_user
233
+ user.update_attribute(:confirmation_sent_at, nil)
234
+ assert_not user.reload.active?
235
+ end
236
+
237
+ end
@@ -0,0 +1,141 @@
1
+ require 'test/test_helper'
2
+
3
+ class RecoverableTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ end
8
+
9
+ test 'should not generate reset password token after creating a record' do
10
+ assert_nil new_user.reset_password_token
11
+ assert_nil create_user.reset_password_token
12
+ end
13
+
14
+ test 'should regenerate reset password token each time' do
15
+ user = create_user
16
+ 3.times do
17
+ token = user.reset_password_token
18
+ user.send_reset_password_instructions
19
+ assert_not_equal token, user.reset_password_token
20
+ end
21
+ end
22
+
23
+ test 'should never generate the same reset password token for different users' do
24
+ reset_password_tokens = []
25
+ 10.times do
26
+ user = create_user
27
+ user.send_reset_password_instructions
28
+ token = user.reset_password_token
29
+ assert !reset_password_tokens.include?(token)
30
+ reset_password_tokens << token
31
+ end
32
+ end
33
+
34
+ test 'should reset password and password confirmation from params' do
35
+ user = create_user
36
+ user.reset_password('123456789', '987654321')
37
+ assert_equal '123456789', user.password
38
+ assert_equal '987654321', user.password_confirmation
39
+ end
40
+
41
+ test 'should reset password and save the record' do
42
+ assert create_user.reset_password!('123456789', '123456789')
43
+ end
44
+
45
+ test 'should clear reset password token while reseting the password' do
46
+ user = create_user
47
+ assert_nil user.reset_password_token
48
+ user.send_reset_password_instructions
49
+ assert_present user.reset_password_token
50
+ assert user.reset_password!('123456789', '123456789')
51
+ assert_nil user.reset_password_token
52
+ end
53
+
54
+ test 'should not clear reset password token if record is invalid' do
55
+ user = create_user
56
+ user.send_reset_password_instructions
57
+ assert_present user.reset_password_token
58
+ assert_not user.reset_password!('123456789', '987654321')
59
+ assert_present user.reset_password_token
60
+ end
61
+
62
+ test 'should not reset password with invalid data' do
63
+ user = create_user
64
+ user.stubs(:valid?).returns(false)
65
+ assert_not user.reset_password!('123456789', '987654321')
66
+ end
67
+
68
+ test 'should reset reset password token and send instructions by email' do
69
+ user = create_user
70
+ assert_email_sent do
71
+ token = user.reset_password_token
72
+ user.send_reset_password_instructions
73
+ assert_not_equal token, user.reset_password_token
74
+ end
75
+ end
76
+
77
+ test 'should find a user to send instructions by email' do
78
+ user = create_user
79
+ reset_password_user = User.send_reset_password_instructions(:email => user.email)
80
+ assert_not_nil reset_password_user
81
+ assert_equal reset_password_user, user
82
+ end
83
+
84
+ test 'should return a new user if no email was found' do
85
+ reset_password_user = User.send_reset_password_instructions(:email => "invalid@email.com")
86
+ assert_not_nil reset_password_user
87
+ 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
+ assert_equal 'not found', reset_password_user.errors[:email]
94
+ end
95
+
96
+ test 'should reset reset password token before send the reset instructions email' do
97
+ user = create_user
98
+ token = user.reset_password_token
99
+ reset_password_user = User.send_reset_password_instructions(:email => user.email)
100
+ assert_not_equal token, user.reload.reset_password_token
101
+ end
102
+
103
+ test 'should send email instructions to the user reset it\'s password' do
104
+ user = create_user
105
+ assert_email_sent do
106
+ User.send_reset_password_instructions(:email => user.email)
107
+ end
108
+ end
109
+
110
+ test 'should find a user to reset it\'s password based on reset_password_token' do
111
+ user = create_user
112
+ reset_password_user = User.reset_password!(:reset_password_token => user.reset_password_token)
113
+ assert_not_nil reset_password_user
114
+ assert_equal reset_password_user, user
115
+ end
116
+
117
+ test 'should return a new user when trying to reset it\'s password if no reset_password_token is found' do
118
+ reset_password_user = User.reset_password!(:reset_password_token => 'invalid_token')
119
+ assert_not_nil reset_password_user
120
+ assert reset_password_user.new_record?
121
+ end
122
+
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]
127
+ end
128
+
129
+ test 'should reset successfully user password given the new password and confirmation' do
130
+ user = create_user
131
+ old_password = user.password
132
+ reset_password_user = User.reset_password!(
133
+ :reset_password_token => user.reset_password_token,
134
+ :password => 'new_password',
135
+ :password_confirmation => 'new_password'
136
+ )
137
+ user.reload
138
+ assert_not user.valid_password?(old_password)
139
+ assert user.valid_password?('new_password')
140
+ end
141
+ end
@@ -0,0 +1,130 @@
1
+ require 'test/test_helper'
2
+
3
+ class RememberableTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ Devise.remember_for = 1
7
+ end
8
+
9
+ test 'should respond to remember_me attribute' do
10
+ user = new_user
11
+ assert user.respond_to?(:remember_me)
12
+ end
13
+
14
+ test 'remember_me should generate a new token and save the record without validating' do
15
+ user = create_user
16
+ user.expects(:valid?).never
17
+ token = user.remember_token
18
+ user.remember_me!
19
+ assert_not_equal token, user.remember_token
20
+ assert_not user.changed?
21
+ end
22
+
23
+ test 'forget_me should clear remember token and save the record without validating' do
24
+ user = create_user
25
+ user.remember_me!
26
+ assert user.remember_token?
27
+ user.expects(:valid?).never
28
+ user.forget_me!
29
+ assert_not user.remember_token?
30
+ assert_not user.changed?
31
+ end
32
+
33
+ test 'forget_me should clear remember_created_at' do
34
+ user = create_user
35
+ user.remember_me!
36
+ assert user.remember_created_at?
37
+ user.forget_me!
38
+ assert_not user.remember_created_at?
39
+ end
40
+
41
+ test 'forget should do nothing if no remember token exists' do
42
+ user = create_user
43
+ user.expects(:save).never
44
+ user.forget_me!
45
+ end
46
+
47
+ test 'valid remember token' do
48
+ user = create_user
49
+ assert_not user.valid_remember_token?(user.remember_token)
50
+ user.remember_me!
51
+ assert user.valid_remember_token?(user.remember_token)
52
+ user.forget_me!
53
+ assert_not user.valid_remember_token?(user.remember_token)
54
+ end
55
+
56
+ test 'valid remember token should also verify if remember is not expired' do
57
+ user = create_user
58
+ user.remember_me!
59
+ user.update_attribute(:remember_created_at, 3.days.ago)
60
+ assert_not user.valid_remember_token?(user.remember_token)
61
+ end
62
+
63
+ test 'serialize into cookie' do
64
+ user = create_user
65
+ user.remember_me!
66
+ assert_equal "#{user.id}::#{user.remember_token}", User.serialize_into_cookie(user)
67
+ end
68
+
69
+ test 'serialize from cookie' do
70
+ user = create_user
71
+ user.remember_me!
72
+ assert_equal user, User.serialize_from_cookie("#{user.id}::#{user.remember_token}")
73
+ end
74
+
75
+ test 'serialize should return nil if no user is found' do
76
+ assert_nil User.serialize_from_cookie('0::123')
77
+ end
78
+
79
+ test 'remember me return nil if is a valid user with invalid token' do
80
+ user = create_user
81
+ assert_nil User.serialize_from_cookie("#{user.id}::#{user.remember_token}123")
82
+ end
83
+
84
+ test 'remember for should fallback to devise remember for default configuration' do
85
+ begin
86
+ remember_for = Devise.remember_for
87
+ user = create_user
88
+ Devise.remember_for = 1.day
89
+ user.remember_me!
90
+ 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
+ end
97
+ end
98
+
99
+ 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
106
+ end
107
+
108
+ 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?
113
+ end
114
+
115
+ 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?
121
+ end
122
+
123
+ 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?
129
+ end
130
+ end
@@ -0,0 +1,99 @@
1
+ require 'test/test_helper'
2
+
3
+ class ValidatableTest < ActiveSupport::TestCase
4
+
5
+ test 'should require email to be set' do
6
+ user = new_user(:email => nil)
7
+ assert user.invalid?
8
+ assert user.errors[:email]
9
+ assert_equal 'can\'t be blank', user.errors[:email]
10
+ end
11
+
12
+ test 'should require uniqueness of email, allowing blank' do
13
+ existing_user = create_user
14
+ user = new_user(:email => '')
15
+ assert user.invalid?
16
+ assert_not_equal 'has already been taken', user.errors[:email]
17
+ user.email = existing_user.email
18
+ assert user.invalid?
19
+ assert user.errors[:email]
20
+ assert_equal 1, user.errors[:email].to_a.size
21
+ assert_equal 'has already been taken', user.errors[:email]
22
+ end
23
+
24
+ test 'should require correct email format, allowing blank' do
25
+ user = new_user(:email => '')
26
+ assert user.invalid?
27
+ assert_not_equal 'is invalid', user.errors[:email]
28
+ %w(invalid_email_format email@invalid invalid$character@mail.com other@not 123).each do |email|
29
+ user.email = email
30
+ assert user.invalid?, 'should be invalid with email ' << email
31
+ assert user.errors[:email]
32
+ assert_equal 1, user.errors[:email].to_a.size
33
+ assert_equal 'is invalid', user.errors[:email]
34
+ end
35
+ end
36
+
37
+ test 'should accept valid emails' do
38
+ %w(a.b.c@example.com test_mail@gmail.com any@any.net email@test.br 123@mail.test).each do |email|
39
+ user = new_user(:email => email)
40
+ assert user.valid?, 'should be valid with email ' << email
41
+ assert_nil user.errors[:email]
42
+ end
43
+ end
44
+
45
+ test 'should require password to be set when creating a new record' do
46
+ user = new_user(:password => '', :password_confirmation => '')
47
+ assert user.invalid?
48
+ assert user.errors[:password]
49
+ assert_equal 'can\'t be blank', user.errors[:password]
50
+ end
51
+
52
+ test 'should require confirmation to be set when creating a new record' do
53
+ user = new_user(:password => 'new_password', :password_confirmation => 'blabla')
54
+ assert user.invalid?
55
+ assert user.errors[:password]
56
+ assert_equal 'doesn\'t match confirmation', user.errors[:password]
57
+ end
58
+
59
+ test 'should require password when updating/reseting password' do
60
+ user = create_user
61
+ user.password = ''
62
+ user.password_confirmation = ''
63
+ assert user.invalid?
64
+ assert user.errors[:password]
65
+ assert_equal 'can\'t be blank', user.errors[:password]
66
+ end
67
+
68
+ test 'should require confirmation when updating/reseting password' do
69
+ user = create_user
70
+ user.password_confirmation = 'another_password'
71
+ assert user.invalid?
72
+ assert user.errors[:password]
73
+ assert_equal 'doesn\'t match confirmation', user.errors[:password]
74
+ end
75
+
76
+ test 'should require a password with minimum of 6 characters' do
77
+ user = new_user(:password => '12345', :password_confirmation => '12345')
78
+ assert user.invalid?
79
+ assert user.errors[:password]
80
+ assert_equal 'is too short (minimum is 6 characters)', user.errors[:password]
81
+ end
82
+
83
+ test 'should require a password with maximum of 20 characters long' do
84
+ user = new_user(:password => 'x'*21, :password_confirmation => 'x'*21)
85
+ assert user.invalid?
86
+ assert user.errors[:password]
87
+ assert_equal 'is too long (maximum is 20 characters)', user.errors[:password]
88
+ end
89
+
90
+ test 'should not require password length when it\'s not changed' do
91
+ user = create_user.reload
92
+ user.password = user.password_confirmation = nil
93
+ assert user.valid?
94
+ user.password_confirmation = 'confirmation'
95
+ assert user.invalid?
96
+ assert user.errors[:password]
97
+ assert_not user.errors[:password].to_a.include?('is too short (minimum is 6 characters)')
98
+ end
99
+ end