debian-mirror-gitlab/spec/controllers/projects/compare_controller_spec.rb

174 lines
5 KiB
Ruby
Raw Normal View History

2015-09-11 14:41:01 +05:30
require 'spec_helper'
describe Projects::CompareController do
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :repository) }
2015-09-11 14:41:01 +05:30
let(:user) { create(:user) }
let(:ref_from) { "improve%2Fawesome" }
let(:ref_to) { "feature" }
before do
sign_in(user)
2018-03-17 18:26:18 +05:30
project.add_master(user)
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
it 'compare shows some diffs' do
2015-09-11 14:41:01 +05:30
get(:show,
2017-08-17 22:00:37 +05:30
namespace_id: project.namespace,
project_id: project,
2015-09-11 14:41:01 +05:30
from: ref_from,
to: ref_to)
expect(response).to be_success
2016-09-13 17:45:13 +05:30
expect(assigns(:diffs).diff_files.first).not_to be_nil
2015-09-11 14:41:01 +05:30
expect(assigns(:commits).length).to be >= 1
end
2015-09-25 12:07:36 +05:30
2016-09-13 17:45:13 +05:30
it 'compare shows some diffs with ignore whitespace change option' do
2015-11-26 14:37:03 +05:30
get(:show,
2017-08-17 22:00:37 +05:30
namespace_id: project.namespace,
project_id: project,
2015-11-26 14:37:03 +05:30
from: '08f22f25',
to: '66eceea0',
w: 1)
expect(response).to be_success
2016-09-13 17:45:13 +05:30
diff_file = assigns(:diffs).diff_files.first
expect(diff_file).not_to be_nil
2015-11-26 14:37:03 +05:30
expect(assigns(:commits).length).to be >= 1
# without whitespace option, there are more than 2 diff_splits
2016-09-13 17:45:13 +05:30
diff_splits = diff_file.diff.diff.split("\n")
2015-11-26 14:37:03 +05:30
expect(diff_splits.length).to be <= 2
end
2015-09-25 12:07:36 +05:30
describe 'non-existent refs' do
2016-09-13 17:45:13 +05:30
it 'uses invalid source ref' do
2015-09-25 12:07:36 +05:30
get(:show,
2017-08-17 22:00:37 +05:30
namespace_id: project.namespace,
project_id: project,
2015-09-25 12:07:36 +05:30
from: 'non-existent',
to: ref_to)
expect(response).to be_success
2016-09-13 17:45:13 +05:30
expect(assigns(:diffs).diff_files.to_a).to eq([])
2015-09-25 12:07:36 +05:30
expect(assigns(:commits)).to eq([])
end
2016-09-13 17:45:13 +05:30
it 'uses invalid target ref' do
2015-09-25 12:07:36 +05:30
get(:show,
2017-08-17 22:00:37 +05:30
namespace_id: project.namespace,
project_id: project,
2015-09-25 12:07:36 +05:30
from: ref_from,
to: 'non-existent')
expect(response).to be_success
expect(assigns(:diffs)).to eq(nil)
expect(assigns(:commits)).to eq(nil)
end
2017-08-17 22:00:37 +05:30
it 'redirects back to index when params[:from] is empty and preserves params[:to]' do
post(:create,
namespace_id: project.namespace,
project_id: project,
from: '',
to: 'master')
2017-09-10 17:25:29 +05:30
expect(response).to redirect_to(project_compare_index_path(project, to: 'master'))
2017-08-17 22:00:37 +05:30
end
it 'redirects back to index when params[:to] is empty and preserves params[:from]' do
post(:create,
namespace_id: project.namespace,
project_id: project,
from: 'master',
to: '')
2017-09-10 17:25:29 +05:30
expect(response).to redirect_to(project_compare_index_path(project, from: 'master'))
2017-08-17 22:00:37 +05:30
end
it 'redirects back to index when params[:from] and params[:to] are empty' do
post(:create,
namespace_id: project.namespace,
project_id: project,
from: '',
to: '')
expect(response).to redirect_to(namespace_project_compare_index_path)
end
2015-09-25 12:07:36 +05:30
end
2016-08-24 12:49:21 +05:30
describe 'GET diff_for_path' do
def diff_for_path(extra_params = {})
params = {
2017-08-17 22:00:37 +05:30
namespace_id: project.namespace,
project_id: project
2016-08-24 12:49:21 +05:30
}
get :diff_for_path, params.merge(extra_params)
end
let(:existing_path) { 'files/ruby/feature.rb' }
context 'when the from and to refs exist' do
context 'when the user has access to the project' do
context 'when the path exists in the diff' do
it 'disables diff notes' do
diff_for_path(from: ref_from, to: ref_to, old_path: existing_path, new_path: existing_path)
expect(assigns(:diff_notes_disabled)).to be_truthy
end
it 'only renders the diffs for the path given' do
2016-09-13 17:45:13 +05:30
expect(controller).to receive(:render_diff_for_path).and_wrap_original do |meth, diffs|
expect(diffs.diff_files.map(&:new_path)).to contain_exactly(existing_path)
meth.call(diffs)
2016-08-24 12:49:21 +05:30
end
diff_for_path(from: ref_from, to: ref_to, old_path: existing_path, new_path: existing_path)
end
end
context 'when the path does not exist in the diff' do
2017-09-10 17:25:29 +05:30
before do
diff_for_path(from: ref_from, to: ref_to, old_path: existing_path.succ, new_path: existing_path.succ)
end
2016-08-24 12:49:21 +05:30
it 'returns a 404' do
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
2016-08-24 12:49:21 +05:30
end
end
end
context 'when the user does not have access to the project' do
before do
project.team.truncate
diff_for_path(from: ref_from, to: ref_to, 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)
2016-08-24 12:49:21 +05:30
end
end
end
context 'when the from ref does not exist' do
2017-09-10 17:25:29 +05:30
before do
diff_for_path(from: ref_from.succ, to: ref_to, old_path: existing_path, new_path: existing_path)
end
2016-08-24 12:49:21 +05:30
it 'returns a 404' do
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
2016-08-24 12:49:21 +05:30
end
end
context 'when the to ref does not exist' do
2017-09-10 17:25:29 +05:30
before do
diff_for_path(from: ref_from, to: ref_to.succ, old_path: existing_path, new_path: existing_path)
end
2016-08-24 12:49:21 +05:30
it 'returns a 404' do
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
2016-08-24 12:49:21 +05:30
end
end
end
2015-09-11 14:41:01 +05:30
end