Callback redirect not preserving session
Asked Answered
B

1

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)
Bertrand answered 12/4, 2015 at 4:58 Comment(2)
Any solution to this problem? Did you find anything?Asafetida
well please review the header of the request for real and for the mock, may be you should add something in the mockCentralism
L
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)

Laevo answered 3/10, 2016 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.