View The Space is Hiring

Tuesday, September 17, 2013

Testing SSL under capybara

If your website runs under ssl than your integration tests should too.  This post outlines how to set up testing over https with capybara 2.1 and a few popular capybara drivers.  The majority of this info was borrowed from this capybara github issue.

In order to run your capybara test server under ssl, you will need to generate a self signed certificate.
You'll want to check the certificate and private key into your project.  I added them to my spec/support directory.

Now that the certificate is all set, start capybara with ssl enabled using your certificates.  Hint: If you are using rspec, the following snippets can be pasted into your spec_helper.rb.



Next tell capybara, to make requests using https:


Now you'll need to instruct your web driver to automatically accepted your self signed certificate even though you are running from localhost.



Finally, add the following monkey patch capybara to make http with ssl enabled:



I am not proud of the last step, and jnicklas indicated in this closed issue that this should not be needed, but not sure how else to get it done.  Plan on following up with him and updating the post.



5 comments:

  1. Thanks! This is very helpful, as I am trying to setup SSL in my test environment.
    For some reason though, I keep getting a "Rack application timed out during boot" error when I try to run any test. Not sure why.
    Also, doesn't line 1 of capybara_ssl_responsive.rb need to be "module Capybara" instead of just "Capybara"?

    ReplyDelete
    Replies
    1. Definitely missing the "module". Copy paste error :-).

      Not sure why you are getting the timeouts. I don't recall running into it but i don't recall a lot of things. Try checking the app server log? In my example that is "./log/capybara_test.log".

      thx!

      Delete
  2. Thanks!

    That's a lot of steps! But it worked for me.

    Please change your code that reads

    ```
    :SSLPrivateKey => OpenSSL::PKey::RSA.new("./spec/support/server.key"),
    ```

    to

    ```
    :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read "./spec/support/server.key"),
    ```

    That part tripped me up. See http://stackoverflow.com/a/24669024/135101.

    ReplyDelete
  3. The above is outdated. Found this updated version:
    https://github.com/leandog/til/blob/master/capybaraSSL.md

    ReplyDelete
  4. Nice blog! Thanks for sharing.
    In normal cases, data is transferred or shared in simple text form, which gives advantage to hackers to misuse or modify it easily, which is a big security threat. To overcome this, SSL Certificate provides an encrypted link, which converts the data into non-readable form, and then this encrypted data is transferred between the server and the browser through secure link that nullifies the risk of data being hacked. To get this kind of security, all you need is SSL Certificate.

    ReplyDelete