2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-02-28 13:09:07 -05:00
|
|
|
RSpec.shared_examples 'a deploy token creation service' do
|
2018-03-19 12:11:12 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:deploy_token_params) { attributes_for(:deploy_token) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
2020-02-28 13:09:07 -05:00
|
|
|
subject { described_class.new(entity, user, deploy_token_params).execute }
|
2018-03-19 12:11:12 -04:00
|
|
|
|
|
|
|
context 'when the deploy token is valid' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'creates a new DeployToken' do
|
2018-03-29 22:11:36 -04:00
|
|
|
expect { subject }.to change { DeployToken.count }.by(1)
|
2018-03-19 12:11:12 -04:00
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'creates a new ProjectDeployToken' do
|
2020-02-28 13:09:07 -05:00
|
|
|
expect { subject }.to change { deploy_token_class.count }.by(1)
|
2018-03-29 22:11:36 -04:00
|
|
|
end
|
2018-03-19 12:11:12 -04:00
|
|
|
|
2018-04-05 13:22:34 -04:00
|
|
|
it 'returns a DeployToken' do
|
2020-04-01 08:08:00 -04:00
|
|
|
expect(subject[:deploy_token]).to be_an_instance_of DeployToken
|
2018-03-19 12:11:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-06 12:23:45 -04:00
|
|
|
context 'when expires at date is not passed' do
|
|
|
|
let(:deploy_token_params) { attributes_for(:deploy_token, expires_at: '') }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'sets Forever.date' do
|
2020-04-01 08:08:00 -04:00
|
|
|
expect(subject[:deploy_token].read_attribute(:expires_at)).to eq(Forever.date)
|
2018-04-06 12:23:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-02 14:56:48 -04:00
|
|
|
context 'when username is empty string' do
|
|
|
|
let(:deploy_token_params) { attributes_for(:deploy_token, username: '') }
|
|
|
|
|
|
|
|
it 'converts it to nil' do
|
2020-04-01 08:08:00 -04:00
|
|
|
expect(subject[:deploy_token].read_attribute(:username)).to be_nil
|
2019-07-02 14:56:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when username is provided' do
|
|
|
|
let(:deploy_token_params) { attributes_for(:deploy_token, username: 'deployer') }
|
|
|
|
|
|
|
|
it 'keeps the provided username' do
|
2020-04-01 08:08:00 -04:00
|
|
|
expect(subject[:deploy_token].read_attribute(:username)).to eq('deployer')
|
2019-07-02 14:56:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-19 12:11:12 -04:00
|
|
|
context 'when the deploy token is invalid' do
|
2018-04-05 13:22:34 -04:00
|
|
|
let(:deploy_token_params) { attributes_for(:deploy_token, read_repository: false, read_registry: false) }
|
2018-03-19 12:11:12 -04:00
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'does not create a new DeployToken' do
|
2018-03-29 22:11:36 -04:00
|
|
|
expect { subject }.not_to change { DeployToken.count }
|
2018-03-19 12:11:12 -04:00
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'does not create a new ProjectDeployToken' do
|
2020-02-28 13:09:07 -05:00
|
|
|
expect { subject }.not_to change { deploy_token_class.count }
|
2018-04-05 13:22:34 -04:00
|
|
|
end
|
2018-03-19 12:11:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|