environment_spec.rb 2.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
require 'spec_helper'

describe Environment, models: true do
  let(:environment) { create(:environment) }

  it { is_expected.to belong_to(:project) }
  it { is_expected.to have_many(:deployments) }

  it { is_expected.to delegate_method(:last_deployment).to(:deployments).as(:last) }

  it { is_expected.to validate_presence_of(:name) }
  it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) }
  it { is_expected.to validate_length_of(:name).is_within(0..255) }
14 15 16 17 18 19 20 21 22 23

  it { is_expected.to validate_length_of(:external_url).is_within(0..255) }

  # To circumvent a not null violation of the name column:
  # https://github.com/thoughtbot/shoulda-matchers/issues/336
  it 'validates uniqueness of :external_url' do
    create(:environment)

    is_expected.to validate_uniqueness_of(:external_url).scoped_to(:project_id)
  end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
24 25 26 27 28 29

  describe '#nullify_external_url' do
    it 'replaces a blank url with nil' do
      env = build(:environment, external_url: "")

      expect(env.save).to be true
30
      expect(env.external_url).to be_nil
Z.J. van de Weg's avatar
Z.J. van de Weg committed
31 32
    end
  end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
33

34
  describe '#includes_commit?' do
Z.J. van de Weg's avatar
Z.J. van de Weg committed
35 36
    context 'without a last deployment' do
      it "returns false" do
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
        expect(environment.includes_commit?('HEAD')).to be false
      end
    end

    context 'with a last deployment' do
      let(:project)     { create(:project) }
      let(:environment) { create(:environment, project: project) }

      let!(:deployment) do
        create(:deployment, environment: environment, sha: project.commit('master').id)
      end

      context 'in the same branch' do
        it 'returns true' do
          expect(environment.includes_commit?(RepoHelpers.sample_commit)).to be true
        end
      end

      context 'not in the same branch' do
        before do
          deployment.update(sha: project.commit('feature').id)
        end

        it 'returns false' do
          expect(environment.includes_commit?(RepoHelpers.sample_commit)).to be false
        end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
63 64 65
      end
    end
  end
66 67 68 69 70

  describe '#environment_type' do
    subject { environment.environment_type }

    it 'sets a environment type if name has multiple segments' do
71
      environment.update!(name: 'production/worker.gitlab.com')
72 73 74 75 76

      is_expected.to eq('production')
    end

    it 'nullifies a type if it\'s a simple name' do
77
      environment.update!(name: 'production')
78 79 80 81

      is_expected.to be_nil
    end
  end
82
end