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

351 lines
10 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
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) }
before do
sign_in(user)
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2015-09-11 14:41:01 +05:30
end
2018-10-15 14:42:47 +05:30
describe 'GET index' do
render_views
before do
2019-02-15 15:39:39 +05:30
get :index, params: { namespace_id: project.namespace, project_id: project }
2018-10-15 14:42:47 +05:30
end
2015-09-11 14:41:01 +05:30
2018-10-15 14:42:47 +05:30
it 'returns successfully' do
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2018-10-15 14:42:47 +05:30
end
2015-09-11 14:41:01 +05:30
end
2015-09-25 12:07:36 +05:30
2018-10-15 14:42:47 +05:30
describe 'GET show' do
render_views
2019-02-15 15:39:39 +05:30
subject(:show_request) { get :show, params: request_params }
2018-10-15 14:42:47 +05:30
let(:request_params) do
{
2017-08-17 22:00:37 +05:30
namespace_id: project.namespace,
project_id: project,
2018-10-15 14:42:47 +05:30
from: source_ref,
to: target_ref,
w: whitespace
}
end
2015-11-26 14:37:03 +05:30
2018-10-15 14:42:47 +05:30
let(:whitespace) { nil }
2015-09-25 12:07:36 +05:30
2018-10-15 14:42:47 +05:30
context 'when the refs exist' do
context 'when we set the white space param' do
let(:source_ref) { "08f22f25" }
let(:target_ref) { "66eceea0" }
let(:whitespace) { 1 }
2015-09-25 12:07:36 +05:30
2018-10-15 14:42:47 +05:30
it 'shows some diffs with ignore whitespace change option' do
show_request
2015-09-25 12:07:36 +05:30
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2018-10-15 14:42:47 +05:30
diff_file = assigns(:diffs).diff_files.first
expect(diff_file).not_to be_nil
expect(assigns(:commits).length).to be >= 1
# without whitespace option, there are more than 2 diff_splits
diff_splits = diff_file.diff.diff.split("\n")
expect(diff_splits.length).to be <= 2
end
end
context 'when we do not set the white space param' do
let(:source_ref) { "improve%2Fawesome" }
let(:target_ref) { "feature" }
let(:whitespace) { nil }
2017-08-17 22:00:37 +05:30
2018-10-15 14:42:47 +05:30
it 'sets the diffs and commits ivars' do
show_request
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2018-10-15 14:42:47 +05:30
expect(assigns(:diffs).diff_files.first).not_to be_nil
expect(assigns(:commits).length).to be >= 1
end
end
2017-08-17 22:00:37 +05:30
end
2018-10-15 14:42:47 +05:30
context 'when the source ref does not exist' do
let(:source_ref) { 'non-existent-source-ref' }
let(:target_ref) { "feature" }
it 'sets empty diff and commit ivars' do
show_request
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2019-07-31 22:56:46 +05:30
expect(assigns(:diffs)).to eq([])
2018-10-15 14:42:47 +05:30
expect(assigns(:commits)).to eq([])
end
2017-08-17 22:00:37 +05:30
end
2018-10-15 14:42:47 +05:30
context 'when the target ref does not exist' do
let(:target_ref) { 'non-existent-target-ref' }
let(:source_ref) { "improve%2Fawesome" }
2017-08-17 22:00:37 +05:30
2018-10-15 14:42:47 +05:30
it 'sets empty diff and commit ivars' do
show_request
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2018-10-15 14:42:47 +05:30
expect(assigns(:diffs)).to eq([])
expect(assigns(:commits)).to eq([])
end
2018-12-13 13:39:08 +05:30
end
context 'when the target ref is invalid' do
let(:target_ref) { "master%' AND 2554=4423 AND '%'='" }
let(:source_ref) { "improve%2Fawesome" }
it 'shows a flash message and redirects' do
show_request
expect(flash[:alert]).to eq('Invalid branch name')
expect(response).to have_http_status(302)
end
end
context 'when the source ref is invalid' do
let(:source_ref) { "master%' AND 2554=4423 AND '%'='" }
let(:target_ref) { "improve%2Fawesome" }
it 'shows a flash message and redirects' do
show_request
expect(flash[:alert]).to eq('Invalid branch name')
expect(response).to have_http_status(302)
end
2017-08-17 22:00:37 +05:30
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
}
2019-02-15 15:39:39 +05:30
get :diff_for_path, params: params.merge(extra_params)
2016-08-24 12:49:21 +05:30
end
let(:existing_path) { 'files/ruby/feature.rb' }
2018-10-15 14:42:47 +05:30
let(:source_ref) { "improve%2Fawesome" }
let(:target_ref) { "feature" }
2016-08-24 12:49:21 +05:30
2018-10-15 14:42:47 +05:30
context 'when the source and target refs exist' do
context 'when the user has access target the project' do
2016-08-24 12:49:21 +05:30
context 'when the path exists in the diff' do
it 'disables diff notes' do
2018-10-15 14:42:47 +05:30
diff_for_path(from: source_ref, to: target_ref, old_path: existing_path, new_path: existing_path)
2016-08-24 12:49:21 +05:30
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
2018-10-15 14:42:47 +05:30
diff_for_path(from: source_ref, to: target_ref, old_path: existing_path, new_path: existing_path)
2016-08-24 12:49:21 +05:30
end
end
context 'when the path does not exist in the diff' do
2017-09-10 17:25:29 +05:30
before do
2018-10-15 14:42:47 +05:30
diff_for_path(from: source_ref, to: target_ref, old_path: existing_path.succ, new_path: existing_path.succ)
2017-09-10 17:25:29 +05:30
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
2018-10-15 14:42:47 +05:30
context 'when the user does not have access target the project' do
2016-08-24 12:49:21 +05:30
before do
project.team.truncate
2018-10-15 14:42:47 +05:30
diff_for_path(from: source_ref, to: target_ref, old_path: existing_path, new_path: existing_path)
2016-08-24 12:49:21 +05:30
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
2018-10-15 14:42:47 +05:30
context 'when the source ref does not exist' do
2017-09-10 17:25:29 +05:30
before do
2018-10-15 14:42:47 +05:30
diff_for_path(from: source_ref.succ, to: target_ref, old_path: existing_path, new_path: existing_path)
2017-09-10 17:25:29 +05:30
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
2018-10-15 14:42:47 +05:30
context 'when the target ref does not exist' do
2017-09-10 17:25:29 +05:30
before do
2018-10-15 14:42:47 +05:30
diff_for_path(from: source_ref, to: target_ref.succ, old_path: existing_path, new_path: existing_path)
2017-09-10 17:25:29 +05:30
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
2018-10-15 14:42:47 +05:30
describe 'POST create' do
2019-02-15 15:39:39 +05:30
subject(:create_request) { post :create, params: request_params }
2018-10-15 14:42:47 +05:30
let(:request_params) do
{
namespace_id: project.namespace,
project_id: project,
from: source_ref,
to: target_ref
}
end
context 'when sending valid params' do
let(:source_ref) { "improve%2Fawesome" }
let(:target_ref) { "feature" }
it 'redirects back to show' do
create_request
expect(response).to redirect_to(project_compare_path(project, to: target_ref, from: source_ref))
end
end
context 'when sending invalid params' do
context 'when the source ref is empty and target ref is set' do
let(:source_ref) { '' }
let(:target_ref) { 'master' }
it 'redirects back to index and preserves the target ref' do
create_request
expect(response).to redirect_to(project_compare_index_path(project, to: target_ref))
end
end
context 'when the target ref is empty and source ref is set' do
let(:source_ref) { 'master' }
let(:target_ref) { '' }
it 'redirects back to index and preserves source ref' do
create_request
expect(response).to redirect_to(project_compare_index_path(project, from: source_ref))
end
end
context 'when the target and source ref are empty' do
let(:source_ref) { '' }
let(:target_ref) { '' }
it 'redirects back to index' do
create_request
expect(response).to redirect_to(namespace_project_compare_index_path)
end
end
end
end
describe 'GET signatures' do
2019-02-15 15:39:39 +05:30
subject(:signatures_request) { get :signatures, params: request_params }
2018-10-15 14:42:47 +05:30
let(:request_params) do
{
namespace_id: project.namespace,
project_id: project,
from: source_ref,
to: target_ref,
format: :json
}
end
context 'when the source and target refs exist' do
let(:source_ref) { "improve%2Fawesome" }
let(:target_ref) { "feature" }
context 'when the user has access to the project' do
render_views
let(:signature_commit) { build(:commit, project: project, safe_message: "message", sha: 'signature_commit') }
let(:non_signature_commit) { build(:commit, project: project, safe_message: "message", sha: 'non_signature_commit') }
before do
escaped_source_ref = Addressable::URI.unescape(source_ref)
escaped_target_ref = Addressable::URI.unescape(target_ref)
compare_service = CompareService.new(project, escaped_target_ref)
compare = compare_service.execute(project, escaped_source_ref)
expect(CompareService).to receive(:new).with(project, escaped_target_ref).and_return(compare_service)
expect(compare_service).to receive(:execute).with(project, escaped_source_ref).and_return(compare)
expect(compare).to receive(:commits).and_return([signature_commit, non_signature_commit])
expect(non_signature_commit).to receive(:has_signature?).and_return(false)
end
it 'returns only the commit with a signature' do
signatures_request
expect(response).to have_gitlab_http_status(200)
2019-09-30 21:07:59 +05:30
signatures = json_response['signatures']
2018-10-15 14:42:47 +05:30
expect(signatures.size).to eq(1)
expect(signatures.first['commit_sha']).to eq(signature_commit.sha)
expect(signatures.first['html']).to be_present
end
end
context 'when the user does not have access to the project' do
before do
project.team.truncate
end
it 'returns a 404' do
signatures_request
expect(response).to have_gitlab_http_status(404)
end
end
end
context 'when the source ref does not exist' do
let(:source_ref) { 'non-existent-ref-source' }
let(:target_ref) { "feature" }
it 'returns no signatures' do
signatures_request
expect(response).to have_gitlab_http_status(200)
2019-09-30 21:07:59 +05:30
expect(json_response['signatures']).to be_empty
2018-10-15 14:42:47 +05:30
end
end
context 'when the target ref does not exist' do
let(:target_ref) { 'non-existent-ref-target' }
let(:source_ref) { "improve%2Fawesome" }
it 'returns no signatures' do
signatures_request
expect(response).to have_gitlab_http_status(200)
2019-09-30 21:07:59 +05:30
expect(json_response['signatures']).to be_empty
2018-10-15 14:42:47 +05:30
end
end
end
2015-09-11 14:41:01 +05:30
end