2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-19 12:11:12 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe DeployToken do
|
2018-04-05 13:22:34 -04:00
|
|
|
subject(:deploy_token) { create(:deploy_token) }
|
2018-03-29 18:56:35 -04:00
|
|
|
|
2018-04-05 13:22:34 -04:00
|
|
|
it { is_expected.to have_many :project_deploy_tokens }
|
|
|
|
it { is_expected.to have_many(:projects).through(:project_deploy_tokens) }
|
2018-03-19 12:11:12 -04:00
|
|
|
|
2018-04-05 13:22:34 -04:00
|
|
|
describe '#ensure_token' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'ensures a token' do
|
2018-04-05 13:22:34 -04:00
|
|
|
deploy_token.token = nil
|
|
|
|
deploy_token.save
|
|
|
|
|
|
|
|
expect(deploy_token.token).not_to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ensure_at_least_one_scope' do
|
|
|
|
context 'with at least one scope' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'is valid' do
|
2018-04-05 13:22:34 -04:00
|
|
|
is_expected.to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no scopes' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'is invalid' do
|
2018-04-05 13:22:34 -04:00
|
|
|
deploy_token = build(:deploy_token, read_repository: false, read_registry: false)
|
2018-03-19 12:11:12 -04:00
|
|
|
|
2018-03-29 18:56:35 -04:00
|
|
|
expect(deploy_token).not_to be_valid
|
2018-04-05 13:22:34 -04:00
|
|
|
expect(deploy_token.errors[:base].first).to eq("Scopes can't be blank")
|
2018-03-19 12:11:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-05 13:22:34 -04:00
|
|
|
describe '#scopes' do
|
|
|
|
context 'with all the scopes' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns scopes assigned to DeployToken' do
|
2018-04-05 13:22:34 -04:00
|
|
|
expect(deploy_token.scopes).to eq([:read_repository, :read_registry])
|
|
|
|
end
|
|
|
|
end
|
2018-03-19 12:11:12 -04:00
|
|
|
|
2018-04-05 13:22:34 -04:00
|
|
|
context 'with only one scope' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns scopes assigned to DeployToken' do
|
2018-04-05 13:22:34 -04:00
|
|
|
deploy_token = create(:deploy_token, read_registry: false)
|
|
|
|
expect(deploy_token.scopes).to eq([:read_repository])
|
|
|
|
end
|
2018-03-19 12:11:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#revoke!' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'updates revoke attribute' do
|
2018-03-29 18:56:35 -04:00
|
|
|
deploy_token.revoke!
|
|
|
|
expect(deploy_token.revoked?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#active?" do
|
|
|
|
context "when it has been revoked" do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns false' do
|
2018-03-29 18:56:35 -04:00
|
|
|
deploy_token.revoke!
|
|
|
|
expect(deploy_token.active?).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-23 05:23:08 -04:00
|
|
|
context "when it hasn't been revoked and is not expired" do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns true' do
|
2018-03-29 18:56:35 -04:00
|
|
|
expect(deploy_token.active?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2018-07-23 05:23:08 -04:00
|
|
|
|
|
|
|
context "when it hasn't been revoked and is expired" do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns true' do
|
2018-07-23 05:23:08 -04:00
|
|
|
deploy_token.update_attribute(:expires_at, Date.today - 5.days)
|
|
|
|
expect(deploy_token.active?).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
2018-08-02 13:43:36 -04:00
|
|
|
|
|
|
|
context "when it hasn't been revoked and has no expiry" do
|
|
|
|
let(:deploy_token) { create(:deploy_token, expires_at: nil) }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns true' do
|
2018-08-02 13:43:36 -04:00
|
|
|
expect(deploy_token.active?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2018-03-29 18:56:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#username' do
|
2018-04-05 23:02:13 -04:00
|
|
|
it 'returns a harcoded username' do
|
2018-04-05 13:22:34 -04:00
|
|
|
expect(deploy_token.username).to eq("gitlab+deploy-token-#{deploy_token.id}")
|
2018-03-19 12:11:12 -04:00
|
|
|
end
|
|
|
|
end
|
2018-04-05 23:02:13 -04:00
|
|
|
|
|
|
|
describe '#has_access_to?' do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
|
2018-04-10 03:31:30 -04:00
|
|
|
subject { deploy_token.has_access_to?(project) }
|
2018-04-05 23:02:13 -04:00
|
|
|
|
2018-04-10 03:31:30 -04:00
|
|
|
context 'when deploy token is active and related to project' do
|
|
|
|
let(:deploy_token) { create(:deploy_token, projects: [project]) }
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
2018-04-05 23:02:13 -04:00
|
|
|
end
|
|
|
|
|
2018-04-10 03:31:30 -04:00
|
|
|
context 'when deploy token is active but not related to project' do
|
|
|
|
let(:deploy_token) { create(:deploy_token) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when deploy token is revoked and related to project' do
|
|
|
|
let(:deploy_token) { create(:deploy_token, :revoked, projects: [project]) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when deploy token is revoked and not related to the project' do
|
|
|
|
let(:deploy_token) { create(:deploy_token, :revoked) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsy }
|
2018-04-05 23:02:13 -04:00
|
|
|
end
|
|
|
|
end
|
2018-04-06 15:48:17 -04:00
|
|
|
|
|
|
|
describe '#expires_at' do
|
|
|
|
context 'when using Forever.date' do
|
|
|
|
let(:deploy_token) { create(:deploy_token, expires_at: nil) }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns nil' do
|
2018-04-06 15:48:17 -04:00
|
|
|
expect(deploy_token.expires_at).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using a personalized date' do
|
|
|
|
let(:expires_at) { Date.today + 5.months }
|
|
|
|
let(:deploy_token) { create(:deploy_token, expires_at: expires_at) }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns the personalized date' do
|
2018-04-06 15:48:17 -04:00
|
|
|
expect(deploy_token.expires_at).to eq(expires_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#expires_at=' do
|
|
|
|
context 'when passing nil' do
|
|
|
|
let(:deploy_token) { create(:deploy_token, expires_at: nil) }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'assigns Forever.date' do
|
2018-04-06 15:48:17 -04:00
|
|
|
expect(deploy_token.read_attribute(:expires_at)).to eq(Forever.date)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when passign a value' do
|
|
|
|
let(:expires_at) { Date.today + 5.months }
|
|
|
|
let(:deploy_token) { create(:deploy_token, expires_at: expires_at) }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'respects the value' do
|
2018-04-06 15:48:17 -04:00
|
|
|
expect(deploy_token.read_attribute(:expires_at)).to eq(expires_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-20 11:05:16 -04:00
|
|
|
|
|
|
|
describe '.gitlab_deploy_token' do
|
|
|
|
let(:project) { create(:project ) }
|
|
|
|
|
|
|
|
subject { project.deploy_tokens.gitlab_deploy_token }
|
|
|
|
|
|
|
|
context 'with a gitlab deploy token associated' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns the gitlab deploy token' do
|
2018-04-20 11:05:16 -04:00
|
|
|
deploy_token = create(:deploy_token, :gitlab_deploy_token, projects: [project])
|
|
|
|
is_expected.to eq(deploy_token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no gitlab deploy token associated' do
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'returns nil' do
|
2018-04-20 11:05:16 -04:00
|
|
|
is_expected.to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-19 12:11:12 -04:00
|
|
|
end
|