debian-mirror-gitlab/spec/requests/projects/merge_requests/diffs_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

197 lines
6 KiB
Ruby
Raw Normal View History

2021-09-30 23:02:18 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-17 16:20:25 +05:30
RSpec.describe 'Merge Requests Diffs', feature_category: :code_review_workflow do
2021-09-30 23:02:18 +05:30
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
2022-08-27 11:52:29 +05:30
expect(response).to have_gitlab_http_status(:success)
2021-09-30 23:02:18 +05:30
end
end
def collection_arguments(pagination_data = {})
{
merge_request: merge_request,
2022-01-26 12:08:38 +05:30
commit: nil,
2021-09-30 23:02:18 +05:30
diff_view: :inline,
merge_ref_head_diff: nil,
pagination_data: {
total_pages: nil
}.merge(pagination_data)
}
end
2022-08-27 11:52:29 +05:30
def go(headers: {}, **extra_params)
2021-09-30 23:02:18 +05:30
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
2022-10-11 01:57:18 +05:30
context 'without caching' do
2022-08-27 11:52:29 +05:30
subject { go(headers: headers, page: 0, per_page: 5) }
let(:headers) { {} }
2022-10-11 01:57:18 +05:30
let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
let(:expected_options) { collection_arguments(total_pages: 20) }
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
it_behaves_like 'serializes diffs with expected arguments'
end
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
context 'with caching', :use_clean_rails_memory_store_caching do
subject { go(headers: headers, page: 0, per_page: 5) }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
let(:headers) { { 'If-None-Match' => response.etag } }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
before do
go(page: 0, per_page: 5)
end
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
it 'does not serialize diffs' do
expect(PaginatedDiffSerializer).not_to receive(:new)
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
go(headers: headers, page: 0, per_page: 5)
2021-10-27 15:23:28 +05:30
2022-10-11 01:57:18 +05:30
expect(response).to have_gitlab_http_status(:not_modified)
end
2021-10-27 15:23:28 +05:30
2022-10-11 01:57:18 +05:30
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) }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
before do
project.add_maintainer(another_user)
sign_in(another_user)
2021-10-27 15:23:28 +05:30
end
2022-10-11 01:57:18 +05:30
it_behaves_like 'serializes diffs with expected arguments'
end
2021-10-27 15:23:28 +05:30
2022-10-11 01:57:18 +05:30
context 'with a new unfoldable diff position' do
let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
let(:expected_options) { collection_arguments(total_pages: 20) }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
let(:unfoldable_position) do
create(:diff_position)
2021-10-27 15:23:28 +05:30
end
2022-10-11 01:57:18 +05:30
before do
expect_next_instance_of(Gitlab::Diff::PositionCollection) do |instance|
expect(instance)
.to receive(:unfoldable)
.and_return([unfoldable_position])
2021-10-27 15:23:28 +05:30
end
end
2022-10-11 01:57:18 +05:30
it_behaves_like 'serializes diffs with expected arguments'
end
2021-10-27 15:23:28 +05:30
2022-10-11 01:57:18 +05:30
context 'with diff_head option' do
subject { go(page: 0, per_page: 5, diff_head: true) }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
let(:expected_options) { collection_arguments(total_pages: 20).merge(merge_ref_head_diff: true) }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
before do
merge_request.create_merge_head_diff!
2021-09-30 23:02:18 +05:30
end
2022-10-11 01:57:18 +05:30
it_behaves_like 'serializes diffs with expected arguments'
end
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
context 'with the different pagination option' do
subject { go(page: 5, per_page: 5) }
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
let(:expected_options) { collection_arguments(total_pages: 20) }
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
it_behaves_like 'serializes diffs with expected arguments'
end
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
context 'with the different diff_view' do
subject { go(page: 0, per_page: 5, view: :parallel) }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
let(:expected_options) { collection_arguments(total_pages: 20).merge(diff_view: :parallel) }
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
it_behaves_like 'serializes diffs with expected arguments'
end
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
context 'with the different expanded option' do
subject { go(page: 0, per_page: 5, expanded: true ) }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
let(:expected_options) { collection_arguments(total_pages: 20) }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
it_behaves_like 'serializes diffs with expected arguments'
2021-09-30 23:02:18 +05:30
end
2022-10-11 01:57:18 +05:30
context 'with the different ignore_whitespace_change option' do
subject { go(page: 0, per_page: 5, w: 1) }
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
let(:collection) { Gitlab::Diff::FileCollection::Compare }
let(:expected_options) { collection_arguments(total_pages: 20) }
2021-09-30 23:02:18 +05:30
2022-10-11 01:57:18 +05:30
it_behaves_like 'serializes diffs with expected arguments'
end
end
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
context 'when the paths is given' do
subject { go(headers: headers, page: 0, per_page: 5, paths: %w[README CHANGELOG]) }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
before do
go(page: 0, per_page: 5, paths: %w[README CHANGELOG])
end
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
context 'when using ETag caching' do
let(:headers) { { 'If-None-Match' => response.etag } }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
it 'does not serialize diffs' do
expect(PaginatedDiffSerializer).not_to receive(:new)
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
subject
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
expect(response).to have_gitlab_http_status(:not_modified)
2022-08-27 11:52:29 +05:30
end
2022-10-11 01:57:18 +05:30
end
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
context 'when not using ETag caching' do
let(:headers) { {} }
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
it 'does not use cache' do
expect(Rails.cache).not_to receive(:fetch).with(/cache:gitlab:PaginatedDiffSerializer/).and_call_original
2022-08-27 11:52:29 +05:30
2022-10-11 01:57:18 +05:30
subject
expect(response).to have_gitlab_http_status(:success)
2021-09-30 23:02:18 +05:30
end
end
end
end
end