Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 5 Authentication Rails 5.2 #92

Open
scope2229 opened this issue Oct 14, 2018 · 0 comments
Open

Chapter 5 Authentication Rails 5.2 #92

scope2229 opened this issue Oct 14, 2018 · 0 comments

Comments

@scope2229
Copy link

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
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant