summaryrefslogtreecommitdiff
path: root/spec/services/members/import_project_team_service_spec.rb
blob: 96e8db1ba738dce9575660a6e180b155f2361748 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Members::ImportProjectTeamService do
  describe '#execute' do
    let_it_be(:source_project) { create(:project) }
    let_it_be(:target_project) { create(:project) }
    let_it_be(:user) { create(:user) }

    subject { described_class.new(user, { id: target_project_id, project_id: source_project_id }) }

    before_all do
      source_project.add_guest(user)
      target_project.add_maintainer(user)
    end

    context 'when project team members are imported successfully' do
      let(:source_project_id) { source_project.id }
      let(:target_project_id) { target_project.id }

      it 'returns true' do
        expect(subject.execute).to be(true)
      end
    end

    context 'when the project team import fails' do
      context 'when the target project cannot be found' do
        let(:source_project_id) { source_project.id }
        let(:target_project_id) { non_existing_record_id }

        it 'returns false' do
          expect(subject.execute).to be(false)
        end
      end

      context 'when the source project cannot be found' do
        let(:source_project_id) { non_existing_record_id }
        let(:target_project_id) { target_project.id }

        it 'returns false' do
          expect(subject.execute).to be(false)
        end
      end

      context 'when the user doing the import does not exist' do
        let(:user) { nil }
        let(:source_project_id) { source_project.id }
        let(:target_project_id) { target_project.id }

        it 'returns false' do
          expect(subject.execute).to be(false)
        end
      end

      context 'when the user does not have permission to read the source project members' do
        let(:user) { create(:user) }
        let(:source_project_id) { create(:project, :private).id }
        let(:target_project_id) { target_project.id }

        it 'returns false' do
          expect(subject.execute).to be(false)
        end
      end

      context 'when the user does not have permission to admin the target project' do
        let(:source_project_id) { source_project.id }
        let(:target_project_id) { create(:project).id }

        it 'returns false' do
          expect(subject.execute).to be(false)
        end
      end

      context 'when the source and target project are valid but the ProjectTeam#import command fails' do
        let(:source_project_id) { source_project.id }
        let(:target_project_id) { target_project.id }

        before do
          allow_next_instance_of(ProjectTeam) do |project_team|
            allow(project_team).to receive(:import).and_return(false)
          end
        end

        it 'returns false' do
          expect(subject.execute).to be(false)
        end
      end
    end
  end
end