summaryrefslogtreecommitdiff
path: root/spec/services/groups/group_links/destroy_service_spec.rb
blob: 8989f02426201c25b1675a889b56daf20620b2ba (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
# frozen_string_literal: true

require 'spec_helper'

describe Groups::GroupLinks::DestroyService, '#execute' do
  let(:user) { create(:user) }

  let_it_be(:group) { create(:group, :private) }
  let_it_be(:shared_group) { create(:group, :private) }
  let_it_be(:project) { create(:project, group: shared_group) }
  let_it_be(:owner) { create(:user) }

  before do
    group.add_developer(owner)
    shared_group.add_owner(owner)
  end

  subject { described_class.new(shared_group, owner) }

  context 'single link' do
    let!(:link) { create(:group_group_link, shared_group: shared_group, shared_with_group: group) }

    it 'destroys link' do
      expect { subject.execute(link) }.to change { shared_group.shared_with_group_links.count }.from(1).to(0)
    end

    it 'revokes project authorization' do
      group.add_developer(user)

      expect { subject.execute(link) }.to(
        change { Ability.allowed?(user, :read_project, project) }.from(true).to(false))
    end
  end

  context 'multiple links' do
    let_it_be(:another_group) { create(:group, :private) }
    let_it_be(:another_shared_group) { create(:group, :private) }

    let!(:links) do
      [
        create(:group_group_link, shared_group: shared_group, shared_with_group: group),
        create(:group_group_link, shared_group: shared_group, shared_with_group: another_group),
        create(:group_group_link, shared_group: another_shared_group, shared_with_group: group),
        create(:group_group_link, shared_group: another_shared_group, shared_with_group: another_group)
      ]
    end

    it 'updates project authorization once per group' do
      expect(GroupGroupLink).to receive(:delete).and_call_original
      expect(group).to receive(:refresh_members_authorized_projects).once
      expect(another_group).to receive(:refresh_members_authorized_projects).once

      subject.execute(links)
    end
  end
end