Skip to content

Chapter 5 Authentication Rails 5.2  #92

@scope2229

Description

@scope2229

I'm having a probelm with my authentication test for the sessions controller.

`class Api::V1::Auth::SessionsController < ApplicationController
respond_to :json

def create
parent_password = params[:session][:password]
parent_email = params[:session][:email]
parent = parent_email.present? && Parent.find_by(email: parent_email)

if parent && parent.valid_password?(parent_password)
  sign_in parent
  parent.generate_authentication_token!
  parent.save
  render json: parent, status: 200, location: [:api, :auth, parent]
else
  render json: { errors: "Invalid email or password" }, status: 422
end

end

def destroy
parent = Parent.find_by(auth_token: params[:id])
parent.generate_authentication_token!
parent.save
head 204
end
end`

When i run my test suite using guard:rspec

`require 'rails_helper'

RSpec.describe Api::V1::Auth::SessionsController, type: :controller do
describe "POST #create" do
before(:each) do
@parent = FactoryBot.create :parent
end

context "when the credentials are correct" do
  before(:each) do
    credentials = { email: @parent.email, password: "12345678" }
    post :create, :params => { session: credentials }
  end

  it "returns the parent record corresponding to the given credentials" do
    @parent.reload
    expect(json_response[:auth_token]).to eql @parent.auth_token
  end

  it { should respond_with 200 }
end

context "when the credentials are incorrect" do

  before(:each) do
    credentials = { email: @parent.email, password: "invalidpassword" }
    post :create, :params => { session: credentials }
  end

  it "returns a json with an error" do
    expect(json_response[:errors]).to eql "Invalid email or password"
  end

  it { should respond_with 422 }
end

end

describe "DELETE #destroy" do
before(:each) do
@parent = FactoryBot.create :parent
sign_in @parent
delete :destroy, params: { id: @parent.auth_token }
end

it { should respond_with 204 }

end
end`

I'm getting this error

Api::V1::Auth::SessionsController POST #create when the credentials are correct returns the parent record corresponding to the given credentials
Failure/Error: expect(json_response[:auth_token]).to eql @parent.auth_token

   expected: "cevDjZY9yLAh8YK5YBVZ"
        got: "HSYy-QsvC6N6zTDje7zZ"
 
   (compared using eql?)

my parent controller and model look like this

`class Api::V1::Auth::ParentsController < ApplicationController
respond_to :json

def show
respond_with Parent.find(params[:id])
end

def create
parent = Parent.new(parent_params)
if parent.save
render json: parent, status: 201, location: [:api, :auth, parent]
else
render json: { errors: parent.errors }, status: 422
end
end
def update
parent = Parent.find(params[:id])
if parent.update(parent_params)
render json: parent, status: 200, location: [:api, :auth, parent]
else
render json: { errors: parent.errors }, status: 422
end
end
def destroy
parent = Parent.find(params[:id])
parent.destroy
head 204
end

private

def parent_params
params.require(:parent).permit(:email, :password, :password_confirmation)
end
end`

`class Parent < ApplicationRecord

devise :database_authenticatable, :registerable,
:recoverable, :rememberable,
:validatable, :jwt_authenticatable,
jwt_revocation_strategy: JwtBlacklist

validates :email, presence: true
validates :password, presence: true
validates :password_confirmation, presence: true
validates_uniqueness_of :email
validates :auth_token, uniqueness: true

before_create :generate_authentication_token!

def generate_authentication_token!
begin
self.auth_token = Devise.friendly_token
end while self.class.exists?(auth_token: auth_token)
end

end
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions