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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

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-06-23 00:09:42 +05:30
RSpec.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
2021-04-29 21:17:54 +05:30
context 'application setting :admin_mode is enabled' do
2019-12-21 20:55:43 +05:30
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
2021-04-29 21:17:54 +05:30
context 'application setting :admin_mode is disabled' do
2019-12-21 20:55:43 +05:30
before do
2021-04-29 21:17:54 +05:30
stub_application_setting(admin_mode: false)
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
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