diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
commit | 6e4e1050d9dba2b7b2523fdd1768823ab85feef4 (patch) | |
tree | 78be5963ec075d80116a932011d695dd33910b4e /spec/controllers/import | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'spec/controllers/import')
6 files changed, 197 insertions, 14 deletions
diff --git a/spec/controllers/import/available_namespaces_controller_spec.rb b/spec/controllers/import/available_namespaces_controller_spec.rb new file mode 100644 index 00000000000..ebccc862a13 --- /dev/null +++ b/spec/controllers/import/available_namespaces_controller_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Import::AvailableNamespacesController do + let_it_be(:user) { create(:user) } + let_it_be(:manageable_groups) { [create(:group), create(:group)] } + + before do + sign_in(user) + manageable_groups.each { |group| group.add_maintainer(user) } + end + + describe "GET index" do + it "returns list of available namespaces" do + unrelated_group = create(:group) + + get :index + + expect(response).to have_gitlab_http_status(:ok) + expect(json_response).to be_kind_of(Array) + + response_ids = json_response.map { |n| n["id"] } + + expect(response_ids).not_to include(unrelated_group.id) + expect(response_ids).to contain_exactly(*manageable_groups.map(&:id)) + end + + context "with an anonymous user" do + before do + sign_out(user) + end + + it "redirects to sign-in page" do + get :index + + expect(response).to redirect_to(new_user_session_path) + end + end + end +end diff --git a/spec/controllers/import/bitbucket_server_controller_spec.rb b/spec/controllers/import/bitbucket_server_controller_spec.rb index bb80de6425f..d5f94be65b6 100644 --- a/spec/controllers/import/bitbucket_server_controller_spec.rb +++ b/spec/controllers/import/bitbucket_server_controller_spec.rb @@ -139,8 +139,6 @@ RSpec.describe Import::BitbucketServerController do describe 'GET status' do render_views - let(:repos) { instance_double(BitbucketServer::Collection) } - before do allow(controller).to receive(:client).and_return(client) @@ -157,14 +155,14 @@ RSpec.describe Import::BitbucketServerController do expect(response).to have_gitlab_http_status(:ok) expect(json_response['incompatible_repos'].length).to eq(1) - expect(json_response.dig("incompatible_repos", 0, "id")).to eq(@invalid_repo.full_name) + expect(json_response.dig("incompatible_repos", 0, "id")).to eq("#{@invalid_repo.project_key}/#{@invalid_repo.slug}") expect(json_response['provider_repos'].length).to eq(1) expect(json_response.dig("provider_repos", 0, "id")).to eq(@repo.full_name) end it_behaves_like 'import controller status' do let(:repo) { @repo } - let(:repo_id) { @repo.full_name } + let(:repo_id) { "#{@repo.project_key}/#{@repo.slug}" } let(:import_source) { @repo.browse_url } let(:provider_name) { 'bitbucket_server' } let(:client_repos_field) { :repos } diff --git a/spec/controllers/import/gitea_controller_spec.rb b/spec/controllers/import/gitea_controller_spec.rb index 9001faef408..3e4b159271a 100644 --- a/spec/controllers/import/gitea_controller_spec.rb +++ b/spec/controllers/import/gitea_controller_spec.rb @@ -34,6 +34,14 @@ RSpec.describe Import::GiteaController do assign_host_url end + it "requests provider repos list" do + expect(stub_client(repos: [], orgs: [])).to receive(:repos) + + get :status + + expect(response).to have_gitlab_http_status(:ok) + end + context 'when host url is local or not http' do %w[https://localhost:3000 http://192.168.0.1 ftp://testing].each do |url| let(:host_url) { url } diff --git a/spec/controllers/import/github_controller_spec.rb b/spec/controllers/import/github_controller_spec.rb index a5a3dc463d3..e19b6caca5b 100644 --- a/spec/controllers/import/github_controller_spec.rb +++ b/spec/controllers/import/github_controller_spec.rb @@ -15,10 +15,7 @@ RSpec.describe Import::GithubController do it "redirects to GitHub for an access token if logged in with GitHub" do allow(controller).to receive(:logged_in_with_provider?).and_return(true) expect(controller).to receive(:go_to_provider_for_permissions).and_call_original - allow_any_instance_of(Gitlab::LegacyGithubImport::Client) - .to receive(:authorize_url) - .with(users_import_github_callback_url) - .and_call_original + allow(controller).to receive(:authorize_url).and_call_original get :new @@ -46,13 +43,15 @@ RSpec.describe Import::GithubController do end describe "GET callback" do + before do + allow(controller).to receive(:get_token).and_return(token) + allow(controller).to receive(:oauth_options).and_return({}) + + stub_omniauth_provider('github') + end + it "updates access token" do token = "asdasd12345" - allow_any_instance_of(Gitlab::LegacyGithubImport::Client) - .to receive(:get_token).and_return(token) - allow_any_instance_of(Gitlab::LegacyGithubImport::Client) - .to receive(:github_options).and_return({}) - stub_omniauth_provider('github') get :callback @@ -66,7 +65,86 @@ RSpec.describe Import::GithubController do end describe "GET status" do - it_behaves_like 'a GitHub-ish import controller: GET status' + context 'when using OAuth' do + before do + allow(controller).to receive(:logged_in_with_provider?).and_return(true) + end + + context 'when OAuth config is missing' do + let(:new_import_url) { public_send("new_import_#{provider}_url") } + + before do + allow(controller).to receive(:oauth_config).and_return(nil) + end + + it 'returns missing config error' do + expect(controller).to receive(:go_to_provider_for_permissions).and_call_original + + get :status + + expect(session[:"#{provider}_access_token"]).to be_nil + expect(controller).to redirect_to(new_import_url) + expect(flash[:alert]).to eq('Missing OAuth configuration for GitHub.') + end + end + end + + context 'when feature remove_legacy_github_client is disabled' do + before do + stub_feature_flags(remove_legacy_github_client: false) + session[:"#{provider}_access_token"] = 'asdasd12345' + end + + it_behaves_like 'a GitHub-ish import controller: GET status' + + it 'uses Gitlab::LegacyGitHubImport::Client' do + expect(controller.send(:client)).to be_instance_of(Gitlab::LegacyGithubImport::Client) + end + + it 'fetches repos using legacy client' do + expect_next_instance_of(Gitlab::LegacyGithubImport::Client) do |client| + expect(client).to receive(:repos) + end + + get :status + end + end + + context 'when feature remove_legacy_github_client is enabled' do + before do + stub_feature_flags(remove_legacy_github_client: true) + session[:"#{provider}_access_token"] = 'asdasd12345' + end + + it_behaves_like 'a GitHub-ish import controller: GET status' + + it 'uses Gitlab::GithubImport::Client' do + expect(controller.send(:client)).to be_instance_of(Gitlab::GithubImport::Client) + end + + it 'fetches repos using latest github client' do + expect_next_instance_of(Gitlab::GithubImport::Client) do |client| + expect(client).to receive(:each_page).with(:repos).and_return([].to_enum) + end + + get :status + end + + it 'concatenates list of repos from multiple pages' do + repo_1 = OpenStruct.new(login: 'emacs', full_name: 'asd/emacs', name: 'emacs', owner: { login: 'owner' }) + repo_2 = OpenStruct.new(login: 'vim', full_name: 'asd/vim', name: 'vim', owner: { login: 'owner' }) + repos = [OpenStruct.new(objects: [repo_1]), OpenStruct.new(objects: [repo_2])].to_enum + + allow(stub_client).to receive(:each_page).and_return(repos) + + get :status, format: :json + + expect(response).to have_gitlab_http_status(:ok) + expect(json_response.dig('provider_repos').count).to eq(2) + expect(json_response.dig('provider_repos', 0, 'id')).to eq(repo_1.id) + expect(json_response.dig('provider_repos', 1, 'id')).to eq(repo_2.id) + end + end end describe "POST create" do diff --git a/spec/controllers/import/gitlab_controller_spec.rb b/spec/controllers/import/gitlab_controller_spec.rb index 42c4348dac2..826625ba9c3 100644 --- a/spec/controllers/import/gitlab_controller_spec.rb +++ b/spec/controllers/import/gitlab_controller_spec.rb @@ -53,6 +53,7 @@ RSpec.describe Import::GitlabController do let(:gitlab_user) do { username: gitlab_username }.with_indifferent_access end + let(:gitlab_repo) do { path: 'vim', diff --git a/spec/controllers/import/manifest_controller_spec.rb b/spec/controllers/import/manifest_controller_spec.rb new file mode 100644 index 00000000000..ec8bd45b65c --- /dev/null +++ b/spec/controllers/import/manifest_controller_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Import::ManifestController do + include ImportSpecHelper + + let_it_be(:user) { create(:user) } + let_it_be(:group) { create(:group)} + + before(:all) do + group.add_maintainer(user) + end + + before do + sign_in(user) + end + + def assign_session_group + session[:manifest_import_repositories] = [] + session[:manifest_import_group_id] = group.id + end + + describe 'GET status' do + let(:repo1) { OpenStruct.new(id: 'test1', url: 'http://demo.host/test1') } + let(:repo2) { OpenStruct.new(id: 'test2', url: 'http://demo.host/test2') } + let(:repos) { [repo1, repo2] } + + before do + assign_session_group + + session[:manifest_import_repositories] = repos + end + + it "returns variables for json request" do + project = create(:project, import_type: 'manifest', creator_id: user.id) + + get :status, format: :json + + expect(response).to have_gitlab_http_status(:ok) + expect(json_response.dig("imported_projects", 0, "id")).to eq(project.id) + expect(json_response.dig("provider_repos", 0, "id")).to eq(repo1.id) + expect(json_response.dig("provider_repos", 1, "id")).to eq(repo2.id) + expect(json_response.dig("namespaces", 0, "id")).to eq(group.id) + end + + it "does not show already added project" do + project = create(:project, import_type: 'manifest', namespace: user.namespace, import_status: :finished, import_url: repo1.url) + + get :status, format: :json + + expect(json_response.dig("imported_projects", 0, "id")).to eq(project.id) + expect(json_response.dig("provider_repos").length).to eq(1) + expect(json_response.dig("provider_repos", 0, "id")).not_to eq(repo1.id) + end + end +end |