vicmaster
1/19/2015 - 11:04 PM

Mocking Github Omniauth

Mocking Github Omniauth

The mock_auth configuration allows you to set per-provider (or default) authentication hashes to return during integration testing. You can set it like so:

module Omniauth

  module Mock
    def auth_mock
      OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new({
        provider: 'github',
        uid: '123545',
        info: {
              name: 'John',
              email: 'test@github.com',
              nickname: 'johngit',
              image: 'something.png'
              },
        credentials: {
                token: '3232322321cdd2'
              }
      })
    end
  end

  module SessionHelpers
    def signin
      visit root_path
      expect(page).to have_content("Sign in")
      auth_mock # Calling auth github mock
      click_link "Sign in"
    end
  end

end

You can set the :default key to return a hash for providers that haven't been specified. Once you set the mock auth hash and turn on test mode, all requests to OmniAuth will return an auth hash from the mock.