cluster_presenter_spec.rb 6.67 KB
Newer Older
1 2
require 'spec_helper'

Shinya Maeda's avatar
Shinya Maeda committed
3
describe Clusters::ClusterPresenter do
4 5 6
  include Gitlab::Routing.url_helpers

  let(:cluster) { create(:cluster, :provided_by_gcp, :project) }
Thong Kuah's avatar
Thong Kuah committed
7
  let(:user) { create(:user) }
8 9

  subject(:presenter) do
Thong Kuah's avatar
Thong Kuah committed
10
    described_class.new(cluster, current_user: user)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  end

  it 'inherits from Gitlab::View::Presenter::Delegated' do
    expect(described_class.superclass).to eq(Gitlab::View::Presenter::Delegated)
  end

  describe '#initialize' do
    it 'takes a cluster and optional params' do
      expect { presenter }.not_to raise_error
    end

    it 'exposes cluster' do
      expect(presenter.cluster).to eq(cluster)
    end

    it 'forwards missing methods to cluster' do
Shinya Maeda's avatar
Shinya Maeda committed
27
      expect(presenter.status).to eq(cluster.status)
28 29 30
    end
  end

Thong Kuah's avatar
Thong Kuah committed
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 150 151 152 153
  describe '#item_link' do
    let(:clusterable_presenter) { double('ClusterablePresenter', subject: clusterable) }

    subject { presenter.item_link(clusterable_presenter) }

    context 'for a group cluster' do
      let(:cluster) { create(:cluster, cluster_type: :group_type, groups: [group]) }
      let(:group) { create(:group, name: 'Foo') }
      let(:cluster_link) { "<a href=\"#{group_cluster_path(cluster.group, cluster)}\">#{cluster.name}</a>" }

      before do
        group.add_maintainer(user)
      end

      shared_examples 'ancestor clusters' do
        context 'ancestor clusters', :nested_groups do
          let(:root_group) { create(:group, name: 'Root Group') }
          let(:parent) { create(:group, name: 'parent', parent: root_group) }
          let(:child) { create(:group, name: 'child', parent: parent) }
          let(:group) { create(:group, name: 'group', parent: child) }

          before do
            root_group.add_maintainer(user)
          end

          context 'top level group cluster' do
            let(:cluster) { create(:cluster, cluster_type: :group_type, groups: [root_group]) }

            it 'returns full group names and link for cluster' do
              expect(subject).to eq("Root Group / #{cluster_link}")
            end

            it 'is html safe' do
              expect(presenter).to receive(:sanitize).with('Root Group').and_call_original

              expect(subject).to be_html_safe
            end
          end

          context 'first level group cluster' do
            let(:cluster) { create(:cluster, cluster_type: :group_type, groups: [parent]) }

            it 'returns full group names and link for cluster' do
              expect(subject).to eq("Root Group / parent / #{cluster_link}")
            end

            it 'is html safe' do
              expect(presenter).to receive(:sanitize).with('Root Group / parent').and_call_original

              expect(subject).to be_html_safe
            end
          end

          context 'second level group cluster' do
            let(:cluster) { create(:cluster, cluster_type: :group_type, groups: [child]) }

            let(:ellipsis_h) do
              /.*ellipsis_h.*/
            end

            it 'returns clipped group names and link for cluster' do
              expect(subject).to match("Root Group / #{ellipsis_h} / child / #{cluster_link}")
            end

            it 'is html safe' do
              expect(presenter).to receive(:sanitize).with('Root Group / parent / child').and_call_original

              expect(subject).to be_html_safe
            end
          end
        end
      end

      context 'for a project clusterable' do
        let(:clusterable) { project }
        let(:project) { create(:project, group: group) }

        it 'returns the group name and the link for cluster' do
          expect(subject).to eq("Foo / #{cluster_link}")
        end

        it 'is html safe' do
          expect(presenter).to receive(:sanitize).with('Foo').and_call_original

          expect(subject).to be_html_safe
        end

        include_examples 'ancestor clusters'
      end

      context 'for the group clusterable for the cluster' do
        let(:clusterable) { group }

        it 'returns link for cluster' do
          expect(subject).to eq(cluster_link)
        end

        include_examples 'ancestor clusters'

        it 'is html safe' do
          expect(subject).to be_html_safe
        end
      end
    end

    context 'for a project cluster' do
      let(:cluster) { create(:cluster, :project) }
      let(:cluster_link) { "<a href=\"#{project_cluster_path(cluster.project, cluster)}\">#{cluster.name}</a>" }

      before do
        cluster.project.add_maintainer(user)
      end

      context 'for the project clusterable' do
        let(:clusterable) { cluster.project }

        it 'returns link for cluster' do
          expect(subject).to eq(cluster_link)
        end
      end
    end
  end

154 155 156
  describe '#gke_cluster_url' do
    subject { described_class.new(cluster).gke_cluster_url }

Shinya Maeda's avatar
Shinya Maeda committed
157 158
    it { is_expected.to include(cluster.provider.zone) }
    it { is_expected.to include(cluster.name) }
159
  end
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

  describe '#can_toggle_cluster' do
    let(:user) { create(:user) }

    before do
      allow(cluster).to receive(:current_user).and_return(user)
    end

    subject { described_class.new(cluster).can_toggle_cluster? }

    context 'when user can update' do
      before do
        allow_any_instance_of(described_class).to receive(:can?).with(user, :update_cluster, cluster).and_return(true)
      end

      context 'when cluster is created' do
        before do
          allow(cluster).to receive(:created?).and_return(true)
        end

        it { is_expected.to eq(true) }
      end

      context 'when cluster is not created' do
        before do
          allow(cluster).to receive(:created?).and_return(false)
        end

        it { is_expected.to eq(false) }
      end
    end

    context 'when user can not update' do
      before do
        allow_any_instance_of(described_class).to receive(:can?).with(user, :update_cluster, cluster).and_return(false)
      end

      it { is_expected.to eq(false) }
    end
  end
200

201 202 203 204 205 206 207 208 209 210 211 212 213 214
  describe '#cluster_type_description' do
    subject { described_class.new(cluster).cluster_type_description }

    context 'project_type cluster' do
      it { is_expected.to eq('Project cluster') }
    end

    context 'group_type cluster' do
      let(:cluster) { create(:cluster, :provided_by_gcp, :group) }

      it { is_expected.to eq('Group cluster') }
    end
  end

215 216 217 218 219 220 221 222
  describe '#show_path' do
    subject { described_class.new(cluster).show_path }

    context 'project_type cluster' do
      let(:project) { cluster.project }

      it { is_expected.to eq(project_cluster_path(project, cluster)) }
    end
223 224 225 226 227 228 229

    context 'group_type cluster' do
      let(:group) { cluster.group }
      let(:cluster) { create(:cluster, :provided_by_gcp, :group) }

      it { is_expected.to eq(group_cluster_path(group, cluster)) }
    end
230
  end
231
end