BigW Consortium Gitlab

performance_bar_spec.rb 2.5 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::PerformanceBar do
4
  shared_examples 'allowed user IDs are cached' do
5 6
    before do
      # Warm the Redis cache
7
      described_class.enabled?(user)
8 9
    end

10
    it 'caches the allowed user IDs in cache', :use_clean_rails_memory_store_caching do
11
      expect do
12
        expect(described_class.enabled?(user)).to be_truthy
13 14 15 16
      end.not_to exceed_query_limit(0)
    end
  end

17
  describe '.enabled?' do
18
    let(:user) { create(:user) }
19

20
    before do
21 22 23 24 25
      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
26 27
    end

28 29 30 31 32 33 34
    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
35
      it 'returns false' do
36
        expect(described_class.enabled?(user)).to be_falsy
37
      end
38
    end
39

40 41 42
    context 'when allowed group exists' do
      let!(:my_group) { create(:group, path: 'my-group') }

43 44 45 46
      before do
        stub_application_setting(performance_bar_allowed_group_id: my_group.id)
      end

47
      context 'when user is not a member of the allowed group' do
48
        it 'returns false' do
49
          expect(described_class.enabled?(user)).to be_falsy
50
        end
51

52
        it_behaves_like 'allowed user IDs are cached'
53 54
      end

55 56 57
      context 'when user is a member of the allowed group' do
        before do
          my_group.add_developer(user)
58 59
        end

60
        it 'returns true' do
61
          expect(described_class.enabled?(user)).to be_truthy
62 63
        end

64
        it_behaves_like 'allowed user IDs are cached'
65 66
      end
    end
67 68 69 70 71 72

    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')
73
        nested_my_group.add_developer(user)
74
        stub_application_setting(performance_bar_allowed_group_id: nested_my_group.id)
75 76 77
      end

      it 'returns the nested group' do
78
        expect(described_class.enabled?(user)).to be_truthy
79 80 81 82 83
      end
    end

    context 'when a nested group has the same path', :nested_groups do
      before do
84
        create(:group, :nested, path: 'my-group').add_developer(user)
85 86 87
      end

      it 'returns false' do
88
        expect(described_class.enabled?(user)).to be_falsy
89 90
      end
    end
91 92
  end
end