summaryrefslogtreecommitdiff
path: root/spec/requests/self_monitoring_project_spec.rb
blob: f7227f71b056afe231ebb5b00ab3cee2e40a3087 (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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Self-Monitoring project requests' do
  let(:admin) { create(:admin) }

  describe 'POST #create_self_monitoring_project' do
    let(:worker_class) { SelfMonitoringProjectCreateWorker }

    subject { post create_self_monitoring_project_admin_application_settings_path }

    it_behaves_like 'not accessible to non-admin users'

    context 'with admin user', :enable_admin_mode do
      before do
        login_as(admin)
      end

      context 'when the self monitoring project is created' do
        let(:status_api) { status_create_self_monitoring_project_admin_application_settings_path }

        it_behaves_like 'triggers async worker, returns sidekiq job_id with response accepted'
      end
    end
  end

  describe 'GET #status_create_self_monitoring_project' do
    let(:worker_class) { SelfMonitoringProjectCreateWorker }
    let(:job_id) { 'job_id' }

    subject do
      get status_create_self_monitoring_project_admin_application_settings_path,
        params: { job_id: job_id }
    end

    it_behaves_like 'not accessible to non-admin users'

    context 'with admin user', :enable_admin_mode do
      before do
        login_as(admin)
      end

      context 'when the self monitoring project is being created' do
        it_behaves_like 'handles invalid job_id'

        context 'when job is in progress' do
          before do
            allow(worker_class).to receive(:in_progress?)
              .with(job_id)
              .and_return(true)
          end

          it_behaves_like 'sets polling header and returns accepted' do
            let(:in_progress_message) { 'Job to create self-monitoring project is in progress' }
          end
        end

        context 'when self-monitoring project and job do not exist' do
          let(:job_id) { nil }

          it 'returns bad_request' do
            create(:application_setting)

            subject

            aggregate_failures do
              expect(response).to have_gitlab_http_status(:bad_request)
              expect(json_response).to eq(
                'message' => 'Self-monitoring project does not exist. Please check logs ' \
                  'for any error messages'
              )
            end
          end
        end

        context 'when self-monitoring project exists' do
          let(:project) { create(:project) }

          before do
            create(:application_setting, self_monitoring_project_id: project.id)
          end

          it 'does not need job_id' do
            get status_create_self_monitoring_project_admin_application_settings_path

            aggregate_failures do
              expect(response).to have_gitlab_http_status(:success)
              expect(json_response).to eq(
                'project_id' => project.id,
                'project_full_path' => project.full_path
              )
            end
          end

          it 'returns success with job_id' do
            subject

            aggregate_failures do
              expect(response).to have_gitlab_http_status(:success)
              expect(json_response).to eq(
                'project_id' => project.id,
                'project_full_path' => project.full_path
              )
            end
          end
        end
      end
    end
  end

  describe 'DELETE #delete_self_monitoring_project' do
    let(:worker_class) { SelfMonitoringProjectDeleteWorker }

    subject { delete delete_self_monitoring_project_admin_application_settings_path }

    it_behaves_like 'not accessible to non-admin users'

    context 'with admin user', :enable_admin_mode do
      before do
        login_as(admin)
      end

      context 'when the self monitoring project is deleted' do
        let(:status_api) { status_delete_self_monitoring_project_admin_application_settings_path }

        it_behaves_like 'triggers async worker, returns sidekiq job_id with response accepted'
      end
    end
  end

  describe 'GET #status_delete_self_monitoring_project' do
    let(:worker_class) { SelfMonitoringProjectDeleteWorker }
    let(:job_id) { 'job_id' }

    subject do
      get status_delete_self_monitoring_project_admin_application_settings_path,
        params: { job_id: job_id }
    end

    it_behaves_like 'not accessible to non-admin users'

    context 'with admin user', :enable_admin_mode do
      before do
        login_as(admin)
      end

      context 'when the self monitoring project is being deleted' do
        it_behaves_like 'handles invalid job_id'

        context 'when job is in progress' do
          before do
            allow(worker_class).to receive(:in_progress?)
              .with(job_id)
              .and_return(true)

            stub_application_setting(self_monitoring_project_id: 1)
          end

          it_behaves_like 'sets polling header and returns accepted' do
            let(:in_progress_message) { 'Job to delete self-monitoring project is in progress' }
          end
        end

        context 'when self-monitoring project exists and job does not exist' do
          before do
            create(:application_setting, self_monitoring_project_id: create(:project).id)
          end

          it 'returns bad_request' do
            subject

            aggregate_failures do
              expect(response).to have_gitlab_http_status(:bad_request)
              expect(json_response).to eq(
                'message' => 'Self-monitoring project was not deleted. Please check logs ' \
                  'for any error messages'
              )
            end
          end
        end

        context 'when self-monitoring project does not exist' do
          before do
            create(:application_setting)
          end

          it 'does not need job_id' do
            get status_delete_self_monitoring_project_admin_application_settings_path

            aggregate_failures do
              expect(response).to have_gitlab_http_status(:success)
              expect(json_response).to eq(
                'message' => 'Self-monitoring project has been successfully deleted'
              )
            end
          end

          it 'returns success with job_id' do
            subject

            aggregate_failures do
              expect(response).to have_gitlab_http_status(:success)
              expect(json_response).to eq(
                'message' => 'Self-monitoring project has been successfully deleted'
              )
            end
          end
        end
      end
    end
  end
end