ben
ben

Reputation: 29797

Turning on test mode in OmniAuth does not redirect requests when using Cucumber

I am trying to test my OmniAuth login process by providing a faked authentication hash when a request is made to /auth/facebook, as described here and here. The problem is, when I turn test mode on, the request returns as an error, which is the same behaviour as when test mode is not turned on.

user_management.feature

Feature: User management
    @omniauth_test
    Scenario: Login
        Given a user exists
        And that user is signed in

web_steps.rb

...
And /^that user is signed in$/ do
  visit "/auth/facebook"
end
...

omniauth.rb

Before('@omniauth_test') do
  OmniAuth.config.test_mode = true
  p "OmniAuth.config.test_mode is #{OmniAuth.config.test_mode}"
  # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
  OmniAuth.config.mock_auth[:facebook] = {
      "provider"=>"facebook",
      "uid"=>"uid",
      "user_info"=>{"email"=>"[email protected]", "first_name"=>"Test", "last_name"=>"User", "name"=>"Test User"}
  }
end

After('@omniauth_test') do
  OmniAuth.config.test_mode = false
end

Outcome

Feature: User management

  @omniauth_test
  Scenario: Login                # features/user_management.feature:3
"OmniAuth.config.test_mode is true"
    Given a user exists     # features/step_definitions/pickle_steps.rb:4
    And that user is signed in # features/step_definitions/web_steps.rb:40
      No route matches [GET] "/auth/facebook" (ActionController::RoutingError)
      ./features/step_definitions/web_steps.rb:41:in `/^that user is signed in$/'
      features/testing.feature:5:in `And that user is signed in'

Upvotes: 1

Views: 1498

Answers (3)

Pratik Khadloya
Pratik Khadloya

Reputation: 12879

Throw the following in omniauth.rb under features/support and mark your scenario which requires fb login with @omniauth_test

Before('@omniauth_test') do  
  OmniAuth.config.test_mode = true  

  # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
  OmniAuth.config.mock_auth[:facebook] = {
   :provider => 'facebook',
   :uid => '1234567',
   :info => {
     :nickname => 'test',
     :email => '[email protected]',
     :name => 'Test User',
     :first_name => 'Test',
     :last_name => 'User',
     :location => 'California',
     :verified => true
    }.stringify_keys!
  }.stringify_keys!
end

After('@omniauth_test') do
  OmniAuth.config.test_mode = false
end

Upvotes: 1

fuzzyalej
fuzzyalej

Reputation: 5973

You should throw these two in an test initializer:

request.env["devise.mapping"] = Devise.mappings[:user] 
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]

Upvotes: 1

Patrik Björklund
Patrik Björklund

Reputation: 1237

The problem is not with your tests. It's with your routing, or more specifically with omniauths routing.

Are you sure that you have a strategy set up in config/initializers/omniauth.rb for facebook?

You can get it in gemform https://github.com/mkdynamic/omniauth-facebook

Also, remember to restart your webserver after adding a new strategy. (That got me once ;))

Upvotes: 0

Related Questions