2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-19 12:11:12 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-04-06 12:23:45 -04:00
|
|
|
describe DeployTokens::CreateService do
|
2018-03-19 12:11:12 -04:00
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:deploy_token_params) { attributes_for(:deploy_token) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
2018-03-29 22:11:36 -04:00
|
|
|
subject { described_class.new(project, 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
|
2018-04-05 13:22:34 -04:00
|
|
|
expect { subject }.to change { ProjectDeployToken.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
|
|
|
|
expect(subject).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
|
2018-04-06 15:48:17 -04:00
|
|
|
expect(subject.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
|
|
|
|
expect(subject.read_attribute(:username)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when username is provided' do
|
|
|
|
let(:deploy_token_params) { attributes_for(:deploy_token, username: 'deployer') }
|
|
|
|
|
|
|
|
it 'keeps the provided username' do
|
|
|
|
expect(subject.read_attribute(:username)).to eq('deployer')
|
|
|
|
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
|
2018-04-05 13:22:34 -04:00
|
|
|
expect { subject }.not_to change { ProjectDeployToken.count }
|
|
|
|
end
|
2018-03-19 12:11:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|