NganCun
NganCun

Reputation: 184

Rspec request test with omniauth doesn't call destroy method when sign out is called

I have a rails 6.1.7 project with omniauth + devise and a custom openid connect server that I'm connecting to. I'm trying to write a rspec request test for the log out feature with some logic in the destroy method which it isn't calling for some reason. I can't figure out why.

routes.rb

devise_for :users, controllers: {
    sessions: 'users/sessions',
    passwords: 'users/passwords',
    confirmations: 'users/confirmations',
    omniauth_callbacks: 'users/omniauth_callbacks'
  }

devise.rb

config.sign_out_via = :get
config.omniauth :custom_openid_connect, {
...
}

sessions_controller.rb

module Users
  class SessionsController < Devise::SessionsController
    def create
      cookies['visited'] = '1'
      super
    end

    def destroy
      cookies.delete 'visited'
      super
    end

  end
end

omniauth_callbacks_controller.rb

module Users
  class OmniauthCallbacksController < Devise::OmniauthCallbacksController
    def custom
      @user = User.find(1)
      sign_in_and_redirect @user
    end
  end
end

sessions_spec.rb

require 'rails_helper'

RSpec.describe 'Users::Sessions', type: :request do
  include Devise::Test::IntegrationHelpers

  let!(:user) { FactoryBot.create(:user) }

  describe 'GET /users/sign_out' do
    before(:each) do
      cookies['visited'] = '1'
    end

    context 'log out success' do
      it 'should delete cookies' do
        get destroy_user_session_path(user)
        expect(response).to be_successful
        expect(cookies['visited']).to be_nil
      end
    end
  end
end

It failed on expecting cookies to be nil. Why when I didn't setup any user, the sign_out route still return successful? And if the destroy method in sessions_controller.rb isn't called, then which method was it called?

Upvotes: 0

Views: 52

Answers (0)

Related Questions