2020-06-15 08:08:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-26 05:08:59 -04:00
|
|
|
RSpec.describe GroupDeployKey do
|
2020-06-15 08:08:44 -04:00
|
|
|
it { is_expected.to validate_presence_of(:user) }
|
2020-08-13 23:10:00 -04:00
|
|
|
it { is_expected.to belong_to(:user) }
|
|
|
|
it { is_expected.to have_many(:groups) }
|
|
|
|
|
|
|
|
let_it_be(:group_deploy_key) { create(:group_deploy_key) }
|
|
|
|
let_it_be(:group) { create(:group) }
|
2020-06-15 08:08:44 -04:00
|
|
|
|
|
|
|
it 'is of type DeployKey' do
|
|
|
|
expect(build(:group_deploy_key).type).to eq('DeployKey')
|
|
|
|
end
|
2020-08-13 23:10:00 -04:00
|
|
|
|
|
|
|
describe '#group_deploy_keys_group_for' do
|
|
|
|
subject { group_deploy_key.group_deploy_keys_group_for(group) }
|
|
|
|
|
|
|
|
context 'when this group deploy key is linked to a given group' do
|
|
|
|
it 'returns the relevant group_deploy_keys_group association' do
|
|
|
|
group_deploy_keys_group = create(:group_deploy_keys_group, group: group, group_deploy_key: group_deploy_key)
|
|
|
|
|
|
|
|
expect(subject).to eq(group_deploy_keys_group)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when this group deploy key is not linked to a given group' do
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#can_be_edited_for' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
subject { group_deploy_key.can_be_edited_for?(user, group) }
|
|
|
|
|
|
|
|
context 'when a given user has the :update_group_deploy_key permission for that key' do
|
|
|
|
it 'is true' do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :update_group_deploy_key, group_deploy_key).and_return(true)
|
|
|
|
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a given user does not have the :update_group_deploy_key permission for that key' do
|
|
|
|
before do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :update_group_deploy_key, group_deploy_key).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is true when this user has the :update_group_deploy_key_for_group permission for this group' do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :update_group_deploy_key_for_group, group_deploy_key.group_deploy_keys_group_for(group)).and_return(true)
|
|
|
|
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is false when this user does not have the :update_group_deploy_key_for_group permission for this group' do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :update_group_deploy_key_for_group, group_deploy_key.group_deploy_keys_group_for(group)).and_return(false)
|
|
|
|
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#group_deploy_keys_groups_for_user' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
context 'when a group has a group deploy key' do
|
|
|
|
let_it_be(:expected_association) { create(:group_deploy_keys_group, group: group, group_deploy_key: group_deploy_key) }
|
|
|
|
|
|
|
|
it 'returns the related group_deploy_keys_group association when the user can read the group' do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :read_group, group).and_return(true)
|
|
|
|
|
|
|
|
expect(group_deploy_key.group_deploy_keys_groups_for_user(user))
|
|
|
|
.to contain_exactly(expected_association)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return the related group_deploy_keys_group association when the user cannot read the group' do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :read_group, group).and_return(false)
|
|
|
|
|
|
|
|
expect(group_deploy_key.group_deploy_keys_groups_for_user(user)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-15 08:08:44 -04:00
|
|
|
end
|