2017-06-21 10:59:13 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::PerformanceBar do
|
2017-07-06 20:34:51 -04:00
|
|
|
shared_examples 'allowed user IDs are cached' do
|
2017-07-04 10:15:24 -04:00
|
|
|
before do
|
|
|
|
# Warm the Redis cache
|
2017-07-06 20:34:51 -04:00
|
|
|
described_class.enabled?(user)
|
2017-07-04 10:15:24 -04:00
|
|
|
end
|
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
it 'caches the allowed user IDs in cache', :use_clean_rails_memory_store_caching do
|
2017-07-04 10:15:24 -04:00
|
|
|
expect do
|
2017-07-06 20:34:51 -04:00
|
|
|
expect(described_class.enabled?(user)).to be_truthy
|
2017-07-04 10:15:24 -04:00
|
|
|
end.not_to exceed_query_limit(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-06 20:34:51 -04:00
|
|
|
describe '.enabled?' do
|
2017-06-30 11:34:06 -04:00
|
|
|
let(:user) { create(:user) }
|
2017-06-21 10:59:13 -04:00
|
|
|
|
2017-06-30 11:34:06 -04:00
|
|
|
before do
|
2017-07-06 20:34:51 -04:00
|
|
|
stub_application_setting(performance_bar_allowed_group_id: -1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when given user is nil' do
|
|
|
|
expect(described_class.enabled?(nil)).to be_falsy
|
2017-06-21 10:59:13 -04:00
|
|
|
end
|
|
|
|
|
2018-04-02 15:17:17 -04:00
|
|
|
it 'returns true when given user is an admin' do
|
|
|
|
user = build_stubbed(:user, :admin)
|
|
|
|
|
|
|
|
expect(described_class.enabled?(user)).to be_truthy
|
|
|
|
end
|
|
|
|
|
2017-07-06 20:34:51 -04:00
|
|
|
it 'returns false when allowed_group_id is nil' do
|
|
|
|
expect(described_class).to receive(:allowed_group_id).and_return(nil)
|
|
|
|
|
|
|
|
expect(described_class.enabled?(user)).to be_falsy
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when allowed group ID does not exist' do
|
2017-06-30 11:34:06 -04:00
|
|
|
it 'returns false' do
|
2017-07-06 20:34:51 -04:00
|
|
|
expect(described_class.enabled?(user)).to be_falsy
|
2017-06-21 10:59:13 -04:00
|
|
|
end
|
2017-06-30 11:34:06 -04:00
|
|
|
end
|
2017-06-21 10:59:13 -04:00
|
|
|
|
2017-06-30 11:34:06 -04:00
|
|
|
context 'when allowed group exists' do
|
|
|
|
let!(:my_group) { create(:group, path: 'my-group') }
|
|
|
|
|
2017-07-06 20:34:51 -04:00
|
|
|
before do
|
|
|
|
stub_application_setting(performance_bar_allowed_group_id: my_group.id)
|
|
|
|
end
|
|
|
|
|
2017-06-30 11:34:06 -04:00
|
|
|
context 'when user is not a member of the allowed group' do
|
2017-06-21 10:59:13 -04:00
|
|
|
it 'returns false' do
|
2017-07-06 20:34:51 -04:00
|
|
|
expect(described_class.enabled?(user)).to be_falsy
|
2017-06-21 10:59:13 -04:00
|
|
|
end
|
2017-07-04 10:15:24 -04:00
|
|
|
|
2017-07-06 20:34:51 -04:00
|
|
|
it_behaves_like 'allowed user IDs are cached'
|
2017-06-21 10:59:13 -04:00
|
|
|
end
|
|
|
|
|
2017-06-30 11:34:06 -04:00
|
|
|
context 'when user is a member of the allowed group' do
|
|
|
|
before do
|
|
|
|
my_group.add_developer(user)
|
2017-06-21 10:59:13 -04:00
|
|
|
end
|
|
|
|
|
2017-06-30 11:34:06 -04:00
|
|
|
it 'returns true' do
|
2017-07-06 20:34:51 -04:00
|
|
|
expect(described_class.enabled?(user)).to be_truthy
|
2017-06-21 10:59:13 -04:00
|
|
|
end
|
|
|
|
|
2017-07-06 20:34:51 -04:00
|
|
|
it_behaves_like 'allowed user IDs are cached'
|
2017-06-21 10:59:13 -04:00
|
|
|
end
|
|
|
|
end
|
2017-07-03 13:09:14 -04:00
|
|
|
|
|
|
|
context 'when allowed group is nested', :nested_groups do
|
|
|
|
let!(:nested_my_group) { create(:group, parent: create(:group, path: 'my-org'), path: 'my-group') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:group, path: 'my-group')
|
2017-07-04 10:15:24 -04:00
|
|
|
nested_my_group.add_developer(user)
|
2017-07-06 20:34:51 -04:00
|
|
|
stub_application_setting(performance_bar_allowed_group_id: nested_my_group.id)
|
2017-07-03 13:09:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the nested group' do
|
2017-07-06 20:34:51 -04:00
|
|
|
expect(described_class.enabled?(user)).to be_truthy
|
2017-07-03 13:09:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a nested group has the same path', :nested_groups do
|
|
|
|
before do
|
2017-07-04 10:15:24 -04:00
|
|
|
create(:group, :nested, path: 'my-group').add_developer(user)
|
2017-07-03 13:09:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
2017-07-06 20:34:51 -04:00
|
|
|
expect(described_class.enabled?(user)).to be_falsy
|
2017-07-03 13:09:14 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-21 10:59:13 -04:00
|
|
|
end
|
|
|
|
end
|