2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::MergeRequests::DiffsController do
|
2018-03-17 18:26:18 +05:30
|
|
|
include ProjectForksHelper
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
2019-07-07 11:18:12 +05:30
|
|
|
let(:user) { create(:user) }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:merge_request) { create(:merge_request_with_diffs, target_project: project, source_project: project) }
|
|
|
|
|
|
|
|
before do
|
2019-07-07 11:18:12 +05:30
|
|
|
project.add_maintainer(user)
|
2017-09-10 17:25:29 +05:30
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET show' do
|
|
|
|
def go(extra_params = {})
|
|
|
|
params = {
|
|
|
|
namespace_id: project.namespace.to_param,
|
|
|
|
project_id: project,
|
|
|
|
id: merge_request.iid,
|
|
|
|
format: 'json'
|
|
|
|
}
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :show, params: params.merge(extra_params)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with default params' do
|
|
|
|
context 'for the same project' do
|
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
allow(controller).to receive(:rendered_for_merge_request?).and_return(true)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'serializes merge request diff collection' do
|
|
|
|
expect_any_instance_of(DiffsSerializer).to receive(:represent).with(an_instance_of(Gitlab::Diff::FileCollection::MergeRequestDiff), an_instance_of(Hash))
|
|
|
|
|
|
|
|
go
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
context 'when note has no position' do
|
|
|
|
before do
|
|
|
|
create(:legacy_diff_note_on_merge_request, project: project, noteable: merge_request, position: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'serializes merge request diff collection' do
|
|
|
|
expect_any_instance_of(DiffsSerializer).to receive(:represent).with(an_instance_of(Gitlab::Diff::FileCollection::MergeRequestDiff), an_instance_of(Hash))
|
|
|
|
|
|
|
|
go
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'with forked projects with submodules' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
let(:project) { create(:project, :repository) }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:forked_project) { fork_project_with_submodules(project) }
|
|
|
|
let(:merge_request) { create(:merge_request_with_diffs, source_project: forked_project, source_branch: 'add-submodule-version-bump', target_branch: 'master', target_project: project) }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
before do
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_developer(user)
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
merge_request.reload
|
|
|
|
go
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders' do
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.body).to have_content('Subproject commit')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with view' do
|
|
|
|
before do
|
|
|
|
go(view: 'parallel')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves the preferred diff view in a cookie' do
|
|
|
|
expect(response.cookies['diff_view']).to eq('parallel')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET diff_for_path' do
|
|
|
|
def diff_for_path(extra_params = {})
|
|
|
|
params = {
|
|
|
|
namespace_id: project.namespace.to_param,
|
|
|
|
project_id: project,
|
|
|
|
id: merge_request.iid,
|
|
|
|
format: 'json'
|
|
|
|
}
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :diff_for_path, params: params.merge(extra_params)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:existing_path) { 'files/ruby/popen.rb' }
|
|
|
|
|
|
|
|
context 'when the merge request exists' do
|
|
|
|
context 'when the user can view the merge request' do
|
|
|
|
context 'when the path exists in the diff' do
|
|
|
|
it 'enables diff notes' do
|
|
|
|
diff_for_path(old_path: existing_path, new_path: existing_path)
|
|
|
|
|
|
|
|
expect(assigns(:diff_notes_disabled)).to be_falsey
|
|
|
|
expect(assigns(:new_diff_note_attrs)).to eq(noteable_type: 'MergeRequest',
|
2018-03-17 18:26:18 +05:30
|
|
|
noteable_id: merge_request.id,
|
|
|
|
commit_id: nil)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'only renders the diffs for the path given' do
|
|
|
|
diff_for_path(old_path: existing_path, new_path: existing_path)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
paths = JSON.parse(response.body)["diff_files"].map { |file| file['new_path'] }
|
|
|
|
|
|
|
|
expect(paths).to include(existing_path)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user cannot view the merge request' do
|
|
|
|
before do
|
|
|
|
project.team.truncate
|
|
|
|
diff_for_path(old_path: existing_path, new_path: existing_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 404' do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the merge request does not exist' do
|
|
|
|
before do
|
|
|
|
diff_for_path(id: merge_request.iid.succ, old_path: existing_path, new_path: existing_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 404' do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the merge request belongs to a different project' do
|
|
|
|
let(:other_project) { create(:project) }
|
|
|
|
|
|
|
|
before do
|
2018-11-18 11:00:15 +05:30
|
|
|
other_project.add_maintainer(user)
|
2017-09-10 17:25:29 +05:30
|
|
|
diff_for_path(old_path: existing_path, new_path: existing_path, project_id: other_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 404' do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|