summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/services/merge_request_shared_examples.rb
blob: 178b6bc47e1996603356c695728632b6a1673596 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true

RSpec.shared_examples 'reviewer_ids filter' do
  context 'filter_reviewer' do
    let(:opts) { super().merge(reviewer_ids_param) }

    context 'without reviewer_ids' do
      let(:reviewer_ids_param) { {} }

      it 'contains no reviewer_ids' do
        expect(execute.reviewers).to eq []
      end
    end

    context 'with reviewer_ids' do
      let(:reviewer_ids_param) { { reviewer_ids: [reviewer1.id, reviewer2.id] } }

      let(:reviewer1) { create(:user) }
      let(:reviewer2) { create(:user) }

      context 'when the current user can admin the merge_request' do
        context 'when merge_request_reviewer feature is enabled' do
          before do
            stub_feature_flags(merge_request_reviewer: true)
          end

          context 'with a reviewer who can read the merge_request' do
            before do
              project.add_developer(reviewer1)
            end

            it 'contains reviewers who can read the merge_request' do
              expect(execute.reviewers).to contain_exactly(reviewer1)
            end
          end
        end

        context 'when merge_request_reviewer feature is disabled' do
          before do
            stub_feature_flags(merge_request_reviewer: false)
          end

          it 'contains no reviewers' do
            expect(execute.reviewers).to eq []
          end
        end
      end

      context 'when the current_user cannot admin the merge_request' do
        before do
          project.add_developer(user)
        end

        it 'contains no reviewers' do
          expect(execute.reviewers).to eq []
        end
      end
    end
  end
end

RSpec.shared_examples 'merge request reviewers cache counters invalidator' do
  let(:reviewer_1) { create(:user) }
  let(:reviewer_2) { create(:user) }

  before do
    merge_request.update!(reviewers: [reviewer_1, reviewer_2])
  end

  it 'invalidates counter cache for reviewers' do
    expect(merge_request.reviewers).to all(receive(:invalidate_merge_request_cache_counts))

    described_class.new(project, user, {}).execute(merge_request)
  end
end

RSpec.shared_examples_for 'a service that can create a merge request' do
  subject(:last_mr) { MergeRequest.last }

  it 'creates a merge request with the correct target branch' do
    branch = push_options[:target] || project.default_branch

    expect { service.execute }.to change { MergeRequest.count }.by(1)
    expect(last_mr.target_branch).to eq(branch)
  end

  context 'when project has been forked', :sidekiq_might_not_need_inline do
    let(:forked_project) { fork_project(project, user1, repository: true) }
    let(:service) { described_class.new(forked_project, user1, changes, push_options) }

    before do
      allow(forked_project).to receive(:empty_repo?).and_return(false)
    end

    it 'sets the correct source and target project' do
      service.execute

      expect(last_mr.source_project).to eq(forked_project)
      expect(last_mr.target_project).to eq(project)
    end
  end
end

RSpec.shared_examples_for 'a service that does not create a merge request' do
  it do
    expect { service.execute }.not_to change { MergeRequest.count }
  end
end

# In the non-foss version of GitLab, there can be many assignees, so
# there 'count' can be something other than 0 or 1. In the foss
# version of GitLab, there can be only one assignee though, so 'count'
# can only be 0 or 1.
RSpec.shared_examples_for 'a service that can change assignees of a merge request' do |count|
  subject(:last_mr) { MergeRequest.last }

  it 'changes assignee count' do
    service.execute

    expect(last_mr.assignees.count).to eq(count)
  end
end

RSpec.shared_examples 'with an existing branch that has a merge request open' do |count|
  let(:changes) { existing_branch_changes }
  let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch)}

  it_behaves_like 'a service that does not create a merge request'
  it_behaves_like 'a service that can change assignees of a merge request', count
end

RSpec.shared_examples 'when coupled with the `create` push option' do |count|
  let(:push_options) { { create: true, assign: assigned, unassign: unassigned } }

  it_behaves_like 'a service that can create a merge request'
  it_behaves_like 'a service that can change assignees of a merge request', count
end

RSpec.shared_examples 'with a new branch' do |count|
  let(:changes) { new_branch_changes }

  it_behaves_like 'a service that does not create a merge request'

  it 'adds an error to the service' do
    service.execute

    expect(service.errors).to include(error_mr_required)
  end

  it_behaves_like 'when coupled with the `create` push option', count
end

RSpec.shared_examples 'with an existing branch but no open MR' do |count|
  let(:changes) { existing_branch_changes }

  it_behaves_like 'a service that does not create a merge request'

  it 'adds an error to the service' do
    service.execute

    expect(service.errors).to include(error_mr_required)
  end

  it_behaves_like 'when coupled with the `create` push option', count
end