BigW Consortium Gitlab

environments_finder_spec.rb 3.79 KB
Newer Older
Douwe Maan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
require 'spec_helper'

describe EnvironmentsFinder do
  describe '#execute' do
    let(:project) { create(:project, :repository) }
    let(:user) { project.creator }
    let(:environment) { create(:environment, project: project) }

    before do
      project.add_master(user)
    end

    context 'tagged deployment' do
      before do
15
        create(:deployment, environment: environment, ref: 'v1.1.0', tag: true, sha: project.commit.id)
Douwe Maan committed
16 17 18
      end

      it 'returns environment when with_tags is set' do
19
        expect(described_class.new(project, user, ref: 'master', commit: project.commit, with_tags: true).execute)
Douwe Maan committed
20 21 22 23
          .to contain_exactly(environment)
      end

      it 'does not return environment when no with_tags is set' do
24
        expect(described_class.new(project, user, ref: 'master', commit: project.commit).execute)
Douwe Maan committed
25 26 27 28
          .to be_empty
      end

      it 'does not return environment when commit is not part of deployment' do
29
        expect(described_class.new(project, user, ref: 'master', commit: project.commit('feature')).execute)
Douwe Maan committed
30 31 32 33 34 35 36 37 38 39
          .to be_empty
      end
    end

    context 'branch deployment' do
      before do
        create(:deployment, environment: environment, ref: 'master', sha: project.commit.id)
      end

      it 'returns environment when ref is set' do
40
        expect(described_class.new(project, user, ref: 'master', commit: project.commit).execute)
Douwe Maan committed
41 42 43 44
          .to contain_exactly(environment)
      end

      it 'does not environment when ref is different' do
45
        expect(described_class.new(project, user, ref: 'feature', commit: project.commit).execute)
Douwe Maan committed
46 47 48 49
          .to be_empty
      end

      it 'does not return environment when commit is not part of deployment' do
50
        expect(described_class.new(project, user, ref: 'master', commit: project.commit('feature')).execute)
Douwe Maan committed
51 52 53 54
          .to be_empty
      end

      it 'returns environment when commit constraint is not set' do
55
        expect(described_class.new(project, user, ref: 'master').execute)
Douwe Maan committed
56 57 58 59 60 61 62 63 64 65
          .to contain_exactly(environment)
      end
    end

    context 'commit deployment' do
      before do
        create(:deployment, environment: environment, ref: 'master', sha: project.commit.id)
      end

      it 'returns environment' do
66
        expect(described_class.new(project, user, commit: project.commit).execute)
Douwe Maan committed
67 68 69 70 71 72 73 74 75 76 77
          .to contain_exactly(environment)
      end
    end

    context 'recently updated' do
      context 'when last deployment to environment is the most recent one' do
        before do
          create(:deployment, environment: environment, ref: 'feature')
        end

        it 'finds recently updated environment' do
78
          expect(described_class.new(project, user, ref: 'feature', recently_updated: true).execute)
Douwe Maan committed
79 80 81 82 83 84 85 86 87 88 89
            .to contain_exactly(environment)
        end
      end

      context 'when last deployment to environment is not the most recent' do
        before do
          create(:deployment, environment: environment, ref: 'feature')
          create(:deployment, environment: environment, ref: 'master')
        end

        it 'does not find environment' do
90
          expect(described_class.new(project, user, ref: 'feature', recently_updated: true).execute)
Douwe Maan committed
91 92 93 94 95 96 97 98 99 100 101 102 103
            .to be_empty
        end
      end

      context 'when there are two environments that deploy to the same branch' do
        let(:second_environment) { create(:environment, project: project) }

        before do
          create(:deployment, environment: environment, ref: 'feature')
          create(:deployment, environment: second_environment, ref: 'feature')
        end

        it 'finds both environments' do
104
          expect(described_class.new(project, user, ref: 'feature', recently_updated: true).execute)
Douwe Maan committed
105 106 107 108 109 110
            .to contain_exactly(environment, second_environment)
        end
      end
    end
  end
end