summaryrefslogtreecommitdiff
path: root/spec/models/group_deploy_key_spec.rb
blob: dfb4fee593f9ea273c3bd2d0289c4934736eb5f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe GroupDeployKey do
  it { is_expected.to validate_presence_of(:user) }
  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) }

  it 'is of type DeployKey' do
    expect(build(:group_deploy_key).type).to eq('DeployKey')
  end

  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

  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
end