简体   繁体   中英

Integration Testing with Devise + Omniauth + Rspec + Capybara

I am trying to write some integration tests using Rspec (feature) alongside Devise and Omniauth. The OAuth provider I am using is azure_activedirectory .

I have followed the tutorial here at the Omniauth wiki

I don't think I am doing it right. When I launch an integration test, the link for the login (localhost:3000/omniauth/azure_activedirectory) behaves like it would in dev or production, with the link directing the client to the omniauth portal page.

Of course given this is a test, I can't store credentials here.

It appears from the code in the wiki link that it should instead feed a login using a mock_auth.

Here is my current spec code:

require 'rails_helper'

Capybara.app_host = "http://lvh.me"
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:azure_activedirectory, {:uid => '12345'})

RSpec.feature "AuthenticatesUser", type: :feature do

  before do 
    Rails.application.env_config["devise.mapping"] = Devise.mappings[:user]
    Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:azure_activedirectory]
  end

  scenario "User can login", js: true do
    WebMock.allow_net_connect! 
    visit '/'
    click_link 'login'
    binding.pry
    WebMock.disable_net_connect!(allow_localhost: true) # Re enable with local host
  end
end

So what am I doing wrong? I feel that I am using the example code in a very incorrect way or not understanding the process.

Or is the next step just setting up a testing oauth instance and configuring it that way to use live credentials from Azure.

Thanks for the help in advance.

UPDATE

This has to do with Capybara and the JS driver starting or running on a different server.

If in the interactive ruby console in the Selenium browser OmniAuth.config.test_mode == false . If I set it using the console in the web browser, everything 'works'.

I found the solution.

You can not put OmniAuth into test mode in your specs. As capybara with js is in a separate thread, it has no idea about any config that was being set in the specs.

The solution was moving the declarations

OmniAuth.config.test_mode = true

OmniAuth.config.auth_mocks = NewAuthMock

into the test.rb environment file, that way Capybara would spawn with the correct config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM