summaryrefslogtreecommitdiff
path: root/spec/requests/projects/metrics_dashboard_spec.rb
blob: 7e94bc6134da132ffc7ed1fa5420b08945a61fbe (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Projects::MetricsDashboardController', feature_category: :metrics do
  let_it_be(:project) { create(:project) }
  let_it_be(:environment) { create(:environment, project: project) }
  let_it_be(:environment2) { create(:environment, project: project) }
  let_it_be(:user) { project.first_owner }

  before do
    stub_feature_flags(remove_monitor_metrics: false)
    project.add_developer(user)
    login_as(user)
    stub_feature_flags(remove_monitor_metrics: false)
  end

  shared_examples 'metrics dashboard is unavailable' do
    context 'when metrics dashboard feature is unavailable' do
      before do
        stub_feature_flags(remove_monitor_metrics: true)
      end

      it 'returns 404 not found' do
        send_request
        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe 'GET /:namespace/:project/-/metrics' do
    include_examples 'metrics dashboard is unavailable'

    it "redirects to default environment's metrics dashboard" do
      send_request
      expect(response).to redirect_to(dashboard_route(environment: environment))
    end

    it 'assigns default_environment' do
      send_request
      expect(assigns(:default_environment).id).to eq(environment.id)
    end

    it 'retains existing parameters when redirecting' do
      params = {
        dashboard_path: '.gitlab/dashboards/dashboard_path.yml',
        page: 'panel/new',
        group: 'System metrics (Kubernetes)',
        title: 'Memory Usage (Pod average)',
        y_label: 'Memory Used per Pod (MB)'
      }
      send_request(params)

      expect(response).to redirect_to(dashboard_route(params.merge(environment: environment.id)))
    end

    context 'with remove_monitor_metrics returning true' do
      before do
        stub_feature_flags(remove_monitor_metrics: true)
      end

      it 'renders 404 page' do
        send_request
        expect(response).to have_gitlab_http_status(:not_found)
      end
    end

    context 'with anonymous user and public dashboard visibility' do
      let(:anonymous_user) { create(:user) }
      let(:project) do
        create(:project, :public, :metrics_dashboard_enabled)
      end

      before do
        project.update!(metrics_dashboard_access_level: 'enabled')

        login_as(anonymous_user)
      end

      it 'returns 200' do
        send_request

        expect(response).to have_gitlab_http_status(:ok)
      end
    end
  end

  describe 'GET /:namespace/:project/-/metrics?environment=:environment.id' do
    include_examples 'metrics dashboard is unavailable'

    it 'returns 200' do
      send_request(environment: environment2.id)
      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'assigns query param environment' do
      send_request(environment: environment2.id)
      expect(assigns(:environment).id).to eq(environment2.id)
    end

    context 'when query param environment does not exist' do
      it 'responds with 404' do
        send_request(environment: non_existing_record_id)
        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe 'GET /:namespace/:project/-/metrics/:dashboard_path' do
    let(:dashboard_path) { '.gitlab/dashboards/dashboard_path.yml' }

    include_examples 'metrics dashboard is unavailable'

    it 'returns 200' do
      send_request(dashboard_path: dashboard_path, environment: environment.id)
      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'assigns environment' do
      send_request(dashboard_path: dashboard_path, environment: environment.id)
      expect(assigns(:environment).id).to eq(environment.id)
    end
  end

  describe 'GET :/namespace/:project/-/metrics/:dashboard_path?environment=:environment.id' do
    let(:dashboard_path) { '.gitlab/dashboards/dashboard_path.yml' }

    include_examples 'metrics dashboard is unavailable'

    it 'returns 200' do
      send_request(dahboard_path: dashboard_path, environment: environment.id)
      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'assigns query param environment' do
      send_request(dashboard_path: dashboard_path, environment: environment2.id)
      expect(assigns(:environment).id).to eq(environment2.id)
    end

    context 'when query param environment does not exist' do
      it 'responds with 404' do
        send_request(dashboard_path: dashboard_path, environment: non_existing_record_id)
        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe 'GET :/namespace/:project/-/metrics/:page' do
    include_examples 'metrics dashboard is unavailable'

    it 'returns 200 with path param page' do
      send_request(page: 'panel/new', environment: environment.id)

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'returns 200 with dashboard and path param page' do
      send_request(dashboard_path: 'dashboard.yml', page: 'panel/new', environment: environment.id)

      expect(response).to have_gitlab_http_status(:ok)
    end
  end

  def send_request(params = {})
    get dashboard_route(params)
  end

  def dashboard_route(params = {})
    namespace_project_metrics_dashboard_path(namespace_id: project.namespace, project_id: project, **params)
  end
end