From 05f0ebba3a2c8ddf39e436f412dc2ab5bf1353b2 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Wed, 18 Jan 2023 19:00:14 +0000 Subject: Add latest changes from gitlab-org/gitlab@15-8-stable-ee --- .../import/github/gists_import_service_spec.rb | 26 +++++++++- spec/services/import/github_service_spec.rb | 56 ++++++++++++++++++++-- 2 files changed, 75 insertions(+), 7 deletions(-) (limited to 'spec/services/import') diff --git a/spec/services/import/github/gists_import_service_spec.rb b/spec/services/import/github/gists_import_service_spec.rb index c5d73e6479d..32d04a580da 100644 --- a/spec/services/import/github/gists_import_service_spec.rb +++ b/spec/services/import/github/gists_import_service_spec.rb @@ -2,16 +2,19 @@ require 'spec_helper' -RSpec.describe Import::Github::GistsImportService, feature_category: :importer do - subject(:import) { described_class.new(user, params) } +RSpec.describe Import::Github::GistsImportService, feature_category: :importers do + subject(:import) { described_class.new(user, client, params) } let_it_be(:user) { create(:user) } let(:params) { { github_access_token: 'token' } } let(:import_status) { instance_double('Gitlab::GithubGistsImport::Status') } + let(:client) { Gitlab::GithubImport::Client.new(params[:github_access_token]) } + let(:octokit_user) { { login: 'user_login' } } describe '#execute', :aggregate_failures do before do allow(Gitlab::GithubGistsImport::Status).to receive(:new).and_return(import_status) + allow(client.octokit).to receive(:user).and_return(octokit_user) end context 'when import in progress' do @@ -43,5 +46,24 @@ RSpec.describe Import::Github::GistsImportService, feature_category: :importer d expect(import.execute).to eq({ status: :success }) end end + + context 'when user token is invalid' do + before do + allow(client.octokit).to receive(:user).and_raise(Octokit::Unauthorized) + allow(import_status).to receive(:started?).and_return(false) + end + + let(:expected_result) do + { + http_status: 401, + message: 'Access denied to the GitHub account.', + status: :error + } + end + + it 'returns 401 error' do + expect(import.execute).to eq(expected_result) + end + end end end diff --git a/spec/services/import/github_service_spec.rb b/spec/services/import/github_service_spec.rb index d1b372c5e87..293e247c140 100644 --- a/spec/services/import/github_service_spec.rb +++ b/spec/services/import/github_service_spec.rb @@ -7,22 +7,19 @@ RSpec.describe Import::GithubService do let_it_be(:token) { 'complex-token' } let_it_be(:access_params) { { github_access_token: 'github-complex-token' } } let(:settings) { instance_double(Gitlab::GithubImport::Settings) } + let(:user_namespace_path) { user.namespace_path } let(:optional_stages) { nil } let(:params) do { repo_id: 123, new_name: 'new_repo', - target_namespace: 'root', + target_namespace: user_namespace_path, optional_stages: optional_stages } end subject(:github_importer) { described_class.new(client, user, params) } - before do - allow(subject).to receive(:authorized?).and_return(true) - end - shared_examples 'handles errors' do |klass| let(:client) { klass.new(token) } let(:project_double) { instance_double(Project, persisted?: true) } @@ -74,6 +71,7 @@ RSpec.describe Import::GithubService do let(:repository_double) { { name: 'repository', size: 99 } } before do + allow(subject).to receive(:authorized?).and_return(true) expect(client).to receive(:repository).and_return(repository_double) allow_next_instance_of(Gitlab::LegacyGithubImport::ProjectCreator) do |creator| @@ -215,6 +213,38 @@ RSpec.describe Import::GithubService do end end end + + context 'when target_namespace is blank' do + before do + params[:target_namespace] = '' + end + + it 'raises an exception' do + expect { subject.execute(access_params, :github) }.to raise_error(ArgumentError, 'Target namespace is required') + end + end + + context 'when namespace to import repository into does not exist' do + before do + params[:target_namespace] = 'unknown_path' + end + + it 'returns an error' do + expect(github_importer.execute(access_params, :github)).to include(not_existed_namespace_error) + end + end + + context 'when user has no permissions to import repository into the specified namespace' do + let_it_be(:group) { create(:group) } + + before do + params[:target_namespace] = group.full_path + end + + it 'returns an error' do + expect(github_importer.execute(access_params, :github)).to include(taken_namespace_error) + end + end end context 'when remove_legacy_github_client feature flag is enabled' do @@ -248,4 +278,20 @@ RSpec.describe Import::GithubService do message: "Invalid URL: #{url}" } end + + def not_existed_namespace_error + { + status: :error, + http_status: :unprocessable_entity, + message: 'Namespace or group to import repository into does not exist.' + } + end + + def taken_namespace_error + { + status: :error, + http_status: :unprocessable_entity, + message: 'This namespace has already been taken. Choose a different one.' + } + end end -- cgit v1.2.3