summaryrefslogtreecommitdiff
path: root/spec/serializers/environment_entity_spec.rb
blob: f5d6706a8443a16405a5e32ee0aaa4216aff1bc2 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe EnvironmentEntity do
  include Gitlab::Routing.url_helpers

  let(:request) { double('request') }
  let(:entity) do
    described_class.new(environment, request: spy('request'))
  end

  let_it_be(:user)    { create(:user) }
  let_it_be(:project) { create(:project) }
  let_it_be(:environment) { create(:environment, project: project) }

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

  subject { entity.as_json }

  it 'exposes latest deployment' do
    expect(subject).to include(:last_deployment)
  end

  it 'exposes core elements of environment' do
    expect(subject).to include(:id, :global_id, :name, :state, :environment_path)
  end

  it 'exposes folder path' do
    expect(subject).to include(:folder_path)
  end

  context 'metrics disabled' do
    before do
      allow(environment).to receive(:has_metrics?).and_return(false)
    end

    it "doesn't expose metrics path" do
      expect(subject).not_to include(:metrics_path)
    end
  end

  context 'metrics enabled' do
    before do
      allow(environment).to receive(:has_metrics?).and_return(true)
    end

    it 'exposes metrics path' do
      expect(subject).to include(:metrics_path)
    end
  end

  context 'with deployment platform' do
    let(:project) { create(:project, :repository) }
    let(:environment) { create(:environment, project: project) }

    context 'when deployment platform is a cluster' do
      before do
        create(:cluster,
               :provided_by_gcp,
               :project,
               environment_scope: '*',
               projects: [project])
      end

      it 'includes cluster_type' do
        expect(subject).to include(:cluster_type)
        expect(subject[:cluster_type]).to eq('project_type')
      end
    end
  end

  context 'with auto_stop_in' do
    let(:environment) { create(:environment, :will_auto_stop, project: project) }

    it 'exposes auto stop related information' do
      project.add_maintainer(user)

      expect(subject).to include(:cancel_auto_stop_path, :auto_stop_at)
    end
  end

  context 'pod_logs' do
    context 'with reporter access' do
      before do
        project.add_reporter(user)
      end

      it 'does not expose logs keys' do
        expect(subject).not_to include(:logs_path)
        expect(subject).not_to include(:logs_api_path)
        expect(subject).not_to include(:enable_advanced_logs_querying)
      end
    end

    context 'with developer access' do
      before do
        project.add_developer(user)
      end

      it 'exposes logs keys' do
        expect(subject).to include(:logs_path)
        expect(subject).to include(:logs_api_path)
        expect(subject).to include(:enable_advanced_logs_querying)
      end

      it 'uses k8s api when ES is not available' do
        expect(subject[:logs_api_path]).to eq(k8s_project_logs_path(project, environment_name: environment.name, format: :json))
      end

      it 'uses ES api when ES is available' do
        allow(environment).to receive(:elastic_stack_available?).and_return(true)

        expect(subject[:logs_api_path]).to eq(elasticsearch_project_logs_path(project, environment_name: environment.name, format: :json))
      end
    end
  end
end