Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Capybara + Selenium to test over SSL

The core part of my app makes calls to Etsy API. I'm trying to write tests using Rspec, Capybara and Selenium to test that API. After I make one successful call I'd like to stub those out.

My problem is that the Etsy auth process only works with SSL. So when I try to test with http I get error 400.

I used this tutorial to set up SSL but I keep getting this error:

  1) New connection authenticates with Etsy
     Failure/Error: res = http.get('/__identify__')

     OpenSSL::SSL::SSLError:
       SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol
     # ./spec/spec_helper.rb:68:in `responsive?'

Here's my spec_helper.rb:

require 'webmock/rspec'
require 'capybara/rspec'
include WebMock::API
require 'etsy'
WebMock.disable_net_connect!(allow_localhost: true)
#Capybara.current_driver = :selenium
Capybara.default_max_wait_time = 20

require 'webrick/https'
require 'rack/handler/webrick'

def run_ssl_server(app, port)

  opts = {
    :Port => port,
    :SSLEnable => true,
    :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
    :SSLPrivateKey => OpenSSL::PKey::RSA.new("./spec/support/server.key"),
    :SSLCertificate => OpenSSL::X509::Certificate.new(File.read "./spec/support/server.crt"),
    :SSLCertName => [["US", 'localhost.key']],
    :AccessLog => [],
    :Logger => WEBrick::Log::new(Rails.root.join("./log/capybara_test.log").to_s)
  }
  profile = Selenium::WebDriver::Firefox::Profile.new
  profile.assume_untrusted_certificate_issuer = false

   Capybara::Driver::Selenium.new(app, :browser=> :firefox, :profile => profile)
  Rack::Handler::WEBrick.run(app, opts)
end

Capybara.server do |app, port|
  run_ssl_server(app, port)
end

Capybara.server_port = 3001
Capybara.app_host = "https://localhost:%d" % Capybara.server_port

  Capybara.register_driver :selenium do |app|
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile.assume_untrusted_certificate_issuer = false
    Capybara::Selenium::Driver.new(app, :profile => profile)
  end

module Capybara
  class Server
    def responsive?
      return false if @server_thread && @server_thread.join(0)

      http = Net::HTTP.new(host, @port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      res = http.get('/__identify__')

      if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
        return res.body == @app.object_id.to_s
      end
    rescue SystemCallError
      return false
    end
  end
end

Thanks in advance! here's the files I generated

like image 318
Maayan Naveh Avatar asked Oct 24 '25 19:10

Maayan Naveh


1 Answers

You appear to have extra stuff in your run_ssl_server which doesn't make any sense

profile = Selenium::WebDriver::Firefox::Profile.new
profile.assume_untrusted_certificate_issuer = false

Capybara::Driver::Selenium.new(app, :browser=> :firefox, :profile => profile)

so I assume that was just a copy/paste error?

Moving on the error you're getting appears to be telling you that the SSLv2/SSLv3 protocols aren't known/supported by the connection. You can disable them in your WEBRick options by specifying the SSLOptions, SSLCiphers, and SSLVersion options as shown in How to harden rails+webrick+https with insecure ciphers removed on Ruby 2.2

An easier option may be to not register your own server, patch Capybara, fix the server port or set app_host, but instead switch over to using Capybara >= 3.1.0 and just configure Puma to run SSL

Capybara.server = :puma, { Host: "ssl://#{Capybara.server_host}?key=#{key_file_path}&cert=#{cert_file_path}" }

Then you can register whatever browser you're using with Selenium to allow self signed certs (if necessary)

Capybara.register_driver :insecure_selenium do |app|
  Capybara::Selenium::Driver.new(
    app,
    browser: :firefox,
    desired_capabilities: { accept_insecure_certs: true }
  )
end

Capybara.javascript_driver = :insecure_selenium # https://github.com/teamcapybara/capybara#drivers
like image 157
Thomas Walpole Avatar answered Oct 26 '25 09:10

Thomas Walpole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!