2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
RSpec.shared_examples 'a deploy token creation service' do
|
2018-05-09 12:01:36 +05:30
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:deploy_token_params) { attributes_for(:deploy_token) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
2020-04-08 14:13:33 +05:30
|
|
|
subject { described_class.new(entity, user, deploy_token_params).execute }
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
context 'when the deploy token is valid' do
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'creates a new DeployToken' do
|
2018-05-09 12:01:36 +05:30
|
|
|
expect { subject }.to change { DeployToken.count }.by(1)
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'creates a new ProjectDeployToken' do
|
2020-04-08 14:13:33 +05:30
|
|
|
expect { subject }.to change { deploy_token_class.count }.by(1)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a DeployToken' do
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(subject[:deploy_token]).to be_an_instance_of DeployToken
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when expires at date is not passed' do
|
|
|
|
let(:deploy_token_params) { attributes_for(:deploy_token, expires_at: '') }
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'sets Forever.date' do
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(subject[:deploy_token].read_attribute(:expires_at)).to eq(Forever.date)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
context 'when username is empty string' do
|
|
|
|
let(:deploy_token_params) { attributes_for(:deploy_token, username: '') }
|
|
|
|
|
|
|
|
it 'converts it to nil' do
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(subject[:deploy_token].read_attribute(:username)).to be_nil
|
2019-09-30 21:07:59 +05:30
|
|
|
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-22 19:07:51 +05:30
|
|
|
expect(subject[:deploy_token].read_attribute(:username)).to eq('deployer')
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
context 'when the deploy token is invalid' do
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:deploy_token_params) { attributes_for(:deploy_token, read_repository: false, read_registry: false, write_registry: false) }
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'does not create a new DeployToken' do
|
2018-05-09 12:01:36 +05:30
|
|
|
expect { subject }.not_to change { DeployToken.count }
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'does not create a new ProjectDeployToken' do
|
2020-04-08 14:13:33 +05:30
|
|
|
expect { subject }.not_to change { deploy_token_class.count }
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
RSpec.shared_examples 'a deploy token deletion service' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:deploy_token_params) { { token_id: deploy_token.id } }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
subject { described_class.new(entity, user, deploy_token_params).execute }
|
|
|
|
|
|
|
|
it "destroys a token record and it's associated DeployToken" do
|
|
|
|
expect { subject }.to change { deploy_token_class.count }.by(-1)
|
|
|
|
.and change { DeployToken.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'invalid token id' do
|
|
|
|
let(:deploy_token_params) { { token_id: 9999 } }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|