devise_invitable 1.1.8 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +5 -0
- data/README.rdoc +39 -12
- data/app/controllers/devise/invitations_controller.rb +20 -6
- data/app/controllers/devise_invitable/registrations_controller.rb +2 -2
- data/app/views/devise/mailer/invitation_instructions.html.erb +1 -1
- data/lib/devise_invitable/mailer.rb +3 -7
- data/lib/devise_invitable/model.rb +53 -59
- data/lib/devise_invitable/parameter_sanitizer.rb +11 -0
- data/lib/devise_invitable/rails.rb +1 -0
- data/lib/devise_invitable/version.rb +1 -1
- data/lib/devise_invitable.rb +1 -0
- data/lib/generators/active_record/templates/migration.rb +3 -2
- data/test/functional/controller_helpers_test.rb +39 -0
- data/test/functional/registrations_controller_test.rb +59 -0
- data/test/generators/views_generator_test.rb +40 -0
- data/test/generators_test.rb +34 -0
- data/test/integration/invitation_remove_test.rb +29 -0
- data/test/integration/invitation_test.rb +222 -0
- data/test/integration_tests_helper.rb +48 -0
- data/test/mailers/invitation_mail_test.rb +69 -0
- data/test/model_tests_helper.rb +33 -0
- data/test/models/invitable_test.rb +558 -0
- data/test/models_test.rb +74 -0
- data/test/orm/active_record.rb +4 -0
- data/test/orm/mongoid.rb +20 -0
- data/test/rails_app/Rakefile +7 -0
- data/test/rails_app/app/controllers/admins_controller.rb +6 -0
- data/test/rails_app/app/controllers/application_controller.rb +10 -0
- data/test/rails_app/app/controllers/free_invitations_controller.rb +6 -0
- data/test/rails_app/app/controllers/home_controller.rb +4 -0
- data/test/rails_app/app/controllers/users_controller.rb +12 -0
- data/test/rails_app/app/helpers/application_helper.rb +2 -0
- data/test/rails_app/app/models/admin.rb +23 -0
- data/test/rails_app/app/models/octopussy.rb +15 -0
- data/test/rails_app/app/models/user.rb +56 -0
- data/test/rails_app/app/views/admins/new.html.erb +12 -0
- data/test/rails_app/app/views/free_invitations/new.html.erb +12 -0
- data/test/rails_app/app/views/home/index.html.erb +0 -0
- data/test/rails_app/app/views/layouts/application.html.erb +16 -0
- data/test/rails_app/app/views/users/invitations/new.html.erb +15 -0
- data/test/rails_app/config/application.rb +24 -0
- data/test/rails_app/config/boot.rb +11 -0
- data/test/rails_app/config/database.yml +22 -0
- data/test/rails_app/config/environment.rb +5 -0
- data/test/rails_app/config/environments/development.rb +25 -0
- data/test/rails_app/config/environments/production.rb +49 -0
- data/test/rails_app/config/environments/test.rb +33 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/test/rails_app/config/initializers/devise.rb +213 -0
- data/test/rails_app/config/initializers/inflections.rb +10 -0
- data/test/rails_app/config/initializers/mime_types.rb +5 -0
- data/test/rails_app/config/initializers/secret_token.rb +7 -0
- data/test/rails_app/config/initializers/session_store.rb +8 -0
- data/test/rails_app/config/initializers/wrap_parameters.rb +14 -0
- data/test/rails_app/config/locales/devise.en.yml +57 -0
- data/test/rails_app/config/locales/en.yml +14 -0
- data/test/rails_app/config/routes.rb +9 -0
- data/test/rails_app/config.ru +4 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +39 -0
- data/test/rails_app/mongoid.yml +10 -0
- data/test/rails_app/script/rails +6 -0
- data/test/routes_test.rb +20 -0
- data/test/test_helper.rb +24 -0
- metadata +135 -47
- data/app/controllers/devise_invitable/registrations_controller.rb~ +0 -15
- data/lib/devise_invitable/controllers/helpers.rb~ +0 -21
- data/lib/devise_invitable/controllers/registrations.rb~ +0 -21
- data/lib/devise_invitable/model.rb~ +0 -224
- data/lib/devise_invitable/rails.rb~ +0 -21
- data/lib/devise_invitable.rb~ +0 -65
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
require 'model_tests_helper'
|
|
3
|
+
|
|
4
|
+
class InvitableTest < ActiveSupport::TestCase
|
|
5
|
+
|
|
6
|
+
def setup
|
|
7
|
+
setup_mailer
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
test 'should not generate invitation token after creating a record' do
|
|
11
|
+
assert_nil new_user.invitation_token
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
test 'should regenerate invitation token each time' do
|
|
15
|
+
user = new_user
|
|
16
|
+
user.invite!
|
|
17
|
+
token = user.invitation_token
|
|
18
|
+
assert_not_nil user.invitation_token
|
|
19
|
+
assert_not_nil user.invitation_created_at
|
|
20
|
+
3.times do
|
|
21
|
+
user = User.find(user.id)
|
|
22
|
+
user.invite!
|
|
23
|
+
assert_not_equal token, user.invitation_token
|
|
24
|
+
token = user.invitation_token
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
test 'should set invitation created and sent at each time' do
|
|
29
|
+
user = new_user
|
|
30
|
+
user.invite!
|
|
31
|
+
old_invitation_created_at = 3.days.ago
|
|
32
|
+
old_invitation_sent_at = 3.days.ago
|
|
33
|
+
user.update_attributes(:invitation_sent_at => old_invitation_sent_at, :invitation_created_at => old_invitation_created_at)
|
|
34
|
+
3.times do
|
|
35
|
+
user.invite!
|
|
36
|
+
assert_not_equal old_invitation_sent_at, user.invitation_sent_at
|
|
37
|
+
assert_not_equal old_invitation_created_at, user.invitation_created_at
|
|
38
|
+
user.update_attributes(:invitation_sent_at => old_invitation_sent_at, :invitation_created_at => old_invitation_created_at)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
test 'should test invitation sent at with invite_for configuration value' do
|
|
43
|
+
user = User.invite!(:email => "valid@email.com")
|
|
44
|
+
|
|
45
|
+
User.stubs(:invite_for).returns(nil)
|
|
46
|
+
user.invitation_created_at = Time.now.utc
|
|
47
|
+
assert user.valid_invitation?
|
|
48
|
+
|
|
49
|
+
User.stubs(:invite_for).returns(nil)
|
|
50
|
+
user.invitation_created_at = 1.year.ago
|
|
51
|
+
assert user.valid_invitation?
|
|
52
|
+
|
|
53
|
+
User.stubs(:invite_for).returns(0)
|
|
54
|
+
user.invitation_created_at = Time.now.utc
|
|
55
|
+
assert user.valid_invitation?
|
|
56
|
+
|
|
57
|
+
User.stubs(:invite_for).returns(0)
|
|
58
|
+
user.invitation_created_at = 1.day.ago
|
|
59
|
+
assert user.valid_invitation?
|
|
60
|
+
|
|
61
|
+
User.stubs(:invite_for).returns(1.day)
|
|
62
|
+
user.invitation_created_at = Time.now.utc
|
|
63
|
+
assert user.valid_invitation?
|
|
64
|
+
|
|
65
|
+
User.stubs(:invite_for).returns(1.day)
|
|
66
|
+
user.invitation_created_at = 2.days.ago
|
|
67
|
+
assert !user.valid_invitation?
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
test 'should never generate the same invitation token for different users' do
|
|
71
|
+
invitation_tokens = []
|
|
72
|
+
3.times do
|
|
73
|
+
user = new_user
|
|
74
|
+
user.invite!
|
|
75
|
+
token = user.invitation_token
|
|
76
|
+
assert !invitation_tokens.include?(token)
|
|
77
|
+
invitation_tokens << token
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
test 'should invite with mutiple columns for invite key' do
|
|
82
|
+
User.stubs(:invite_key).returns(:email => Devise.email_regexp, :username => /\A.+\z/)
|
|
83
|
+
user = User.invite!(:email => "valid@email.com", :username => "name")
|
|
84
|
+
assert user.persisted?
|
|
85
|
+
assert user.errors.empty?
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
test 'should not invite with some missing columns when invite key is an array' do
|
|
89
|
+
User.stubs(:invite_key).returns(:email => Devise.email_regexp, :username => /\A.+\z/)
|
|
90
|
+
user = User.invite!(:email => "valid@email.com")
|
|
91
|
+
assert user.new_record?
|
|
92
|
+
assert user.errors.present?
|
|
93
|
+
assert user.errors[:username]
|
|
94
|
+
assert user.errors[:email].empty?
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
test 'should return mail object' do
|
|
98
|
+
mail = User.invite_mail!(:email => 'valid@email.com')
|
|
99
|
+
assert mail.class.name == 'Mail::Message'
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
test 'should disallow login when invited' do
|
|
103
|
+
invited_user = User.invite!(:email => "valid@email.com")
|
|
104
|
+
assert !invited_user.valid_password?('1234')
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
test 'should set password and password confirmation from params' do
|
|
108
|
+
invited_user = User.invite!(:email => "valid@email.com")
|
|
109
|
+
user = User.accept_invitation!(:invitation_token => Thread.current[:token], :password => '123456789', :password_confirmation => '123456789')
|
|
110
|
+
assert user.valid_password?('123456789')
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
test 'should set password and save the record' do
|
|
114
|
+
user = User.invite!(:email => "valid@email.com")
|
|
115
|
+
old_encrypted_password = user.encrypted_password
|
|
116
|
+
user = User.accept_invitation!(:invitation_token => Thread.current[:token], :password => '123456789', :password_confirmation => '123456789')
|
|
117
|
+
assert_not_equal old_encrypted_password, user.encrypted_password
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
test 'should clear invitation token and set invitation_accepted_at while accepting the password' do
|
|
121
|
+
user = User.invite!(:email => "valid@email.com")
|
|
122
|
+
assert_present user.invitation_token
|
|
123
|
+
assert_nil user.invitation_accepted_at
|
|
124
|
+
user.accept_invitation!
|
|
125
|
+
user.reload
|
|
126
|
+
assert_nil user.invitation_token
|
|
127
|
+
assert_present user.invitation_accepted_at
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
test 'should not clear invitation token or set accepted_at if record is invalid' do
|
|
131
|
+
user = User.invite!(:email => "valid@email.com")
|
|
132
|
+
assert_present user.invitation_token
|
|
133
|
+
assert_nil user.invitation_accepted_at
|
|
134
|
+
User.accept_invitation!(:invitation_token => user.invitation_token, :password => '123456789', :password_confirmation => '987654321')
|
|
135
|
+
user.reload
|
|
136
|
+
assert_present user.invitation_token
|
|
137
|
+
assert_nil user.invitation_accepted_at
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
test 'should clear invitation token while resetting the password' do
|
|
141
|
+
user = User.invite!(:email => "valid@email.com")
|
|
142
|
+
assert user.invited_to_sign_up?
|
|
143
|
+
token, user.reset_password_token = Devise.token_generator.generate(User, :reset_password_token)
|
|
144
|
+
user.reset_password_sent_at = Time.now.utc
|
|
145
|
+
user.save
|
|
146
|
+
|
|
147
|
+
assert_present user.reset_password_token
|
|
148
|
+
assert_present user.invitation_token
|
|
149
|
+
User.reset_password_by_token(:reset_password_token => token, :password => '123456789', :password_confirmation => '123456789')
|
|
150
|
+
assert_nil user.reload.reset_password_token
|
|
151
|
+
assert_nil user.reload.invitation_token
|
|
152
|
+
assert !user.invited_to_sign_up?
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
test 'should not accept invitation on failing to reset the password' do
|
|
156
|
+
user = User.invite!(:email => "valid@email.com")
|
|
157
|
+
assert user.invited_to_sign_up?
|
|
158
|
+
token, user.reset_password_token = Devise.token_generator.generate(User, :reset_password_token)
|
|
159
|
+
user.reset_password_sent_at = Time.now.utc
|
|
160
|
+
user.save
|
|
161
|
+
|
|
162
|
+
assert_present user.reset_password_token
|
|
163
|
+
assert_present user.invitation_token
|
|
164
|
+
User.reset_password_by_token(:reset_password_token => token, :password => '123456789', :password_confirmation => '12345678')
|
|
165
|
+
assert_present user.reload.reset_password_token
|
|
166
|
+
assert_present user.reload.invitation_token
|
|
167
|
+
assert user.invited_to_sign_up?
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
test 'should not set invitation_accepted_at if just resetting password' do
|
|
171
|
+
user = User.create!(:email => "valid@email.com", :password => "123456780")
|
|
172
|
+
assert !user.invited_to_sign_up?
|
|
173
|
+
token, user.reset_password_token = Devise.token_generator.generate(User, :reset_password_token)
|
|
174
|
+
user.reset_password_sent_at = Time.now.utc
|
|
175
|
+
user.save
|
|
176
|
+
|
|
177
|
+
assert_present user.reset_password_token
|
|
178
|
+
assert_nil user.invitation_token
|
|
179
|
+
User.reset_password_by_token(:reset_password_token => token, :password => '123456789', :password_confirmation => '123456789')
|
|
180
|
+
assert_nil user.reload.invitation_token
|
|
181
|
+
assert_nil user.reload.invitation_accepted_at
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
test 'should reset invitation token and send invitation by email' do
|
|
185
|
+
user = new_user
|
|
186
|
+
assert_difference('ActionMailer::Base.deliveries.size') do
|
|
187
|
+
token = user.invitation_token
|
|
188
|
+
user.invite!
|
|
189
|
+
assert_not_equal token, user.invitation_token
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
test 'should return a record with invitation token and no errors to send invitation by email' do
|
|
194
|
+
invited_user = User.invite!(:email => "valid@email.com")
|
|
195
|
+
assert invited_user.errors.blank?
|
|
196
|
+
assert_present invited_user.invitation_token
|
|
197
|
+
assert_equal 'valid@email.com', invited_user.email
|
|
198
|
+
assert invited_user.persisted?
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
test 'should set all attributes with no errors' do
|
|
202
|
+
invited_user = User.invite!(:email => "valid@email.com", :username => 'first name')
|
|
203
|
+
assert invited_user.errors.blank?
|
|
204
|
+
assert_equal 'first name', invited_user.username
|
|
205
|
+
assert invited_user.persisted?
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
test 'should not validate other attributes when validate_on_invite is disabled' do
|
|
209
|
+
validate_on_invite = User.validate_on_invite
|
|
210
|
+
User.validate_on_invite = false
|
|
211
|
+
invited_user = User.invite!(:email => "valid@email.com", :username => "a"*50)
|
|
212
|
+
assert invited_user.errors.empty?
|
|
213
|
+
User.validate_on_invite = validate_on_invite
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
test 'should validate other attributes when validate_on_invite is enabled' do
|
|
217
|
+
validate_on_invite = User.validate_on_invite
|
|
218
|
+
User.validate_on_invite = true
|
|
219
|
+
invited_user = User.invite!(:email => "valid@email.com", :username => "a"*50)
|
|
220
|
+
assert invited_user.errors[:username].present?
|
|
221
|
+
User.validate_on_invite = validate_on_invite
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
test 'should not validate password when validate_on_invite is enabled' do
|
|
225
|
+
validate_on_invite = User.validate_on_invite
|
|
226
|
+
User.validate_on_invite = true
|
|
227
|
+
invited_user = User.invite!(:email => "valid@email.com", :username => "a"*50)
|
|
228
|
+
assert invited_user.errors.present?
|
|
229
|
+
assert invited_user.errors[:password].empty?
|
|
230
|
+
User.validate_on_invite = validate_on_invite
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
test 'should validate other attributes when validate_on_invite is enabled and email is not present' do
|
|
234
|
+
validate_on_invite = User.validate_on_invite
|
|
235
|
+
User.validate_on_invite = true
|
|
236
|
+
invited_user = User.invite!(:email => "", :username => "a"*50)
|
|
237
|
+
assert invited_user.errors[:email].present?
|
|
238
|
+
assert invited_user.errors[:username].present?
|
|
239
|
+
User.validate_on_invite = validate_on_invite
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
test 'should return a record with errors if user was found by e-mail' do
|
|
243
|
+
existing_user = User.new(:email => "valid@email.com")
|
|
244
|
+
existing_user.save(:validate => false)
|
|
245
|
+
user = User.invite!(:email => "valid@email.com")
|
|
246
|
+
assert_equal user, existing_user
|
|
247
|
+
assert_equal ['has already been taken'], user.errors[:email]
|
|
248
|
+
same_user = User.invite!("email" => "valid@email.com")
|
|
249
|
+
assert_equal same_user, existing_user
|
|
250
|
+
assert_equal ['has already been taken'], same_user.errors[:email]
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
test 'should return a record with errors if user with pending invitation was found by e-mail' do
|
|
254
|
+
existing_user = User.invite!(:email => "valid@email.com")
|
|
255
|
+
user = User.invite!(:email => "valid@email.com")
|
|
256
|
+
assert_equal user, existing_user
|
|
257
|
+
assert_equal [], user.errors[:email]
|
|
258
|
+
resend_invitation = User.resend_invitation
|
|
259
|
+
begin
|
|
260
|
+
User.resend_invitation = false
|
|
261
|
+
|
|
262
|
+
user = User.invite!(:email => "valid@email.com")
|
|
263
|
+
assert_equal user, existing_user
|
|
264
|
+
assert_equal ['has already been taken'], user.errors[:email]
|
|
265
|
+
ensure
|
|
266
|
+
User.resend_invitation = resend_invitation
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
test 'should return a record with errors if user was found by e-mail with validate_on_invite' do
|
|
271
|
+
begin
|
|
272
|
+
validate_on_invite = User.validate_on_invite
|
|
273
|
+
User.validate_on_invite = true
|
|
274
|
+
existing_user = User.new(:email => "valid@email.com")
|
|
275
|
+
existing_user.save(:validate => false)
|
|
276
|
+
user = User.invite!(:email => "valid@email.com", :username => "a"*50)
|
|
277
|
+
assert_equal user, existing_user
|
|
278
|
+
assert_equal ['has already been taken'], user.errors[:email]
|
|
279
|
+
assert user.errors[:username].present?
|
|
280
|
+
ensure
|
|
281
|
+
User.validate_on_invite = validate_on_invite
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
test 'should return a new record with errors if e-mail is blank' do
|
|
286
|
+
invited_user = User.invite!(:email => '')
|
|
287
|
+
assert invited_user.new_record?
|
|
288
|
+
assert_equal ["can't be blank"], invited_user.errors[:email]
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
test 'should return a new record with errors if e-mail is invalid' do
|
|
292
|
+
invited_user = User.invite!(:email => 'invalid_email')
|
|
293
|
+
assert invited_user.new_record?
|
|
294
|
+
assert_equal ["is invalid"], invited_user.errors[:email]
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
test 'should set all attributes with errors if e-mail is invalid' do
|
|
298
|
+
invited_user = User.invite!(:email => "invalid_email.com", :username => 'first name')
|
|
299
|
+
assert invited_user.new_record?
|
|
300
|
+
assert_equal 'first name', invited_user.username
|
|
301
|
+
assert invited_user.errors.present?
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
test 'should find a user to set his password based on invitation_token' do
|
|
305
|
+
user = new_user
|
|
306
|
+
user.invite!
|
|
307
|
+
invited_user = User.accept_invitation!(:invitation_token => Thread.current[:token])
|
|
308
|
+
assert_equal invited_user, user
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
test 'should return a new record with errors if no invitation_token is found' do
|
|
312
|
+
invited_user = User.accept_invitation!(:invitation_token => 'invalid_token')
|
|
313
|
+
assert invited_user.new_record?
|
|
314
|
+
assert_equal ['is invalid'], invited_user.errors[:invitation_token]
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
test 'should return a new record with errors if invitation_token is blank' do
|
|
318
|
+
invited_user = User.accept_invitation!(:invitation_token => '')
|
|
319
|
+
assert invited_user.new_record?
|
|
320
|
+
assert_equal ["can't be blank"], invited_user.errors[:invitation_token]
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
test 'should return record with errors if invitation_token has expired' do
|
|
324
|
+
User.stubs(:invite_for).returns(10.hours)
|
|
325
|
+
invited_user = User.invite!(:email => "valid@email.com")
|
|
326
|
+
invited_user.invitation_created_at = 2.days.ago
|
|
327
|
+
invited_user.save(:validate => false)
|
|
328
|
+
user = User.accept_invitation!(:invitation_token => Thread.current[:token])
|
|
329
|
+
assert_equal user, invited_user
|
|
330
|
+
assert_equal ["is invalid"], user.errors[:invitation_token]
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
test 'should allow record modification using block' do
|
|
334
|
+
invited_user = User.invite!(:email => "valid@email.com", :username => "a"*50) do |u|
|
|
335
|
+
u.password = '123123'
|
|
336
|
+
u.password_confirmation = '123123'
|
|
337
|
+
end
|
|
338
|
+
assert_equal '123123', invited_user.reload.password
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
test 'should set successfully user password given the new password and confirmation' do
|
|
342
|
+
user = new_user(:password => nil, :password_confirmation => nil)
|
|
343
|
+
user.invite!
|
|
344
|
+
|
|
345
|
+
invited_user = User.accept_invitation!(
|
|
346
|
+
:invitation_token => Thread.current[:token],
|
|
347
|
+
:password => 'new_password',
|
|
348
|
+
:password_confirmation => 'new_password'
|
|
349
|
+
)
|
|
350
|
+
user.reload
|
|
351
|
+
|
|
352
|
+
assert user.valid_password?('new_password')
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
test 'should return errors on other attributes even when password is valid' do
|
|
356
|
+
user = new_user(:password => nil, :password_confirmation => nil)
|
|
357
|
+
user.invite!
|
|
358
|
+
|
|
359
|
+
invited_user = User.accept_invitation!(
|
|
360
|
+
:invitation_token => Thread.current[:token],
|
|
361
|
+
:password => 'new_password',
|
|
362
|
+
:password_confirmation => 'new_password',
|
|
363
|
+
:username => 'a'*50
|
|
364
|
+
)
|
|
365
|
+
assert invited_user.errors[:username].present?
|
|
366
|
+
|
|
367
|
+
user.reload
|
|
368
|
+
assert !user.valid_password?('new_password')
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
test 'should set other attributes on accepting invitation' do
|
|
372
|
+
user = new_user(:password => nil, :password_confirmation => nil)
|
|
373
|
+
user.invite!
|
|
374
|
+
|
|
375
|
+
invited_user = User.accept_invitation!(
|
|
376
|
+
:invitation_token => Thread.current[:token],
|
|
377
|
+
:password => 'new_password',
|
|
378
|
+
:password_confirmation => 'new_password',
|
|
379
|
+
:username => 'a'
|
|
380
|
+
)
|
|
381
|
+
assert invited_user.errors[:username].blank?
|
|
382
|
+
|
|
383
|
+
user.reload
|
|
384
|
+
assert_equal 'a', user.username
|
|
385
|
+
assert user.valid_password?('new_password')
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
test 'should not confirm user on invite' do
|
|
389
|
+
user = new_user
|
|
390
|
+
|
|
391
|
+
user.invite!
|
|
392
|
+
|
|
393
|
+
assert !user.confirmed?
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
test 'user.has_invitations_left? test' do
|
|
397
|
+
# By default with invitation_limit nil, users can send unlimited invitations
|
|
398
|
+
user = new_user
|
|
399
|
+
assert_nil user.invitation_limit
|
|
400
|
+
assert user.has_invitations_left?
|
|
401
|
+
|
|
402
|
+
# With invitation_limit set to a value, all users can send that many invitations
|
|
403
|
+
User.stubs(:invitation_limit).returns(2)
|
|
404
|
+
assert user.has_invitations_left?
|
|
405
|
+
|
|
406
|
+
# With an individual invitation_limit of 0, a user shouldn't be able to send an invitation
|
|
407
|
+
user.invitation_limit = 0
|
|
408
|
+
assert user.save
|
|
409
|
+
assert !user.has_invitations_left?
|
|
410
|
+
|
|
411
|
+
# With in invitation_limit of 2, a user should be able to send two invitations
|
|
412
|
+
user.invitation_limit = 2
|
|
413
|
+
assert user.save
|
|
414
|
+
assert user.has_invitations_left?
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
test 'should not send an invitation if we want to skip the invitation' do
|
|
418
|
+
assert_no_difference('ActionMailer::Base.deliveries.size') do
|
|
419
|
+
invited_user = User.invite!(:email => "valid@email.com", :username => "a"*50, :skip_invitation => true)
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
test 'should not send an invitation if we want to skip the invitation with block' do
|
|
424
|
+
assert_no_difference('ActionMailer::Base.deliveries.size') do
|
|
425
|
+
invited_user = User.invite!(:email => "valid@email.com", :username => "a"*50) do |u|
|
|
426
|
+
u.skip_invitation = true
|
|
427
|
+
end
|
|
428
|
+
end
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
test 'user.invite! should not send an invitation if we want to skip the invitation' do
|
|
432
|
+
user = new_user
|
|
433
|
+
user.skip_invitation = true
|
|
434
|
+
assert_no_difference('ActionMailer::Base.deliveries.size') do
|
|
435
|
+
user.invite!
|
|
436
|
+
end
|
|
437
|
+
assert_present user.invitation_created_at
|
|
438
|
+
assert_nil user.invitation_sent_at
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
test 'user.invite! should not set the invited_by attribute if not passed' do
|
|
442
|
+
user = new_user
|
|
443
|
+
user.invite!
|
|
444
|
+
assert_equal nil, user.invited_by
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
test 'user.invite! should set the invited_by attribute if passed' do
|
|
448
|
+
user = new_user
|
|
449
|
+
inviting_user = User.new(:email => "valid@email.com")
|
|
450
|
+
inviting_user.save(:validate => false)
|
|
451
|
+
user.invite!(inviting_user)
|
|
452
|
+
assert_equal inviting_user, user.invited_by
|
|
453
|
+
assert_equal inviting_user.class.to_s, user.invited_by_type
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
test 'user.accept_invitation! should trigger callbacks' do
|
|
457
|
+
user = User.invite!(:email => "valid@email.com")
|
|
458
|
+
assert_callbacks_not_fired user
|
|
459
|
+
user.accept_invitation!
|
|
460
|
+
assert_callbacks_fired user
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
test 'user.accept_invitation! should not trigger callbacks if validation fails' do
|
|
464
|
+
user = User.invite!(:email => "valid@email.com")
|
|
465
|
+
assert_callbacks_not_fired user
|
|
466
|
+
user.username='a'*50
|
|
467
|
+
user.accept_invitation!
|
|
468
|
+
assert_callbacks_not_fired user
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
test 'user.accept_invitation! should confirm user if confirmable' do
|
|
472
|
+
user = User.invite!(:email => "valid@email.com")
|
|
473
|
+
user.accept_invitation!
|
|
474
|
+
|
|
475
|
+
assert user.confirmed?
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
test 'user.accept_invitation! should not confirm user if validation fails' do
|
|
479
|
+
user = User.invite!(:email => "valid@email.com")
|
|
480
|
+
user.username='a'*50
|
|
481
|
+
user.accept_invitation!
|
|
482
|
+
|
|
483
|
+
assert !user.confirmed?
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
def assert_callbacks_fired(user)
|
|
487
|
+
assert_callbacks_status user, true
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
def assert_callbacks_not_fired(user)
|
|
491
|
+
assert_callbacks_status user, nil
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
def assert_callbacks_status(user, fired)
|
|
495
|
+
assert_equal fired, user.callback_works
|
|
496
|
+
end
|
|
497
|
+
|
|
498
|
+
test "user.invite! should downcase the class's case_insensitive_keys" do
|
|
499
|
+
# Devise default is :email
|
|
500
|
+
user = User.invite!(:email => "UPPERCASE@email.com")
|
|
501
|
+
assert user.email == "uppercase@email.com"
|
|
502
|
+
end
|
|
503
|
+
|
|
504
|
+
test "user.invite! should strip whitespace from the class's strip_whitespace_keys" do
|
|
505
|
+
# Devise default is email
|
|
506
|
+
user = User.invite!(:email => " valid@email.com ")
|
|
507
|
+
assert user.email == "valid@email.com"
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
test 'should pass validation before accept if field is required in post-invited instance' do
|
|
511
|
+
user = User.invite!(:email => "valid@email.com")
|
|
512
|
+
user.testing_accepting_or_not_invited = true
|
|
513
|
+
assert_equal true, user.valid?
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
test 'should fail validation after accept if field is required in post-invited instance' do
|
|
517
|
+
user = User.invite!(:email => "valid@email.com")
|
|
518
|
+
user.testing_accepting_or_not_invited = true
|
|
519
|
+
user.accept_invitation!
|
|
520
|
+
assert_equal false, user.valid?
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
test 'should pass validation after accept if field is required in post-invited instance' do
|
|
524
|
+
user = User.invite!(:email => "valid@email.com")
|
|
525
|
+
user.username = 'test'
|
|
526
|
+
user.testing_accepting_or_not_invited = true
|
|
527
|
+
user.bio = "Test"
|
|
528
|
+
user.accept_invitation!
|
|
529
|
+
assert_equal true, user.valid?
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
test 'should return instance with errors if invitation_token is nil' do
|
|
533
|
+
registered_user = User.create(:email => 'admin@test.com', :password => '123456', :password_confirmation => '123456')
|
|
534
|
+
user = User.accept_invitation!
|
|
535
|
+
assert !user.errors.empty?
|
|
536
|
+
end
|
|
537
|
+
|
|
538
|
+
test "should count accepted and not accepted invitations" do
|
|
539
|
+
assert_equal 0, User.invitation_not_accepted.count
|
|
540
|
+
assert_equal 0, User.invitation_accepted.count
|
|
541
|
+
|
|
542
|
+
User.invite!(:email => "invalid@email.com")
|
|
543
|
+
user = User.invite!(:email => "valid@email.com")
|
|
544
|
+
|
|
545
|
+
assert_equal 2, User.invitation_not_accepted.count
|
|
546
|
+
assert_equal 0, User.invitation_accepted.count
|
|
547
|
+
|
|
548
|
+
user.accept_invitation!
|
|
549
|
+
assert_equal 1, User.invitation_not_accepted.count
|
|
550
|
+
assert_equal 1, User.invitation_accepted.count
|
|
551
|
+
end
|
|
552
|
+
|
|
553
|
+
test "should preserve return values of Devise::Recoverable#reset_password!" do
|
|
554
|
+
user = new_user
|
|
555
|
+
retval = user.reset_password!('anewpassword', 'anewpassword')
|
|
556
|
+
assert_equal true, retval
|
|
557
|
+
end
|
|
558
|
+
end
|
data/test/models_test.rb
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class Invitable < User
|
|
4
|
+
devise :invitable, :invite_for => 5.days, :validate_on_invite => true
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
class ModelsTest < ActiveSupport::TestCase
|
|
8
|
+
def include_module?(klass, mod)
|
|
9
|
+
klass.devise_modules.include?(mod) &&
|
|
10
|
+
klass.included_modules.include?(Devise::Models::const_get(mod.to_s.classify))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def assert_include_modules(klass, *modules)
|
|
14
|
+
modules.each do |mod|
|
|
15
|
+
assert include_module?(klass, mod), "#{klass} not include #{mod}"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
(Devise::ALL - modules).each do |mod|
|
|
19
|
+
assert !include_module?(klass, mod), "#{klass} include #{mod}"
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test 'should include Devise modules' do
|
|
24
|
+
assert_include_modules User, :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test 'should have a default value for invite_for' do
|
|
28
|
+
assert_equal 0, User.invite_for
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
test 'should have a default value for invitation_limit' do
|
|
32
|
+
assert_nil User.invitation_limit
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
test 'should have a default value for invite_key' do
|
|
36
|
+
assert !User.invite_key.nil?
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
test 'set a custom value for invite_for' do
|
|
40
|
+
old_invite_for = User.invite_for
|
|
41
|
+
User.invite_for = 5.days
|
|
42
|
+
|
|
43
|
+
assert_equal 5.days, User.invite_for
|
|
44
|
+
|
|
45
|
+
User.invite_for = old_invite_for
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
test 'set a custom value for invite_key' do
|
|
49
|
+
old_invite_key = User.invite_key
|
|
50
|
+
User.invite_key = {:username => /\A.+\z/}
|
|
51
|
+
|
|
52
|
+
assert_equal({:username => /\A.+\z/}, User.invite_key)
|
|
53
|
+
|
|
54
|
+
User.invite_key = old_invite_key
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
test 'set a custom value for invitation_limit' do
|
|
58
|
+
old_invitation_limit = User.invitation_limit
|
|
59
|
+
User.invitation_limit = 2
|
|
60
|
+
|
|
61
|
+
assert_equal 2, User.invitation_limit
|
|
62
|
+
|
|
63
|
+
User.invitation_limit = old_invitation_limit
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
test 'set a default value for validate_on_invite' do
|
|
67
|
+
assert_equal true, Invitable.validate_on_invite
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
test 'invitable attributes' do
|
|
71
|
+
assert_nil User.new.invitation_token
|
|
72
|
+
assert_nil User.new.invitation_sent_at
|
|
73
|
+
end
|
|
74
|
+
end
|
data/test/orm/mongoid.rb
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Mongoid.configure do |config|
|
|
2
|
+
if Mongoid::VERSION < '3.0.0'
|
|
3
|
+
config.master = Mongo::Connection.new('127.0.0.1', 27017).db("devise_invitable-test-suite")
|
|
4
|
+
else
|
|
5
|
+
config.connect_to("devise_invitable-test-suite")
|
|
6
|
+
config.use_utc = true
|
|
7
|
+
config.include_root_in_json = true
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class ActiveSupport::TestCase
|
|
12
|
+
setup do
|
|
13
|
+
if Mongoid::VERSION < '3.0.0'
|
|
14
|
+
User.delete_all
|
|
15
|
+
Admin.delete_all
|
|
16
|
+
else
|
|
17
|
+
Mongoid.purge!
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
|
2
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
|
3
|
+
|
|
4
|
+
require File.expand_path('../config/application', __FILE__)
|
|
5
|
+
require 'rake'
|
|
6
|
+
|
|
7
|
+
RailsApp::Application.load_tasks
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
class ApplicationController < ActionController::Base
|
|
2
|
+
protect_from_forgery
|
|
3
|
+
before_filter :configure_permitted_parameters, :if => :devise_controller?
|
|
4
|
+
|
|
5
|
+
protected
|
|
6
|
+
|
|
7
|
+
def configure_permitted_parameters
|
|
8
|
+
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :bio) } if defined?(ActionController::StrongParameters)
|
|
9
|
+
end
|
|
10
|
+
end
|