summaryrefslogtreecommitdiff
path: root/spec/features/projects/badges/pipeline_badge_spec.rb
blob: 9d8f9872a1aae851f5ce1d94ac70c8396e5002c4 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Pipeline Badge' do
  let_it_be(:project) { create(:project, :repository, :public) }

  let(:ref) { project.default_branch }

  context 'when the project has a pipeline' do
    let!(:pipeline) { create(:ci_empty_pipeline, project: project, ref: ref, sha: project.commit(ref).sha) }
    let!(:job) { create(:ci_build, pipeline: pipeline) }

    context 'when the pipeline was successful' do
      it 'displays so on the badge', :sidekiq_might_not_need_inline do
        job.success

        visit pipeline_project_badges_path(project, ref: ref, format: :svg)

        expect(page.status_code).to eq(200)
        expect_badge('passed')
      end
    end

    context 'when the pipeline failed' do
      it 'shows displays so on the badge', :sidekiq_might_not_need_inline do
        job.drop

        visit pipeline_project_badges_path(project, ref: ref, format: :svg)

        expect(page.status_code).to eq(200)
        expect_badge('failed')
      end
    end

    context 'when the pipeline is preparing' do
      let!(:job) { create(:ci_build, status: 'created', pipeline: pipeline) }

      before do
        # Prevent skipping directly to 'pending'
        allow(Ci::BuildPrepareWorker).to receive(:perform_async)
        allow(job).to receive(:prerequisites).and_return([double])
      end

      it 'displays the preparing badge', :sidekiq_might_not_need_inline do
        job.enqueue

        visit pipeline_project_badges_path(project, ref: ref, format: :svg)

        expect(page.status_code).to eq(200)
        expect_badge('preparing')
      end
    end

    context 'when the pipeline is running' do
      it 'shows displays so on the badge', :sidekiq_might_not_need_inline do
        create(:ci_build, pipeline: pipeline, name: 'second build', status_event: 'run')

        visit pipeline_project_badges_path(project, ref: ref, format: :svg)

        expect(page.status_code).to eq(200)
        expect_badge('running')
      end
    end

    context 'when a new pipeline is created' do
      it 'shows a fresh badge' do
        visit pipeline_project_badges_path(project, ref: ref, format: :svg)

        expect(page.status_code).to eq(200)
        expect(page.response_headers['Cache-Control']).to eq('no-store')
      end
    end

    def expect_badge(status)
      svg = Nokogiri::XML.parse(page.body)
      expect(page.response_headers['Content-Type']).to include('image/svg+xml')
      expect(svg.at(%Q{text:contains("#{status}")})).to be_truthy
    end
  end
end