Posts Tagged ‘ testing ’

Should I Mock Kernel#exit

Monday, April 26th, 2010

I don’t know what the right answer is, so any insight would be appreciated. From Googling around, I came across this thread on StackOverflow which talks about Validating exits and aborts in Rspec. I have a few issues with this, namely that I am using shoulda and I don’t want to validate the exit, I want to stop it from happening inside of the test.
(more…)

Peertester Community Testing

Tuesday, March 16th, 2010

I am all about community and people participating in a community. Its the way social media succeeds. But there are non-social media communities out there too (yes I know that’s shocking). I was recently asked to participate in a beta testing for Peer Tester. I think this is an absolutely fantastic idea. Developers helping other developers test their apps. Hats off to the guys and gals over at Engine Yard for designing and putting this application together. Other than the fact that I got tired of looking at that olive green color after years in the military, this site is an great tool for developers.
(more…)

Testing Sessions with Digest::SHA256 Passwords In Rails

Friday, July 17th, 2009

I am fairly new to Rails, but from what I have learned thus far, its fantastic. I plan on posting more obscure Rails items as I come across them.

I am writing an application that is similar in functionality to Yelp (in some ways). I have had some help along the way from a seasoned Rails vet. One of the things he helped me with was setting up the sessions_controller. He did this in such a way that I wasn’t initially sure how to write the functional tests for it. The sessions_controller contained code like this (it’s a bit more in depth than this, but this will give you the idea):

1
2
3
4
5
6
7
8
def create
    @current_user = User.find_by_username( params[:username] )
    if @current_user.nil? or Digest::SHA256.hexdigest( params[:password] + @current_user.password ) != @current_user.password_salt
      redirect_to( login_path )
    else
      redirect_to( user_path )
  end
end

So when it came time to write the functional tests for this (using fixtures), I couldn’t get away with just a user.yml like this:

1
2
3
4
5
6
7
8
test_user:
    username: test_user
    password: secretuser
    firstname: General
    lastname: User
    email: test_user@user.com
    role: normal
    nickname: Mr. Testy

The reason for this is because the user table contains an extra column pertaining to the password called password_salt (which you’ll notice referenced above). Therefore my user fixture needed to look something more like this:

1
2
3
4
5
6
7
8
9
test_user:
    username: test_user
    password: A0Mrhnu4  # secretsalt
    password_salt: 868532ea243f03c2ce5f6f99dcf9e27342c39ce51819fa0605302ab4b5c3841e
    firstname: Test
    lastname: User
    email: test_user@example.com
    role: normal
    nickname: Mr. Testy

The other fixture still has it usefulness in testing (like testing logins that won’t work), so don’t get rid of it so quickly.

How do I generate that salt and the hashed password? I’m glad you asked. I’m sure there is a better way to do it than I did, but I just went into the application, created a user and then went into the console, grabbed the user information and copied it into the fixture. Then I used the following code in my sessions_controller_test.rb for the functional test:

1
2
3
4
5
  def test_should_logout_and_clear_session
    post :destroy, :username => 'test_salt', :password => 'secretsalt',
      :return_to => '/'
    assert_redirected_to '/'
  end