diff options
Diffstat (limited to 'spec/controllers/projects')
9 files changed, 183 insertions, 289 deletions
diff --git a/spec/controllers/projects/analytics/cycle_analytics/summary_controller_spec.rb b/spec/controllers/projects/analytics/cycle_analytics/summary_controller_spec.rb index 1832b84ab6e..a366b2583d4 100644 --- a/spec/controllers/projects/analytics/cycle_analytics/summary_controller_spec.rb +++ b/spec/controllers/projects/analytics/cycle_analytics/summary_controller_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Projects::Analytics::CycleAnalytics::SummaryController do let_it_be(:user) { create(:user) } let_it_be(:project) { create(:project) } - let(:params) { { namespace_id: project.namespace.to_param, project_id: project.to_param, created_after: '2010-01-01', created_before: '2010-01-02' } } + let(:params) { { namespace_id: project.namespace.to_param, project_id: project.to_param, created_after: '2010-01-01', created_before: '2010-02-01' } } before do sign_in(user) @@ -42,5 +42,39 @@ RSpec.describe Projects::Analytics::CycleAnalytics::SummaryController do expect(response).to have_gitlab_http_status(:not_found) end end + + context 'when filters are applied' do + let_it_be(:author) { create(:user) } + let_it_be(:milestone) { create(:milestone, title: 'milestone 1', project: project) } + let_it_be(:issue_with_author) { create(:issue, project: project, author: author, created_at: Date.new(2010, 1, 15)) } + let_it_be(:issue_with_other_author) { create(:issue, project: project, author: user, created_at: Date.new(2010, 1, 15)) } + let_it_be(:issue_with_milestone) { create(:issue, project: project, milestone: milestone, created_at: Date.new(2010, 1, 15)) } + + before do + project.add_reporter(user) + end + + it 'filters by author username' do + params[:author_username] = author.username + + subject + + expect(response).to be_successful + + issue_count = json_response.first + expect(issue_count['value']).to eq('1') + end + + it 'filters by milestone title' do + params[:milestone_title] = milestone.title + + subject + + expect(response).to be_successful + + issue_count = json_response.first + expect(issue_count['value']).to eq('1') + end + end end end diff --git a/spec/controllers/projects/environments_controller_spec.rb b/spec/controllers/projects/environments_controller_spec.rb index 7103d7df5c5..0fcdeb2edde 100644 --- a/spec/controllers/projects/environments_controller_spec.rb +++ b/spec/controllers/projects/environments_controller_spec.rb @@ -222,6 +222,16 @@ RSpec.describe Projects::EnvironmentsController do expect(response).to have_gitlab_http_status(:bad_request) end end + + context 'when name is passed' do + let(:params) { environment_params.merge(environment: { name: "new name" }) } + + it 'ignores name' do + expect do + subject + end.not_to change { environment.reload.name } + end + end end describe 'PATCH #stop' do diff --git a/spec/controllers/projects/feature_flags_controller_spec.rb b/spec/controllers/projects/feature_flags_controller_spec.rb index e038b247eff..fd95aa44568 100644 --- a/spec/controllers/projects/feature_flags_controller_spec.rb +++ b/spec/controllers/projects/feature_flags_controller_spec.rb @@ -94,20 +94,6 @@ RSpec.describe Projects::FeatureFlagsController do is_expected.to match_response_schema('feature_flags') end - it 'returns false for active when the feature flag is inactive even if it has an active scope' do - create(:operations_feature_flag_scope, - feature_flag: feature_flag_inactive, - environment_scope: 'production', - active: true) - - subject - - expect(response).to have_gitlab_http_status(:ok) - feature_flag_json = json_response['feature_flags'].second - - expect(feature_flag_json['active']).to eq(false) - end - it 'returns the feature flag iid' do subject @@ -181,7 +167,7 @@ RSpec.describe Projects::FeatureFlagsController do subject { get(:show, params: params, format: :json) } let!(:feature_flag) do - create(:operations_feature_flag, :legacy_flag, project: project) + create(:operations_feature_flag, project: project) end let(:params) do @@ -197,7 +183,7 @@ RSpec.describe Projects::FeatureFlagsController do expect(json_response['name']).to eq(feature_flag.name) expect(json_response['active']).to eq(feature_flag.active) - expect(json_response['version']).to eq('legacy_flag') + expect(json_response['version']).to eq('new_version_flag') end it 'matches json schema' do @@ -245,46 +231,6 @@ RSpec.describe Projects::FeatureFlagsController do end end - context 'when feature flags have additional scopes' do - context 'when there is at least one active scope' do - let!(:feature_flag) do - create(:operations_feature_flag, project: project, active: false) - end - - let!(:feature_flag_scope_production) do - create(:operations_feature_flag_scope, - feature_flag: feature_flag, - environment_scope: 'review/*', - active: true) - end - - it 'returns false for active' do - subject - - expect(json_response['active']).to eq(false) - end - end - - context 'when all scopes are inactive' do - let!(:feature_flag) do - create(:operations_feature_flag, project: project, active: false) - end - - let!(:feature_flag_scope_production) do - create(:operations_feature_flag_scope, - feature_flag: feature_flag, - environment_scope: 'production', - active: false) - end - - it 'recognizes the feature flag as inactive' do - subject - - expect(json_response['active']).to be_falsy - end - end - end - context 'with a version 2 feature flag' do let!(:new_version_feature_flag) do create(:operations_feature_flag, :new_version_flag, project: project) @@ -320,22 +266,6 @@ RSpec.describe Projects::FeatureFlagsController do describe 'GET edit' do subject { get(:edit, params: params) } - context 'with legacy flags' do - let!(:feature_flag) { create(:operations_feature_flag, :legacy_flag, project: project) } - - let(:params) do - { - namespace_id: project.namespace, - project_id: project, - iid: feature_flag.iid - } - end - - it 'returns not found' do - is_expected.to have_gitlab_http_status(:not_found) - end - end - context 'with new version flags' do let!(:feature_flag) { create(:operations_feature_flag, project: project) } @@ -378,14 +308,6 @@ RSpec.describe Projects::FeatureFlagsController do expect(json_response['active']).to be_truthy end - it 'creates a default scope' do - subject - - expect(json_response['scopes'].count).to eq(1) - expect(json_response['scopes'].first['environment_scope']).to eq('*') - expect(json_response['scopes'].first['active']).to be_truthy - end - it 'matches json schema' do is_expected.to match_response_schema('feature_flag') end @@ -435,119 +357,6 @@ RSpec.describe Projects::FeatureFlagsController do end end - context 'when creates additional scope' do - let(:params) do - view_params.merge({ - operations_feature_flag: { - name: 'my_feature_flag', - active: true, - scopes_attributes: [{ environment_scope: '*', active: true }, - { environment_scope: 'production', active: false }] - } - }) - end - - it 'creates feature flag scopes successfully' do - expect { subject }.to change { Operations::FeatureFlagScope.count }.by(2) - - expect(response).to have_gitlab_http_status(:ok) - end - - it 'creates feature flag scopes in a correct order' do - subject - - expect(json_response['scopes'].first['environment_scope']).to eq('*') - expect(json_response['scopes'].second['environment_scope']).to eq('production') - end - - context 'when default scope is not placed first' do - let(:params) do - view_params.merge({ - operations_feature_flag: { - name: 'my_feature_flag', - active: true, - scopes_attributes: [{ environment_scope: 'production', active: false }, - { environment_scope: '*', active: true }] - } - }) - end - - it 'returns 400' do - subject - - expect(response).to have_gitlab_http_status(:bad_request) - expect(json_response['message']) - .to include('Default scope has to be the first element') - end - end - end - - context 'when creates additional scope with a percentage rollout' do - it 'creates a strategy for the scope' do - params = view_params.merge({ - operations_feature_flag: { - name: 'my_feature_flag', - active: true, - scopes_attributes: [{ environment_scope: '*', active: true }, - { environment_scope: 'production', active: false, - strategies: [{ name: 'gradualRolloutUserId', - parameters: { groupId: 'default', percentage: '42' } }] }] - } - }) - - post(:create, params: params, format: :json) - - expect(response).to have_gitlab_http_status(:ok) - production_strategies_json = json_response['scopes'].second['strategies'] - expect(production_strategies_json).to eq([{ - 'name' => 'gradualRolloutUserId', - 'parameters' => { "groupId" => "default", "percentage" => "42" } - }]) - end - end - - context 'when creates additional scope with a userWithId strategy' do - it 'creates a strategy for the scope' do - params = view_params.merge({ - operations_feature_flag: { - name: 'my_feature_flag', - active: true, - scopes_attributes: [{ environment_scope: '*', active: true }, - { environment_scope: 'production', active: false, - strategies: [{ name: 'userWithId', - parameters: { userIds: '123,4,6722' } }] }] - } - }) - - post(:create, params: params, format: :json) - - expect(response).to have_gitlab_http_status(:ok) - production_strategies_json = json_response['scopes'].second['strategies'] - expect(production_strategies_json).to eq([{ - 'name' => 'userWithId', - 'parameters' => { "userIds" => "123,4,6722" } - }]) - end - end - - context 'when creates an additional scope without a strategy' do - it 'creates a default strategy' do - params = view_params.merge({ - operations_feature_flag: { - name: 'my_feature_flag', - active: true, - scopes_attributes: [{ environment_scope: '*', active: true }] - } - }) - - post(:create, params: params, format: :json) - - expect(response).to have_gitlab_http_status(:ok) - default_strategies_json = json_response['scopes'].first['strategies'] - expect(default_strategies_json).to eq([{ "name" => "default", "parameters" => {} }]) - end - end - context 'when creating a version 2 feature flag' do let(:params) do { @@ -744,7 +553,7 @@ RSpec.describe Projects::FeatureFlagsController do describe 'DELETE destroy.json' do subject { delete(:destroy, params: params, format: :json) } - let!(:feature_flag) { create(:operations_feature_flag, :legacy_flag, project: project) } + let!(:feature_flag) { create(:operations_feature_flag, project: project) } let(:params) do { @@ -762,10 +571,6 @@ RSpec.describe Projects::FeatureFlagsController do expect { subject }.to change { Operations::FeatureFlag.count }.by(-1) end - it 'destroys the default scope' do - expect { subject }.to change { Operations::FeatureFlagScope.count }.by(-1) - end - it 'matches json schema' do is_expected.to match_response_schema('feature_flag') end @@ -792,14 +597,6 @@ RSpec.describe Projects::FeatureFlagsController do end end - context 'when there is an additional scope' do - let!(:scope) { create_scope(feature_flag, 'production', false) } - - it 'destroys the default scope and production scope' do - expect { subject }.to change { Operations::FeatureFlagScope.count }.by(-2) - end - end - context 'with a version 2 flag' do let!(:new_version_flag) { create(:operations_feature_flag, :new_version_flag, project: project) } let(:params) do @@ -828,70 +625,9 @@ RSpec.describe Projects::FeatureFlagsController do put(:update, params: params, format: :json, as: :json) end - context 'with a legacy feature flag' do - subject { put(:update, params: params, format: :json) } - - let!(:feature_flag) do - create(:operations_feature_flag, - :legacy_flag, - name: 'ci_live_trace', - active: true, - project: project) - end - - let(:params) do - { - namespace_id: project.namespace, - project_id: project, - iid: feature_flag.iid, - operations_feature_flag: { - name: 'ci_new_live_trace' - } - } - end - - context 'when user is reporter' do - let(:user) { reporter } - - it 'returns 404' do - is_expected.to have_gitlab_http_status(:not_found) - end - end - - context "when changing default scope's spec" do - let(:params) do - { - namespace_id: project.namespace, - project_id: project, - iid: feature_flag.iid, - operations_feature_flag: { - scopes_attributes: [ - { - id: feature_flag.default_scope.id, - environment_scope: 'review/*' - } - ] - } - } - end - - it 'returns 400' do - is_expected.to have_gitlab_http_status(:bad_request) - end - end - - it 'does not update a legacy feature flag' do - put_request(feature_flag, name: 'ci_new_live_trace') - - expect(response).to have_gitlab_http_status(:bad_request) - expect(json_response['message']).to eq(["Legacy feature flags are read-only"]) - end - end - context 'with a version 2 feature flag' do let!(:new_version_flag) do create(:operations_feature_flag, - :new_version_flag, name: 'new-feature', active: true, project: project) diff --git a/spec/controllers/projects/issues_controller_spec.rb b/spec/controllers/projects/issues_controller_spec.rb index 0c29280316a..977879b453c 100644 --- a/spec/controllers/projects/issues_controller_spec.rb +++ b/spec/controllers/projects/issues_controller_spec.rb @@ -109,6 +109,14 @@ RSpec.describe Projects::IssuesController do end end + it_behaves_like 'issuable list with anonymous search disabled' do + let(:params) { { namespace_id: project.namespace, project_id: project } } + + before do + project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + end + end + it_behaves_like 'paginated collection' do let!(:issue_list) { create_list(:issue, 2, project: project) } let(:collection) { project.issues } @@ -301,6 +309,8 @@ RSpec.describe Projects::IssuesController do it 'fills in an issue for a discussion' do note = create(:note_on_merge_request, project: project) + expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter).to receive(:track_resolve_thread_in_issue_action).with(user: user) + get :new, params: { namespace_id: project.namespace.path, project_id: project, merge_request_to_resolve_discussions_of: note.noteable.iid, discussion_to_resolve: note.discussion_id } expect(assigns(:issue).title).not_to be_empty @@ -1176,12 +1186,22 @@ RSpec.describe Projects::IssuesController do project.issues.first end + context 'when creating an incident' do + it 'sets the correct issue_type' do + issue = post_new_issue(issue_type: 'incident') + + expect(issue.issue_type).to eq('incident') + expect(issue.work_item_type.base_type).to eq('incident') + end + end + it 'creates the issue successfully', :aggregate_failures do issue = post_new_issue expect(issue).to be_a(Issue) expect(issue.persisted?).to eq(true) expect(issue.issue_type).to eq('issue') + expect(issue.work_item_type.base_type).to eq('issue') end context 'resolving discussions in MergeRequest' do diff --git a/spec/controllers/projects/jobs_controller_spec.rb b/spec/controllers/projects/jobs_controller_spec.rb index e9e7c3c3bb3..06c29e767ad 100644 --- a/spec/controllers/projects/jobs_controller_spec.rb +++ b/spec/controllers/projects/jobs_controller_spec.rb @@ -755,23 +755,52 @@ RSpec.describe Projects::JobsController, :clean_gitlab_redis_shared_state do before do project.add_developer(user) sign_in(user) - - post_retry end context 'when job is retryable' do let(:job) { create(:ci_build, :retryable, pipeline: pipeline) } it 'redirects to the retried job page' do + post_retry + expect(response).to have_gitlab_http_status(:found) expect(response).to redirect_to(namespace_project_job_path(id: Ci::Build.last.id)) end + + shared_examples_for 'retried job has the same attributes' do + it 'creates a new build has the same attributes from the previous build' do + expect { post_retry }.to change { Ci::Build.count }.by(1) + + retried_build = Ci::Build.last + + Ci::RetryBuildService.clone_accessors.each do |accessor| + expect(job.read_attribute(accessor)) + .to eq(retried_build.read_attribute(accessor)), + "Mismatched attribute on \"#{accessor}\". " \ + "It was \"#{job.read_attribute(accessor)}\" but changed to \"#{retried_build.read_attribute(accessor)}\"" + end + end + end + + context 'with branch pipeline' do + let!(:job) { create(:ci_build, :retryable, tag: true, when: 'on_success', pipeline: pipeline) } + + it_behaves_like 'retried job has the same attributes' + end + + context 'with tag pipeline' do + let!(:job) { create(:ci_build, :retryable, tag: false, when: 'on_success', pipeline: pipeline) } + + it_behaves_like 'retried job has the same attributes' + end end context 'when job is not retryable' do let(:job) { create(:ci_build, pipeline: pipeline) } it 'renders unprocessable_entity' do + post_retry + expect(response).to have_gitlab_http_status(:unprocessable_entity) end end diff --git a/spec/controllers/projects/learn_gitlab_controller_spec.rb b/spec/controllers/projects/learn_gitlab_controller_spec.rb index f633f7aa246..620982f73be 100644 --- a/spec/controllers/projects/learn_gitlab_controller_spec.rb +++ b/spec/controllers/projects/learn_gitlab_controller_spec.rb @@ -7,13 +7,13 @@ RSpec.describe Projects::LearnGitlabController do let_it_be(:user) { create(:user) } let_it_be(:project) { create(:project, namespace: user.namespace) } - let(:learn_gitlab_experiment_enabled) { true } + let(:learn_gitlab_enabled) { true } let(:params) { { namespace_id: project.namespace.to_param, project_id: project } } subject { get :index, params: params } before do - allow(controller.helpers).to receive(:learn_gitlab_experiment_enabled?).and_return(learn_gitlab_experiment_enabled) + allow(controller.helpers).to receive(:learn_gitlab_enabled?).and_return(learn_gitlab_enabled) end context 'unauthenticated user' do @@ -27,15 +27,8 @@ RSpec.describe Projects::LearnGitlabController do it { is_expected.to render_template(:index) } - it 'pushes experiment to frontend' do - expect(controller).to receive(:push_frontend_experiment).with(:learn_gitlab_a, subject: user) - expect(controller).to receive(:push_frontend_experiment).with(:learn_gitlab_b, subject: user) - - subject - end - context 'learn_gitlab experiment not enabled' do - let(:learn_gitlab_experiment_enabled) { false } + let(:learn_gitlab_enabled) { false } it { is_expected.to have_gitlab_http_status(:not_found) } end diff --git a/spec/controllers/projects/merge_requests_controller_spec.rb b/spec/controllers/projects/merge_requests_controller_spec.rb index 7b5a58fe2e5..0da8a30611c 100644 --- a/spec/controllers/projects/merge_requests_controller_spec.rb +++ b/spec/controllers/projects/merge_requests_controller_spec.rb @@ -349,6 +349,15 @@ RSpec.describe Projects::MergeRequestsController do end end end + + it_behaves_like 'issuable list with anonymous search disabled' do + let(:params) { { namespace_id: project.namespace, project_id: project } } + + before do + sign_out(user) + project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + end + end end describe 'PUT update' do diff --git a/spec/controllers/projects/pipelines_controller_spec.rb b/spec/controllers/projects/pipelines_controller_spec.rb index 65a563fac7c..1354e894872 100644 --- a/spec/controllers/projects/pipelines_controller_spec.rb +++ b/spec/controllers/projects/pipelines_controller_spec.rb @@ -311,23 +311,42 @@ RSpec.describe Projects::PipelinesController do let_it_be(:pipeline) { create(:ci_pipeline, project: project) } - def create_build_with_artifacts(stage, stage_idx, name) - create(:ci_build, :artifacts, :tags, pipeline: pipeline, stage: stage, stage_idx: stage_idx, name: name) + def create_build_with_artifacts(stage, stage_idx, name, status) + create(:ci_build, :artifacts, :tags, status, user: user, pipeline: pipeline, stage: stage, stage_idx: stage_idx, name: name) + end + + def create_bridge(stage, stage_idx, name, status) + create(:ci_bridge, status, pipeline: pipeline, stage: stage, stage_idx: stage_idx, name: name) end before do - create_build_with_artifacts('build', 0, 'job1') - create_build_with_artifacts('build', 0, 'job2') + create_build_with_artifacts('build', 0, 'job1', :failed) + create_build_with_artifacts('build', 0, 'job2', :running) + create_build_with_artifacts('build', 0, 'job3', :pending) + create_bridge('deploy', 1, 'deploy-a', :failed) + create_bridge('deploy', 1, 'deploy-b', :created) end - it 'avoids N+1 database queries', :request_store do - control_count = ActiveRecord::QueryRecorder.new { get_pipeline_html }.count + it 'avoids N+1 database queries', :request_store, :use_sql_query_cache do + # warm up + get_pipeline_html expect(response).to have_gitlab_http_status(:ok) - create_build_with_artifacts('build', 0, 'job3') + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do + get_pipeline_html + expect(response).to have_gitlab_http_status(:ok) + end - expect { get_pipeline_html }.not_to exceed_query_limit(control_count) - expect(response).to have_gitlab_http_status(:ok) + create_build_with_artifacts('build', 0, 'job4', :failed) + create_build_with_artifacts('build', 0, 'job5', :running) + create_build_with_artifacts('build', 0, 'job6', :pending) + create_bridge('deploy', 1, 'deploy-c', :failed) + create_bridge('deploy', 1, 'deploy-d', :created) + + expect do + get_pipeline_html + expect(response).to have_gitlab_http_status(:ok) + end.not_to exceed_all_query_limit(control) end end @@ -1273,6 +1292,38 @@ RSpec.describe Projects::PipelinesController do end end + context 'when project uses external project ci config' do + let(:other_project) { create(:project) } + let(:sha) { 'master' } + let(:service) { ::Ci::ListConfigVariablesService.new(other_project, user) } + + let(:ci_config) do + { + variables: { + KEY1: { value: 'val 1', description: 'description 1' } + }, + test: { + stage: 'test', + script: 'echo' + } + } + end + + before do + project.update!(ci_config_path: ".gitlab-ci.yml@#{other_project.full_path}") + synchronous_reactive_cache(service) + end + + it 'returns other project config variables' do + expect(::Ci::ListConfigVariablesService).to receive(:new).with(other_project, anything).and_return(service) + + get_config_variables + + expect(response).to have_gitlab_http_status(:ok) + expect(json_response['KEY1']).to eq({ 'value' => 'val 1', 'description' => 'description 1' }) + end + end + private def stub_gitlab_ci_yml_for_sha(sha, result) diff --git a/spec/controllers/projects/services_controller_spec.rb b/spec/controllers/projects/services_controller_spec.rb index 419b5c7e101..482ba552f8f 100644 --- a/spec/controllers/projects/services_controller_spec.rb +++ b/spec/controllers/projects/services_controller_spec.rb @@ -18,6 +18,18 @@ RSpec.describe Projects::ServicesController do project.add_maintainer(user) end + it_behaves_like IntegrationsActions do + let(:integration_attributes) { { project: project } } + + let(:routing_params) do + { + namespace_id: project.namespace, + project_id: project, + id: integration.to_param + } + end + end + describe '#test' do context 'when the integration is not testable' do it 'renders 404' do |