Rails Unit Testing won't update database
I'm trying to run the following unit-test:
def test_passwordchange
# check success
assert_equal @longbob, Usuario.autenticar("longbob", "longtest")
#change password
@longbob.password = "nonbobpasswd"
@longbob.password_confirmation = "nonbobpasswd"
assert @longbob.save!
#new password works
assert_equal @longbob, Usuario.autenticar("longbob", "nonbobpasswd")
#old pasword doesn't work anymore
assert_nil Usuario.autenticar("longbob", "longtest")
#change back again
@longbob.password = "longtest"
@longbob.password_confirmation = "longtest"
assert @longbob.save!
assert_equal @longbob, Usuario.autenticar("longbob", "longtest")
assert_nil Usuario.autenticar("longbob", "nonbobpasswd")
end
However, it throws error on the 1st line that contains "assert_equal" that says:
<#<Usuario ID: 1000003, login: "longbob", hashed_password: "078cf6ae2de80ed6c004c8c8576a5572e077a52c", salt: "1000", nombre: nil, apellido: nil, email: "lbob@mcbob.com", telefono: nil, tipo_usuario: nil, foto: nil, bol_activo: nil>> expected but was <nil>.
Here's my authenticate method:
def self.authenticate (login, pass)
u=find(:first, :conditions=>["login = ?", login])
return nil if u.nil?
return u if Usuario.encrypt(pass, u.salt)==u.hashed_password
nil
end
Also, I defined the following:
def password=(pass)
@password=pass
self.salt = Usuario.random_string(10) if !self.salt?
self.hashed_password = Usuario.encrypt(@password, self.salt)
end
So, I guess that should update the hashed_password every time I reassigned something to "password"... right?
Whats happening?
Thx.
UPDATE: I noticed that if I change:
assert_equal @longbob, Usuario.autenticar("longbob", "nonbobpasswd")
to
assert_equal @longbob2, Usuario.autenticar("longbob", "nonbobpasswd")
It passes that test, however it fails in the following lin开发者_运维知识库e... Trowing the same error... What's up with that?
To answer after my comment, we find out that the problem is the call to save
does not update the record in the database. I have suggested you to write another test to see this behaviour and be able to fix it, the test you have writed start to be too long and in fact the bug does not have anything related to the authenticate mechanism.
Here is the test I would write :
def test_change_password_save
old_hash = @longbob.hashed_password
@longbob.password = "nonbobpasswd"
@longbob.password_confirmation = "nonbobpasswd"
@longbob.save
assert_not_equal(old_hash, @longbox.reload.hashed_password)
end
If this test failed, I would suggest you to write another question in stackoverflow for this bug.
You're probably getting a validation error. Check the contents of @longbob.errors
.
What happens if you split this into two separate statements? (Which is good practice anyway)
@longbob.password = @longbob.password_confirmation = "nonbobpasswd"
See, #password_confirmation=
is actually a method, which might not return the value that was passed to it, depending on how the method was implemented.
Is it possible that changing the password variables doesn't update the hashed_password
field in the database.
You probably need something like this in Usuario
:
before_save :rehash
def rehash
self.hashed_password = ??? #replace ??? with whatever your hashing logic is
end
精彩评论