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

150 lines
3.9 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe ControllerWithCrossProjectAccessCheck do
2018-03-27 19:54:05 +05:30
let(:user) { create(:user) }
before do
sign_in user
end
render_views
context 'When reading cross project is not allowed' do
before do
allow(Ability).to receive(:allowed).and_call_original
2020-04-22 19:07:51 +05:30
expect(Ability).to receive(:allowed?).with(user, :log_in, :global).and_call_original
2018-03-27 19:54:05 +05:30
allow(Ability).to receive(:allowed?)
.with(user, :read_cross_project, :global)
.and_return(false)
end
describe '#requires_cross_project_access' do
controller(ApplicationController) do
# `described_class` is not available in this context
2020-03-13 15:44:24 +05:30
include ControllerWithCrossProjectAccessCheck
2018-03-27 19:54:05 +05:30
requires_cross_project_access :index, show: false,
unless: -> { unless_condition },
if: -> { if_condition }
def index
2019-02-15 15:39:39 +05:30
head :ok
2018-03-27 19:54:05 +05:30
end
def show
2019-02-15 15:39:39 +05:30
head :ok
2018-03-27 19:54:05 +05:30
end
def unless_condition
false
end
def if_condition
true
end
end
2018-10-15 14:42:47 +05:30
it 'renders a 403 with trying to access a cross project page' do
2018-03-27 19:54:05 +05:30
message = "This page is unavailable because you are not allowed to read "\
"information across multiple projects."
get :index
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2018-03-27 19:54:05 +05:30
expect(response.body).to match(/#{message}/)
end
it 'is skipped when the `if` condition returns false' do
expect(controller).to receive(:if_condition).and_return(false)
get :index
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-03-27 19:54:05 +05:30
end
it 'is skipped when the `unless` condition returns true' do
expect(controller).to receive(:unless_condition).and_return(true)
get :index
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-03-27 19:54:05 +05:30
end
it 'correctly renders an action that does not require cross project access' do
2019-02-15 15:39:39 +05:30
get :show, params: { id: 'nothing' }
2018-03-27 19:54:05 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-03-27 19:54:05 +05:30
end
end
describe '#skip_cross_project_access_check' do
controller(ApplicationController) do
# `described_class` is not available in this context
2020-03-13 15:44:24 +05:30
include ControllerWithCrossProjectAccessCheck
2018-03-27 19:54:05 +05:30
requires_cross_project_access
skip_cross_project_access_check index: true, show: false,
unless: -> { unless_condition },
if: -> { if_condition }
def index
2019-02-15 15:39:39 +05:30
head :ok
2018-03-27 19:54:05 +05:30
end
def show
2019-02-15 15:39:39 +05:30
head :ok
2018-03-27 19:54:05 +05:30
end
def edit
2019-02-15 15:39:39 +05:30
head :ok
2018-03-27 19:54:05 +05:30
end
def unless_condition
false
end
def if_condition
true
end
end
it 'renders a success when the check is skipped' do
get :index
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-03-27 19:54:05 +05:30
end
it 'is executed when the `if` condition returns false' do
expect(controller).to receive(:if_condition).and_return(false)
get :index
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2018-03-27 19:54:05 +05:30
end
it 'is executed when the `unless` condition returns true' do
expect(controller).to receive(:unless_condition).and_return(true)
get :index
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2018-03-27 19:54:05 +05:30
end
it 'does not skip the check on an action that is not skipped' do
2019-02-15 15:39:39 +05:30
get :show, params: { id: 'hello' }
2018-03-27 19:54:05 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2018-03-27 19:54:05 +05:30
end
it 'does not skip the check on an action that was not defined to skip' do
2019-02-15 15:39:39 +05:30
get :edit, params: { id: 'hello' }
2018-03-27 19:54:05 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2018-03-27 19:54:05 +05:30
end
end
end
end