diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-08-08 12:12:15 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-08-08 12:12:15 +0300 |
commit | 5bd8b9d24f16a0b9773c55cd0680ed98d345b07c (patch) | |
tree | 14f131de11550de67ef8a38c369b4362245272b2 /spec | |
parent | 5bc16a3b1a97fc22d34be1b23cb2fc9050a9bd06 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
11 files changed, 214 insertions, 40 deletions
diff --git a/spec/controllers/projects/issues_controller_spec.rb b/spec/controllers/projects/issues_controller_spec.rb index badac688229..c48be8efb1b 100644 --- a/spec/controllers/projects/issues_controller_spec.rb +++ b/spec/controllers/projects/issues_controller_spec.rb @@ -1607,22 +1607,32 @@ RSpec.describe Projects::IssuesController do project.add_developer(user) end - it "returns 302 for project members with developer role" do - import_csv + context 'when upload proceeds correctly' do + it "returns 302 for project members with developer role" do + import_csv - expect(flash[:notice]).to eq(_("Your issues are being imported. Once finished, you'll get a confirmation email.")) - expect(response).to redirect_to(project_issues_path(project)) - end + expect(flash[:notice]).to eq(_("Your issues are being imported. Once finished, you'll get a confirmation email.")) + expect(response).to redirect_to(project_issues_path(project)) + end + + it 'enqueues an import job' do + expect(ImportIssuesCsvWorker).to receive(:perform_async).with(user.id, project.id, Integer) - it "shows error when upload fails" do - expect_next_instance_of(UploadService) do |upload_service| - expect(upload_service).to receive(:execute).and_return(nil) + import_csv end + end - import_csv + context 'when upload fails' do + it "shows error when upload fails" do + expect_next_instance_of(UploadService) do |upload_service| + expect(upload_service).to receive(:execute).and_return(nil) + end - expect(flash[:alert]).to include(_('File upload error.')) - expect(response).to redirect_to(project_issues_path(project)) + import_csv + + expect(flash[:alert]).to include(_('File upload error.')) + expect(response).to redirect_to(project_issues_path(project)) + end end end diff --git a/spec/frontend/ide/utils_spec.js b/spec/frontend/ide/utils_spec.js index 2f8447af518..fd9d481251d 100644 --- a/spec/frontend/ide/utils_spec.js +++ b/spec/frontend/ide/utils_spec.js @@ -46,7 +46,7 @@ describe('WebIDE utils', () => { content: 'SELECT "éêė" from tablename', mimeType: 'application/sql', }), - ).toBeFalsy(); + ).toBe(false); }); it('returns true for ASCII only content for unknown types', () => { @@ -56,7 +56,7 @@ describe('WebIDE utils', () => { content: 'plain text', mimeType: 'application/x-new-type', }), - ).toBeTruthy(); + ).toBe(true); }); it('returns false for non-ASCII content for unknown types', () => { @@ -66,7 +66,7 @@ describe('WebIDE utils', () => { content: '{"éêė":"value"}', mimeType: 'application/octet-stream', }), - ).toBeFalsy(); + ).toBe(false); }); it.each` diff --git a/spec/lib/gitlab/ci/pipeline/chain/evaluate_workflow_rules_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/evaluate_workflow_rules_spec.rb index e30a78546af..eb5a37f19f4 100644 --- a/spec/lib/gitlab/ci/pipeline/chain/evaluate_workflow_rules_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/chain/evaluate_workflow_rules_spec.rb @@ -45,7 +45,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Chain::EvaluateWorkflowRules do before do allow(step).to receive(:workflow_rules_result) .and_return( - double(pass?: true, variables: { 'VAR1' => 'val2' }) + double(pass?: true, variables: { 'VAR1' => 'val2', 'VAR2' => 3 }) ) step.perform! @@ -65,7 +65,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Chain::EvaluateWorkflowRules do end it 'saves workflow_rules_result' do - expect(command.workflow_rules_result.variables).to eq({ 'VAR1' => 'val2' }) + expect(command.workflow_rules_result.variables).to eq({ 'VAR1' => 'val2', 'VAR2' => 3 }) end end end diff --git a/spec/lib/gitlab/ci/pipeline/chain/seed_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/seed_spec.rb index 1e73a7efc65..f7774e199fb 100644 --- a/spec/lib/gitlab/ci/pipeline/chain/seed_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/chain/seed_spec.rb @@ -205,6 +205,30 @@ RSpec.describe Gitlab::Ci::Pipeline::Chain::Seed do end end + describe '#rule_variables' do + let(:config) do + { + variables: { VAR1: 11 }, + workflow: { + rules: [{ if: '$CI_PIPELINE_SOURCE', + variables: { SYMBOL: :symbol, STRING: "string", INTEGER: 1 } }, + { when: 'always' }] + }, + rspec: { script: 'rake' } + } + end + + let(:rspec_variables) { command.pipeline_seed.stages[0].statuses[0].variables.to_hash } + + it 'correctly parses rule variables' do + run_chain + + expect(rspec_variables['SYMBOL']).to eq("symbol") + expect(rspec_variables['STRING']).to eq("string") + expect(rspec_variables['INTEGER']).to eq("1") + end + end + context 'N+1 queries' do it 'avoids N+1 queries when calculating variables of jobs', :use_sql_query_cache do warm_up_pipeline, warm_up_command = prepare_pipeline1 diff --git a/spec/lib/gitlab/ci/variables/helpers_spec.rb b/spec/lib/gitlab/ci/variables/helpers_spec.rb index f13b334c10e..fc1055751bd 100644 --- a/spec/lib/gitlab/ci/variables/helpers_spec.rb +++ b/spec/lib/gitlab/ci/variables/helpers_spec.rb @@ -99,6 +99,18 @@ RSpec.describe Gitlab::Ci::Variables::Helpers do it { is_expected.to eq(result) } end + + context 'when variables contain integers and symbols' do + let(:variables) do + { key1: 1, key2: :value2 } + end + + let(:result1) do + { 'key1' => '1', 'key2' => 'value2' } + end + + it { is_expected.to eq(result1) } + end end describe '.inherit_yaml_variables' do diff --git a/spec/services/ci/runners/reconcile_existing_runner_versions_service_spec.rb b/spec/services/ci/runners/reconcile_existing_runner_versions_service_spec.rb index 47a7ae4af4d..1690190320a 100644 --- a/spec/services/ci/runners/reconcile_existing_runner_versions_service_spec.rb +++ b/spec/services/ci/runners/reconcile_existing_runner_versions_service_spec.rb @@ -42,14 +42,13 @@ RSpec.describe ::Ci::Runners::ReconcileExistingRunnerVersionsService, '#execute' .once .and_call_original - result = nil - expect { result = execute } + expect { execute } .to change { runner_version_14_0_0.reload.status }.from('not_available').to('recommended') .and change { runner_version_14_0_1.reload.status }.from('not_available').to('recommended') .and change { ::Ci::RunnerVersion.find_by(version: '14.0.2')&.status }.from(nil).to('not_available') - expect(result).to eq({ - status: :success, + expect(execute).to be_success + expect(execute.payload).to eq({ total_inserted: 1, # 14.0.2 is inserted total_updated: 3, # 14.0.0, 14.0.1 are updated, and newly inserted 14.0.2's status is calculated total_deleted: 0 @@ -66,13 +65,12 @@ RSpec.describe ::Ci::Runners::ReconcileExistingRunnerVersionsService, '#execute' end it 'deletes orphan ci_runner_versions entry', :aggregate_failures do - result = nil - expect { result = execute } + expect { execute } .to change { ::Ci::RunnerVersion.find_by_version('14.0.2')&.status }.from('not_available').to(nil) .and not_change { runner_version_14_0_1.reload.status }.from('not_available') - expect(result).to eq({ - status: :success, + expect(execute).to be_success + expect(execute.payload).to eq({ total_inserted: 0, total_updated: 0, total_deleted: 1 # 14.0.2 is deleted @@ -87,11 +85,10 @@ RSpec.describe ::Ci::Runners::ReconcileExistingRunnerVersionsService, '#execute' end it 'does not modify ci_runner_versions entries', :aggregate_failures do - result = nil - expect { result = execute }.not_to change { runner_version_14_0_1.reload.status }.from('not_available') + expect { execute }.not_to change { runner_version_14_0_1.reload.status }.from('not_available') - expect(result).to eq({ - status: :success, + expect(execute).to be_success + expect(execute.payload).to eq({ total_inserted: 0, total_updated: 0, total_deleted: 0 @@ -106,11 +103,10 @@ RSpec.describe ::Ci::Runners::ReconcileExistingRunnerVersionsService, '#execute' end it 'makes no changes to ci_runner_versions', :aggregate_failures do - result = nil - expect { result = execute }.not_to change { runner_version_14_0_1.reload.status }.from('not_available') + expect { execute }.not_to change { runner_version_14_0_1.reload.status }.from('not_available') - expect(result).to eq({ - status: :success, + expect(execute).to be_success + expect(execute.payload).to eq({ total_inserted: 0, total_updated: 0, total_deleted: 0 @@ -125,11 +121,10 @@ RSpec.describe ::Ci::Runners::ReconcileExistingRunnerVersionsService, '#execute' end it 'does not modify ci_runner_versions entries', :aggregate_failures do - result = nil - expect { result = execute }.not_to change { runner_version_14_0_1.reload.status }.from('not_available') + expect { execute }.not_to change { runner_version_14_0_1.reload.status }.from('not_available') - expect(result).to eq({ - status: :success, + expect(execute).to be_success + expect(execute.payload).to eq({ total_inserted: 0, total_updated: 0, total_deleted: 0 diff --git a/spec/services/ci/runners/unregister_runner_service_spec.rb b/spec/services/ci/runners/unregister_runner_service_spec.rb index df1a0a90067..77fc299e4e1 100644 --- a/spec/services/ci/runners/unregister_runner_service_spec.rb +++ b/spec/services/ci/runners/unregister_runner_service_spec.rb @@ -3,13 +3,16 @@ require 'spec_helper' RSpec.describe ::Ci::Runners::UnregisterRunnerService, '#execute' do - subject { described_class.new(runner, 'some_token').execute } + subject(:execute) { described_class.new(runner, 'some_token').execute } let(:runner) { create(:ci_runner) } it 'destroys runner' do expect(runner).to receive(:destroy).once.and_call_original - expect { subject }.to change { Ci::Runner.count }.by(-1) + + expect do + expect(execute).to be_success + end.to change { Ci::Runner.count }.by(-1) expect(runner[:errors]).to be_nil end end diff --git a/spec/services/import/prepare_service_spec.rb b/spec/services/import/prepare_service_spec.rb new file mode 100644 index 00000000000..0097198f7a9 --- /dev/null +++ b/spec/services/import/prepare_service_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Import::PrepareService do + let_it_be(:project) { create(:project) } + let_it_be(:user) { create(:user) } + + let(:file) { double } + let(:upload_service) { double } + let(:uploader) { double } + let(:upload) { double } + + let(:service) { described_class.new(project, user, file: file) } + + subject { service.execute } + + context 'when file is uploaded correctly' do + let(:upload_id) { 99 } + + before do + mock_upload + end + + it 'raises NotImplemented error for worker' do + expect { subject }.to raise_error(NotImplementedError) + end + + context 'when a job is enqueued' do + before do + worker = double + + allow(service).to receive(:worker).and_return(worker) + allow(worker).to receive(:perform_async) + end + + it 'raises NotImplemented error for success_message when a job is enqueued' do + expect { subject }.to raise_error(NotImplementedError) + end + + it 'returns a success respnse when a success_message is implemented' do + message = 'It works!' + + allow(service).to receive(:success_message).and_return(message) + + result = subject + + expect(result).to be_success + expect(result.message).to eq(message) + end + end + end + + context 'when file upload fails' do + before do + mock_upload(false) + end + + it 'returns an error message' do + result = subject + + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq('File upload error.') + end + end +end diff --git a/spec/services/issues/prepare_import_csv_service_spec.rb b/spec/services/issues/prepare_import_csv_service_spec.rb new file mode 100644 index 00000000000..ded23ee43b9 --- /dev/null +++ b/spec/services/issues/prepare_import_csv_service_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Issues::PrepareImportCsvService do + let_it_be(:project) { create(:project) } + let_it_be(:user) { create(:user) } + + let(:file) { double } + let(:upload_service) { double } + let(:uploader) { double } + let(:upload) { double } + + let(:subject) do + described_class.new(project, user, file: file).execute + end + + context 'when file is uploaded correctly' do + let(:upload_id) { 99 } + + before do + mock_upload + end + + it 'returns a success message' do + result = subject + + expect(result[:status]).to eq(:success) + expect(result[:message]).to eq("Your issues are being imported. Once finished, you'll get a confirmation email.") + end + + it 'enqueues the ImportRequirementsCsvWorker' do + expect(ImportIssuesCsvWorker).to receive(:perform_async).with(user.id, project.id, upload_id) + + subject + end + end + + context 'when file upload fails' do + before do + mock_upload(false) + end + + it 'returns an error message' do + result = subject + + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq('File upload error.') + end + end +end diff --git a/spec/support/shared_contexts/upload_type_check_shared_context.rb b/spec/support/shared_contexts/upload_type_check_shared_context.rb index 60b547c53b5..57b8d7472df 100644 --- a/spec/support/shared_contexts/upload_type_check_shared_context.rb +++ b/spec/support/shared_contexts/upload_type_check_shared_context.rb @@ -16,3 +16,15 @@ RSpec.shared_context 'force content type detection to mime_type' do allow(Gitlab::Utils::MimeType).to receive(:from_io).and_return(mime_type) end end + +def mock_upload(success = true) + allow(UploadService).to receive(:new).with(project, file).and_return(upload_service) + + if success + allow(upload_service).to receive(:execute).and_return(uploader) + allow(uploader).to receive(:upload).and_return(upload) + allow(upload).to receive(:id).and_return(upload_id) + else + allow(upload_service).to receive(:execute).and_return(nil) + end +end diff --git a/spec/workers/ci/runners/reconcile_existing_runner_versions_cron_worker_spec.rb b/spec/workers/ci/runners/reconcile_existing_runner_versions_cron_worker_spec.rb index ab310bffe37..1292df62ce5 100644 --- a/spec/workers/ci/runners/reconcile_existing_runner_versions_cron_worker_spec.rb +++ b/spec/workers/ci/runners/reconcile_existing_runner_versions_cron_worker_spec.rb @@ -21,7 +21,7 @@ RSpec.describe Ci::Runners::ReconcileExistingRunnerVersionsCronWorker do it 'executes the service' do expect_next_instance_of(Ci::Runners::ReconcileExistingRunnerVersionsService) do |service| - expect(service).to receive(:execute).and_return({}) + expect(service).to receive(:execute).and_return(ServiceResponse.success) end.exactly(worker_exec_times) perform @@ -30,7 +30,8 @@ RSpec.describe Ci::Runners::ReconcileExistingRunnerVersionsCronWorker do it 'logs the service result' do expect_next_instance_of(Ci::Runners::ReconcileExistingRunnerVersionsService) do |service| - expect(service).to receive(:execute).and_return({ some_job_result_key: 'some_value' }) + expect(service).to receive(:execute) + .and_return(ServiceResponse.success(payload: { some_job_result_key: 'some_value' })) end worker.perform(false) |