Search code examples
ruby-on-railsrspecmockingcapybaracapybara-webkit

Mocking an external SSO login in RSpec feature specs with Capybara Webkit


My application redirects to an external party for Single-Sign On (SSO).

Once logged in, that external party redirects that user back to my app's callback route.

This is implemented as follows in the controller:

SsoController < ApplicationController

  def connect
    # do stuff

    # Redirect to external party with some params
    redirect_to "www.external-party.com?foo=\"bar\""
  end

  def callback
    # Receive response from external party
    response = params[:response]

    # do stuff
  end
end

In my feature specs I'd love to actually have this mocked out so I can test the end-to-end functionality from the user's perspective.

I'm using RSpec + Capybara and I'd like to do model the following flow -

  • User clicks some button (e.g. "sign in")
  • That button is tied to the connect action above which redirects to some external party
  • RSpec mocks a response based on the params sent over and sends a POST request back to my app (callback action)
  • Further business logic to sign in user and take them to their target

I'm unsure of how to do the 3rd step, particularly with intercepting the call, constructing the response, and POST-ing something back

Thanks!

Edit: A POST request back to my app is preferred, but is not a must-have. If it's a GET request or a redirect, I can configure my app to allow those HTTP verbs on the test environment only.


Solution

  • Rather than attempting to mock this in the app code (which is discouraged in feature specs) the cleanest way to do what you want is using a programmable proxy like puffing billy - https://github.com/oesmith/puffing-billy. That will let you respond to the test browser making a request to "www.external-party.com?foo=\"bar\"" in any way you want.