Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regarding Mocking Failure in Integration Testing #1045

Open
rosewcs345 opened this issue Apr 29, 2021 · 1 comment
Open

Regarding Mocking Failure in Integration Testing #1045

rosewcs345 opened this issue Apr 29, 2021 · 1 comment

Comments

@rosewcs345
Copy link

Can't get failing test to redirect to /auth/failure, all other cucumber and rspec tests passing for other behaviors.

Documentation says:

Mocking Failure
If you set a provider's mock to a symbol instead of a hash, it will fail with that message.

OmniAuth.config.mock_auth[:twitter] = :invalid_credentials
You will be redirected back to /auth/failure?message=invalid_credentials.

However, all the mock scenarios use a symbol with no effect.

Documentation says:
By default Omniauth will raise an exception for invalid credentials in the development and test environments. If you'd like to be redirected to the /auth/failure endpoint in those environments, include this code:

OmniAuth.config.on_failure = Proc.new { |env|
OmniAuth::FailureEndpoint.new(env).redirect_to_failure
}

It is unclear where, exactly, to insert this code, and it is unclear how env gets set.

Configuration

  • Provider Gem: omniauth-github
  • Ruby Version: 2.6.6
  • Framework: Rails 4.2.11
  • Platform: Ubuntu 18.04.3 LTS

Expected Behavior

in test mode, should route to 'auth/failure'

Actual Behavior

Authentication passes in test mode, then fails in sessions/create because of validations

Steps to Reproduce

cucumber test

# features/failure.feature

@omniauth_test5
Scenario: failure
  Given I am on the RottenPotatoes Landing Page 
  When I press "Register or Login with GitHub"
  Then I will see "Failure"  # this would be edited if I could see what was returned

Applicable new step_definition (not in web_steps.rb):

# features/step_definitions/movie_steps.rb

Then /^I will see "([^"]*)"$/ do |message|
  puts page.body   # for debug
  expect(page.body).to have_content(message)
end

Test setup :

# features/support/env.rb

Before('@omniauth_test5') do
  OmniAuth.config.test_mode = true
  Capybara.default_host = 'http://example.com'
  
  OmniAuth.config.mock_auth[:github] = :invalid_credentials 


  OmniAuth.config.on_failure = Proc.new { |env|
  OmniAuth::FailureEndpoint.new(env).redirect_to_failure
}
  Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:github]  
end

After('@omniauth_test5') do
  OmniAuth.config.test_mode = false
  OmniAuth.config.mock_auth[:github] = nil
end

Omniauth initializer:

# config/initializers/omniauth.rb

OmniAuth.config.logger = Rails.logger
OmniAuth.config.allowed_request_methods = [:post] # recently added because of vulnerability
#************************************************
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :github, ENV['GITHUB_CLIENT_ID'],ENV['GITHUB_CLIENT_SECRET'],
         { :name => "github", :scope => ['read:user','user:email']}
end

controller:

# app/controllers/sessions_controller.rb

  def failure    
    begin
    rescue Exception => exception    
      flash[:error] = "#{exception.class}:  #{exception.message}" 
      redirect_to welcome_landing_path
    end 

Have tried inserting following code in a number of places, but it doesn't have any effect.

OmniAuth.config.on_failure = Proc.new { |env|
  OmniAuth::FailureEndpoint.new(env).redirect_to_failure
}
@airdrummingfool
Copy link

This issue looks related to #654

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants