0
votes

I am working through the Hartl rails tutorial, within Chapter 9 I am trying to replicate the tests for restricting the edit action to the current user:

require 'spec_helper'

describe "Authentication" do
subject { page }
 ....
 ....
 ....

describe "as wrong user" do
    let(:user) { FactoryGirl.create(:user) }
    let(:wrong_user) { FactoryGirl.create(:user, email: "[email protected]") }
    before { sign_in user, no_capybara: true }

    describe "submitting a GET request to the Users#edit action" do
        before { get edit_user_path(wrong_user) }
        specify { expect(response.body).not_to match(full_title('Edit user')) }
        specify { expect(response).to redirect_to(root_url) }
    end

    describe "submitting a PATCH request to the Users#update action" do
        before { patch user_path(wrong_user) }
        specify { expect(response).to redirect_to(root_url) }
    end 
end
end

I have then implemented the code as described within the book, but the tests still fail:

Failures:

  1) Authentication as wrong user submitting a PATCH request to the Users#update action 
     Failure/Error: specify { expect(response).to redirect_to(root_url) }
       Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/signin>.
       Expected "http://www.example.com/" to be === "http://www.example.com/signin".
     # ./spec/requests/authentication_pages_spec.rb:100:in `block (4 levels) in <top (required)>'

I have investigated by adding some logging to the sign_in method called within the test:

def sign_in(user, options={})
  Rails.logger.debug "sign_in "+"*"*15
  if options[:no_capybara]
    # Sign in when not using Capybara.
    remember_token = User.new_remember_token
    cookies[:remember_token] = remember_token
      Rails.logger.debug "Created cookie rt: "+ cookies[:remember_token].to_s
    user.update_attribute(:remember_token, User.encrypt(remember_token))

  else
    visit signin_path
    fill_in "Email",    with: user.email
    fill_in "Password", with: user.password
    click_button "Sign in"
  end
end

Within the log the create cookie remember_token is reported as blank. If I change the sign in to sign_in user (without the no capybara option) I can see that the user is signed in and the cookie populated, but when I visit the edit action the cookie is no longer populated.

How can I investigate further as to why the cookie is being destroyed?

Thanks...

For information I've included the application code below:

content of users_controller.rb

   class UsersController < ApplicationController
before_action :signed_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
def new
      @user = User.new
end

def show
@user = User.find(params[:id])
logger.debug "User show: "+ @user.to_yaml
logger.debug "cookie on user show: "+ cookies[:remember_token].to_yaml
end

def create
    @user = User.new(user_params)

    if @user.save
    sign_in @user
    flash[:success] = "Welcome to the Sample App!"
    redirect_to @user
      else
    render 'new'
    end
end

def update
    @user = User.find(params[:id])

    if @user.update_attributes(user_params)
         flash[:success] = "Profile updated"
         sign_in @user
         redirect_to @user
    else
        render 'edit'
    end
end

def edit
        logger.debug "Edit user method"
    @user = User.find(params[:id])
end

private

    def user_params
        params.require(:user).permit(:name, :email, :password,
            :password_confirmation)
    end 

    # Before filters
    def signed_in_user
        si = signed_in?
        logger.debug "*"*200
        logger.debug "signed in user: signed in?: "+si.to_s
        redirect_to signin_url, notice: "Please sign in." unless si
    end

    def correct_user
        @user = User.find(params[:id])
        redirect_to(root_url) unless current_user?(@user)
    end
         end

Content of sessions_controller.rb

class SessionsController < ApplicationController

        def new
        end

        def create
            user = User.find_by_email(params[:email].downcase)

            if user && user.authenticate(params[:password])
              sign_in user
              redirect_to user
            else
              flash.now[:error] = 'Invalid email/password combination'
              render 'new'
            end  
        end

        def destroy
            sign_out
            redirect_to root_url
        end

    end

Content of sessions_helper.rb

module SessionsHelper

    def sign_in(user)
            logger.debug "Sessions Helper sign in Method"
        remember_token = User.new_remember_token
        cookies.permanent[:remember_token] = remember_token
        user.update_attribute(:remember_token, User.encrypt(remember_token))
        self.current_user = user
    end 

    def signed_in?

        s_in = !current_user.nil?
        logger.debug "signed_in?: "+s_in.to_s
        s_in

    end

    def sign_out
        self.current_user = nil
        cookies.delete(:remember_token)
    end

    def current_user=(user)
        @current_user = user
    end

    def current_user
            logger.debug "getting current user"
            logger.debug "cookie: "+ cookies[:remember_token].to_s
        remember_token = User.encrypt(cookies[:remember_token])
            logger.debug "rt: "+ remember_token.to_yaml
            logger.debug "current_user before: "+ @current_user.to_yaml

        @current_user ||= User.find_by(remember_token: remember_token)
            logger.debug "current_user after: "+ @current_user.to_yaml
        @current_user

    end

    def current_user?
        remember_token = User.encrypt(cookies[:remember_token])
        @current_user ||= User.find_by(remember_token: remember_token)
    end

    def current_user?(user)
        user == current_user
    end
end
1

1 Answers

0
votes

There is nothing wrong with your authentication test

Its the code in users_controller.rb file

please remove the indicated(<---) line from your update method from users_controller.rb file.

It is clear from the error itself that it is expected to be at root_ulr but landing on sign_in_url.

This is happening because you have added one line "sign_in @user" to your users_controller.rb file in update method.so as per you code it is redirecting to "sign_in_url" which is unnecessary as the user have already logged in and updated his profile.I hope this will be helpful and resolve your issue.

update method in users_controller.rb file

def update
        @user = User.find(params[:id])

        if @user.update_attributes(user_params)
             flash[:success] = "Profile updated"
             sign_in @user  `<----------- Remove this line
             redirect_to @user
        else
            render 'edit'
        end
    end