diff options
Diffstat (limited to 'spec/controllers/projects')
6 files changed, 360 insertions, 111 deletions
diff --git a/spec/controllers/projects/compare_controller_spec.rb b/spec/controllers/projects/compare_controller_spec.rb index 046ce027965..b15cde4314e 100644 --- a/spec/controllers/projects/compare_controller_spec.rb +++ b/spec/controllers/projects/compare_controller_spec.rb @@ -3,96 +3,99 @@ require 'spec_helper' describe Projects::CompareController do let(:project) { create(:project, :repository) } let(:user) { create(:user) } - let(:ref_from) { "improve%2Fawesome" } - let(:ref_to) { "feature" } before do sign_in(user) project.add_master(user) end - it 'compare shows some diffs' do - get(:show, - namespace_id: project.namespace, - project_id: project, - from: ref_from, - to: ref_to) + describe 'GET index' do + render_views + + before do + get :index, namespace_id: project.namespace, project_id: project + end - expect(response).to be_success - expect(assigns(:diffs).diff_files.first).not_to be_nil - expect(assigns(:commits).length).to be >= 1 + it 'returns successfully' do + expect(response).to be_success + end end - it 'compare shows some diffs with ignore whitespace change option' do - get(:show, + describe 'GET show' do + render_views + + subject(:show_request) { get :show, request_params } + + let(:request_params) do + { namespace_id: project.namespace, project_id: project, - from: '08f22f25', - to: '66eceea0', - w: 1) - - expect(response).to be_success - 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 + from: source_ref, + to: target_ref, + w: whitespace + } + end - describe 'non-existent refs' do - it 'uses invalid source ref' do - get(:show, - namespace_id: project.namespace, - project_id: project, - from: 'non-existent', - to: ref_to) + let(:whitespace) { nil } - expect(response).to be_success - expect(assigns(:diffs).diff_files.to_a).to eq([]) - expect(assigns(:commits)).to eq([]) - end + 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 } - it 'uses invalid target ref' do - get(:show, - namespace_id: project.namespace, - project_id: project, - from: ref_from, - to: 'non-existent') + it 'shows some diffs with ignore whitespace change option' do + show_request - expect(response).to be_success - expect(assigns(:diffs)).to eq(nil) - expect(assigns(:commits)).to eq(nil) - end + expect(response).to be_success + 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 } - 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') + it 'sets the diffs and commits ivars' do + show_request - expect(response).to redirect_to(project_compare_index_path(project, to: 'master')) + expect(response).to be_success + expect(assigns(:diffs).diff_files.first).not_to be_nil + expect(assigns(:commits).length).to be >= 1 + end + end 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: '') + 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 - expect(response).to redirect_to(project_compare_index_path(project, from: 'master')) + expect(response).to be_success + expect(assigns(:diffs).diff_files.to_a).to eq([]) + expect(assigns(:commits)).to eq([]) + end 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: '') + context 'when the target ref does not exist' do + let(:target_ref) { 'non-existent-target-ref' } + let(:source_ref) { "improve%2Fawesome" } - expect(response).to redirect_to(namespace_project_compare_index_path) + it 'sets empty diff and commit ivars' do + show_request + + expect(response).to be_success + expect(assigns(:diffs)).to eq([]) + expect(assigns(:commits)).to eq([]) + end end end @@ -107,12 +110,14 @@ describe Projects::CompareController do end let(:existing_path) { 'files/ruby/feature.rb' } + let(:source_ref) { "improve%2Fawesome" } + let(:target_ref) { "feature" } - context 'when the from and to refs exist' do - context 'when the user has access to the project' do + context 'when the source and target refs exist' do + context 'when the user has access target 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) + diff_for_path(from: source_ref, to: target_ref, old_path: existing_path, new_path: existing_path) expect(assigns(:diff_notes_disabled)).to be_truthy end @@ -123,13 +128,13 @@ describe Projects::CompareController do meth.call(diffs) end - diff_for_path(from: ref_from, to: ref_to, old_path: existing_path, new_path: existing_path) + diff_for_path(from: source_ref, to: target_ref, old_path: existing_path, new_path: existing_path) end end context 'when the path does not exist in the diff' do before do - diff_for_path(from: ref_from, to: ref_to, old_path: existing_path.succ, new_path: existing_path.succ) + diff_for_path(from: source_ref, to: target_ref, old_path: existing_path.succ, new_path: existing_path.succ) end it 'returns a 404' do @@ -138,10 +143,10 @@ describe Projects::CompareController do end end - context 'when the user does not have access to the project' do + context 'when the user does not have access target 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) + diff_for_path(from: source_ref, to: target_ref, old_path: existing_path, new_path: existing_path) end it 'returns a 404' do @@ -150,9 +155,9 @@ describe Projects::CompareController do end end - context 'when the from ref does not exist' do + context 'when the source ref does not exist' do before do - diff_for_path(from: ref_from.succ, to: ref_to, old_path: existing_path, new_path: existing_path) + diff_for_path(from: source_ref.succ, to: target_ref, old_path: existing_path, new_path: existing_path) end it 'returns a 404' do @@ -160,9 +165,9 @@ describe Projects::CompareController do end end - context 'when the to ref does not exist' do + context 'when the target ref does not exist' do before do - diff_for_path(from: ref_from, to: ref_to.succ, old_path: existing_path, new_path: existing_path) + diff_for_path(from: source_ref, to: target_ref.succ, old_path: existing_path, new_path: existing_path) end it 'returns a 404' do @@ -170,4 +175,153 @@ describe Projects::CompareController do end end end + + describe 'POST create' do + subject(:create_request) { post :create, request_params } + + 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 + subject(:signatures_request) { get :signatures, request_params } + + 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) + parsed_body = JSON.parse(response.body) + signatures = parsed_body['signatures'] + + 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) + parsed_body = JSON.parse(response.body) + expect(parsed_body['signatures']).to be_empty + 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) + parsed_body = JSON.parse(response.body) + expect(parsed_body['signatures']).to be_empty + end + end + end end diff --git a/spec/controllers/projects/jobs_controller_spec.rb b/spec/controllers/projects/jobs_controller_spec.rb index b9a979044fe..2281cb420d9 100644 --- a/spec/controllers/projects/jobs_controller_spec.rb +++ b/spec/controllers/projects/jobs_controller_spec.rb @@ -1,7 +1,7 @@ # coding: utf-8 require 'spec_helper' -describe Projects::JobsController do +describe Projects::JobsController, :clean_gitlab_redis_shared_state do include ApiHelpers include HttpIOHelpers @@ -10,6 +10,7 @@ describe Projects::JobsController do let(:user) { create(:user) } before do + stub_feature_flags(ci_enable_live_trace: true) stub_not_protect_default_branch end diff --git a/spec/controllers/projects/merge_requests/creations_controller_spec.rb b/spec/controllers/projects/merge_requests/creations_controller_spec.rb index 24310b847e8..00d76f3c39a 100644 --- a/spec/controllers/projects/merge_requests/creations_controller_spec.rb +++ b/spec/controllers/projects/merge_requests/creations_controller_spec.rb @@ -157,34 +157,4 @@ describe Projects::MergeRequests::CreationsController do expect(response).to have_gitlab_http_status(200) end end - - describe 'GET #update_branches' do - before do - allow(Ability).to receive(:allowed?).and_call_original - end - - it 'lists the branches of another fork if the user has access' do - expect(Ability).to receive(:allowed?).with(user, :read_project, project) { true } - - get :update_branches, - namespace_id: fork_project.namespace, - project_id: fork_project, - target_project_id: project.id - - expect(assigns(:target_branches)).not_to be_empty - expect(response).to have_gitlab_http_status(200) - end - - it 'does not list branches when the user cannot read the project' do - expect(Ability).to receive(:allowed?).with(user, :read_project, project) { false } - - get :update_branches, - namespace_id: fork_project.namespace, - project_id: fork_project, - target_project_id: project.id - - expect(response).to have_gitlab_http_status(200) - expect(assigns(:target_branches)).to eq([]) - end - end end diff --git a/spec/controllers/projects/mirrors_controller_spec.rb b/spec/controllers/projects/mirrors_controller_spec.rb new file mode 100644 index 00000000000..45c1218a39c --- /dev/null +++ b/spec/controllers/projects/mirrors_controller_spec.rb @@ -0,0 +1,72 @@ +require 'spec_helper' + +describe Projects::MirrorsController do + include ReactiveCachingHelpers + + describe 'setting up a remote mirror' do + set(:project) { create(:project, :repository) } + + context 'when the current project is not a mirror' do + it 'allows to create a remote mirror' do + sign_in(project.owner) + + expect do + do_put(project, remote_mirrors_attributes: { '0' => { 'enabled' => 1, 'url' => 'http://foo.com' } }) + end.to change { RemoteMirror.count }.to(1) + end + end + end + + describe '#update' do + let(:project) { create(:project, :repository, :remote_mirror) } + + before do + sign_in(project.owner) + end + + around do |example| + Sidekiq::Testing.fake! { example.run } + end + + context 'With valid URL for a push' do + let(:remote_mirror_attributes) do + { "0" => { "enabled" => "0", url: 'https://updated.example.com' } } + end + + it 'processes a successful update' do + do_put(project, remote_mirrors_attributes: remote_mirror_attributes) + + expect(response).to redirect_to(project_settings_repository_path(project)) + expect(flash[:notice]).to match(/successfully updated/) + end + + it 'should create a RemoteMirror object' do + expect { do_put(project, remote_mirrors_attributes: remote_mirror_attributes) }.to change(RemoteMirror, :count).by(1) + end + end + + context 'With invalid URL for a push' do + let(:remote_mirror_attributes) do + { "0" => { "enabled" => "0", url: 'ftp://invalid.invalid' } } + end + + it 'processes an unsuccessful update' do + do_put(project, remote_mirrors_attributes: remote_mirror_attributes) + + expect(response).to redirect_to(project_settings_repository_path(project)) + expect(flash[:alert]).to match(/must be a valid URL/) + end + + it 'should not create a RemoteMirror object' do + expect { do_put(project, remote_mirrors_attributes: remote_mirror_attributes) }.not_to change(RemoteMirror, :count) + end + end + end + + def do_put(project, options, extra_attrs = {}) + attrs = extra_attrs.merge(namespace_id: project.namespace.to_param, project_id: project.to_param) + attrs[:project] = options + + put :update, attrs + end +end diff --git a/spec/controllers/projects/pipelines_controller_spec.rb b/spec/controllers/projects/pipelines_controller_spec.rb index 35ac999cc65..a451bbb97b6 100644 --- a/spec/controllers/projects/pipelines_controller_spec.rb +++ b/spec/controllers/projects/pipelines_controller_spec.rb @@ -109,8 +109,7 @@ describe Projects::PipelinesController do it 'returns html source for stage dropdown' do expect(response).to have_gitlab_http_status(:ok) - expect(response).to render_template('projects/pipelines/_stage') - expect(json_response).to include('html') + expect(response).to match_response_schema('pipeline_stage') end end @@ -133,6 +132,42 @@ describe Projects::PipelinesController do end end + describe 'GET stages_ajax.json' do + let(:pipeline) { create(:ci_pipeline, project: project) } + + context 'when accessing existing stage' do + before do + create(:ci_build, pipeline: pipeline, stage: 'build') + + get_stage_ajax('build') + end + + it 'returns html source for stage dropdown' do + expect(response).to have_gitlab_http_status(:ok) + expect(response).to render_template('projects/pipelines/_stage') + expect(json_response).to include('html') + end + end + + context 'when accessing unknown stage' do + before do + get_stage_ajax('test') + end + + it 'responds with not found' do + expect(response).to have_gitlab_http_status(:not_found) + end + end + + def get_stage_ajax(name) + get :stage_ajax, namespace_id: project.namespace, + project_id: project, + id: pipeline.id, + stage: name, + format: :json + end + end + describe 'GET status.json' do let(:pipeline) { create(:ci_pipeline, project: project) } let(:status) { pipeline.detailed_status(double('user')) } diff --git a/spec/controllers/projects/settings/ci_cd_controller_spec.rb b/spec/controllers/projects/settings/ci_cd_controller_spec.rb index 7dae9b85d78..a91c868cbaf 100644 --- a/spec/controllers/projects/settings/ci_cd_controller_spec.rb +++ b/spec/controllers/projects/settings/ci_cd_controller_spec.rb @@ -17,6 +17,23 @@ describe Projects::Settings::CiCdController do expect(response).to have_gitlab_http_status(200) expect(response).to render_template(:show) end + + context 'with group runners' do + let(:group_runner) { create(:ci_runner) } + let(:parent_group) { create(:group) } + let(:group) { create(:group, runners: [group_runner], parent: parent_group) } + let(:other_project) { create(:project, group: group) } + let!(:project_runner) { create(:ci_runner, projects: [other_project]) } + let!(:shared_runner) { create(:ci_runner, :shared) } + + it 'sets assignable project runners only' do + group.add_master(user) + + get :show, namespace_id: project.namespace, project_id: project + + expect(assigns(:assignable_runners)).to eq [project_runner] + end + end end describe '#reset_cache' do |