Commit 0b5133ba authored by Jacopo's avatar Jacopo

Extract "@request.env['devise.mapping'] = Devise.mappings[:user]" to a test helper

Extracted `@request.env['devise.mapping'] = Devise.mappings[:user]` and `Rails.application.env_config['devise.mapping'] = Devise.mappings[:user]`
in our tests into the helper method `set_devise_mapping`
parent 64701b51
---
title: Extract "@request.env[devise.mapping] = Devise.mappings[:user]" to a test helper
merge_request: 12742
author: Jacopo Beschi @jacopo-beschi
require 'spec_helper'
describe SessionsController do
include DeviseHelpers
describe '#new' do
before do
@request.env['devise.mapping'] = Devise.mappings[:user]
set_devise_mapping(context: @request)
end
context 'when auto sign-in is enabled' do
......@@ -34,7 +36,7 @@ describe SessionsController do
describe '#create' do
before do
@request.env['devise.mapping'] = Devise.mappings[:user]
set_devise_mapping(context: @request)
end
context 'when using standard authentications' do
......@@ -257,7 +259,7 @@ describe SessionsController do
describe '#new' do
before do
@request.env['devise.mapping'] = Devise.mappings[:user]
set_devise_mapping(context: @request)
end
it 'redirects correctly for referer on same host with params' do
......
require 'spec_helper'
feature 'OAuth Login', js: true do
include DeviseHelpers
def enter_code(code)
fill_in 'user_otp_attempt', with: code
click_button 'Verify code'
......@@ -8,7 +10,7 @@ feature 'OAuth Login', js: true do
def stub_omniauth_config(provider)
OmniAuth.config.add_mock(provider, OmniAuth::AuthHash.new(provider: provider.to_s, uid: "12345"))
Rails.application.env_config['devise.mapping'] = Devise.mappings[:user]
set_devise_mapping(context: Rails.application)
Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[provider]
end
......
......@@ -10,10 +10,12 @@ context 'U2F' do
end
describe SessionsController, '(JavaScript fixtures)', type: :controller do
include DeviseHelpers
render_views
before do
@request.env['devise.mapping'] = Devise.mappings[:user]
set_devise_mapping(context: @request)
end
it 'u2f/authenticate.html.raw' do |example|
......
module DeviseHelpers
# explicitly tells Devise which mapping to use
# this is needed when we are testing a Devise controller bypassing the router
def set_devise_mapping(context:)
env =
if context.respond_to?(:env_config)
context.env_config
elsif context.respond_to?(:env)
context.env
end
env['devise.mapping'] = Devise.mappings[:user] if env
end
end
module LoginHelpers
include DeviseHelpers
# Internal: Log in as a specific user or a new user of a specific role
#
# user_or_role - User object, or a role to create (e.g., :admin, :user)
......@@ -106,7 +108,7 @@ module LoginHelpers
end
def stub_omniauth_saml_config(messages)
Rails.application.env_config['devise.mapping'] = Devise.mappings[:user]
set_devise_mapping(context: Rails.application)
Rails.application.routes.disable_clear_and_finalize = true
Rails.application.routes.draw do
post '/users/auth/saml' => 'omniauth_callbacks#saml'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment