debian-mirror-gitlab/spec/controllers/concerns/enforces_admin_authentication_spec.rb

99 lines
2.3 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-04-22 19:07:51 +05:30
describe EnforcesAdminAuthentication do
2019-12-21 20:55:43 +05:30
include AdminModeHelper
2019-07-31 22:56:46 +05:30
let(:user) { create(:user) }
before do
sign_in(user)
end
controller(ApplicationController) do
2019-12-21 20:55:43 +05:30
include EnforcesAdminAuthentication
2019-07-31 22:56:46 +05:30
def index
head :ok
end
end
2019-12-21 20:55:43 +05:30
context 'feature flag :user_mode_in_session is enabled' do
describe 'authenticate_admin!' do
context 'as an admin' do
let(:user) { create(:admin) }
2019-07-31 22:56:46 +05:30
2019-12-21 20:55:43 +05:30
it 'renders redirect for re-authentication and does not set admin mode' do
get :index
expect(response).to redirect_to new_admin_session_path
expect(assigns(:current_user_mode)&.admin_mode?).to be(false)
end
context 'when admin mode is active' do
before do
enable_admin_mode!(user)
end
it 'renders ok' do
get :index
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-21 20:55:43 +05:30
end
end
end
context 'as a user' do
it 'renders a 404' do
get :index
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-12-21 20:55:43 +05:30
end
it 'does not set admin mode' do
get :index
2019-07-31 22:56:46 +05:30
2019-12-21 20:55:43 +05:30
# check for nil too since on 404, current_user_mode might not be initialized
expect(assigns(:current_user_mode)&.admin_mode?).to be_falsey
end
2019-07-31 22:56:46 +05:30
end
end
2019-12-21 20:55:43 +05:30
end
context 'feature flag :user_mode_in_session is disabled' do
before do
stub_feature_flags(user_mode_in_session: false)
end
2019-07-31 22:56:46 +05:30
2019-12-21 20:55:43 +05:30
describe 'authenticate_admin!' do
before do
2019-07-31 22:56:46 +05:30
get :index
2019-12-21 20:55:43 +05:30
end
context 'as an admin' do
let(:user) { create(:admin) }
it 'allows direct access to page' do
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-21 20:55:43 +05:30
end
it 'does not set admin mode' do
expect(assigns(:current_user_mode)&.admin_mode?).to be_falsey
end
end
context 'as a user' do
it 'renders a 404' do
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-12-21 20:55:43 +05:30
end
2019-07-31 22:56:46 +05:30
2019-12-21 20:55:43 +05:30
it 'does not set admin mode' do
# check for nil too since on 404, current_user_mode might not be initialized
expect(assigns(:current_user_mode)&.admin_mode?).to be_falsey
end
2019-07-31 22:56:46 +05:30
end
end
end
end