0

On a Pact verification stage I need to mock user authentication. What is the best way to do it? I'm using Rails 4 with Devise.

I found a very hacky looking solution, which works but looks ugly and I hope there's another way to do it. I'm replacing the app inside of the Pact suite and manually setting user for warden if it's required. User ID is being set inside the provider state. It looks like this:

Pact.service_provider "Some Service Provider" do
  app { ProxyApp.new(Rails.application) }
  ...

class ProxyApp
  ...

  def call(env)
    # Mock user authentication
    env['warden'] ||= begin
      manager = Warden::Manager.new(nil) do |config|
        config.merge! Devise.warden_config
      end
      Warden::Proxy.new(env, manager)
    end
    user_id = EnvMock.get_user_id # user ID is being set inside the provider state
    env['warden'].set_user(Account.find(user_id), scope: :account) if user_id

    ...
  end
end

I really hope that there's some completely different perspective that allows to accomplish this task in much more beautiful way.

J_A_X
  • 12,857
  • 1
  • 25
  • 31
Vladimir Mikhaylovskiy
  • 1,955
  • 4
  • 19
  • 28
  • Using a proxy app is the way I would have approached it too. I don't know of a more elegant solution. – Beth Skurrie Apr 17 '17 at 23:25
  • 1
    I do remember a conversation about using Warden from the pact-support group though - Andre created an example code base [here](https://github.com/allavena/sinatra-warden-example). – Beth Skurrie Apr 17 '17 at 23:32
  • Possible duplicate of [How do I verify pacts against an API that requires an auth token?](http://stackoverflow.com/questions/40777493/how-do-i-verify-pacts-against-an-api-that-requires-an-auth-token) – J_A_X May 15 '17 at 23:31

0 Answers0