2017-06-21 14:59:13 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::PerformanceBar do
|
|
|
|
describe '.enabled?' do
|
2017-06-28 17:18:16 +00:00
|
|
|
it 'returns false when given actor is nil' do
|
2017-06-21 14:59:13 +00:00
|
|
|
expect(described_class.enabled?(nil)).to be_falsy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when feature is disabled' do
|
2017-06-28 17:18:16 +00:00
|
|
|
actor = double('actor')
|
2017-06-21 14:59:13 +00:00
|
|
|
|
|
|
|
expect(Feature).to receive(:enabled?)
|
2017-06-28 17:18:16 +00:00
|
|
|
.with(:gitlab_performance_bar, actor).and_return(false)
|
2017-06-21 14:59:13 +00:00
|
|
|
|
2017-06-28 17:18:16 +00:00
|
|
|
expect(described_class.enabled?(actor)).to be_falsy
|
2017-06-21 14:59:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when feature is enabled' do
|
2017-06-28 17:18:16 +00:00
|
|
|
actor = double('actor')
|
2017-06-21 14:59:13 +00:00
|
|
|
|
|
|
|
expect(Feature).to receive(:enabled?)
|
2017-06-28 17:18:16 +00:00
|
|
|
.with(:gitlab_performance_bar, actor).and_return(true)
|
2017-06-21 14:59:13 +00:00
|
|
|
|
2017-06-28 17:18:16 +00:00
|
|
|
expect(described_class.enabled?(actor)).to be_truthy
|
2017-06-21 14:59:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-04 14:15:24 +00:00
|
|
|
shared_examples 'allowed user IDs are cached in Redis for 10 minutes' do
|
|
|
|
before do
|
|
|
|
# Warm the Redis cache
|
|
|
|
described_class.allowed_user?(user)
|
|
|
|
end
|
|
|
|
|
2017-07-06 15:11:20 +00:00
|
|
|
it 'caches the allowed user IDs in cache', :caching do
|
2017-07-04 14:15:24 +00:00
|
|
|
expect do
|
|
|
|
expect(described_class.allowed_user?(user)).to be_truthy
|
|
|
|
end.not_to exceed_query_limit(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-30 15:34:06 +00:00
|
|
|
describe '.allowed_user?' do
|
|
|
|
let(:user) { create(:user) }
|
2017-06-21 14:59:13 +00:00
|
|
|
|
2017-06-30 15:34:06 +00:00
|
|
|
before do
|
|
|
|
stub_performance_bar_setting(allowed_group: 'my-group')
|
2017-06-21 14:59:13 +00:00
|
|
|
end
|
|
|
|
|
2017-06-30 15:34:06 +00:00
|
|
|
context 'when allowed group does not exist' do
|
|
|
|
it 'returns false' do
|
|
|
|
expect(described_class.allowed_user?(user)).to be_falsy
|
2017-06-21 14:59:13 +00:00
|
|
|
end
|
2017-06-30 15:34:06 +00:00
|
|
|
end
|
2017-06-21 14:59:13 +00:00
|
|
|
|
2017-06-30 15:34:06 +00:00
|
|
|
context 'when allowed group exists' do
|
|
|
|
let!(:my_group) { create(:group, path: 'my-group') }
|
|
|
|
|
|
|
|
context 'when user is not a member of the allowed group' do
|
2017-06-21 14:59:13 +00:00
|
|
|
it 'returns false' do
|
2017-06-30 15:34:06 +00:00
|
|
|
expect(described_class.allowed_user?(user)).to be_falsy
|
2017-06-21 14:59:13 +00:00
|
|
|
end
|
2017-07-04 14:15:24 +00:00
|
|
|
|
|
|
|
it_behaves_like 'allowed user IDs are cached in Redis for 10 minutes'
|
2017-06-21 14:59:13 +00:00
|
|
|
end
|
|
|
|
|
2017-06-30 15:34:06 +00:00
|
|
|
context 'when user is a member of the allowed group' do
|
|
|
|
before do
|
|
|
|
my_group.add_developer(user)
|
2017-06-21 14:59:13 +00:00
|
|
|
end
|
|
|
|
|
2017-06-30 15:34:06 +00:00
|
|
|
it 'returns true' do
|
|
|
|
expect(described_class.allowed_user?(user)).to be_truthy
|
2017-06-21 14:59:13 +00:00
|
|
|
end
|
|
|
|
|
2017-07-04 14:15:24 +00:00
|
|
|
it_behaves_like 'allowed user IDs are cached in Redis for 10 minutes'
|
2017-06-21 14:59:13 +00:00
|
|
|
end
|
|
|
|
end
|
2017-07-03 17:09:14 +00: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 14:15:24 +00:00
|
|
|
nested_my_group.add_developer(user)
|
2017-07-03 17:09:14 +00:00
|
|
|
stub_performance_bar_setting(allowed_group: 'my-org/my-group')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the nested group' do
|
2017-07-04 14:15:24 +00:00
|
|
|
expect(described_class.allowed_user?(user)).to be_truthy
|
2017-07-03 17:09:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a nested group has the same path', :nested_groups do
|
|
|
|
before do
|
2017-07-04 14:15:24 +00:00
|
|
|
create(:group, :nested, path: 'my-group').add_developer(user)
|
2017-07-03 17:09:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
2017-07-04 14:15:24 +00:00
|
|
|
expect(described_class.allowed_user?(user)).to be_falsy
|
2017-07-03 17:09:14 +00:00
|
|
|
end
|
|
|
|
end
|
2017-06-21 14:59:13 +00:00
|
|
|
end
|
|
|
|
end
|