diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-20 09:07:57 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-20 09:07:57 +0000 |
commit | 7881eb30eaa8b01dbcfe87faa09927c75c7d6e45 (patch) | |
tree | 298bc8d2c62b2f2c29cb8ecbcf3de3eaaa6466d9 /spec/controllers/projects/merge_requests/diffs_controller_spec.rb | |
parent | 64b66e0cb6d1bfd27abf24e06653f00bddb60597 (diff) | |
download | gitlab-ce-7881eb30eaa8b01dbcfe87faa09927c75c7d6e45.tar.gz |
Add latest changes from gitlab-org/gitlab@12-6-stable-ee
Diffstat (limited to 'spec/controllers/projects/merge_requests/diffs_controller_spec.rb')
-rw-r--r-- | spec/controllers/projects/merge_requests/diffs_controller_spec.rb | 151 |
1 files changed, 107 insertions, 44 deletions
diff --git a/spec/controllers/projects/merge_requests/diffs_controller_spec.rb b/spec/controllers/projects/merge_requests/diffs_controller_spec.rb index 06d9af33189..d7e790360e3 100644 --- a/spec/controllers/projects/merge_requests/diffs_controller_spec.rb +++ b/spec/controllers/projects/merge_requests/diffs_controller_spec.rb @@ -5,6 +5,19 @@ require 'spec_helper' describe Projects::MergeRequests::DiffsController do include ProjectForksHelper + shared_examples '404 for unexistent diffable' do + context 'when diffable does not exists' do + it 'returns 404' do + unexistent_diff_id = 9999 + + go(diff_id: unexistent_diff_id) + + expect(MergeRequestDiff.find_by(id: unexistent_diff_id)).to be_nil + expect(response).to have_gitlab_http_status(404) + end + end + end + shared_examples 'forked project with submodules' do render_views @@ -25,6 +38,16 @@ describe Projects::MergeRequests::DiffsController do end end + shared_examples 'cached diff collection' do + it 'ensures diff highlighting cache writing' do + expect_next_instance_of(Gitlab::Diff::HighlightCache) do |cache| + expect(cache).to receive(:write_if_empty).once + end + + go + end + end + shared_examples 'persisted preferred diff view cookie' do context 'with view param' do before do @@ -112,6 +135,7 @@ describe Projects::MergeRequests::DiffsController do end it_behaves_like 'persisted preferred diff view cookie' + it_behaves_like 'cached diff collection' end describe 'GET diffs_metadata' do @@ -126,6 +150,8 @@ describe Projects::MergeRequests::DiffsController do get :diffs_metadata, params: params.merge(extra_params) end + it_behaves_like '404 for unexistent diffable' + context 'when not authorized' do let(:another_user) { create(:user) } @@ -148,14 +174,6 @@ describe Projects::MergeRequests::DiffsController do end end - context 'when diffable does not exists' do - it 'returns 404' do - go(diff_id: 9999) - - expect(response).to have_gitlab_http_status(404) - end - end - context 'with valid diff_id' do it 'returns success' do go(diff_id: merge_request.merge_request_diff.id) @@ -317,17 +335,53 @@ describe Projects::MergeRequests::DiffsController do 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 + end + end + + shared_examples_for 'successful request' do + it 'returns success' do + subject + + expect(response).to have_gitlab_http_status(200) + end + end + + def collection_arguments(pagination_data = {}) + { + merge_request: merge_request, + diff_view: :inline, + pagination_data: { + current_page: nil, + next_page: nil, + total_pages: nil + }.merge(pagination_data) + } + end + def go(extra_params = {}) params = { namespace_id: project.namespace.to_param, project_id: project, id: merge_request.iid, + page: 1, + per_page: 20, format: 'json' } get :diffs_batch, params: params.merge(extra_params) end + it_behaves_like '404 for unexistent diffable' + context 'when feature is disabled' do before do stub_feature_flags(diffs_batch_load: false) @@ -354,56 +408,65 @@ describe Projects::MergeRequests::DiffsController do end end - context 'with default params' do - let(:expected_options) do - { - merge_request: merge_request, - diff_view: :inline, - pagination_data: { - current_page: 1, - next_page: nil, - total_pages: 1 - } - } + context 'with valid diff_id' do + subject { go(diff_id: merge_request.merge_request_diff.id) } + + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch } + let(:expected_options) { collection_arguments(current_page: 1, total_pages: 1) } end - it 'serializes paginated merge request diff collection' do - expect_next_instance_of(PaginatedDiffSerializer) do |instance| - expect(instance).to receive(:represent) - .with(an_instance_of(Gitlab::Diff::FileCollection::MergeRequestDiffBatch), expected_options) - .and_call_original - end + it_behaves_like 'successful request' + end - go + context 'with commit_id param' do + subject { go(commit_id: merge_request.diff_head_sha) } + + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::Commit } + let(:expected_options) { collection_arguments } end end - context 'with smaller diff batch params' do - let(:expected_options) do - { - merge_request: merge_request, - diff_view: :inline, - pagination_data: { - current_page: 2, - next_page: 3, - total_pages: 4 - } - } + context 'with diff_id and start_sha params' do + subject do + go(diff_id: merge_request.merge_request_diff.id, + start_sha: merge_request.merge_request_diff.start_commit_sha) end - it 'serializes paginated merge request diff collection' do - expect_next_instance_of(PaginatedDiffSerializer) do |instance| - expect(instance).to receive(:represent) - .with(an_instance_of(Gitlab::Diff::FileCollection::MergeRequestDiffBatch), expected_options) - .and_call_original - end + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::Compare } + let(:expected_options) { collection_arguments } + end + + it_behaves_like 'successful request' + end - go(page: 2, per_page: 5) + context 'with default params' do + subject { go } + + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch } + let(:expected_options) { collection_arguments(current_page: 1, total_pages: 1) } end + + it_behaves_like 'successful request' + end + + context 'with smaller diff batch params' do + subject { go(page: 2, per_page: 5) } + + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch } + let(:expected_options) { collection_arguments(current_page: 2, next_page: 3, total_pages: 4) } + end + + it_behaves_like 'successful request' end it_behaves_like 'forked project with submodules' it_behaves_like 'persisted preferred diff view cookie' + it_behaves_like 'cached diff collection' context 'diff unfolding' do let!(:unfoldable_diff_note) do |