commit_statuses_finder_spec.rb 5.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::CommitStatusesFinder, '#execute' do
  let_it_be(:project) { create(:project, :public, :repository) }
  let_it_be(:release) { create(:release, project: project) }
  let_it_be(:user) { create(:user) }

  context 'tag refs' do
11
    let_it_be(:tags) { project.repository.tags }
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
    let(:subject) { described_class.new(project, project.repository, user, tags).execute }

    context 'no pipelines' do
      it 'returns nil' do
        expect(subject).to be_blank
      end
    end

    context 'when multiple tags exist' do
      before do
        create(:ci_pipeline,
          project: project,
          ref: 'v1.1.0',
          sha: project.commit('v1.1.0').sha,
          status: :running)
        create(:ci_pipeline,
          project: project,
          ref: 'v1.0.0',
          sha: project.commit('v1.0.0').sha,
          status: :success)
      end

      it 'all relevant commit statuses are received' do
        expect(subject['v1.1.0'].group).to eq("running")
        expect(subject['v1.0.0'].group).to eq("success")
      end
    end

    context 'when a tag has multiple pipelines' do
      before do
        create(:ci_pipeline,
          project: project,
          ref: 'v1.0.0',
          sha: project.commit('v1.0.0').sha,
          status: :running,
          created_at: 6.months.ago)
        create(:ci_pipeline,
          project: project,
          ref: 'v1.0.0',
          sha: project.commit('v1.0.0').sha,
          status: :success,
          created_at: 2.months.ago)
      end

      it 'chooses the latest to determine status' do
        expect(subject['v1.0.0'].group).to eq("success")
      end
    end
  end

  context 'branch refs' do
    let(:subject) { described_class.new(project, project.repository, user, branches).execute }

    before do
      project.add_developer(user)
    end

    context 'no pipelines' do
      let(:branches) { BranchesFinder.new(project.repository, {}).execute }

      it 'returns nil' do
        expect(subject).to be_blank
      end
    end

    context 'when a branch has multiple pipelines' do
      let(:branches) { BranchesFinder.new(project.repository, {}).execute }

      before do
        sha = project.repository.create_file(user, generate(:branch), 'content', message: 'message', branch_name: 'master')
        create(:ci_pipeline,
          project: project,
          user: user,
          ref: "master",
          sha: sha,
          status: :running,
          created_at: 6.months.ago)
        create(:ci_pipeline,
          project: project,
          user: user,
          ref: "master",
          sha: sha,
          status: :success,
          created_at: 2.months.ago)
      end

      it 'chooses the latest to determine status' do
        expect(subject["master"].group).to eq("success")
      end
    end

    context 'when multiple branches exist' do
      let(:branches) { BranchesFinder.new(project.repository, {}).execute }

      before do
        master_sha = project.repository.create_file(user, generate(:branch), 'content', message: 'message', branch_name: 'master')
        create(:ci_pipeline,
          project: project,
          user: user,
          ref: "master",
          sha: master_sha,
          status: :running,
          created_at: 6.months.ago)
        test_sha = project.repository.create_file(user, generate(:branch), 'content', message: 'message', branch_name: 'test')
        create(:ci_pipeline,
          project: project,
          user: user,
          ref: "test",
          sha: test_sha,
          status: :success,
          created_at: 2.months.ago)
      end

      it 'all relevant commit statuses are received' do
        expect(subject["master"].group).to eq("running")
        expect(subject["test"].group).to eq("success")
      end
    end
  end

  context 'CI pipelines visible to' do
134
    let_it_be(:tags) { project.repository.tags }
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    let(:subject) { described_class.new(project, project.repository, user, tags).execute }

    before do
      create(:ci_pipeline,
        project: project,
        ref: 'v1.1.0',
        sha: project.commit('v1.1.0').sha,
        status: :running)
    end

    context 'everyone' do
      it 'returns something' do
        expect(subject).not_to be_blank
      end
    end

    context 'project members only' do
      before do
        project.project_feature.update!(builds_access_level: ProjectFeature::PRIVATE)
      end

157 158
      it 'returns a blank hash' do
        expect(subject).to eq({})
159 160 161 162 163
      end
    end

    context 'when not a member of a private project' do
      let(:private_project) { create(:project, :private, :repository) }
164
      let(:private_tags) { private_tags.repository.tags }
165 166 167 168 169 170 171 172 173 174
      let(:private_subject) { described_class.new(private_project, private_project.repository, user, tags).execute }

      before do
        create(:ci_pipeline,
          project: private_project,
          ref: 'v1.1.0',
          sha: private_project.commit('v1.1.0').sha,
          status: :running)
      end

175 176
      it 'returns a blank hash' do
        expect(private_subject).to eq({})
177 178 179 180
      end
    end
  end
end