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.