2019-04-15 10:17:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-12-31 13:07:48 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-04 03:08:05 +00:00
|
|
|
RSpec.describe Import::GithubController do
|
2015-05-21 21:40:13 +00:00
|
|
|
include ImportSpecHelper
|
|
|
|
|
2016-12-15 16:31:14 +00:00
|
|
|
let(:provider) { :github }
|
2015-08-07 07:06:20 +00:00
|
|
|
|
2016-12-15 16:31:14 +00:00
|
|
|
include_context 'a GitHub-ish import controller'
|
2014-12-31 13:07:48 +00:00
|
|
|
|
2016-05-02 15:22:38 +00:00
|
|
|
describe "GET new" do
|
2016-12-15 16:31:14 +00:00
|
|
|
it_behaves_like 'a GitHub-ish import controller: GET new'
|
2016-05-02 15:22:38 +00:00
|
|
|
|
2016-12-15 16:31:14 +00:00
|
|
|
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)
|
2019-01-22 17:38:08 +00:00
|
|
|
expect(controller).to receive(:go_to_provider_for_permissions).and_call_original
|
2020-08-11 12:09:55 +00:00
|
|
|
allow(controller).to receive(:authorize_url).and_call_original
|
2016-05-02 15:22:38 +00:00
|
|
|
|
|
|
|
get :new
|
2019-01-22 17:38:08 +00:00
|
|
|
|
2020-02-06 18:08:54 +00:00
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2016-05-02 15:22:38 +00:00
|
|
|
end
|
2018-12-10 19:52:04 +00:00
|
|
|
|
|
|
|
it "prompts for an access token if GitHub not configured" do
|
|
|
|
allow(controller).to receive(:github_import_configured?).and_return(false)
|
|
|
|
expect(controller).not_to receive(:go_to_provider_for_permissions)
|
|
|
|
|
|
|
|
get :new
|
|
|
|
|
2020-02-06 18:08:54 +00:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-12-10 19:52:04 +00:00
|
|
|
end
|
2019-07-15 17:15:10 +00:00
|
|
|
|
|
|
|
context 'when importing a CI/CD project' do
|
|
|
|
it 'always prompts for an access token' do
|
|
|
|
allow(controller).to receive(:github_import_configured?).and_return(true)
|
|
|
|
|
|
|
|
get :new, params: { ci_cd_only: true }
|
|
|
|
|
|
|
|
expect(response).to render_template(:new)
|
|
|
|
end
|
|
|
|
end
|
2016-05-02 15:22:38 +00:00
|
|
|
end
|
|
|
|
|
2014-12-31 13:07:48 +00:00
|
|
|
describe "GET callback" do
|
2020-08-11 12:09:55 +00:00
|
|
|
before do
|
|
|
|
allow(controller).to receive(:get_token).and_return(token)
|
|
|
|
allow(controller).to receive(:oauth_options).and_return({})
|
|
|
|
|
|
|
|
stub_omniauth_provider('github')
|
|
|
|
end
|
|
|
|
|
2014-12-31 13:07:48 +00:00
|
|
|
it "updates access token" do
|
|
|
|
token = "asdasd12345"
|
|
|
|
|
|
|
|
get :callback
|
2015-02-12 18:32:58 +00:00
|
|
|
|
2016-12-16 16:43:34 +00:00
|
|
|
expect(session[:github_access_token]).to eq(token)
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(controller).to redirect_to(status_import_github_url)
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-02 15:22:38 +00:00
|
|
|
describe "POST personal_access_token" do
|
2016-12-15 16:31:14 +00:00
|
|
|
it_behaves_like 'a GitHub-ish import controller: POST personal_access_token'
|
2016-05-02 15:22:38 +00:00
|
|
|
end
|
|
|
|
|
2014-12-31 13:07:48 +00:00
|
|
|
describe "GET status" do
|
2020-08-11 12:09:55 +00:00
|
|
|
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
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "POST create" do
|
2016-12-15 16:31:14 +00:00
|
|
|
it_behaves_like 'a GitHub-ish import controller: POST create'
|
2020-01-24 00:08:51 +00:00
|
|
|
|
|
|
|
it_behaves_like 'project import rate limiter'
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|
2018-11-07 16:44:21 +00:00
|
|
|
|
|
|
|
describe "GET realtime_changes" do
|
|
|
|
it_behaves_like 'a GitHub-ish import controller: GET realtime_changes'
|
|
|
|
end
|
2014-12-31 13:07:48 +00:00
|
|
|
end
|