devise_invitable 1.7.4 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of devise_invitable might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/{CHANGELOG → CHANGELOG.md} +54 -19
- data/README.rdoc +98 -111
- data/app/controllers/devise/invitations_controller.rb +41 -41
- data/app/controllers/devise_invitable/registrations_controller.rb +11 -11
- data/app/views/devise/invitations/edit.html.erb +14 -8
- data/app/views/devise/invitations/new.html.erb +11 -7
- data/app/views/devise/mailer/invitation_instructions.html.erb +2 -2
- data/app/views/devise/mailer/invitation_instructions.text.erb +2 -2
- data/config/locales/ar.yml +23 -0
- data/config/locales/da.yml +41 -0
- data/config/locales/de.yml +31 -0
- data/config/locales/en.yml +1 -1
- data/config/locales/es.yml +31 -0
- data/config/locales/et.yml +23 -0
- data/config/locales/fa.yml +31 -0
- data/config/locales/fr.yml +34 -0
- data/config/locales/it.yml +31 -0
- data/config/locales/ja.yml +32 -0
- data/config/locales/ko.yml +24 -0
- data/config/locales/nl.yml +32 -0
- data/config/locales/no.yml +17 -0
- data/config/locales/pl.yml +31 -0
- data/config/locales/pt-BR.yml +23 -0
- data/config/locales/pt.yml +23 -0
- data/config/locales/ru.yml +23 -0
- data/config/locales/tr.yml +24 -0
- data/config/locales/ua.yml +31 -0
- data/config/locales/vi.yml +25 -0
- data/config/locales/zh-HK.yml +31 -0
- data/config/locales/zh-TW.yml +31 -0
- data/lib/devise_invitable.rb +5 -4
- data/lib/devise_invitable/controllers/helpers.rb +3 -4
- data/lib/devise_invitable/inviter.rb +4 -3
- data/lib/devise_invitable/mailer.rb +1 -1
- data/lib/devise_invitable/mapping.rb +6 -5
- data/lib/devise_invitable/models.rb +48 -32
- data/lib/devise_invitable/parameter_sanitizer.rb +18 -18
- data/lib/devise_invitable/routes.rb +5 -5
- data/lib/devise_invitable/version.rb +1 -1
- data/lib/generators/active_record/devise_invitable_generator.rb +3 -3
- data/lib/generators/active_record/templates/migration.rb +0 -1
- data/lib/generators/devise_invitable/devise_invitable_generator.rb +4 -8
- data/lib/generators/devise_invitable/install_generator.rb +11 -14
- data/lib/generators/devise_invitable/templates/simple_form_for/invitations/edit.html.erb +10 -6
- data/lib/generators/devise_invitable/templates/simple_form_for/invitations/new.html.erb +10 -6
- data/lib/generators/devise_invitable/views_generator.rb +6 -6
- data/test/functional/registrations_controller_test.rb +24 -25
- data/test/generators/views_generator_test.rb +7 -6
- data/test/generators_test.rb +3 -2
- data/test/integration/invitation_remove_test.rb +8 -8
- data/test/integration/invitation_test.rb +46 -46
- data/test/integration_tests_helper.rb +8 -9
- data/test/model_tests_helper.rb +5 -5
- data/test/models/invitable_test.rb +123 -108
- data/test/models_test.rb +3 -3
- data/test/orm/active_record.rb +5 -1
- data/test/orm/mongoid.rb +2 -2
- data/test/rails_app/app/controllers/admins_controller.rb +4 -3
- data/test/rails_app/app/controllers/application_controller.rb +11 -10
- data/test/rails_app/app/controllers/free_invitations_controller.rb +12 -9
- data/test/rails_app/app/controllers/users_controller.rb +2 -2
- data/test/rails_app/app/models/admin.rb +7 -10
- data/test/rails_app/app/models/octopussy.rb +4 -4
- data/test/rails_app/app/models/user.rb +20 -20
- data/test/rails_app/app/views/admins/new.html.erb +9 -5
- data/test/rails_app/app/views/devise/sessions/new.html.erb +14 -6
- data/test/rails_app/app/views/free_invitations/new.html.erb +9 -5
- data/test/rails_app/app/views/layouts/application.html.erb +3 -4
- data/test/rails_app/app/views/users/invitations/new.html.erb +15 -9
- data/test/rails_app/config/application.rb +7 -6
- data/test/rails_app/config/boot.rb +2 -2
- data/test/rails_app/config/credentials.yml.enc +1 -0
- data/test/rails_app/config/initializers/devise.rb +4 -3
- data/test/rails_app/config/initializers/secret_token.rb +3 -1
- data/test/rails_app/config/initializers/session_store.rb +1 -1
- data/test/rails_app/config/initializers/wrap_parameters.rb +1 -1
- data/test/rails_app/config/master.key +1 -0
- data/test/rails_app/config/routes.rb +3 -3
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -2
- data/test/routes_test.rb +4 -4
- data/test/test_helper.rb +6 -19
- metadata +48 -23
data/test/model_tests_helper.rb
CHANGED
@@ -21,13 +21,13 @@ class ActiveSupport::TestCase
|
|
21
21
|
"test#{@@email_count}@email.com"
|
22
22
|
end
|
23
23
|
|
24
|
-
def valid_attributes(attributes={})
|
25
|
-
{ :
|
26
|
-
:
|
27
|
-
:
|
24
|
+
def valid_attributes(attributes = {})
|
25
|
+
{ email: generate_unique_email,
|
26
|
+
password: '123456',
|
27
|
+
password_confirmation: '123456' }.update(attributes)
|
28
28
|
end
|
29
29
|
|
30
|
-
def new_user(attributes={})
|
30
|
+
def new_user(attributes = {})
|
31
31
|
User.new(valid_attributes(attributes))
|
32
32
|
end
|
33
33
|
|
@@ -15,7 +15,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
15
15
|
if defined?(ActiveRecord)
|
16
16
|
current_user = new_user
|
17
17
|
2.times do |index|
|
18
|
-
User.invite!({:
|
18
|
+
User.invite!({ email: "valid#{index}@email.com" }, current_user)
|
19
19
|
end
|
20
20
|
assert_equal current_user.reload.invitations_count, 2
|
21
21
|
end
|
@@ -74,7 +74,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
74
74
|
test 'should return the correct raw_invitation_token ' do
|
75
75
|
user = new_user
|
76
76
|
raw, enc = Devise.token_generator.generate(user.class, :invitation_token)
|
77
|
-
#stub the generator so the tokens are the same
|
77
|
+
# stub the generator so the tokens are the same
|
78
78
|
Devise.token_generator.stubs(:generate).returns([raw, enc])
|
79
79
|
user.invite!
|
80
80
|
assert_equal user.raw_invitation_token, raw
|
@@ -85,17 +85,17 @@ class InvitableTest < ActiveSupport::TestCase
|
|
85
85
|
user.invite!
|
86
86
|
old_invitation_created_at = 3.days.ago
|
87
87
|
old_invitation_sent_at = 3.days.ago
|
88
|
-
user.update_attributes(:
|
88
|
+
user.update_attributes(invitation_sent_at: old_invitation_sent_at, invitation_created_at: old_invitation_created_at)
|
89
89
|
3.times do
|
90
90
|
user.invite!
|
91
91
|
refute_equal old_invitation_sent_at, user.invitation_sent_at
|
92
92
|
refute_equal old_invitation_created_at, user.invitation_created_at
|
93
|
-
user.update_attributes(:
|
93
|
+
user.update_attributes(invitation_sent_at: old_invitation_sent_at, invitation_created_at: old_invitation_created_at)
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
97
|
test 'should test invitation sent at with invite_for configuration value' do
|
98
|
-
user = User.invite!(:
|
98
|
+
user = User.invite!(email: 'valid@email.com')
|
99
99
|
|
100
100
|
User.stubs(:invite_for).returns(nil)
|
101
101
|
user.invitation_created_at = Time.now.utc
|
@@ -125,7 +125,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
125
125
|
test 'should return token validity when there is invite_for' do
|
126
126
|
User.stubs(:invite_for).returns(1.day)
|
127
127
|
|
128
|
-
user = User.invite!(:
|
128
|
+
user = User.invite!(email: 'valid@email.com')
|
129
129
|
sent_at = user.invitation_created_at || user.invitation_sent_at
|
130
130
|
valid_until = sent_at + User.invite_for
|
131
131
|
|
@@ -134,7 +134,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
134
134
|
|
135
135
|
test 'should return nil for invitation due date when invite_for is nil' do
|
136
136
|
User.stubs(:invite_for).returns(nil)
|
137
|
-
user = User.invite!(:
|
137
|
+
user = User.invite!(email: 'valid@email.com')
|
138
138
|
|
139
139
|
assert_nil user.invitation_due_at
|
140
140
|
end
|
@@ -158,22 +158,22 @@ class InvitableTest < ActiveSupport::TestCase
|
|
158
158
|
end
|
159
159
|
|
160
160
|
test 'should invite with multiple columns for invite key' do
|
161
|
-
User.stubs(:invite_key).returns(:
|
162
|
-
user = User.invite!(:
|
161
|
+
User.stubs(:invite_key).returns(email: Devise.email_regexp, username: /\A.+\z/)
|
162
|
+
user = User.invite!(email: 'valid@email.com', username: "name")
|
163
163
|
assert_predicate user, :persisted?
|
164
164
|
assert_empty user.errors
|
165
165
|
end
|
166
166
|
|
167
167
|
test 'should allow non-string columns for invite key' do
|
168
|
-
User.stubs(:invite_key).returns(:
|
169
|
-
user = User.invite!(:
|
168
|
+
User.stubs(:invite_key).returns(email: Devise.email_regexp, profile_id: :present?.to_proc)
|
169
|
+
user = User.invite!(email: 'valid@email.com', profile_id: 1)
|
170
170
|
assert_predicate user, :persisted?
|
171
171
|
assert_empty user.errors
|
172
172
|
end
|
173
173
|
|
174
174
|
test 'should not invite with some missing columns when invite key is an array' do
|
175
|
-
User.stubs(:invite_key).returns(:
|
176
|
-
user = User.invite!(:
|
175
|
+
User.stubs(:invite_key).returns(email: Devise.email_regexp, username: /\A.+\z/, profile_id: :present?.to_proc, active: true)
|
176
|
+
user = User.invite!(email: 'valid@email.com')
|
177
177
|
assert_predicate user, :new_record?
|
178
178
|
refute_empty user.errors
|
179
179
|
assert user.errors[:username]
|
@@ -183,70 +183,85 @@ class InvitableTest < ActiveSupport::TestCase
|
|
183
183
|
end
|
184
184
|
|
185
185
|
test 'should return mail object' do
|
186
|
-
mail = User.invite_mail!(:
|
186
|
+
mail = User.invite_mail!(email: 'valid@email.com')
|
187
187
|
assert_instance_of Mail::Message, mail
|
188
188
|
end
|
189
189
|
|
190
190
|
test 'should disallow login when invited' do
|
191
|
-
invited_user = User.invite!(:
|
191
|
+
invited_user = User.invite!(email: 'valid@email.com')
|
192
192
|
refute invited_user.valid_password?('1234')
|
193
193
|
end
|
194
194
|
|
195
195
|
test 'should not accept invite without password' do
|
196
|
-
User.invite!(:
|
197
|
-
User.accept_invitation!(:
|
198
|
-
refute_predicate User.where(:
|
196
|
+
User.invite!(email: 'valid@email.com')
|
197
|
+
User.accept_invitation!(invitation_token: Thread.current[:token])
|
198
|
+
refute_predicate User.where(email: 'valid@email.com').first, :invitation_accepted?
|
199
199
|
end
|
200
200
|
|
201
201
|
test 'should accept invite without password if enforce is disabled' do
|
202
|
-
Devise.stubs(:
|
203
|
-
User.invite!(:
|
204
|
-
User.accept_invitation!(:
|
205
|
-
assert_predicate User.where(:
|
202
|
+
Devise.stubs(require_password_on_accepting: false)
|
203
|
+
User.invite!(email: 'valid@email.com')
|
204
|
+
User.accept_invitation!(invitation_token: Thread.current[:token])
|
205
|
+
assert_predicate User.where(email: 'valid@email.com').first, :invitation_accepted?
|
206
206
|
end
|
207
207
|
|
208
208
|
test 'should set password and password confirmation from params' do
|
209
|
-
User.invite!(:
|
210
|
-
user = User.accept_invitation!(:
|
209
|
+
User.invite!(email: 'valid@email.com')
|
210
|
+
user = User.accept_invitation!(invitation_token: Thread.current[:token], password: '123456789', password_confirmation: '123456789')
|
211
211
|
assert user.valid_password?('123456789')
|
212
212
|
end
|
213
213
|
|
214
214
|
test 'should set password and save the record' do
|
215
|
-
user = User.invite!(:
|
215
|
+
user = User.invite!(email: 'valid@email.com')
|
216
216
|
old_encrypted_password = user.encrypted_password
|
217
|
-
user = User.accept_invitation!(:
|
217
|
+
user = User.accept_invitation!(invitation_token: Thread.current[:token], password: '123456789', password_confirmation: '123456789')
|
218
218
|
refute_equal old_encrypted_password, user.encrypted_password
|
219
219
|
end
|
220
220
|
|
221
221
|
test 'should not override password on invite!' do
|
222
|
-
user = User.invite!(:
|
222
|
+
user = User.invite!(email: 'valid@email.com', password: 'password', password_confirmation: 'password', skip_invitation: true)
|
223
223
|
assert user.valid?
|
224
224
|
end
|
225
225
|
|
226
226
|
test 'should clear invitation token and set invitation_accepted_at while accepting the password' do
|
227
|
-
user = User.invite!(:
|
227
|
+
user = User.invite!(email: 'valid@email.com')
|
228
228
|
assert user.invitation_token.present?
|
229
229
|
assert_nil user.invitation_accepted_at
|
230
230
|
user.accept_invitation!
|
231
|
+
assert_nil user.invitation_token
|
232
|
+
assert user.invitation_accepted_at.present?
|
233
|
+
assert user.invitation_accepted?
|
231
234
|
user.reload
|
232
235
|
assert_nil user.invitation_token
|
233
236
|
assert user.invitation_accepted_at.present?
|
237
|
+
assert user.invitation_accepted?
|
234
238
|
end
|
235
239
|
|
236
240
|
test 'should not clear invitation token or set accepted_at if record is invalid' do
|
237
|
-
user = User.invite!(:
|
241
|
+
user = User.invite!(email: 'valid@email.com')
|
238
242
|
assert user.invitation_token.present?
|
239
243
|
assert_nil user.invitation_accepted_at
|
240
244
|
old_encrypted_password = user.encrypted_password
|
241
|
-
User.accept_invitation!(:
|
245
|
+
User.accept_invitation!(invitation_token: user.invitation_token, password: '123456789', password_confirmation: '987654321')
|
242
246
|
user.reload
|
243
247
|
assert_equal old_encrypted_password, user.encrypted_password
|
244
248
|
assert user.invitation_token.present?
|
245
249
|
assert_nil user.invitation_accepted_at
|
246
250
|
end
|
247
251
|
|
252
|
+
test 'should not require reloading if invalid' do
|
253
|
+
user = User.invite!(email: 'valid@email.com')
|
254
|
+
assert user.invitation_token.present?
|
255
|
+
assert_nil user.invitation_accepted_at
|
256
|
+
user.attributes = { password: '123456789', password_confirmation: '987654321' }
|
257
|
+
user.accept_invitation!
|
258
|
+
assert user.invitation_token.present?
|
259
|
+
assert_nil user.invitation_accepted_at
|
260
|
+
assert !user.invitation_accepted?
|
261
|
+
end
|
262
|
+
|
248
263
|
test 'should clear invitation token while resetting the password' do
|
249
|
-
user = User.invite!(:
|
264
|
+
user = User.invite!(email: 'valid@email.com')
|
250
265
|
assert user.invited_to_sign_up?
|
251
266
|
token, user.reset_password_token = Devise.token_generator.generate(User, :reset_password_token)
|
252
267
|
user.reset_password_sent_at = Time.now.utc
|
@@ -254,14 +269,14 @@ class InvitableTest < ActiveSupport::TestCase
|
|
254
269
|
|
255
270
|
assert user.reset_password_token.present?
|
256
271
|
assert user.invitation_token.present?
|
257
|
-
User.reset_password_by_token(:
|
272
|
+
User.reset_password_by_token(reset_password_token: token, password: '123456789', password_confirmation: '123456789')
|
258
273
|
assert_nil user.reload.reset_password_token
|
259
274
|
assert_nil user.reload.invitation_token
|
260
275
|
refute_predicate user, :invited_to_sign_up?
|
261
276
|
end
|
262
277
|
|
263
278
|
test 'should not accept invitation on failing to reset the password' do
|
264
|
-
user = User.invite!(:
|
279
|
+
user = User.invite!(email: 'valid@email.com')
|
265
280
|
assert user.invited_to_sign_up?
|
266
281
|
token, user.reset_password_token = Devise.token_generator.generate(User, :reset_password_token)
|
267
282
|
user.reset_password_sent_at = Time.now.utc
|
@@ -269,14 +284,14 @@ class InvitableTest < ActiveSupport::TestCase
|
|
269
284
|
|
270
285
|
assert user.reset_password_token.present?
|
271
286
|
assert user.invitation_token.present?
|
272
|
-
User.reset_password_by_token(:
|
287
|
+
User.reset_password_by_token(reset_password_token: token, password: '123456789', password_confirmation: '12345678')
|
273
288
|
assert user.reload.reset_password_token.present?
|
274
289
|
assert user.reload.invitation_token.present?
|
275
290
|
assert user.invited_to_sign_up?
|
276
291
|
end
|
277
292
|
|
278
293
|
test 'should not set invitation_accepted_at if just resetting password' do
|
279
|
-
user = User.create!(:
|
294
|
+
user = User.create!(email: 'valid@email.com', password: '123456780')
|
280
295
|
refute_predicate user, :invited_to_sign_up?
|
281
296
|
token, user.reset_password_token = Devise.token_generator.generate(User, :reset_password_token)
|
282
297
|
user.reset_password_sent_at = Time.now.utc
|
@@ -284,7 +299,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
284
299
|
|
285
300
|
assert user.reset_password_token.present?
|
286
301
|
assert_nil user.invitation_token
|
287
|
-
User.reset_password_by_token(:
|
302
|
+
User.reset_password_by_token(reset_password_token: token, password: '123456789', password_confirmation: '123456789')
|
288
303
|
assert_nil user.reload.invitation_token
|
289
304
|
assert_nil user.reload.invitation_accepted_at
|
290
305
|
end
|
@@ -299,7 +314,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
299
314
|
end
|
300
315
|
|
301
316
|
test 'should return a record with invitation token and no errors to send invitation by email' do
|
302
|
-
invited_user = User.invite!(:
|
317
|
+
invited_user = User.invite!(email: 'valid@email.com')
|
303
318
|
assert_empty invited_user.errors
|
304
319
|
assert_predicate invited_user.invitation_token, :present?
|
305
320
|
assert_equal 'valid@email.com', invited_user.email
|
@@ -307,7 +322,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
307
322
|
end
|
308
323
|
|
309
324
|
test 'should set all attributes with no errors' do
|
310
|
-
invited_user = User.invite!(:
|
325
|
+
invited_user = User.invite!(email: 'valid@email.com', username: 'first name')
|
311
326
|
assert_empty invited_user.errors
|
312
327
|
assert_equal 'first name', invited_user.username
|
313
328
|
assert_predicate invited_user, :persisted?
|
@@ -316,7 +331,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
316
331
|
test 'should not validate other attributes when validate_on_invite is disabled' do
|
317
332
|
validate_on_invite = User.validate_on_invite
|
318
333
|
User.validate_on_invite = false
|
319
|
-
invited_user = User.invite!(:
|
334
|
+
invited_user = User.invite!(email: 'valid@email.com', username: 'a' * 50)
|
320
335
|
assert_empty invited_user.errors
|
321
336
|
User.validate_on_invite = validate_on_invite
|
322
337
|
end
|
@@ -324,7 +339,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
324
339
|
test 'should validate other attributes when validate_on_invite is enabled' do
|
325
340
|
validate_on_invite = User.validate_on_invite
|
326
341
|
User.validate_on_invite = true
|
327
|
-
invited_user = User.invite!(:
|
342
|
+
invited_user = User.invite!(email: 'valid@email.com', username: 'a' * 50)
|
328
343
|
refute_empty invited_user.errors[:username]
|
329
344
|
User.validate_on_invite = validate_on_invite
|
330
345
|
end
|
@@ -332,7 +347,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
332
347
|
test 'should not validate password when validate_on_invite is enabled' do
|
333
348
|
validate_on_invite = User.validate_on_invite
|
334
349
|
User.validate_on_invite = true
|
335
|
-
invited_user = User.invite!(:
|
350
|
+
invited_user = User.invite!(email: 'valid@email.com', username: 'a' * 50)
|
336
351
|
refute_empty invited_user.errors
|
337
352
|
assert_empty invited_user.errors[:password]
|
338
353
|
User.validate_on_invite = validate_on_invite
|
@@ -341,33 +356,33 @@ class InvitableTest < ActiveSupport::TestCase
|
|
341
356
|
test 'should validate other attributes when validate_on_invite is enabled and email is not present' do
|
342
357
|
validate_on_invite = User.validate_on_invite
|
343
358
|
User.validate_on_invite = true
|
344
|
-
invited_user = User.invite!(:
|
359
|
+
invited_user = User.invite!(email: '', username: 'a' * 50)
|
345
360
|
refute_empty invited_user.errors[:email]
|
346
361
|
refute_empty invited_user.errors[:username]
|
347
362
|
User.validate_on_invite = validate_on_invite
|
348
363
|
end
|
349
364
|
|
350
365
|
test 'should return a record with errors if user was found by e-mail' do
|
351
|
-
existing_user = User.new(:
|
352
|
-
existing_user.save(:
|
353
|
-
user = User.invite!(:
|
366
|
+
existing_user = User.new(email: 'valid@email.com')
|
367
|
+
existing_user.save(validate: false)
|
368
|
+
user = User.invite!(email: 'valid@email.com')
|
354
369
|
assert_equal user, existing_user
|
355
370
|
assert_equal ['has already been taken'], user.errors[:email]
|
356
|
-
same_user = User.invite!(
|
371
|
+
same_user = User.invite!(email: 'valid@email.com')
|
357
372
|
assert_equal same_user, existing_user
|
358
373
|
assert_equal ['has already been taken'], same_user.errors[:email]
|
359
374
|
end
|
360
375
|
|
361
376
|
test 'should return a record with errors if user with pending invitation was found by e-mail' do
|
362
|
-
existing_user = User.invite!(:
|
363
|
-
user = User.invite!(:
|
377
|
+
existing_user = User.invite!(email: 'valid@email.com')
|
378
|
+
user = User.invite!(email: 'valid@email.com')
|
364
379
|
assert_equal user, existing_user
|
365
380
|
assert_equal [], user.errors[:email]
|
366
381
|
resend_invitation = User.resend_invitation
|
367
382
|
begin
|
368
383
|
User.resend_invitation = false
|
369
384
|
|
370
|
-
user = User.invite!(:
|
385
|
+
user = User.invite!(email: 'valid@email.com')
|
371
386
|
assert_equal user, existing_user
|
372
387
|
assert_equal ['has already been taken'], user.errors[:email]
|
373
388
|
ensure
|
@@ -379,9 +394,9 @@ class InvitableTest < ActiveSupport::TestCase
|
|
379
394
|
begin
|
380
395
|
validate_on_invite = User.validate_on_invite
|
381
396
|
User.validate_on_invite = true
|
382
|
-
existing_user = User.new(:
|
383
|
-
existing_user.save(:
|
384
|
-
user = User.invite!(:
|
397
|
+
existing_user = User.new(email: 'valid@email.com')
|
398
|
+
existing_user.save(validate: false)
|
399
|
+
user = User.invite!(email: 'valid@email.com', username: 'a' * 50)
|
385
400
|
assert_equal user, existing_user
|
386
401
|
assert_equal ['has already been taken'], user.errors[:email]
|
387
402
|
refute_empty user.errors[:username]
|
@@ -391,19 +406,19 @@ class InvitableTest < ActiveSupport::TestCase
|
|
391
406
|
end
|
392
407
|
|
393
408
|
test 'should return a new record with errors if e-mail is blank' do
|
394
|
-
invited_user = User.invite!(:
|
409
|
+
invited_user = User.invite!(email: '')
|
395
410
|
assert invited_user.new_record?
|
396
411
|
assert_equal ["can't be blank"], invited_user.errors[:email]
|
397
412
|
end
|
398
413
|
|
399
414
|
test 'should return a new record with errors if e-mail is invalid' do
|
400
|
-
invited_user = User.invite!(:
|
415
|
+
invited_user = User.invite!(email: 'invalid_email')
|
401
416
|
assert invited_user.new_record?
|
402
|
-
assert_equal [
|
417
|
+
assert_equal ['is invalid'], invited_user.errors[:email]
|
403
418
|
end
|
404
419
|
|
405
420
|
test 'should set all attributes with errors if e-mail is invalid' do
|
406
|
-
invited_user = User.invite!(:
|
421
|
+
invited_user = User.invite!(email: 'invalid_email.com', username: 'first name')
|
407
422
|
assert invited_user.new_record?
|
408
423
|
assert_equal 'first name', invited_user.username
|
409
424
|
refute_empty invited_user.errors
|
@@ -412,34 +427,34 @@ class InvitableTest < ActiveSupport::TestCase
|
|
412
427
|
test 'should find a user to set his password based on invitation_token' do
|
413
428
|
user = new_user
|
414
429
|
user.invite!
|
415
|
-
invited_user = User.accept_invitation!(:
|
430
|
+
invited_user = User.accept_invitation!(invitation_token: Thread.current[:token])
|
416
431
|
assert_equal invited_user, user
|
417
432
|
end
|
418
433
|
|
419
434
|
test 'should return a new record with errors if no invitation_token is found' do
|
420
|
-
invited_user = User.accept_invitation!(:
|
435
|
+
invited_user = User.accept_invitation!(invitation_token: 'invalid_token')
|
421
436
|
assert invited_user.new_record?
|
422
437
|
assert_equal ['is invalid'], invited_user.errors[:invitation_token]
|
423
438
|
end
|
424
439
|
|
425
440
|
test 'should return a new record with errors if invitation_token is blank' do
|
426
|
-
invited_user = User.accept_invitation!(:
|
441
|
+
invited_user = User.accept_invitation!(invitation_token: '')
|
427
442
|
assert invited_user.new_record?
|
428
443
|
assert_equal ["can't be blank"], invited_user.errors[:invitation_token]
|
429
444
|
end
|
430
445
|
|
431
446
|
test 'should return record with errors if invitation_token has expired' do
|
432
447
|
User.stubs(:invite_for).returns(10.hours)
|
433
|
-
invited_user = User.invite!(:
|
448
|
+
invited_user = User.invite!(email: 'valid@email.com')
|
434
449
|
invited_user.invitation_created_at = 2.days.ago
|
435
|
-
invited_user.save(:
|
436
|
-
user = User.accept_invitation!(:
|
450
|
+
invited_user.save(validate: false)
|
451
|
+
user = User.accept_invitation!(invitation_token: Thread.current[:token])
|
437
452
|
assert_equal user, invited_user
|
438
|
-
assert_equal [
|
453
|
+
assert_equal ['is invalid'], user.errors[:invitation_token]
|
439
454
|
end
|
440
455
|
|
441
456
|
test 'should allow record modification using block' do
|
442
|
-
invited_user = User.invite!(:
|
457
|
+
invited_user = User.invite!(email: 'valid@email.com', username: 'a' * 50) do |u|
|
443
458
|
u.password = '123123'
|
444
459
|
u.password_confirmation = '123123'
|
445
460
|
end
|
@@ -447,13 +462,13 @@ class InvitableTest < ActiveSupport::TestCase
|
|
447
462
|
end
|
448
463
|
|
449
464
|
test 'should set successfully user password given the new password and confirmation' do
|
450
|
-
user = new_user(:
|
465
|
+
user = new_user(password: nil, password_confirmation: nil)
|
451
466
|
user.invite!
|
452
467
|
|
453
468
|
User.accept_invitation!(
|
454
|
-
:
|
455
|
-
:
|
456
|
-
:
|
469
|
+
invitation_token: Thread.current[:token],
|
470
|
+
password: 'new_password',
|
471
|
+
password_confirmation: 'new_password',
|
457
472
|
)
|
458
473
|
user.reload
|
459
474
|
|
@@ -461,14 +476,14 @@ class InvitableTest < ActiveSupport::TestCase
|
|
461
476
|
end
|
462
477
|
|
463
478
|
test 'should return errors on other attributes even when password is valid' do
|
464
|
-
user = new_user(:
|
479
|
+
user = new_user(password: nil, password_confirmation: nil)
|
465
480
|
user.invite!
|
466
481
|
|
467
482
|
invited_user = User.accept_invitation!(
|
468
|
-
:
|
469
|
-
:
|
470
|
-
:
|
471
|
-
:
|
483
|
+
invitation_token: Thread.current[:token],
|
484
|
+
password: 'new_password',
|
485
|
+
password_confirmation: 'new_password',
|
486
|
+
username: 'a' * 50,
|
472
487
|
)
|
473
488
|
refute_empty invited_user.errors[:username]
|
474
489
|
|
@@ -477,14 +492,14 @@ class InvitableTest < ActiveSupport::TestCase
|
|
477
492
|
end
|
478
493
|
|
479
494
|
test 'should check if created by invitation' do
|
480
|
-
user = User.invite!(:
|
495
|
+
user = User.invite!(email: 'valid@email.com')
|
481
496
|
assert user.created_by_invite?
|
482
497
|
|
483
498
|
invited_user = User.accept_invitation!(
|
484
|
-
:
|
485
|
-
:
|
486
|
-
:
|
487
|
-
:
|
499
|
+
invitation_token: Thread.current[:token],
|
500
|
+
password: 'new_password',
|
501
|
+
password_confirmation: 'new_password',
|
502
|
+
username: 'a',
|
488
503
|
)
|
489
504
|
user.reload
|
490
505
|
assert user.created_by_invite?
|
@@ -492,14 +507,14 @@ class InvitableTest < ActiveSupport::TestCase
|
|
492
507
|
|
493
508
|
|
494
509
|
test 'should set other attributes on accepting invitation' do
|
495
|
-
user = new_user(:
|
510
|
+
user = new_user(password: nil, password_confirmation: nil)
|
496
511
|
user.invite!
|
497
512
|
|
498
513
|
invited_user = User.accept_invitation!(
|
499
|
-
:
|
500
|
-
:
|
501
|
-
:
|
502
|
-
:
|
514
|
+
invitation_token: Thread.current[:token],
|
515
|
+
password: 'new_password',
|
516
|
+
password_confirmation: 'new_password',
|
517
|
+
username: 'a',
|
503
518
|
)
|
504
519
|
assert invited_user.errors[:username].blank?
|
505
520
|
|
@@ -539,13 +554,13 @@ class InvitableTest < ActiveSupport::TestCase
|
|
539
554
|
|
540
555
|
test 'should not send an invitation if we want to skip the invitation' do
|
541
556
|
assert_no_difference('ActionMailer::Base.deliveries.size') do
|
542
|
-
User.invite!(:
|
557
|
+
User.invite!(email: 'valid@email.com', username: 'a' * 50, skip_invitation: true)
|
543
558
|
end
|
544
559
|
end
|
545
560
|
|
546
561
|
test 'should not send an invitation if we want to skip the invitation with block' do
|
547
562
|
assert_no_difference('ActionMailer::Base.deliveries.size') do
|
548
|
-
User.invite!(:
|
563
|
+
User.invite!(email: 'valid@email.com', username: 'a' * 50) do |u|
|
549
564
|
u.skip_invitation = true
|
550
565
|
end
|
551
566
|
end
|
@@ -580,22 +595,22 @@ class InvitableTest < ActiveSupport::TestCase
|
|
580
595
|
|
581
596
|
test 'user.invite! should set the invited_by attribute if passed' do
|
582
597
|
user = new_user
|
583
|
-
inviting_user = User.new(:
|
584
|
-
inviting_user.save(:
|
598
|
+
inviting_user = User.new(email: 'valid@email.com')
|
599
|
+
inviting_user.save(validate: false)
|
585
600
|
user.invite!(inviting_user)
|
586
601
|
assert_equal inviting_user, user.invited_by
|
587
602
|
assert_equal inviting_user.class.to_s, user.invited_by_type
|
588
603
|
end
|
589
604
|
|
590
605
|
test 'user.accept_invitation! should trigger callbacks' do
|
591
|
-
user = User.invite!(:
|
606
|
+
user = User.invite!(email: 'valid@email.com')
|
592
607
|
assert_callbacks_not_fired :after_invitation_accepted, user
|
593
608
|
user.accept_invitation!
|
594
609
|
assert_callbacks_fired :after_invitation_accepted, user
|
595
610
|
end
|
596
611
|
|
597
612
|
test 'user.accept_invitation! should not trigger callbacks if validation fails' do
|
598
|
-
user = User.invite!(:
|
613
|
+
user = User.invite!(email: 'valid@email.com')
|
599
614
|
assert_callbacks_not_fired :after_invitation_accepted, user
|
600
615
|
user.username='a'*50
|
601
616
|
user.accept_invitation!
|
@@ -603,7 +618,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
603
618
|
end
|
604
619
|
|
605
620
|
test 'user.accept_invitation! should confirm user if confirmable' do
|
606
|
-
user = User.invite!(:
|
621
|
+
user = User.invite!(email: 'valid@email.com')
|
607
622
|
user.accept_invitation!
|
608
623
|
|
609
624
|
assert user.confirmed?
|
@@ -611,7 +626,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
611
626
|
end
|
612
627
|
|
613
628
|
test 'user.accept_invitation! should not confirm user if validation fails' do
|
614
|
-
user = User.invite!(:
|
629
|
+
user = User.invite!(email: 'valid@email.com')
|
615
630
|
user.username='a'*50
|
616
631
|
user.accept_invitation!
|
617
632
|
user.reload
|
@@ -625,7 +640,7 @@ class InvitableTest < ActiveSupport::TestCase
|
|
625
640
|
begin
|
626
641
|
User.send_password_change_notification = true
|
627
642
|
|
628
|
-
user = User.invite!(:
|
643
|
+
user = User.invite!(email: 'valid@email.com')
|
629
644
|
|
630
645
|
assert_no_difference('ActionMailer::Base.deliveries.size') do
|
631
646
|
user.password = user.password_confirmation = '123456789'
|
@@ -656,53 +671,53 @@ class InvitableTest < ActiveSupport::TestCase
|
|
656
671
|
|
657
672
|
test "user.invite! should downcase the class's case_insensitive_keys" do
|
658
673
|
# Devise default is :email
|
659
|
-
user = User.invite!(:
|
660
|
-
assert user.email ==
|
674
|
+
user = User.invite!(email: 'UPPERCASE@email.com')
|
675
|
+
assert user.email == 'uppercase@email.com'
|
661
676
|
end
|
662
677
|
|
663
678
|
test "user.invite! should strip whitespace from the class's strip_whitespace_keys" do
|
664
679
|
# Devise default is email
|
665
|
-
user = User.invite!(:
|
666
|
-
assert user.email ==
|
680
|
+
user = User.invite!(email: ' valid@email.com ', active: true)
|
681
|
+
assert user.email == 'valid@email.com'
|
667
682
|
assert user.active == true
|
668
683
|
end
|
669
684
|
|
670
685
|
test "user.invite! should trigger callbacks" do
|
671
|
-
user = User.new(email:
|
686
|
+
user = User.new(email: 'valid@email.com')
|
672
687
|
assert_callbacks_not_fired :after_invitation_created, user
|
673
688
|
user.invite!
|
674
689
|
assert_callbacks_fired :after_invitation_created, user
|
675
690
|
end
|
676
691
|
|
677
692
|
test 'should pass validation before accept if field is required in post-invited instance' do
|
678
|
-
user = User.invite!(:
|
693
|
+
user = User.invite!(email: 'valid@email.com')
|
679
694
|
user.testing_accepted_or_not_invited = true
|
680
695
|
user.valid?
|
681
696
|
assert_empty user.errors
|
682
697
|
end
|
683
698
|
|
684
699
|
test 'should fail validation after accept if field is required in post-invited instance' do
|
685
|
-
user = User.invite!(:
|
700
|
+
user = User.invite!(email: 'valid@email.com')
|
686
701
|
user.testing_accepted_or_not_invited = true
|
687
702
|
assert_predicate user, :accept_invitation!
|
688
|
-
user = User.where(:
|
703
|
+
user = User.where(email: 'valid@email.com').first
|
689
704
|
user.valid?
|
690
705
|
refute_empty user.errors
|
691
706
|
end
|
692
707
|
|
693
708
|
test 'should pass validation after accept if field is required in post-invited instance' do
|
694
|
-
user = User.invite!(:
|
709
|
+
user = User.invite!(email: 'valid@email.com')
|
695
710
|
user.username = 'test'
|
696
711
|
user.testing_accepted_or_not_invited = true
|
697
712
|
assert_predicate user, :accept_invitation!
|
698
|
-
user = User.where(:
|
713
|
+
user = User.where(email: 'valid@email.com').first
|
699
714
|
user.bio = "Test"
|
700
715
|
user.valid?
|
701
716
|
assert_empty user.errors
|
702
717
|
end
|
703
718
|
|
704
719
|
test 'should return instance with errors if invitation_token is nil' do
|
705
|
-
User.create(:
|
720
|
+
User.create(email: 'admin@test.com', password: '123456', password_confirmation: '123456')
|
706
721
|
user = User.accept_invitation!
|
707
722
|
refute_empty user.errors
|
708
723
|
end
|
@@ -712,9 +727,9 @@ class InvitableTest < ActiveSupport::TestCase
|
|
712
727
|
assert_equal 0, User.invitation_accepted.count
|
713
728
|
assert_equal 0, User.created_by_invite.count
|
714
729
|
|
715
|
-
User.invite!(:
|
716
|
-
User.invite!(:
|
717
|
-
user = User.invite!(:
|
730
|
+
User.invite!(email: 'invalid@email.com')
|
731
|
+
User.invite!(email: 'another_invalid@email.com')
|
732
|
+
user = User.invite!(email: 'valid@email.com')
|
718
733
|
|
719
734
|
assert_equal 3, User.invitation_not_accepted.count
|
720
735
|
assert_equal 0, User.invitation_accepted.count
|
@@ -739,9 +754,9 @@ class InvitableTest < ActiveSupport::TestCase
|
|
739
754
|
(?=.*[A-Z]) # Must contain an upper case character
|
740
755
|
(?=.*[[:^alnum:]]) # Must contain a symbol
|
741
756
|
/x
|
742
|
-
User.stubs(:invite_key).returns(:
|
757
|
+
User.stubs(:invite_key).returns(password: PASSWORD_FORMAT)
|
743
758
|
Devise.stubs(:friendly_token).returns('onlylowercaseletters')
|
744
|
-
user = User.invite!(:
|
759
|
+
user = User.invite!(email: 'valid@email.com')
|
745
760
|
assert user.persisted?
|
746
761
|
assert user.errors.empty?
|
747
762
|
end
|