Permalink
Browse files

Added tests from week 6

  • Loading branch information...
1 parent 86e219a commit c05eeb6597a8b7b804ff3b35eaeceafe4198cef8 @trevmex committed Feb 22, 2011
View
@@ -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
@@ -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
@@ -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))
View
@@ -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.