BigW Consortium Gitlab

trigger_spec.rb 1.89 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe Ci::Trigger, models: true do
4 5 6 7 8 9 10
  let(:project) { create :empty_project }

  describe 'associations' do
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:owner) }
    it { is_expected.to have_many(:trigger_requests) }
  end
11 12

  describe 'before_validation' do
13
    it 'sets an random token if none provided' do
14 15
      trigger = create(:ci_trigger_without_token, project: project)

Dmitriy Zaporozhets committed
16
      expect(trigger.token).not_to be_nil
17 18
    end

19 20
    it 'does not set a random token if one provided' do
      trigger = create(:ci_trigger, project: project, token: 'token')
21

Dmitriy Zaporozhets committed
22
      expect(trigger.token).to eq('token')
23 24
    end
  end
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

  describe '#short_token' do
    let(:trigger) { create(:ci_trigger, token: '12345678') }

    subject { trigger.short_token }

    it 'returns shortened token' do
      is_expected.to eq('1234')
    end
  end

  describe '#legacy?' do
    let(:trigger) { create(:ci_trigger, owner: owner, project: project) }

    subject { trigger }

    context 'when owner is blank' do
      let(:owner) { nil }

      it { is_expected.to be_legacy }
    end

    context 'when owner is set' do
      let(:owner) { create(:user) }

      it { is_expected.not_to be_legacy }
    end
  end

  describe '#can_access_project?' do
    let(:trigger) { create(:ci_trigger, owner: owner, project: project) }

    context 'when owner is blank' do
      let(:owner) { nil }

      subject { trigger.can_access_project? }

      it { is_expected.to eq(true) }
    end

    context 'when owner is set' do
      let(:owner) { create(:user) }

      subject { trigger.can_access_project? }

      context 'and is member of the project' do
        before do
          project.team << [owner, :developer]
        end

        it { is_expected.to eq(true) }
      end

      context 'and is not member of the project' do
        it { is_expected.to eq(false) }
      end
    end
  end
83
end