Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require File.dirname(__FILE__) + '/../test_helper'
- class PatientTest < ActiveSupport::TestCase
- self.use_instantiated_fixtures = true
- fixtures :patients
- def test_auth
- #check that we can login with a valid patient
- assert_equal @bob, Patient.authenticate("bob","test")
- #wrong username
- assert_nil Patient.authenticate("nonbob", "test")
- #wrong password
- assert_nil Patient.authenticate("bob", "wrongpass")
- #wrong login and pass
- assert_nil Patient.authenticate("nonbob", "wrongpass")
- end
- def test_passwordchange
- #check success
- assert_equal @longbob, Patient.authenticate("longbob", "longtest")
- #change password
- @longbob.password = @longbob.password_confirmation = "nonbobpasswd"
- assert @longbob.save
- #new password works
- assert_equal @longbob, Patient.authenticate("longbob", "nonbobpasswd")
- #old password doesn't work anymore
- assert_nil Patient.authenticate("longbob", "longtest")
- #change back again
- @longbob.password = @longbob.password_confirmation = "longtest"
- assert @longbob.save
- assert_equal @longbob, Patient.authenticate("longbob", "longtest")
- assert_nil Patient.authenticate("longbob", "nonbobpasswd")
- end
- def test_disallowed_passwords
- #check that we can't create a patient with any of the disallowed passwords
- p = Patient.new
- p.login = "nonbob"
- p.email = "nonbob@mcbob.com"
- #too short
- p.password = p.password_confirmation = "tiny"
- assert !p.save
- assert p.errors.invalid?('password')
- #too long
- p.password = p.password_confirmation = "hugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehuge"
- assert !p.save
- assert p.errors.invalid?('password')
- #empty
- p.password = p.password_confirmation = ""
- assert !p.save
- assert p.errors.invalid?('password')
- #ok
- p.password = p.password_confirmation = "bobs_secure_password"
- assert p.save
- assert p.errors.empty?
- end
- def test_bad_logins
- #check that we cant create a patient with an invalid usernaem
- p = Patient.new
- p.password = p.password_confirmation = "bobs_secure_password"
- p.email = "okbob@mcbob.com"
- #too short
- p.login = "x"
- assert !p.save
- assert p.errors.invalid?('login')
- #too long
- p.login = "hugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehugehuge"
- assert !p.save
- assert p.errors.invalid?('login')
- #empty
- p.login = ""
- assert !p.save
- assert p.errors.invalid?('login')
- #ok
- p.login = "okbob"
- assert !p.save
- assert p.errors.invalid?('login')
- #no email
- p.email = nil
- assert !p.save
- assert p.errors.invalid?('email')
- #invalid email
- p.email = 'notavalidemail'
- assert !p.save
- assert p.errors.invalid?('email')
- #ok
- p.email = 'validbob@mcbob.com'
- assert p.save
- assert p.errors.empty?
- end
- def test_collision
- #check that a patient with an existing username can't be created
- p = Patient.new
- p.login = "existingbob"
- p.password = p.password_confirmation = "bobs_secure_password"
- assert !p.save
- end
- def test_create
- #check create works and we can authenticate after creation
- p = Patient.new
- p.login = "nonexisitingbob"
- p.password = p.password_confirmation = "bobs_secure_password"
- p.email ="nonexistingbob@mcbob.com"
- assert_not_nil p.salt
- assert p.save
- assert_equal 10, p.salt.length
- assert_equal p, Patient.authenticate(p.login, p.password)
- p = Patient.new(:login => "newbob", :password => "newpassword", :password_confirmation => "newpassword", :email => "newbob@mcbob.com")
- assert_not_nil p.salt
- assert_not_nil p.password
- assert_not_nil p.hashed_password
- assert p.save
- assert_equal p, Patient.authenticate(p.login, p.password)
- end
- def test_send_new_password
- #check patient authenticates
- assert_equal @bob, Patient.authenticate("bob", "test")
- #send new password
- sent = @bob.send_new_password
- assert_not_nil sent
- #old password no longer works
- assert_nil Patient.authenticate("bob", "test")
- #email sent..
- assert_equal "Your password is ...", sent.subject
- #... to bob
- assert_equal @bob.email, sent.top[0]
- assert_match Regexp.new("Your usernrame is bob."), sent.body
- #can authenticate with the new password
- new_pass = $1 if Regexp.new("Your new password is (\\w+)") =~ sent.body
- assert_not_nil new_pass
- assert_equal @bob, Patient.authenticate("bob", new_pass)
- end
- def test_rand_str
- new_pass = Patient.random_string(10)
- assert_not_nil new_pass
- assert_equal 10, new_pass.length
- end
- def test_sha1
- p = Patient.new
- p.login = "nonexistingbob"
- p.email = "nonexistingbob@mcbob.com"
- p.salt = "1000"
- p.password = p.password_confirmation = "bobs_secure_password"
- assert p.save
- assert_equal 'b1d27036d59f9499d403f90e0bcf43281adaa844', p.hashed_password
- assert_equal 'b1d27036d59f9499d403f90e0bcf43281adaa844', Patient.encrypt("bobs_secure_password", "1000")
- end
- def test_protected_attributes
- #check that attributes are protected.
- p = Patient.new(:id=>999999, :salt => "I-want-to-set-my-salt", :login => "badbob", :password => "newpassword", :password_confirmation => "newpassword", :email => "badbob@mcbob.com")
- assert p.save
- assert_not_equal 999999, p.id
- assert_not_equal "I-want-to-set-my-salt". p.salt
- p.update_attributes(:id => 999999, :salt => "I-want-to-set-my-salt", :login => "verybadbob")
- assert p.save
- assert_not_equal 999999, p.id
- assert_not_equal "I-want-to-set-my-salt". p.salt
- assert_equal "verybadbob", p.login
- end
- end
Add Comment
Please, Sign In to add comment