summaryrefslogtreecommitdiff
path: root/spec/serializers/merge_request_widget_entity_spec.rb
blob: 926b33e8e1f723811a090d7779fc771e138cc339 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe MergeRequestWidgetEntity do
  include ProjectForksHelper

  let(:project) { create :project, :repository }
  let(:resource) { create(:merge_request, source_project: project, target_project: project) }
  let(:pipeline) { create(:ci_empty_pipeline, project: project) }
  let(:user) { create(:user) }

  let(:request) { double('request', current_user: user, project: project) }

  subject do
    described_class.new(resource, request: request).as_json
  end

  describe 'source_project_full_path' do
    it 'includes the full path of the source project' do
      expect(subject[:source_project_full_path]).to be_present
    end

    context 'when the source project is missing' do
      it 'returns `nil` for the source project' do
        resource.allow_broken = true
        resource.update!(source_project: nil)

        expect(subject[:source_project_full_path]).to be_nil
      end
    end
  end

  describe 'can_create_pipeline_in_target_project' do
    context 'when user has permission' do
      before do
        project.add_developer(user)
      end

      it 'includes the correct permission info' do
        expect(subject[:can_create_pipeline_in_target_project]).to eq(true)
      end
    end

    context 'when user does not have permission' do
      before do
        project.add_guest(user)
      end

      it 'includes the correct permission info' do
        expect(subject[:can_create_pipeline_in_target_project]).to eq(false)
      end
    end
  end

  describe 'issues links' do
    it 'includes issues links when requested' do
      data = described_class.new(resource, request: request, issues_links: true).as_json

      expect(data).to include(:issues_links)
      expect(data[:issues_links]).to include(:assign_to_closing, :closing, :mentioned_but_not_closing)
    end

    it 'omits issue links by default' do
      expect(subject).not_to include(:issues_links)
    end
  end

  it 'has email_patches_path' do
    expect(subject[:email_patches_path])
      .to eq("/#{resource.project.full_path}/-/merge_requests/#{resource.iid}.patch")
  end

  it 'has plain_diff_path' do
    expect(subject[:plain_diff_path])
      .to eq("/#{resource.project.full_path}/-/merge_requests/#{resource.iid}.diff")
  end

  describe 'codequality report artifacts', :request_store do
    let(:merge_base_pipeline) { create(:ci_pipeline, :with_codequality_reports, project: project) }

    before do
      project.add_developer(user)

      allow(resource).to receive_messages(
        merge_base_pipeline: merge_base_pipeline,
        base_pipeline: pipeline,
        head_pipeline: pipeline
      )
    end

    context 'with report artifacts' do
      let(:pipeline) { create(:ci_pipeline, :with_codequality_reports, project: project) }
      let(:generic_job_id) { pipeline.builds.first.id }
      let(:merge_base_job_id) { merge_base_pipeline.builds.first.id }

      it 'has head_path and base_path entries' do
        expect(subject[:codeclimate][:head_path]).to include("/jobs/#{generic_job_id}/artifacts/download?file_type=codequality")
        expect(subject[:codeclimate][:base_path]).to include("/jobs/#{generic_job_id}/artifacts/download?file_type=codequality")
      end

      context 'on pipelines for merged results' do
        let(:pipeline) { create(:ci_pipeline, :merged_result_pipeline, :with_codequality_reports, project: project) }

        it 'returns URLs from the head_pipeline and merge_base_pipeline' do
          expect(subject[:codeclimate][:head_path]).to include("/jobs/#{generic_job_id}/artifacts/download?file_type=codequality")
          expect(subject[:codeclimate][:base_path]).to include("/jobs/#{merge_base_job_id}/artifacts/download?file_type=codequality")
        end
      end
    end

    context 'without artifacts' do
      it 'does not have data entry' do
        expect(subject).not_to include(:codeclimate)
      end
    end
  end

  describe 'merge_request_add_ci_config_path' do
    let!(:project_auto_devops) { create(:project_auto_devops, :disabled, project: project) }

    before do
      project.add_role(user, role)
    end

    context 'when there is a standard ci config file in the source project' do
      let(:role) { :developer }

      before do
        project.repository.create_file(user, Gitlab::FileDetector::PATTERNS[:gitlab_ci], 'CONTENT', message: 'Add .gitlab-ci.yml', branch_name: 'master')
      end

      it 'no ci config path' do
        expect(subject[:merge_request_add_ci_config_path]).to be_nil
      end
    end

    context 'when there is no standard ci config file in the source project' do
      context 'when user has permissions' do
        let(:role) { :developer }

        it 'has add ci config path' do
          expected_path = "/#{resource.project.full_path}/-/new/#{resource.source_branch}"

          expect(subject[:merge_request_add_ci_config_path]).to include(expected_path)
        end

        it 'has expected params' do
          expected_params = {
            commit_message: 'Add .gitlab-ci.yml',
            file_name: '.gitlab-ci.yml',
            suggest_gitlab_ci_yml: 'true',
            mr_path: "/#{resource.project.full_path}/-/merge_requests/#{resource.iid}"
          }.with_indifferent_access

          uri = Addressable::URI.parse(subject[:merge_request_add_ci_config_path])

          expect(uri.query_values).to match(expected_params)
        end

        context 'when auto devops is enabled' do
          before do
            project_auto_devops.enabled = true
          end

          it 'returns a blank ci config path' do
            expect(subject[:merge_request_add_ci_config_path]).to be_nil
          end
        end

        context 'when source project is missing' do
          before do
            resource.source_project = nil
          end

          it 'returns a blank ci config path' do
            expect(subject[:merge_request_add_ci_config_path]).to be_nil
          end
        end

        context 'when there are no commits' do
          before do
            allow(resource).to receive(:commits_count).and_return(0)
          end

          it 'returns a blank ci config path' do
            expect(subject[:merge_request_add_ci_config_path]).to be_nil
          end
        end

        context 'when ci_config_path is customized' do
          it 'has no path if ci_config_path is not set to our default setting' do
            project.ci_config_path = 'not_default'

            expect(subject[:merge_request_add_ci_config_path]).to be_nil
          end

          it 'has a path if ci_config_path unset' do
            expect(subject[:merge_request_add_ci_config_path]).not_to be_nil
          end

          it 'has a path if ci_config_path is an empty string' do
            project.ci_config_path = ''

            expect(subject[:merge_request_add_ci_config_path]).not_to be_nil
          end

          it 'has a path if ci_config_path is set to our default file' do
            project.ci_config_path = Gitlab::FileDetector::PATTERNS[:gitlab_ci]

            expect(subject[:merge_request_add_ci_config_path]).not_to be_nil
          end
        end

        context 'when build feature is disabled' do
          before do
            project.project_feature.update!(builds_access_level: ProjectFeature::DISABLED)
          end

          it 'has no path' do
            expect(subject[:merge_request_add_ci_config_path]).to be_nil
          end
        end

        context 'when creating the pipeline is not allowed' do
          before do
            user.state = 'blocked'
          end

          it 'has no path' do
            expect(subject[:merge_request_add_ci_config_path]).to be_nil
          end
        end

        context 'when merge request is merged' do
          before do
            resource.mark_as_merged!
          end

          it 'returns a blank ci config path' do
            expect(subject[:merge_request_add_ci_config_path]).to be_nil
          end
        end

        context 'when merge request is closed' do
          before do
            resource.close!
          end

          it 'returns a blank ci config path' do
            expect(subject[:merge_request_add_ci_config_path]).to be_nil
          end
        end

        context 'when source branch does not exist' do
          before do
            resource.source_project.repository.rm_branch(user, resource.source_branch)
          end

          it 'returns a blank ci config path' do
            expect(subject[:merge_request_add_ci_config_path]).to be_nil
          end
        end
      end

      context 'when user does not have permissions' do
        let(:role) { :reporter }

        it 'has add ci config path' do
          expect(subject[:merge_request_add_ci_config_path]).to be_nil
        end
      end
    end
  end

  describe 'user callouts' do
    subject { described_class.new(resource, request: request).as_json }

    it 'provides a valid path value for user callout path' do
      expect(subject[:user_callouts_path]).to eq '/-/user_callouts'
    end

    it 'provides a valid value for suggest pipeline feature id' do
      expect(subject[:suggest_pipeline_feature_id]).to eq described_class::SUGGEST_PIPELINE
    end

    it 'provides a valid value for if it is dismissed' do
      expect(subject[:is_dismissed_suggest_pipeline]).to be(false)
    end

    context 'when the suggest pipeline has been dismissed' do
      before do
        create(:user_callout, user: user, feature_name: described_class::SUGGEST_PIPELINE)
      end

      it 'indicates suggest pipeline has been dismissed' do
        expect(subject[:is_dismissed_suggest_pipeline]).to be(true)
      end
    end

    context 'when user is not logged in' do
      let(:request) { double('request', current_user: nil, project: project) }

      it 'returns a blank is dismissed value' do
        expect(subject[:is_dismissed_suggest_pipeline]).to be_nil
      end
    end
  end

  it 'has human access' do
    project.add_maintainer(user)

    expect(subject[:human_access])
      .to eq('Maintainer')
  end

  it 'has new pipeline path for project' do
    project.add_maintainer(user)

    expect(subject[:new_project_pipeline_path])
      .to eq("/#{resource.project.full_path}/-/pipelines/new")
  end

  describe 'when source project is deleted' do
    let(:project) { create(:project, :repository) }
    let(:forked_project) { fork_project(project) }
    let(:merge_request) { create(:merge_request, source_project: forked_project, target_project: project) }

    it 'returns a blank rebase_path' do
      allow(merge_request).to receive(:should_be_rebased?).and_return(true)
      forked_project.destroy!
      merge_request.reload

      entity = described_class.new(merge_request, request: request).as_json

      expect(entity[:rebase_path]).to be_nil
    end
  end

  it 'has security_reports_docs_path' do
    expect(subject[:security_reports_docs_path]).not_to be_nil
  end

  describe 'has source_project_default_url' do
    it 'returns the default url to the source project' do
      expect(subject[:source_project_default_url]).to eq project.http_url_to_repo
    end

    context 'when source project is nil' do
      it 'returns nil' do
        allow(resource).to receive(:source_project).and_return(nil)

        expect(subject[:source_project_default_url]).to be_nil
      end
    end
  end
end