debian-mirror-gitlab/spec/models/group_deploy_key_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
3.9 KiB
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe GroupDeployKey do
2020-06-23 00:09:42 +05:30
it { is_expected.to validate_presence_of(:user) }
2020-10-24 23:57:45 +05:30
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-23 00:09:42 +05:30
it 'is of type DeployKey' do
expect(build(:group_deploy_key).type).to eq('DeployKey')
end
2020-10-24 23:57:45 +05:30
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-11-24 15:15:51 +05:30
describe '.for_groups' do
context 'when group deploy keys are enabled for some groups' do
let_it_be(:group1) { create(:group) }
let_it_be(:group2) { create(:group) }
let_it_be(:group3) { create(:group) }
let_it_be(:gdk1) { create(:group_deploy_key) }
let_it_be(:gdk2) { create(:group_deploy_key) }
let_it_be(:gdk3) { create(:group_deploy_key) }
it 'returns these group deploy keys' do
gdk1.groups << group1
gdk1.groups << group2
gdk2.groups << group3
gdk3.groups << group2
expect(described_class.for_groups([group1.id, group3.id])).to contain_exactly(gdk1, gdk2)
expect(described_class.for_groups([group2.id])).to contain_exactly(gdk1, gdk3)
end
end
end
2020-06-23 00:09:42 +05:30
end