Skip to content

Commit

Permalink
clear invitation token when password is reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Cambra committed Feb 21, 2011
1 parent ecafe06 commit 94d859c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/devise_invitable/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def valid_password?(password)

protected

# Clear invitation token when reset password token is cleared too
def clear_reset_password_token
self.invitation_token = nil if invited?
super
end

# Checks if the invitation for the user is within the limit time.
# We do this by calculating if the difference between today and the
# invitation sent date does not exceed the invite for time configured.
Expand Down
9 changes: 9 additions & 0 deletions test/models/invitable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ def setup
assert_present user.invitation_token
end

test 'should clear invitation token while resetting the password' do
user = User.invite!(:email => "valid@email.com")
user.send(:generate_reset_password_token!)
assert_present user.reset_password_token
assert_present user.invitation_token
User.reset_password_by_token(:reset_password_token => user.reset_password_token, :password => '123456789', :password_confirmation => '123456789')
assert_nil user.reload.invitation_token
end

test 'should reset invitation token and send invitation by email' do
user = new_user
assert_difference('ActionMailer::Base.deliveries.size') do
Expand Down
2 changes: 1 addition & 1 deletion test/models_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def assert_include_modules(klass, *modules)
end

test 'should include Devise modules' do
assert_include_modules User, :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
assert_include_modules User, :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable
end

test 'should have a default value for invite_for' do
Expand Down
2 changes: 1 addition & 1 deletion test/rails_app/app/active_record/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable

attr_accessible :email, :username, :password, :password_confirmation

Expand Down
2 changes: 1 addition & 1 deletion test/rails_app/app/mongoid/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class User
field :created_at, :type => DateTime
field :username, :type => String

devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable
devise :database_authenticatable, :registerable, :validatable, :confirmable, :invitable, :recoverable

validates :username, :length => { :maximum => 20 }
end
2 changes: 1 addition & 1 deletion test/rails_app/config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

# If true, uses the password salt as remember token. This should be turned
# to false if you are not using database authenticatable.
config.use_salt_as_remember_token = true
config.use_salt_as_remember_token = false

# ==> Configuration for :validatable
# Range for password length. Default is 6..20.
Expand Down
2 changes: 1 addition & 1 deletion test/rails_app/db/migrate/20100401102949_create_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def self.up
t.string :username
t.confirmable
t.invitable
t.encryptable
t.recoverable

t.timestamps
end
Expand Down

0 comments on commit 94d859c

Please sign in to comment.