debian-mirror-gitlab/spec/models/ci/trigger_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

126 lines
3.5 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2015-09-25 12:07:36 +05:30
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe Ci::Trigger, feature_category: :continuous_integration do
2017-09-10 17:25:29 +05:30
let(:project) { create :project }
2017-08-17 22:00:37 +05:30
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
2015-09-25 12:07:36 +05:30
2020-03-13 15:44:24 +05:30
describe 'validations' do
it { is_expected.to validate_presence_of(:owner) }
end
2015-09-25 12:07:36 +05:30
describe 'before_validation' do
2016-09-13 17:45:13 +05:30
it 'sets an random token if none provided' do
2017-08-17 22:00:37 +05:30
trigger = create(:ci_trigger_without_token, project: project)
2015-09-25 12:07:36 +05:30
expect(trigger.token).not_to be_nil
2022-10-11 01:57:18 +05:30
expect(trigger.token).to start_with(Ci::Trigger::TRIGGER_TOKEN_PREFIX)
2015-09-25 12:07:36 +05:30
end
2017-08-17 22:00:37 +05:30
it 'does not set a random token if one provided' do
trigger = create(:ci_trigger, project: project, token: 'token')
2015-09-25 12:07:36 +05:30
expect(trigger.token).to eq('token')
end
end
2017-08-17 22:00:37 +05:30
describe '#short_token' do
2022-10-11 01:57:18 +05:30
let(:trigger) { create(:ci_trigger) }
2017-08-17 22:00:37 +05:30
subject { trigger.short_token }
2022-10-11 01:57:18 +05:30
it 'returns shortened token without prefix' do
is_expected.not_to start_with(Ci::Trigger::TRIGGER_TOKEN_PREFIX)
end
context 'token does not have a prefix' do
before do
trigger.token = '12345678'
end
it 'returns shortened token' do
is_expected.to eq('1234')
end
2017-08-17 22:00:37 +05:30
end
end
describe '#can_access_project?' do
2019-09-30 21:07:59 +05:30
let(:owner) { create(:user) }
2017-08-17 22:00:37 +05:30
let(:trigger) { create(:ci_trigger, owner: owner, project: project) }
2020-03-13 15:44:24 +05:30
subject { trigger.can_access_project? }
context 'and is member of the project' do
2019-09-30 21:07:59 +05:30
before do
2020-03-13 15:44:24 +05:30
project.add_developer(owner)
2019-09-30 21:07:59 +05:30
end
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
it { is_expected.to eq(true) }
2017-08-17 22:00:37 +05:30
end
2020-03-13 15:44:24 +05:30
context 'and is not member of the project' do
it { is_expected.to eq(false) }
2017-08-17 22:00:37 +05:30
end
end
2021-12-11 22:18:48 +05:30
it_behaves_like 'includes Limitable concern' do
2022-04-04 11:22:00 +05:30
subject { build(:ci_trigger, owner: project.first_owner, project: project) }
end
context 'loose foreign key on ci_triggers.owner_id' do
it_behaves_like 'cleanup by a loose foreign key' do
let!(:parent) { create(:user) }
let!(:model) { create(:ci_trigger, owner: parent) }
end
end
context 'loose foreign key on ci_triggers.project_id' do
it_behaves_like 'cleanup by a loose foreign key' do
let!(:parent) { create(:project) }
let!(:model) { create(:ci_trigger, project: parent) }
end
2021-12-11 22:18:48 +05:30
end
2023-04-23 21:23:45 +05:30
describe 'encrypted_token' do
context 'when token is not provided' do
it 'encrypts the generated token' do
trigger = create(:ci_trigger_without_token, project: project)
expect(trigger.token).not_to be_nil
expect(trigger.encrypted_token).not_to be_nil
expect(trigger.encrypted_token_iv).not_to be_nil
expect(trigger.reload.encrypted_token_tmp).to eq(trigger.token)
end
end
context 'when token is provided' do
it 'encrypts the given token' do
trigger = create(:ci_trigger, project: project)
expect(trigger.token).not_to be_nil
expect(trigger.encrypted_token).not_to be_nil
expect(trigger.encrypted_token_iv).not_to be_nil
expect(trigger.reload.encrypted_token_tmp).to eq(trigger.token)
end
end
context 'when token is being updated' do
it 'encrypts the given token' do
trigger = create(:ci_trigger, project: project, token: "token")
expect { trigger.update!(token: "new token") }
.to change { trigger.encrypted_token }
.and change { trigger.encrypted_token_iv }
.and change { trigger.encrypted_token_tmp }.from("token").to("new token")
end
end
end
2015-09-25 12:07:36 +05:30
end