PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/services/projects/import_service_spec.rb

https://gitlab.com/alexsanford/gitlab-ce
Ruby | 183 lines | 133 code | 50 blank | 0 comment | 0 complexity | 258cd7ea320c6f515af94acec115351d MD5 | raw file
  1. require 'spec_helper'
  2. describe Projects::ImportService, services: true do
  3. let!(:project) { create(:empty_project) }
  4. let(:user) { project.creator }
  5. subject { described_class.new(project, user) }
  6. describe '#execute' do
  7. context 'with unknown url' do
  8. before do
  9. project.import_url = Project::UNKNOWN_IMPORT_URL
  10. end
  11. it 'succeeds if repository is created successfully' do
  12. expect(project).to receive(:create_repository).and_return(true)
  13. result = subject.execute
  14. expect(result[:status]).to eq :success
  15. end
  16. it 'fails if repository creation fails' do
  17. expect(project).to receive(:create_repository).and_return(false)
  18. result = subject.execute
  19. expect(result[:status]).to eq :error
  20. expect(result[:message]).to eq "Error importing repository #{project.import_url} into #{project.path_with_namespace} - The repository could not be created."
  21. end
  22. end
  23. context 'with known url' do
  24. before do
  25. project.import_url = 'https://github.com/vim/vim.git'
  26. project.import_type = 'github'
  27. end
  28. context 'with a Github repository' do
  29. it 'succeeds if repository import is successfully' do
  30. expect_any_instance_of(Repository).to receive(:fetch_remote).and_return(true)
  31. expect_any_instance_of(Gitlab::GithubImport::Importer).to receive(:execute).and_return(true)
  32. result = subject.execute
  33. expect(result[:status]).to eq :success
  34. end
  35. it 'fails if repository import fails' do
  36. expect_any_instance_of(Repository).to receive(:fetch_remote).and_raise(Gitlab::Shell::Error.new('Failed to import the repository'))
  37. result = subject.execute
  38. expect(result[:status]).to eq :error
  39. expect(result[:message]).to eq "Error importing repository #{project.import_url} into #{project.path_with_namespace} - Failed to import the repository"
  40. end
  41. end
  42. context 'with a non Github repository' do
  43. before do
  44. project.import_url = 'https://bitbucket.org/vim/vim.git'
  45. project.import_type = 'bitbucket'
  46. end
  47. it 'succeeds if repository import is successfully' do
  48. expect_any_instance_of(Gitlab::Shell).to receive(:import_repository).and_return(true)
  49. expect_any_instance_of(Gitlab::BitbucketImport::Importer).to receive(:execute).and_return(true)
  50. result = subject.execute
  51. expect(result[:status]).to eq :success
  52. end
  53. it 'fails if repository import fails' do
  54. expect_any_instance_of(Gitlab::Shell).to receive(:import_repository).and_raise(Gitlab::Shell::Error.new('Failed to import the repository'))
  55. result = subject.execute
  56. expect(result[:status]).to eq :error
  57. expect(result[:message]).to eq "Error importing repository #{project.import_url} into #{project.path_with_namespace} - Failed to import the repository"
  58. end
  59. end
  60. end
  61. context 'with valid importer' do
  62. before do
  63. stub_github_omniauth_provider
  64. project.import_url = 'https://github.com/vim/vim.git'
  65. project.import_type = 'github'
  66. allow(project).to receive(:import_data).and_return(double.as_null_object)
  67. end
  68. it 'succeeds if importer succeeds' do
  69. allow_any_instance_of(Repository).to receive(:fetch_remote).and_return(true)
  70. allow_any_instance_of(Gitlab::GithubImport::Importer).to receive(:execute).and_return(true)
  71. result = subject.execute
  72. expect(result[:status]).to eq :success
  73. end
  74. it 'flushes various caches' do
  75. allow_any_instance_of(Repository).to receive(:fetch_remote).
  76. and_return(true)
  77. allow_any_instance_of(Gitlab::GithubImport::Importer).to receive(:execute).
  78. and_return(true)
  79. expect_any_instance_of(Repository).to receive(:expire_content_cache)
  80. subject.execute
  81. end
  82. it 'fails if importer fails' do
  83. allow_any_instance_of(Repository).to receive(:fetch_remote).and_return(true)
  84. allow_any_instance_of(Gitlab::GithubImport::Importer).to receive(:execute).and_return(false)
  85. result = subject.execute
  86. expect(result[:status]).to eq :error
  87. expect(result[:message]).to eq "Error importing repository #{project.import_url} into #{project.path_with_namespace} - The remote data could not be imported."
  88. end
  89. it 'fails if importer raise an error' do
  90. allow_any_instance_of(Gitlab::Shell).to receive(:fetch_remote).and_return(true)
  91. allow_any_instance_of(Gitlab::GithubImport::Importer).to receive(:execute).and_raise(Projects::ImportService::Error.new('Github: failed to connect API'))
  92. result = subject.execute
  93. expect(result[:status]).to eq :error
  94. expect(result[:message]).to eq "Error importing repository #{project.import_url} into #{project.path_with_namespace} - Github: failed to connect API"
  95. end
  96. it 'expires content cache after error' do
  97. allow_any_instance_of(Project).to receive(:repository_exists?).and_return(false, true)
  98. expect_any_instance_of(Gitlab::Shell).to receive(:fetch_remote).and_raise(Gitlab::Shell::Error.new('Failed to import the repository'))
  99. expect_any_instance_of(Repository).to receive(:expire_content_cache)
  100. subject.execute
  101. end
  102. end
  103. context 'with blocked import_URL' do
  104. it 'fails with localhost' do
  105. project.import_url = 'https://localhost:9000/vim/vim.git'
  106. result = described_class.new(project, user).execute
  107. expect(result[:status]).to eq :error
  108. expect(result[:message]).to end_with 'Blocked import URL.'
  109. end
  110. it 'fails with port 25' do
  111. project.import_url = "https://github.com:25/vim/vim.git"
  112. result = described_class.new(project, user).execute
  113. expect(result[:status]).to eq :error
  114. expect(result[:message]).to end_with 'Blocked import URL.'
  115. end
  116. end
  117. def stub_github_omniauth_provider
  118. provider = OpenStruct.new(
  119. 'name' => 'github',
  120. 'app_id' => 'asd123',
  121. 'app_secret' => 'asd123',
  122. 'args' => {
  123. 'client_options' => {
  124. 'site' => 'https://github.com/api/v3',
  125. 'authorize_url' => 'https://github.com/login/oauth/authorize',
  126. 'token_url' => 'https://github.com/login/oauth/access_token'
  127. }
  128. }
  129. )
  130. allow(Gitlab.config.omniauth).to receive(:providers).and_return([provider])
  131. end
  132. end
  133. end