summaryrefslogtreecommitdiff
path: root/spec/services/projects/group_links/create_service_spec.rb
blob: 9bc780fe17726f8af98d44f91234dc965ef9172f (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::GroupLinks::CreateService, '#execute' do
  let_it_be(:user) { create :user }
  let_it_be(:group) { create :group }
  let_it_be(:project) { create :project }
  let(:group_access) { Gitlab::Access::DEVELOPER }
  let(:opts) do
    {
      link_group_access: group_access,
      expires_at: nil
    }
  end

  subject { described_class.new(project, user, opts) }

  before do
    group.add_developer(user)
  end

  it 'adds group to project' do
    expect { subject.execute(group) }.to change { project.project_group_links.count }.from(0).to(1)
  end

  it 'updates authorization', :sidekiq_inline do
    expect { subject.execute(group) }.to(
      change { Ability.allowed?(user, :read_project, project) }
        .from(false).to(true))
  end

  it 'returns false if group is blank' do
    expect { subject.execute(nil) }.not_to change { project.project_group_links.count }
  end

  it 'returns error if user is not allowed to share with a group' do
    expect { subject.execute(create(:group)) }.not_to change { project.project_group_links.count }
  end

  context 'with specialized project_authorization workers' do
    let_it_be(:other_user) { create(:user) }

    before do
      group.add_developer(other_user)
    end

    it 'schedules authorization update for users with access to group' do
      expect(AuthorizedProjectsWorker).not_to(
        receive(:bulk_perform_async)
      )
      expect(AuthorizedProjectUpdate::ProjectGroupLinkCreateWorker).to(
        receive(:perform_async)
          .with(project.id, group.id, group_access)
          .and_call_original
      )
      expect(AuthorizedProjectUpdate::UserRefreshFromReplicaWorker).to(
        receive(:bulk_perform_in)
          .with(1.hour,
                array_including([user.id], [other_user.id]),
                batch_delay: 30.seconds, batch_size: 100)
          .and_call_original
      )

      subject.execute(group)
    end
  end
end