Search code examples
ruby-on-railsrspeccapybaracapybara-webkit

401 errors after switching Capybara default driver?


I'm putting together feature specs for an existing Rails app using Capybara and RSpec. By default, Capybara uses rack_test as the driver, but I want to use capybara-webkit.

I've put together a simple test to log in to my site. Using rack_test I'm able to log in, and my log/test.log file shows the following HTTP requests:

Completed 200 OK in 201ms (Views: 201.3ms | ActiveRecord: 0.0ms)
Started POST "/users/sign_in" for 127.0.0.1 at 2015-12-01 10:20:33 -0600
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "user"=>{"email"=>"foo@bar.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign In"}
  User Load (0.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'foo@bar.com'  ORDER BY "users"."id" ASC LIMIT 1
   # There are a few more SQL queries here that I've removed
Redirected to http://www.example.com/
Completed 302 Found in 36ms (ActiveRecord: 2.3ms)
Started GET "/" for 127.0.0.1 at 2015-12-01 10:20:33 -0600
Processing by InitiativesController#blank as HTML

I'm having issues with running this test when I switch to webkit (by setting Capybara.default_driver :webkit in spec_helper.rb). For some reason I am seeing 401 errors in the log:

Completed 200 OK in 17ms (Views: 16.7ms | ActiveRecord: 0.0ms)
Started POST "/users/sign_in" for 127.0.0.1 at 2015-12-01 10:21:31 -0600
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "user"=>{"email"=>"foo@bar.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign In"}
  User Load (0.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'foo@bar.com'  ORDER BY "users"."id" ASC LIMIT 1
  # no other queries
Completed 401 Unauthorized in 5ms
Processing by Devise::SessionsController#new as HTML

So the log in request is not being accepted. Is there some sort of Capybara configuration that I need to change in order to get this log in request to work? I can't figure out what is causing this.

My spec_helper.rb looks like this:

require 'simplecov'
require 'webmock/rspec'
require 'capybara/rspec'
require 'fakeredis/rspec'
require 'site_prism'

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

Capybara.javascript_driver = :poltergeist
Capybara.default_driver = :webkit
Capybara.default_host = 'http://127.0.0.1:60400'
Capybara.server_port = 60400

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

Capybara::Webkit.configure do |config|
  config.block_unknown_urls
end

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.infer_base_class_for_anonymous_controllers = false

  config.include Devise::TestHelpers, :type => [:controller, :acceptance]

  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do |example|
    DatabaseCleaner.strategy = :truncation 
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  config.order = "random"

  config.before(:all) do
    FactoryGirl.reload
  end

  config.before :each do
    Timecop.return
  end

  config.include Helpers
  config.include Helpers::MassAdsCreation
end

WebMock.disable_net_connect!(allow_localhost: true)

Solution

  • I think you may need to use the Warden::Test::Helpers instead as described here

    Note that they have a specific section dealing with capybara-webkit as well.