PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/services/projects/import_service_spec.rb

https://bitbucket.org/terrchen/gitlab-ce
Ruby | 189 lines | 138 code | 51 blank | 0 comment | 0 complexity | ab5dcc60eb1b7b0206815bc5ce385f19 MD5 | raw file
Possible License(s): Apache-2.0, CC0-1.0
  1. require 'spec_helper'
  2. describe Projects::ImportService do
  3. let!(:project) { create(:project) }
  4. let(:user) { project.creator }
  5. subject { described_class.new(project, user) }
  6. describe '#async?' do
  7. it 'returns true for an asynchronous importer' do
  8. importer_class = double(:importer, async?: true)
  9. allow(subject).to receive(:has_importer?).and_return(true)
  10. allow(subject).to receive(:importer_class).and_return(importer_class)
  11. expect(subject).to be_async
  12. end
  13. it 'returns false for a regular importer' do
  14. importer_class = double(:importer, async?: false)
  15. allow(subject).to receive(:has_importer?).and_return(true)
  16. allow(subject).to receive(:importer_class).and_return(importer_class)
  17. expect(subject).not_to be_async
  18. end
  19. it 'returns false when the importer does not define #async?' do
  20. importer_class = double(:importer)
  21. allow(subject).to receive(:has_importer?).and_return(true)
  22. allow(subject).to receive(:importer_class).and_return(importer_class)
  23. expect(subject).not_to be_async
  24. end
  25. it 'returns false when the importer does not exist' do
  26. allow(subject).to receive(:has_importer?).and_return(false)
  27. expect(subject).not_to be_async
  28. end
  29. end
  30. describe '#execute' do
  31. context 'with unknown url' do
  32. before do
  33. project.import_url = Project::UNKNOWN_IMPORT_URL
  34. end
  35. it 'succeeds if repository is created successfully' do
  36. expect(project).to receive(:create_repository).and_return(true)
  37. result = subject.execute
  38. expect(result[:status]).to eq :success
  39. end
  40. it 'fails if repository creation fails' do
  41. expect(project).to receive(:create_repository).and_return(false)
  42. result = subject.execute
  43. expect(result[:status]).to eq :error
  44. expect(result[:message]).to eq "Error importing repository #{project.import_url} into #{project.full_path} - The repository could not be created."
  45. end
  46. end
  47. context 'with known url' do
  48. before do
  49. project.import_url = 'https://github.com/vim/vim.git'
  50. project.import_type = 'github'
  51. end
  52. context 'with a Github repository' do
  53. it 'succeeds if repository import was scheduled' do
  54. expect_any_instance_of(Gitlab::GithubImport::ParallelImporter)
  55. .to receive(:execute)
  56. .and_return(true)
  57. result = subject.execute
  58. expect(result[:status]).to eq :success
  59. end
  60. it 'fails if repository import was not scheduled' do
  61. expect_any_instance_of(Gitlab::GithubImport::ParallelImporter)
  62. .to receive(:execute)
  63. .and_return(false)
  64. result = subject.execute
  65. expect(result[:status]).to eq :error
  66. end
  67. end
  68. context 'with a non Github repository' do
  69. before do
  70. project.import_url = 'https://bitbucket.org/vim/vim.git'
  71. project.import_type = 'bitbucket'
  72. end
  73. it 'succeeds if repository import is successfully' do
  74. expect_any_instance_of(Gitlab::Shell).to receive(:import_repository).and_return(true)
  75. expect_any_instance_of(Gitlab::BitbucketImport::Importer).to receive(:execute).and_return(true)
  76. result = subject.execute
  77. expect(result[:status]).to eq :success
  78. end
  79. it 'fails if repository import fails' do
  80. expect_any_instance_of(Gitlab::Shell).to receive(:import_repository).and_raise(Gitlab::Shell::Error.new('Failed to import the repository'))
  81. result = subject.execute
  82. expect(result[:status]).to eq :error
  83. expect(result[:message]).to eq "Error importing repository #{project.import_url} into #{project.full_path} - Failed to import the repository"
  84. end
  85. end
  86. end
  87. context 'with valid importer' do
  88. before do
  89. stub_github_omniauth_provider
  90. project.import_url = 'https://github.com/vim/vim.git'
  91. project.import_type = 'github'
  92. allow(project).to receive(:import_data).and_return(double.as_null_object)
  93. end
  94. it 'succeeds if importer succeeds' do
  95. allow_any_instance_of(Gitlab::GithubImport::ParallelImporter)
  96. .to receive(:execute).and_return(true)
  97. result = subject.execute
  98. expect(result[:status]).to eq :success
  99. end
  100. it 'fails if importer fails' do
  101. allow_any_instance_of(Gitlab::GithubImport::ParallelImporter)
  102. .to receive(:execute)
  103. .and_return(false)
  104. result = subject.execute
  105. expect(result[:status]).to eq :error
  106. end
  107. end
  108. context 'with blocked import_URL' do
  109. it 'fails with localhost' do
  110. project.import_url = 'https://localhost:9000/vim/vim.git'
  111. result = described_class.new(project, user).execute
  112. expect(result[:status]).to eq :error
  113. expect(result[:message]).to include('Requests to localhost are not allowed')
  114. end
  115. it 'fails with port 25' do
  116. project.import_url = "https://github.com:25/vim/vim.git"
  117. result = described_class.new(project, user).execute
  118. expect(result[:status]).to eq :error
  119. expect(result[:message]).to include('Only allowed ports are 22, 80, 443')
  120. end
  121. end
  122. def stub_github_omniauth_provider
  123. provider = OpenStruct.new(
  124. 'name' => 'github',
  125. 'app_id' => 'asd123',
  126. 'app_secret' => 'asd123',
  127. 'args' => {
  128. 'client_options' => {
  129. 'site' => 'https://github.com/api/v3',
  130. 'authorize_url' => 'https://github.com/login/oauth/authorize',
  131. 'token_url' => 'https://github.com/login/oauth/access_token'
  132. }
  133. }
  134. )
  135. stub_omniauth_setting(providers: [provider])
  136. end
  137. end
  138. end