cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to simulate failing omniauth request with cucumber?

karmaTrinlema
Occasional Contributor

Is it possible to simulate failing omniauth request with cucumber?

I'm trying to get a cucumber test to follow the failure route in my sessions controller when making an omniauth request with invalid credentials:  

match 'auth/failure', :to => 'sessions#failure', :via => [:get, :post]

But instead of doing that, it seems to return and start the register/login process (which then fails).
I thought I could do this by setting up my features/support/env.rb as:  

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



OmniAuth.config.mock_auth[:github] = :invalid_credentials

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

But this is just failing later in my sessions create method

I inserted this code as well, but I'm not entirely positive it's in the right place.

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

I'm also finding it very difficult to find a reasonable place to put a stop for debugging.

I realize that this isn't strictly a cucumber question, but I'm hoping that others in this community are familiar with how this can be done properly.

 

THANKS!

5 REPLIES 5

Re: Is it possible to simulate failing omniauth request with cucumber?

Hi!

Could you please use the `code` feature of the forum - in the toolbar, [...](expand) then [</>](insert code sample) - for better readability?

 

Your issue seems related to omniauth more than cucumber. You seem to already have looked at their documentation here: https://github.com/omniauth/omniauth/wiki/Integration-Testing#mocking-failurehttps://github.com/omniauth/omniauth/wiki/Integration-Testing#mocking-failure

 

Did you notice the following part?

 


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
}


For debugging, which IDE are you using?

If you are using VS Code, make sure to have `debase` and `ruby-debug-ide` gem installed and the ruby extension for VS Code properly set-up. You'll then have the possibility to have such things in your `launch.json`

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Cucumber",
            "type": "Ruby",
            "request": "launch",
            "useBundler": true,
            "cwd": "${workspaceRoot}",
            "program": "${workspaceRoot}/bin/cucumber",
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "name": "Cucumber current feature file",
            "type": "Ruby",
            "request": "launch",
            "useBundler": true,
            "cwd": "${workspaceRoot}",
            "program": "${workspaceRoot}/bin/cucumber",
            "args": ["${file}"],
            "internalConsoleOptions": "openOnSessionStart"
        }
    ]
}
karmaTrinlema
Occasional Contributor

Re: Is it possible to simulate failing omniauth request with cucumber?

Thanks for your reply.  Yes, I have incorporated the additional code as well, but I wasn't positive about where it went.  I tried several different options, but none seem to work.  
Thanks for pointing out where to find the 'code' option.

 

I have edited my original post to reflect your suggestions.  My IDE is Codio, and I'm using byebug for debugging

Re: Is it possible to simulate failing omniauth request with cucumber?

I don't know "Codio", so I let you dig their documentation 😅

 

Regarding the snippet I gave you about omniauth, you should put it in the

Before('@omniauth_test5') do

 

However Cucumber is not related to that. Make sure you can reproduce the behavior you want to test beside, like in a rails console for example.

I know that they are a few things to be careful with omniauth inside rails.

karmaTrinlema
Occasional Contributor

Re: Is it possible to simulate failing omniauth request with cucumber?

Still not working.  I just hoped that someone in this community had some experience with testing the failure route in Rails4.  Obviously this isn't a cucumber issue, but it is a testing issue.

Re: Is it possible to simulate failing omniauth request with cucumber?

You are using rails 4?

Which version of omniauth are you using?

 

Maybe there is some kind of incompatibility between all those dependencies

 


@karmaTrinlema wrote:

(...) I just hoped that someone in this community had some experience with testing the failure route in Rails4.  Obviously this isn't a cucumber issue, but it is a testing issue.


Yes, of course, and you are welcome to ask. I just wanted to make sure we agree with that 🙂

New Here?
Join us and watch the welcome video: