Skip to content

Commit

Permalink
Added tests from week 6
Browse files Browse the repository at this point in the history
  • Loading branch information
trevmex committed Feb 22, 2011
1 parent 86e219a commit c05eeb6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
4 changes: 2 additions & 2 deletions test/fixtures/users.yml
@@ -1,9 +1,9 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
name: MyString
name: Tester One
description: MyText

two:
name: MyString
name: Tester Two
description: MyText
18 changes: 18 additions & 0 deletions test/functional/sessions_controller_test.rb
@@ -1,9 +1,27 @@
require 'test_helper'

class SessionsControllerTest < ActionController::TestCase
setup do
@user = User.create(:name => "Super Tester", :password => "12345", :password_confirmation => "12345")
end

test "should get new" do
get :new
assert_response :success
end

test "should log in a user" do
post :create, :name => "Super Tester", :password => "12345"
assert_equal(session[:user_id], @user.id)
assert_equal(flash[:notice], "Logged in!")
assert_redirected_to root_url
end

test "should log out a user" do
post :create, :name => "Super Tester", :password => "12345"
delete :destroy
assert_nil(session[:user_id])
assert_equal(flash[:notice], "Logged out!")
assert_redirected_to root_url
end
end
2 changes: 1 addition & 1 deletion test/functional/users_controller_test.rb
Expand Up @@ -18,7 +18,7 @@ class UsersControllerTest < ActionController::TestCase

test "should create user" do
assert_difference('User.count') do
post :create, :user => @user.attributes
post :create, :user => {"name" => "Trevor", "description" => "ME", "password" => "12345", "password_confirmation" => "12345"}
end

assert_redirected_to user_path(assigns(:user))
Expand Down
30 changes: 27 additions & 3 deletions test/unit/user_test.rb
@@ -1,8 +1,32 @@
require 'test_helper'
require 'bcrypt'

class UserTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
test "a password is encrypted" do
user = User.new(:password => "12345")

user.encrypt_password

assert_not_nil(user.password_salt)
assert_not_nil(user.password_hash)
assert_equal(user.password_hash, BCrypt::Engine.hash_secret(user.password, user.password_salt))
end

test "a user can authenticate" do
user = User.create(:name => "Test guy", :password => "54321", :password_confirmation => "54321")

assert_not_nil(user)
assert_equal(User.authenticate("Test guy", "54321"), user)
end

test "a non-existance user cannot authenticate" do
assert_nil(User.authenticate("Bobo deosn't exist", "12345"))
end

test "a user cannot authenticate with a bad password" do
user = User.create(:name => "Test guy 2", :password => "54321", :password_confirmation => "54321")

assert_not_nil(user)
assert_nil(User.authenticate("Test guy 2", "bad password"))
end
end

0 comments on commit c05eeb6

Please sign in to comment.