Welcome to mirror list, hosted at ThFree Co, Russian Federation.

diffs_spec.rb « merge_requests « projects « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 937b0f1d7131f7ec3935ecba46ccd7ab3301e72e (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Merge Requests Diffs' do
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:user) { create(:user) }
  let_it_be(:merge_request) { create(:merge_request_with_diffs, target_project: project, source_project: project) }

  before do
    project.add_maintainer(user)
    sign_in(user)
  end

  describe 'GET diffs_batch' do
    shared_examples_for 'serializes diffs with expected arguments' do
      it 'serializes paginated merge request diff collection' do
        expect_next_instance_of(PaginatedDiffSerializer) do |instance|
          expect(instance).to receive(:represent)
            .with(an_instance_of(collection), expected_options)
            .and_call_original
        end

        subject

        expect(response).to have_gitlab_http_status(:success)
      end
    end

    def collection_arguments(pagination_data = {})
      {
        merge_request: merge_request,
        commit: nil,
        diff_view: :inline,
        merge_ref_head_diff: nil,
        allow_tree_conflicts: true,
        pagination_data: {
          total_pages: nil
        }.merge(pagination_data)
      }
    end

    def go(headers: {}, **extra_params)
      params = {
        namespace_id: project.namespace.to_param,
        project_id: project,
        id: merge_request.iid,
        page: 0,
        per_page: 20,
        format: 'json'
      }

      get diffs_batch_namespace_project_json_merge_request_path(params.merge(extra_params)), headers: headers
    end

    context 'with caching', :use_clean_rails_memory_store_caching do
      subject { go(headers: headers, page: 0, per_page: 5) }

      let(:headers) { {} }

      context 'when the request has not been cached' do
        let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
        let(:expected_options) { collection_arguments(total_pages: 20) }

        it_behaves_like 'serializes diffs with expected arguments'
      end

      context 'when the request has already been cached' do
        before do
          go(page: 0, per_page: 5)
        end

        it 'does not serialize diffs' do
          expect_next_instance_of(PaginatedDiffSerializer) do |instance|
            expect(instance).not_to receive(:represent)
          end

          subject
        end

        context 'when using ETags' do
          context 'when etag_merge_request_diff_batches is true' do
            let(:headers) { { 'If-None-Match' => response.etag } }

            it 'does not serialize diffs' do
              expect(PaginatedDiffSerializer).not_to receive(:new)

              go(headers: headers, page: 0, per_page: 5)

              expect(response).to have_gitlab_http_status(:not_modified)
            end
          end

          context 'when etag_merge_request_diff_batches is false' do
            let(:headers) { { 'If-None-Match' => response.etag } }

            before do
              stub_feature_flags(etag_merge_request_diff_batches: false)
            end

            it 'does not serialize diffs' do
              expect_next_instance_of(PaginatedDiffSerializer) do |instance|
                expect(instance).not_to receive(:represent)
              end

              subject

              expect(response).to have_gitlab_http_status(:success)
            end
          end
        end

        context 'with the different user' do
          let(:another_user) { create(:user) }
          let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
          let(:expected_options) { collection_arguments(total_pages: 20) }

          before do
            project.add_maintainer(another_user)
            sign_in(another_user)
          end

          it_behaves_like 'serializes diffs with expected arguments'

          context 'when using ETag caching' do
            it_behaves_like 'serializes diffs with expected arguments' do
              let(:headers) { { 'If-None-Match' => response.etag } }
            end
          end
        end

        context 'with a new unfoldable diff position' do
          let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
          let(:expected_options) { collection_arguments(total_pages: 20) }

          let(:unfoldable_position) do
            create(:diff_position)
          end

          before do
            expect_next_instance_of(Gitlab::Diff::PositionCollection) do |instance|
              expect(instance)
                .to receive(:unfoldable)
                .and_return([unfoldable_position])
            end
          end

          it_behaves_like 'serializes diffs with expected arguments'

          context 'when using ETag caching' do
            it_behaves_like 'serializes diffs with expected arguments' do
              let(:headers) { { 'If-None-Match' => response.etag } }
            end
          end
        end

        context 'with disabled display_merge_conflicts_in_diff feature' do
          let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
          let(:expected_options) { collection_arguments(total_pages: 20).merge(allow_tree_conflicts: false) }

          before do
            stub_feature_flags(display_merge_conflicts_in_diff: false)
          end

          it_behaves_like 'serializes diffs with expected arguments'

          context 'when using ETag caching' do
            it_behaves_like 'serializes diffs with expected arguments' do
              let(:headers) { { 'If-None-Match' => response.etag } }
            end
          end
        end

        context 'with diff_head option' do
          subject { go(page: 0, per_page: 5, diff_head: true) }

          let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
          let(:expected_options) { collection_arguments(total_pages: 20).merge(merge_ref_head_diff: true) }

          before do
            merge_request.create_merge_head_diff!
          end

          it_behaves_like 'serializes diffs with expected arguments'

          context 'when using ETag caching' do
            it_behaves_like 'serializes diffs with expected arguments' do
              let(:headers) { { 'If-None-Match' => response.etag } }
            end
          end
        end

        context 'with the different pagination option' do
          subject { go(page: 5, per_page: 5) }

          let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
          let(:expected_options) { collection_arguments(total_pages: 20) }

          it_behaves_like 'serializes diffs with expected arguments'

          context 'when using ETag caching' do
            it_behaves_like 'serializes diffs with expected arguments' do
              let(:headers) { { 'If-None-Match' => response.etag } }
            end
          end
        end

        context 'with the different diff_view' do
          subject { go(page: 0, per_page: 5, view: :parallel) }

          let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
          let(:expected_options) { collection_arguments(total_pages: 20).merge(diff_view: :parallel) }

          it_behaves_like 'serializes diffs with expected arguments'

          context 'when using ETag caching' do
            it_behaves_like 'serializes diffs with expected arguments' do
              let(:headers) { { 'If-None-Match' => response.etag } }
            end
          end
        end

        context 'with the different expanded option' do
          subject { go(page: 0, per_page: 5, expanded: true ) }

          let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
          let(:expected_options) { collection_arguments(total_pages: 20) }

          it_behaves_like 'serializes diffs with expected arguments'

          context 'when using ETag caching' do
            it_behaves_like 'serializes diffs with expected arguments' do
              let(:headers) { { 'If-None-Match' => response.etag } }
            end
          end
        end

        context 'with the different ignore_whitespace_change option' do
          subject { go(page: 0, per_page: 5, w: 1) }

          let(:collection) { Gitlab::Diff::FileCollection::Compare }
          let(:expected_options) { collection_arguments(total_pages: 20) }

          it_behaves_like 'serializes diffs with expected arguments'

          context 'when using ETag caching' do
            it_behaves_like 'serializes diffs with expected arguments' do
              let(:headers) { { 'If-None-Match' => response.etag } }
            end
          end
        end
      end

      context 'when the paths is given' do
        subject { go(headers: headers, page: 0, per_page: 5, paths: %w[README CHANGELOG]) }

        before do
          go(page: 0, per_page: 5, paths: %w[README CHANGELOG])
        end

        context 'when using ETag caching' do
          let(:headers) { { 'If-None-Match' => response.etag } }

          context 'when etag_merge_request_diff_batches is true' do
            it 'does not serialize diffs' do
              expect(PaginatedDiffSerializer).not_to receive(:new)

              subject

              expect(response).to have_gitlab_http_status(:not_modified)
            end
          end

          context 'when etag_merge_request_diff_batches is false' do
            before do
              stub_feature_flags(etag_merge_request_diff_batches: false)
            end

            it 'does not use cache' do
              expect(Rails.cache).not_to receive(:fetch).with(/cache:gitlab:PaginatedDiffSerializer/).and_call_original

              subject

              expect(response).to have_gitlab_http_status(:success)
            end
          end
        end

        context 'when not using ETag caching' do
          it 'does not use cache' do
            expect(Rails.cache).not_to receive(:fetch).with(/cache:gitlab:PaginatedDiffSerializer/).and_call_original

            subject

            expect(response).to have_gitlab_http_status(:success)
          end
        end
      end
    end
  end
end