46

I have a scenario that works just fine when I am using real omniauth, but fails when I run it with the mock auth in cucumber/capybara.

In the callback, when I do sign_in @user, it successfully creates the user and logs in... current_user is set. But when I then do redirect_to request.env['omniauth.origin'] || '/', inside the action that follows, current_user is now nil.

I've confirmed via screenshots/pausing the browser that it's not working with the mock auth. The same error occurs in firefox and chrome drivers.

Any idea as to why this would be happening?

/features/support/env.rb:

Cucumber::Rails::Database.javascript_strategy = :truncation

Scenario:

@javascript
Scenario:
    Given I am on the home page
    When I press "Login"
    And I should see "Login with Twitter" in the selector "#login-modal"
    Given Omniauth returns a user with provider "twitter" and uid "1" and nickname "foo"    
    When I login with Twitter
    Then I should be logged in as "foo"

Step Definitions:

Given(/^Omniauth returns a user with provider "(.*?)" and uid "(.*?)" and nickname "(.*?)"$/) do |provider, uid, nickname|
  OmniAuth.config.test_mode = true

 OmniAuth.config.add_mock(provider.to_sym, {
     :uid => uid,
     :info => {
       :name => nickname
     }
   })
end

Then(/^I should be logged in as "(.*?)"$/) do |nickname|
  expect(page).to have_content(nickname)
end

Auth callback:

def twitter
  @user = User.from_omniauth(request.env["omniauth.auth"]) # this works-- I get the mock
  sign_in @user
  puts ">> in auth callback: just signed in user #{current_user.id}"        
  redirect_to request.env['omniauth.origin'] || '/'
end

Controller:

def new
  puts ">> in my_controller#new: current_user = #{current_user.id if current_user}"
end

Cucumber Output:

Given Omniauth returns a user with provider "twitter" and uid "1" and nickname "foo" 
>> in auth callback: just signed in user 1
>> in my_controller#new: current_user =
When I login with Twitter                                                                 
Then I should be logged in as "foo"                                                  
  expected to find text "foo" in [redacted] (RSpec::Expectations::ExpectationNotMetError)
user3378165
  • 6,546
  • 17
  • 62
  • 101
joshwa
  • 1,660
  • 3
  • 17
  • 26

1 Answers1

1

You are getting the user and collecting it to new variable @user but while you are calling the sign_in method again you did initialize the new variable user with using(eg. @user)

Draken
  • 3,134
  • 13
  • 34
  • 54
Parmatma
  • 191
  • 10