summaryrefslogtreecommitdiff
path: root/spec/lib/sidebars/projects/menus/infrastructure_menu_spec.rb
blob: 81114f5a0b3fb22297ec3115ac14969d4b143b2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Sidebars::Projects::Menus::InfrastructureMenu do
  let(:project) { build(:project) }
  let(:user) { project.first_owner }
  let(:context) { Sidebars::Projects::Context.new(current_user: user, container: project, show_cluster_hint: false) }

  describe '#render?' do
    subject { described_class.new(context) }

    context 'when menu does not have any menu items' do
      it 'returns false' do
        allow(subject).to receive(:has_renderable_items?).and_return(false)

        expect(subject.render?).to be false
      end
    end

    context 'when menu has menu items' do
      it 'returns true' do
        expect(subject.render?).to be true
      end
    end
  end

  describe '#link' do
    subject { described_class.new(context) }

    context 'when Kubernetes menu item is visible' do
      it 'menu link points to Kubernetes page' do
        expect(subject.link).to eq find_menu_item(:kubernetes).link
      end
    end

    context 'when Kubernetes menu item is not visible' do
      before do
        subject.renderable_items.delete(find_menu_item(:kubernetes))
      end

      it 'menu link points to Serverless page' do
        expect(subject.link).to eq find_menu_item(:serverless).link
      end

      context 'when Serverless menu is not visible' do
        before do
          subject.renderable_items.delete(find_menu_item(:serverless))
        end

        it 'menu link points to Terraform page' do
          expect(subject.link).to eq find_menu_item(:terraform).link
        end

        context 'when Terraform menu is not visible' do
          before do
            subject.renderable_items.delete(find_menu_item(:terraform))
          end

          it 'menu link points to Google Cloud page' do
            expect(subject.link).to eq find_menu_item(:google_cloud).link
          end
        end
      end
    end

    def find_menu_item(menu_item)
      subject.renderable_items.find { |i| i.item_id == menu_item }
    end
  end

  describe 'Menu Items' do
    subject { described_class.new(context).renderable_items.index { |e| e.item_id == item_id } }

    shared_examples 'access rights checks' do
      specify { is_expected.not_to be_nil }

      describe 'when the user does not have access' do
        let(:user) { nil }

        specify { is_expected.to be_nil }
      end
    end

    describe 'Kubernetes' do
      let(:item_id) { :kubernetes }

      it_behaves_like 'access rights checks'
    end

    describe 'Serverless' do
      let(:item_id) { :serverless }

      it_behaves_like 'access rights checks'

      context 'when feature :deprecated_serverless is disabled' do
        before do
          stub_feature_flags(deprecated_serverless: false)
        end

        it { is_expected.to be_nil }
      end
    end

    describe 'Terraform' do
      let(:item_id) { :terraform }

      it_behaves_like 'access rights checks'
    end

    describe 'Google Cloud' do
      let(:item_id) { :google_cloud }

      it_behaves_like 'access rights checks'

      context 'when feature flag is turned off globally' do
        before do
          stub_feature_flags(incubation_5mp_google_cloud: false)
        end

        it { is_expected.to be_nil }

        context 'when feature flag is enabled for specific project' do
          before do
            stub_feature_flags(incubation_5mp_google_cloud: project)
          end

          it_behaves_like 'access rights checks'
        end

        context 'when feature flag is enabled for specific group' do
          before do
            stub_feature_flags(incubation_5mp_google_cloud: project.group)
          end

          it_behaves_like 'access rights checks'
        end

        context 'when feature flag is enabled for specific project' do
          before do
            stub_feature_flags(incubation_5mp_google_cloud: user)
          end

          it_behaves_like 'access rights checks'
        end
      end
    end
  end
end