summaryrefslogtreecommitdiff
path: root/spec/presenters/clusters/cluster_presenter_spec.rb
blob: e96dbfb73c0b9ef7373e8e1b525e43b65a9d0caa (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
require 'spec_helper'

describe Clusters::ClusterPresenter do
  let(:cluster) { create(:cluster, :provided_by_gcp) }

  subject(:presenter) do
    described_class.new(cluster)
  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
      expect(presenter.status).to eq(cluster.status)
    end
  end

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

    it { is_expected.to include(cluster.provider.zone) }
    it { is_expected.to include(cluster.name) }
  end

  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
end