2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe Import::BitbucketServerController do
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project_key) { 'test-project' }
|
|
|
|
let(:repo_slug) { 'some-repo' }
|
2020-07-28 23:09:34 +05:30
|
|
|
let(:repo_id) { "#{project_key}/#{repo_slug}" }
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:client) { instance_double(BitbucketServer::Client) }
|
|
|
|
|
|
|
|
def assign_session_tokens
|
|
|
|
session[:bitbucket_server_url] = 'http://localhost:7990'
|
|
|
|
session[:bitbucket_server_username] = 'bitbucket'
|
|
|
|
session[:bitbucket_server_personal_access_token] = 'some-token'
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
allow(controller).to receive(:bitbucket_server_import_enabled?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET new' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it 'shows the input form' do
|
|
|
|
get :new
|
|
|
|
|
|
|
|
expect(response.body).to have_text('Bitbucket Server URL')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST create' do
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:project_name) { "my-project_123" }
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
before do
|
2020-06-23 00:09:42 +05:30
|
|
|
allow(controller).to receive(:client).and_return(client)
|
2019-03-02 22:35:43 +05:30
|
|
|
repo = double(name: project_name)
|
2018-11-18 11:00:15 +05:30
|
|
|
allow(client).to receive(:repo).with(project_key, repo_slug).and_return(repo)
|
|
|
|
assign_session_tokens
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
let_it_be(:project) { create(:project) }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
it 'returns the new project' do
|
|
|
|
allow(Gitlab::BitbucketServerImport::ProjectCreator)
|
2019-03-02 22:35:43 +05:30
|
|
|
.to receive(:new).with(project_key, repo_slug, anything, project_name, user.namespace, user, anything)
|
2018-11-18 11:00:15 +05:30
|
|
|
.and_return(double(execute: project))
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
post :create, params: { repo_id: repo_id }, format: :json
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
context 'with project key with tildes' do
|
|
|
|
let(:project_key) { '~someuser_123' }
|
|
|
|
|
|
|
|
it 'successfully creates a project' do
|
|
|
|
allow(Gitlab::BitbucketServerImport::ProjectCreator)
|
|
|
|
.to receive(:new).with(project_key, repo_slug, anything, project_name, user.namespace, user, anything)
|
|
|
|
.and_return(double(execute: project))
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
post :create, params: { repo_id: repo_id }, format: :json
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'returns an error when an invalid project key is used' do
|
2020-07-28 23:09:34 +05:30
|
|
|
post :create, params: { repo_id: 'some&project/repo' }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error when an invalid repository slug is used' do
|
2020-07-28 23:09:34 +05:30
|
|
|
post :create, params: { repo_id: 'some-project/try*this' }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error when the project cannot be found' do
|
|
|
|
allow(client).to receive(:repo).with(project_key, repo_slug).and_return(nil)
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
post :create, params: { repo_id: repo_id }, format: :json
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error when the project cannot be saved' do
|
|
|
|
allow(Gitlab::BitbucketServerImport::ProjectCreator)
|
2019-03-02 22:35:43 +05:30
|
|
|
.to receive(:new).with(project_key, repo_slug, anything, project_name, user.namespace, user, anything)
|
2018-11-18 11:00:15 +05:30
|
|
|
.and_return(double(execute: build(:project)))
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
post :create, params: { repo_id: repo_id }, format: :json
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an error when the server can't be contacted" do
|
2020-07-28 23:09:34 +05:30
|
|
|
allow(client).to receive(:repo).with(project_key, repo_slug).and_raise(::BitbucketServer::Connection::ConnectionError)
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
post :create, params: { repo_id: repo_id }, format: :json
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
it_behaves_like 'project import rate limiter'
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST configure' do
|
|
|
|
let(:token) { 'token' }
|
|
|
|
let(:username) { 'bitbucket-user' }
|
|
|
|
let(:url) { 'http://localhost:7990/bitbucket' }
|
|
|
|
|
|
|
|
it 'clears out existing session' do
|
|
|
|
post :configure
|
|
|
|
|
|
|
|
expect(session[:bitbucket_server_url]).to be_nil
|
|
|
|
expect(session[:bitbucket_server_username]).to be_nil
|
|
|
|
expect(session[:bitbucket_server_personal_access_token]).to be_nil
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(response).to redirect_to(status_import_bitbucket_server_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the session variables' do
|
2020-07-28 23:09:34 +05:30
|
|
|
allow(controller).to receive(:allow_local_requests?).and_return(true)
|
|
|
|
|
|
|
|
post :configure, params: { personal_access_token: token, bitbucket_server_username: username, bitbucket_server_url: url }
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
expect(session[:bitbucket_server_url]).to eq(url)
|
|
|
|
expect(session[:bitbucket_server_username]).to eq(username)
|
|
|
|
expect(session[:bitbucket_server_personal_access_token]).to eq(token)
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(response).to redirect_to(status_import_bitbucket_server_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET status' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
before do
|
2020-06-23 00:09:42 +05:30
|
|
|
allow(controller).to receive(:client).and_return(client)
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
@repo = double(slug: 'vim', project_key: 'asd', full_name: 'asd/vim', "valid?" => true, project_name: 'asd', browse_url: 'http://test', name: 'vim')
|
2020-06-23 00:09:42 +05:30
|
|
|
@invalid_repo = double(slug: 'invalid', project_key: 'foobar', full_name: 'asd/foobar', "valid?" => false, browse_url: 'http://bad-repo', name: 'invalid')
|
2018-12-13 13:39:08 +05:30
|
|
|
@created_repo = double(slug: 'created', project_key: 'existing', full_name: 'group/created', "valid?" => true, browse_url: 'http://existing')
|
2018-11-18 11:00:15 +05:30
|
|
|
assign_session_tokens
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it 'returns invalid repos' do
|
|
|
|
allow(client).to receive(:repos).with(filter: nil, limit: 25, page_offset: 0).and_return([@repo, @invalid_repo])
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
get :status, format: :json
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['incompatible_repos'].length).to eq(1)
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(json_response.dig("incompatible_repos", 0, "id")).to eq("#{@invalid_repo.project_key}/#{@invalid_repo.slug}")
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(json_response['provider_repos'].length).to eq(1)
|
|
|
|
expect(json_response.dig("provider_repos", 0, "id")).to eq(@repo.full_name)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'import controller status' do
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:repo) { @repo }
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:repo_id) { "#{@repo.project_key}/#{@repo.slug}" }
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:import_source) { @repo.browse_url }
|
|
|
|
let(:provider_name) { 'bitbucket_server' }
|
|
|
|
let(:client_repos_field) { :repos }
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'when filtering' do
|
|
|
|
let(:filter) { 'test' }
|
|
|
|
|
|
|
|
it 'passes filter param to bitbucket client' do
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(client).to receive(:repos).with(filter: filter, limit: 25, page_offset: 0).and_return([@repo])
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
get :status, params: { filter: filter }, as: :json
|
|
|
|
end
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|