2019-05-01 21:07:38 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-04-14 14:09:54 -04:00
|
|
|
describe EnforcesAdminAuthentication do
|
2019-09-26 08:06:00 -04:00
|
|
|
include AdminModeHelper
|
|
|
|
|
2019-05-01 21:07:38 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
controller(ApplicationController) do
|
2019-09-26 08:06:00 -04:00
|
|
|
include EnforcesAdminAuthentication
|
2019-05-01 21:07:38 -04:00
|
|
|
|
|
|
|
def index
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-26 08:06:00 -04:00
|
|
|
context 'feature flag :user_mode_in_session is enabled' do
|
|
|
|
describe 'authenticate_admin!' do
|
|
|
|
context 'as an admin' do
|
|
|
|
let(:user) { create(:admin) }
|
2019-05-01 21:07:38 -04:00
|
|
|
|
2019-09-26 08:06:00 -04:00
|
|
|
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-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'as a user' do
|
|
|
|
it 'renders a 404' do
|
|
|
|
get :index
|
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not set admin mode' do
|
|
|
|
get :index
|
2019-05-02 17:30:53 -04:00
|
|
|
|
2019-09-26 08:06:00 -04:00
|
|
|
# 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-05-01 21:07:38 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'feature flag :user_mode_in_session is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(user_mode_in_session: false)
|
|
|
|
end
|
2019-05-01 21:07:38 -04:00
|
|
|
|
2019-09-26 08:06:00 -04:00
|
|
|
describe 'authenticate_admin!' do
|
|
|
|
before do
|
2019-05-01 21:07:38 -04:00
|
|
|
get :index
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'as an admin' do
|
|
|
|
let(:user) { create(:admin) }
|
|
|
|
|
|
|
|
it 'allows direct access to page' do
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-09-26 08:06:00 -04:00
|
|
|
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-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
2019-05-02 17:30:53 -04:00
|
|
|
|
2019-09-26 08:06:00 -04:00
|
|
|
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-05-01 21:07:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|