In a Rails integration test, how do you drive OmniAuth's sign-in and its /auth/failure path without contacting the real provider?
answer
- OmniAuth.config.test_mode = true
- mock_auth[:github] or add_mock
- a Symbol mock simulates failure
- post, then follow_redirect!
- reset the mocks in teardown
basics
~20 sSet OmniAuth.config.test_mode = true and put an OmniAuth::AuthHash in OmniAuth.config.mock_auth[:github]; posting to /auth/github then redirects straight to the callback with that hash. A Symbol mock such as :invalid_credentials makes the strategy fail and redirect to /auth/failure.
solid answer
~40 sWith `OmniAuth.config.test_mode = true`, every strategy runs `mock_call!` instead of talking to the provider. The request phase still runs `request_validation_phase` and then redirects to the callback URL; the callback phase sets `request.env["omniauth.auth"]` from `OmniAuth.config.mock_auth[:github]`, falling back to the `:default` mock (provider "default", uid "1234"), and calls your action. You build the mock with `OmniAuth::AuthHash.new(provider: "github", uid: "42", info: { ... })` or `OmniAuth.config.add_mock(:github, uid: "42")`. Setting the mock to a Symbol, `mock_auth[:github] = :invalid_credentials`, makes the callback call `fail!(:invalid_credentials)`, so the default failure endpoint redirects to `/auth/failure?message=invalid_credentials&strategy=github`. In the test you `post "/auth/github"`, `follow_redirect!`, then assert. Reset the mock and test mode in teardown, because both live in global configuration.
go deeper
Know the two settings, test_mode and mock_auth, and the post then follow_redirect! sequence in an integration test.
Explain what the mock request and callback phases do, the :default fallback, and how a Symbol mock produces a failure.
Keep mocks from leaking across tests, account for the raise-out environments and CSRF settings, and assert on both the success and failure paths.
Decide how much of third-party sign-in the suite proves with mocks and what must be checked against a real provider before release.
## Why test mode exists A real sign-in sends the browser to the provider and back. Tests should not depend on a third-party site, network access or real accounts. OmniAuth therefore has a global **test mode**: when `OmniAuth.config.test_mode` is true, each strategy's `call!` diverts to `mock_call!`, which simulates both phases locally. ## What the mock phases do **Mock request phase** (on the allowed request method, POST by default): 1. stores `omniauth.params` and the origin in the session, as the real phase does; 2. runs `request_validation_phase` and `before_request_phase`; 3. redirects to the strategy's **callback URL** instead of the provider. **Mock callback phase** (on `/auth/:provider/callback`): 1. moves `omniauth.origin` and `omniauth.params` into the env; 2. looks up `OmniAuth.mock_auth_for(:github)`, which is `mock_auth[:github]` or, if unset, `mock_auth[:default]`; 3. if that value is a **Symbol**, calls `fail!` with it, which reaches `on_failure`; 4. otherwise sets `request.env["omniauth.auth"]` to it and calls the app, so your callback action runs. ## Building mocks | Call | Effect | |---|---| | `OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(provider: "github", uid: "42", info: { name: "Ada", email: "[email protected]" })` | the exact hash the action will see | | `OmniAuth.config.add_mock(:github, uid: "42", info: { name: "Ada" })` | merges into the default mock and forces `provider` to `"github"` | | `OmniAuth.config.mock_auth[:github] = :invalid_credentials` | the callback fails with that message key | | leave `mock_auth[:github]` unset | the `:default` mock: provider `"default"`, uid `"1234"`, name "Example User" | Note the `:default` fallback's provider is `"default"`, not `"github"`, so a test that forgets its mock silently signs in with an identity your lookup will not recognise. ## A test, end to end ```ruby class GithubSignInTest < ActionDispatch::IntegrationTest setup do OmniAuth.config.test_mode = true OmniAuth.config.add_mock(:github, uid: "42", info: { name: "Ada", email: "[email protected]" }) end teardown do OmniAuth.config.mock_auth[:github] = nil OmniAuth.config.test_mode = false end test "signs in a new member" do post "/auth/github" follow_redirect! assert_equal "42", Identity.last.uid end end ``` The request validation phase still runs in test mode. With `omniauth-rails_csrf_protection`, it uses Rails' `verified_request?`, and the generated `config/environments/test.rb` sets `config.action_controller.allow_forgery_protection = false`, so the POST needs no token. ## Testing the failure path Set `OmniAuth.config.mock_auth[:github] = :access_denied` (any Symbol works; it becomes `omniauth.error.type`). After `post` and `follow_redirect!`, the callback calls `fail!`, and the default `OmniAuth::FailureEndpoint`: - **redirects** to `/auth/failure?message=access_denied&strategy=github`, so a second `follow_redirect!` reaches your failure action; - or **raises** the error instead, when `ENV["RACK_ENV"]` is listed in `OmniAuth.config.failure_raise_out_environments` (default `["development"]`). If that raise gets in the way, set `OmniAuth.config.on_failure = proc { |env| OmniAuth::FailureEndpoint.new(env).redirect_to_failure }`, which always redirects. ## Local development with the Developer strategy Test mode is for the test suite. For clicking through the flow in a browser before a real provider is registered, OmniAuth ships `provider :developer`: its request phase renders a small form asking for a name and email, and its callback returns them as the auth hash, with the email as `uid` by default (`option :uid_field, :email`). Guard it with `unless Rails.env.production?`, because anyone can type any address. ## Global state `test_mode`, `mock_auth` and `on_failure` live on a singleton configuration shared by every test in the process. Leaving a failure Symbol in `mock_auth[:github]` breaks the next test that expects success, so reset them in `teardown`.
- Why does a test that only does get "/auth/github" never reach the callback in OmniAuth 2?Test mode still respects `allowed_request_methods`, which defaults to `[:post]`. A GET is not intercepted, falls through to Rails and finds no route. Use `post "/auth/github"` and then `follow_redirect!`.
- How would you keep the development-only raise from hiding your /auth/failure page while building it locally?Replace the failure handler: `OmniAuth.config.on_failure = proc { |env| OmniAuth::FailureEndpoint.new(env).redirect_to_failure }`. `redirect_to_failure` skips the raise-out check, so development behaves like production and reaches your failure route.
- In test mode OmniAuth skips the request phase, so tests can GET the callback only.
- add_mock replaces the whole hash, so the default uid never leaks in.
- A failure is simulated by setting mock_auth[:github] to nil.
- test_mode is reset automatically between Rails tests.
- Tests must stub the provider's HTTP API to exercise the callback action.
Assumes OmniAuth 2.1, where the mock request phase honours the POST-only default and still runs the request validation phase.