mercredi 28 octobre 2015

Stub controller redirect_to external url with VCR

Some third-party service which I want to use requires user to log in on their webpage. Handy url is generated in controller below. User goes there and come back to my service after his authentication succeeds

class MyController < App::BaseController
  def login
    redirect_to SOME_API.external_url(ENV['secret_id'])
  end
end

As user comes back to my service he brings some feedback from third-party service in URL params (like: http://ift.tt/1kSwA5O). There are many variants of these params so I need to handle them in my action

  def login_callback
    #SOME MAGIC WITH COOKIES/ERRORS/What ever
    redirect_to root_path
  end

I want to write feature specs for it using RSpec and capybara.

scenario 'User authenticates with third-party thing' do
    visit '/anything'
    click_link 'Go to MyController#login and than get redirect to http://ift.tt/1kSwA5Q'
    # use cassette below
    fill_out_form
    click_button confirm
    # magic happens on their side and their redirect user to my app into #login_callback action
    expect(page).to have(/all fancy things i need/)
  end
end

however calling WebMock.disable_net_connect!(allow_localhost: true) or adding vcr: { cassette_name: 'using_third_party_login', record: :new_episodes } doesn't prevent this scenario to being redirect to external url.

Capybara just let being redirected to external uri and no cassets are being recorded/played. Any idea how to stub redirect_to external_url with cassette play/record?

Aucun commentaire:

Enregistrer un commentaire