summaryrefslogtreecommitdiff
path: root/spec/presenters/ci/pipeline_presenter_spec.rb
blob: 18f79bc930cc1d93bacc5519c46c3339d121be34 (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
358
359
360
361
362
363
364
365
366
367
368
369
370
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::PipelinePresenter do
  include Gitlab::Routing

  let(:user) { create(:user) }
  let(:current_user) { user }
  let(:project) { create(:project, :test_repo) }
  let(:pipeline) { create(:ci_pipeline, project: project) }

  subject(:presenter) do
    described_class.new(pipeline)
  end

  before do
    project.add_developer(user)
    allow(presenter).to receive(:current_user) { current_user }
  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 pipeline and optional params' do
      expect { presenter }.not_to raise_error
    end

    it 'exposes pipeline' do
      expect(presenter.pipeline).to eq(pipeline)
    end

    it 'forwards missing methods to pipeline' do
      expect(presenter.ref).to eq(pipeline.ref)
    end
  end

  describe '#status_title' do
    context 'when pipeline is auto-canceled' do
      before do
        expect(pipeline).to receive(:auto_canceled?).and_return(true)
        expect(pipeline).to receive(:auto_canceled_by_id).and_return(1)
      end

      it 'shows that the pipeline is auto-canceled' do
        status_title = presenter.status_title

        expect(status_title).to include('auto-canceled')
        expect(status_title).to include('Pipeline #1')
      end
    end

    context 'when pipeline is not auto-canceled' do
      before do
        expect(pipeline).to receive(:auto_canceled?).and_return(false)
      end

      it 'does not have a status title' do
        expect(presenter.status_title).to be_nil
      end
    end
  end

  describe '#failure_reason' do
    context 'when pipeline has a failure reason' do
      Enums::Ci::Pipeline.failure_reasons.keys.each do |failure_reason|
        context "when failure reason is #{failure_reason}" do
          before do
            pipeline.failure_reason = failure_reason
          end

          it 'represents a failure reason sentence' do
            expect(presenter.failure_reason).to be_an_instance_of(String)
            expect(presenter.failure_reason).not_to eq(failure_reason.to_s)
          end
        end
      end
    end

    context 'when pipeline does not have failure reason' do
      it 'returns nil' do
        expect(presenter.failure_reason).to be_nil
      end
    end
  end

  describe '#name' do
    before do
      allow(pipeline).to receive(:merge_request_event_type) { event_type }
    end

    subject { presenter.name }

    context 'for a detached merge request pipeline' do
      let(:event_type) { :detached }

      it { is_expected.to eq('Detached merge request pipeline') }
    end

    context 'for a merged result pipeline' do
      let(:event_type) { :merged_result }

      it { is_expected.to eq('Merged result pipeline') }
    end

    context 'for a merge train pipeline' do
      let(:event_type) { :merge_train }

      it { is_expected.to eq('Merge train pipeline') }
    end

    context 'when pipeline is branch pipeline' do
      let(:event_type) { nil }

      it { is_expected.to eq('Pipeline') }
    end
  end

  describe '#ref_text' do
    subject { presenter.ref_text }

    context 'when pipeline is detached merge request pipeline' do
      let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) }
      let(:pipeline) { merge_request.all_pipelines.last }

      it 'returns a correct ref text' do
        is_expected.to eq("for <a class=\"mr-iid\" href=\"#{project_merge_request_path(merge_request.project, merge_request)}\">#{merge_request.to_reference}</a> " \
                          "with <a class=\"ref-name\" href=\"#{project_commits_path(merge_request.source_project, merge_request.source_branch)}\">#{merge_request.source_branch}</a>")
      end
    end

    context 'when pipeline is merge request pipeline' do
      let(:merge_request) { create(:merge_request, :with_merge_request_pipeline) }
      let(:pipeline) { merge_request.all_pipelines.last }

      it 'returns a correct ref text' do
        is_expected.to eq("for <a class=\"mr-iid\" href=\"#{project_merge_request_path(merge_request.project, merge_request)}\">#{merge_request.to_reference}</a> " \
                          "with <a class=\"ref-name\" href=\"#{project_commits_path(merge_request.source_project, merge_request.source_branch)}\">#{merge_request.source_branch}</a> " \
                          "into <a class=\"ref-name\" href=\"#{project_commits_path(merge_request.target_project, merge_request.target_branch)}\">#{merge_request.target_branch}</a>")
      end
    end

    context 'when pipeline is branch pipeline' do
      context 'when ref exists in the repository' do
        before do
          allow(pipeline).to receive(:ref_exists?) { true }
        end

        it 'returns a correct ref text' do
          is_expected.to eq("for <a class=\"ref-name\" href=\"#{project_commits_path(pipeline.project, pipeline.ref)}\">#{pipeline.ref}</a>")
        end

        context 'when ref contains malicious script' do
          let(:pipeline) { create(:ci_pipeline, ref: "<script>alter('1')</script>", project: project) }

          it 'does not include the malicious script' do
            is_expected.not_to include("<script>alter('1')</script>")
          end
        end
      end

      context 'when ref does not exist in the repository' do
        before do
          allow(pipeline).to receive(:ref_exists?) { false }
        end

        it 'returns a correct ref text' do
          is_expected.to eq("for <span class=\"ref-name\">#{pipeline.ref}</span>")
        end

        context 'when ref contains malicious script' do
          let(:pipeline) { create(:ci_pipeline, ref: "<script>alter('1')</script>", project: project) }

          it 'does not include the malicious script' do
            is_expected.not_to include("<script>alter('1')</script>")
          end
        end
      end
    end
  end

  describe '#all_related_merge_request_text' do
    subject { presenter.all_related_merge_request_text }

    let(:mr_1) { create(:merge_request) }
    let(:mr_2) { create(:merge_request) }

    context 'with zero related merge requests (branch pipeline)' do
      it { is_expected.to eq('No related merge requests found.') }
    end

    context 'with one related merge request' do
      before do
        allow(pipeline).to receive(:all_merge_requests).and_return(MergeRequest.where(id: mr_1.id))
      end

      it {
        is_expected.to eq("1 related merge request: " \
          "<a class=\"mr-iid\" href=\"#{merge_request_path(mr_1)}\">#{mr_1.to_reference} #{mr_1.title}</a>")
      }
    end

    context 'with two related merge requests' do
      before do
        allow(pipeline).to receive(:all_merge_requests).and_return(MergeRequest.where(id: [mr_1.id, mr_2.id]))
      end

      it {
        is_expected.to eq("2 related merge requests: " \
          "<a class=\"mr-iid\" href=\"#{merge_request_path(mr_2)}\">#{mr_2.to_reference} #{mr_2.title}</a>, " \
          "<a class=\"mr-iid\" href=\"#{merge_request_path(mr_1)}\">#{mr_1.to_reference} #{mr_1.title}</a>")
      }

      context 'with a limit passed' do
        subject { presenter.all_related_merge_request_text(limit: 1) }

        it {
          is_expected.to eq("2 related merge requests: " \
          "<a class=\"mr-iid\" href=\"#{merge_request_path(mr_2)}\">#{mr_2.to_reference} #{mr_2.title}</a>")
        }
      end
    end
  end

  describe '#all_related_merge_requests' do
    subject(:all_related_merge_requests) do
      presenter.send(:all_related_merge_requests)
    end

    it 'memoizes the returned relation' do
      expect(pipeline).to receive(:all_merge_requests_by_recency).exactly(1).time.and_call_original
      2.times { presenter.send(:all_related_merge_requests).count }
    end

    context 'for a branch pipeline with two open MRs' do
      let!(:one) { create(:merge_request, source_project: project, source_branch: pipeline.ref) }
      let!(:two) { create(:merge_request, source_project: project, source_branch: pipeline.ref, target_branch: 'fix') }

      it { is_expected.to contain_exactly(one, two) }
    end

    context 'permissions' do
      let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
      let(:pipeline) { merge_request.all_pipelines.take }

      shared_examples 'private merge requests' do
        context 'when not logged in' do
          let(:current_user) {}

          it { is_expected.to be_empty }
        end

        context 'when logged in as a non_member' do
          let(:current_user) { create(:user) }

          it { is_expected.to be_empty }
        end

        context 'when logged in as a guest' do
          let(:current_user) { create(:user) }

          before do
            project.add_guest(current_user)
          end

          it { is_expected.to be_empty }
        end

        context 'when logged in as a developer' do
          it { is_expected.to contain_exactly(merge_request) }
        end

        context 'when logged in as a maintainer' do
          let(:current_user) { create(:user) }

          before do
            project.add_maintainer(current_user)
          end

          it { is_expected.to contain_exactly(merge_request) }
        end
      end

      context 'with a private project' do
        it_behaves_like 'private merge requests'
      end

      context 'with a public project with private merge requests' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)

          project
            .project_feature
            .update!(merge_requests_access_level: ProjectFeature::PRIVATE)
        end

        it_behaves_like 'private merge requests'
      end

      context 'with a public project with public merge requests' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)

          project
            .project_feature
            .update!(merge_requests_access_level: ProjectFeature::ENABLED)
        end

        context 'when not logged in' do
          let(:current_user) {}

          it { is_expected.to contain_exactly(merge_request) }
        end
      end
    end
  end

  describe '#link_to_merge_request' do
    subject { presenter.link_to_merge_request }

    context 'with a related merge request' do
      let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
      let(:pipeline) { merge_request.all_pipelines.take }

      it 'returns a correct link' do
        is_expected.to include(project_merge_request_path(project, merge_request))
      end
    end

    context 'when pipeline is branch pipeline' do
      it { is_expected.to be_nil }
    end
  end

  describe '#link_to_merge_request_source_branch' do
    subject { presenter.link_to_merge_request_source_branch }

    context 'with a related merge request' do
      let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
      let(:pipeline) { merge_request.all_pipelines.take }

      it 'returns a correct link' do
        is_expected.to include(project_commits_path(project, merge_request.source_branch))
      end
    end

    context 'when pipeline is branch pipeline' do
      it { is_expected.to be_nil }
    end
  end

  describe '#link_to_merge_request_target_branch' do
    subject { presenter.link_to_merge_request_target_branch }

    context 'with a related merge request' do
      let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
      let(:pipeline) { merge_request.all_pipelines.take }

      it 'returns a correct link' do
        is_expected.to include(project_commits_path(project, merge_request.target_branch))
      end
    end

    context 'when pipeline is branch pipeline' do
      it { is_expected.to be_nil }
    end
  end
end