Forum Discussion

karmaTrinlema's avatar
karmaTrinlema
Occasional Contributor
3 years ago

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

  • 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's avatar
      karmaTrinlema
      Occasional Contributor

      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

      • aurelien-reeves's avatar
        aurelien-reeves
        Staff

        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.