summaryrefslogtreecommitdiff
path: root/spec/lib
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-06-29 19:31:31 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-06-29 19:31:31 +0000
commite810b8327513c3b07cb779dbce6c75dbcb49ca84 (patch)
tree65b220240faa9e191388c6ab233fed03da2b8713 /spec/lib
parent11e9b7b58837da351f08c18e6f0f4faba4d7d301 (diff)
downloadgitlab-ce-e810b8327513c3b07cb779dbce6c75dbcb49ca84.tar.gz
Add latest changes from gitlab-org/security/gitlab@13-1-stable-ee
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/gitlab/import_export/snippet_repo_restorer_spec.rb64
-rw-r--r--spec/lib/gitlab/import_export/snippets_repo_restorer_spec.rb1
2 files changed, 48 insertions, 17 deletions
diff --git a/spec/lib/gitlab/import_export/snippet_repo_restorer_spec.rb b/spec/lib/gitlab/import_export/snippet_repo_restorer_spec.rb
index 779b65e33d8..43c4b164b2d 100644
--- a/spec/lib/gitlab/import_export/snippet_repo_restorer_spec.rb
+++ b/spec/lib/gitlab/import_export/snippet_repo_restorer_spec.rb
@@ -4,9 +4,9 @@ require 'spec_helper'
describe Gitlab::ImportExport::SnippetRepoRestorer do
let_it_be(:user) { create(:user) }
- let_it_be(:project) { create(:project, namespace: user.namespace) }
- let(:snippet) { create(:project_snippet, project: project, author: user) }
+ let(:project) { create(:project, namespace: user.namespace) }
+ let(:snippet) { create(:project_snippet, project: project, author: user) }
let(:shared) { project.import_export_shared }
let(:exporter) { Gitlab::ImportExport::SnippetsRepoSaver.new(project: project, shared: shared, current_user: user) }
let(:restorer) do
@@ -57,33 +57,63 @@ describe Gitlab::ImportExport::SnippetRepoRestorer do
it_behaves_like 'no bundle file present'
end
- context 'when the snippet bundle exists' do
- let!(:snippet_with_repo) { create(:project_snippet, :repository, project: project) }
+ context 'when the snippet repository bundle exists' do
+ let!(:snippet_with_repo) { create(:project_snippet, :repository, project: project, author: user) }
let(:bundle_path) { ::Gitlab::ImportExport.snippets_repo_bundle_path(shared.export_path) }
let(:snippet_bundle_path) { File.join(bundle_path, "#{snippet_with_repo.hexdigest}.bundle") }
let(:result) { exporter.save }
+ let(:repository) { snippet.repository }
before do
expect(exporter.save).to be_truthy
end
- it 'creates the repository from the bundle' do
- expect(snippet.repository_exists?).to be_falsey
- expect(snippet.snippet_repository).to be_nil
- expect(snippet.repository).to receive(:create_from_bundle).and_call_original
+ context 'when it is valid' do
+ before do
+ allow(repository).to receive(:branch_count).and_return(1)
+ allow(repository).to receive(:tag_count).and_return(0)
+ allow(repository).to receive(:branch_names).and_return(['master'])
+ allow(repository).to receive(:ls_files).and_return(['foo'])
+ end
- expect(restorer.restore).to be_truthy
- expect(snippet.repository_exists?).to be_truthy
- expect(snippet.snippet_repository).not_to be_nil
- end
+ it 'creates the repository from the bundle' do
+ expect(snippet.repository_exists?).to be_falsey
+ expect(snippet.snippet_repository).to be_nil
+ expect(repository).to receive(:create_from_bundle).and_call_original
- it 'sets same shard in snippet repository as in the repository storage' do
- expect(snippet).to receive(:repository_storage).and_return('picked')
- expect(snippet.repository).to receive(:create_from_bundle)
+ expect(restorer.restore).to be_truthy
+ expect(snippet.repository_exists?).to be_truthy
+ expect(snippet.snippet_repository).not_to be_nil
+ end
- restorer.restore
+ it 'sets same shard in snippet repository as in the repository storage' do
+ expect(repository).to receive(:storage).and_return('picked')
+ expect(repository).to receive(:create_from_bundle)
- expect(snippet.snippet_repository.shard_name).to eq 'picked'
+ expect(restorer.restore).to be_truthy
+ expect(snippet.snippet_repository.shard_name).to eq 'picked'
+ end
+ end
+
+ context 'when it is invalid' do
+ it 'returns false and deletes the repository from disk and the database' do
+ gitlab_shell = Gitlab::Shell.new
+ shard_name = snippet.repository.shard
+ path = snippet.disk_path + '.git'
+ error_response = ServiceResponse.error(message: 'Foo', http_status: 400)
+
+ allow_next_instance_of(Snippets::RepositoryValidationService) do |instance|
+ allow(instance).to receive(:execute).and_return(error_response)
+ end
+
+ aggregate_failures do
+ expect(restorer.restore).to be false
+ expect(shared.errors.first).to match(/Invalid repository bundle/)
+ expect(snippet.repository_exists?).to eq false
+ expect(snippet.reload.snippet_repository).to be_nil
+ expect(gitlab_shell.repository_exists?(shard_name, path)).to eq false
+ end
+ end
end
end
end
diff --git a/spec/lib/gitlab/import_export/snippets_repo_restorer_spec.rb b/spec/lib/gitlab/import_export/snippets_repo_restorer_spec.rb
index fdae259c2f1..ac73462073e 100644
--- a/spec/lib/gitlab/import_export/snippets_repo_restorer_spec.rb
+++ b/spec/lib/gitlab/import_export/snippets_repo_restorer_spec.rb
@@ -38,6 +38,7 @@ describe Gitlab::ImportExport::SnippetsRepoRestorer do
expect(snippet1.repository_exists?).to be false
expect(snippet2.repository_exists?).to be false
+ allow_any_instance_of(Snippets::RepositoryValidationService).to receive(:execute).and_return(ServiceResponse.success)
expect(Gitlab::ImportExport::SnippetRepoRestorer).to receive(:new).with(hash_including(snippet: snippet1, path_to_bundle: bundle_path(snippet1))).and_call_original
expect(Gitlab::ImportExport::SnippetRepoRestorer).to receive(:new).with(hash_including(snippet: snippet2, path_to_bundle: bundle_path(snippet2))).and_call_original
expect(restorer.restore).to be_truthy