Commit 60b28e7c authored by Rémy Coutable's avatar Rémy Coutable
Browse files

Merge branch '34549-extract-devise-mappings-into-helper' into 'master'

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

Closes #34549

See merge request !12742
---
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 '#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 @@ def authenticate_2fa_u2f(user_params)
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 @@ def enter_code(code)
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 @@
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 @@ def mock_saml_config
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