0
votes

I'm new to Ruby and have am trying to go through some tutorials to set up User Authentication.

I used a scaffold to create the user model/ controller/ views/ tests with the password_digest column already in place.

When I added the has_secure_password attribute it broke the controller create user and update user tests and I can't seem to figure out how to fix them.

Here is the testing code test/controllers/users_controllers_test.rb:

require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  setup do
    @user = users(:one)
  end

  test "should create user" do
    assert_difference('User.count') do
      post :create, user: { email: @user.email, password: @user.password, password_confirmation: @user.password_confirmation, user_name: @user.user_name }
    end

    assert_redirected_to user_path(assigns(:user))
  end

  test "should update user" do
    patch :update, id: @user, user: { email: @user.email, password: @user.password, password_confirmation: @user.password_confirmation, user_name: @user.user_name }
    assert_redirected_to user_path(assigns(:user))
  end
end

Here is the code for the controller:

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  def new
    @user = User.new
 end

  def edit
  end

  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
       format.json { render :show, status: :ok, location: @user }
     else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
   end
 end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

   # Never trust parameters from the scary internet, only allow the white list through.
   def user_params
     params.require(:user).permit(:user_name, :email, :password, :password_confirmation)
   end
end

And here is my users.yml file for the fixture:

one:
  user_name: MyString
  email: MyString
  password_digest: MyString

two:
  user_name: MyString
  email: MyString
  password_digest: MyString

The two failures I get are: FAIL["test_should_create_user", UsersControllerTest, 0.5491727360058576] test_should_create_user#UsersControllerTest (0.55s) "User.count" didn't change by 1. Expected: 3 Actual: 2 test/controllers/users_controller_test.rb:15:in `block in '

FAIL["test_should_update_user", UsersControllerTest, 0.5988616980030201] test_should_update_user#UsersControllerTest (0.60s) Expected response to be a , but was <200> test/controllers/users_controller_test.rb:34:in `block in '

Any ideas how I can get this to pass???

Thanks!


EDIT:

I've been fiddling and was able to modify the users.yml file to:

one:
  user_name: "name"
  email: "USER@EXAMPLE.INC"
  password_digest: "$2a$10$tAS/0m5JGW.k0o/h.eYwrOx6UTfJCmasKnRE0tS3kAqZWGMCZ.jba"

And I added the logging of puts assigns(:user).errors.inspect

I get the following error listed out:

#<ActiveModel::Errors:0x007f8a1f0935e8 
@base=#<User id: 980190962, 
user_name: "name", 
email: "USER@EXAMPLE.INC", 
password_digest: "$2a$10$tAS/0m5JGW.k0o/h.eYwrOx6UTfJCmasKnRE0tS3kAq...", 
created_at: "2016-04-08 20:14:16", 
updated_at: "2016-04-08 20:14:16">, 
@messages={:password=>["can't be blank", "is too short (minimum is 6 characters)"], :user_name=>[], :email=>[], :password_confirmation=>[]}>

But when I try to add a password: attribute to my users.yml file, it makes all of my tests error out with the error: table "users" has no column named "password".

Any ideas??

1

1 Answers

0
votes

First of all don't create user based on fixtures. Fixtures are inserted into database on starts of tests.

Therefore:

  1. Revert fixture :one to its original shape
  2. Change code of test to:

require 'test_helper'

class UsersControllerTest < ActionController::TestCase

  setup do
    @user = users(:one)
  end

  test "should create user" do    
    assert_difference('User.count') do
      post :create, user: { email: "test_123@example.com", password: 'testpassword', password_confirmation: 'testpassword', user_name: 'User name' }
    end

    assert_redirected_to user_path(assigns(:user))
  end

  test "should update user" do
    patch :update, id: @user, user: { email: @user.email, password: 'testpassword', password_confirmation: 'testpassword', user_name: @user.user_name }
    assert_redirected_to user_path(assigns(:user))
  end
end