2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-08 10:38:07 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Import::BitbucketServerController do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project_key) { 'test-project' }
|
|
|
|
let(:repo_slug) { 'some-repo' }
|
2018-07-09 19:04:41 -04:00
|
|
|
let(:client) { instance_double(BitbucketServer::Client) }
|
2018-07-08 10:38:07 -04:00
|
|
|
|
|
|
|
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
|
2018-12-05 19:43:29 -05:00
|
|
|
let(:project_name) { "my-project_123" }
|
|
|
|
|
2018-07-08 10:38:07 -04:00
|
|
|
before do
|
2018-07-08 17:49:09 -04:00
|
|
|
allow(controller).to receive(:bitbucket_client).and_return(client)
|
2018-12-05 19:43:29 -05:00
|
|
|
repo = double(name: project_name)
|
2018-07-08 17:49:09 -04:00
|
|
|
allow(client).to receive(:repo).with(project_key, repo_slug).and_return(repo)
|
2018-07-08 10:38:07 -04:00
|
|
|
assign_session_tokens
|
|
|
|
end
|
|
|
|
|
|
|
|
set(:project) { create(:project) }
|
|
|
|
|
|
|
|
it 'returns the new project' do
|
|
|
|
allow(Gitlab::BitbucketServerImport::ProjectCreator)
|
2018-12-05 19:43:29 -05:00
|
|
|
.to receive(:new).with(project_key, repo_slug, anything, project_name, user.namespace, user, anything)
|
2018-07-08 10:38:07 -04:00
|
|
|
.and_return(double(execute: project))
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post :create, params: { project: project_key, repository: repo_slug }, format: :json
|
2018-07-08 10:38:07 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
|
2018-12-05 19:43:29 -05:00
|
|
|
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))
|
|
|
|
|
|
|
|
post :create, params: { project: project_key, repository: repo_slug, format: :json }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-08 10:38:07 -04:00
|
|
|
it 'returns an error when an invalid project key is used' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post :create, params: { project: 'some&project' }
|
2018-07-08 10:44:33 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(422)
|
2018-07-08 10:38:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error when an invalid repository slug is used' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post :create, params: { project: 'some-project', repository: 'try*this' }
|
2018-07-08 10:44:33 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(422)
|
2018-07-08 10:38:07 -04:00
|
|
|
end
|
|
|
|
|
2018-07-08 10:49:47 -04:00
|
|
|
it 'returns an error when the project cannot be found' do
|
2018-07-08 17:49:09 -04:00
|
|
|
allow(client).to receive(:repo).with(project_key, repo_slug).and_return(nil)
|
2018-07-08 10:49:47 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post :create, params: { project: project_key, repository: repo_slug }, format: :json
|
2018-07-08 10:49:47 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(422)
|
|
|
|
end
|
|
|
|
|
2018-07-08 10:38:07 -04:00
|
|
|
it 'returns an error when the project cannot be saved' do
|
2018-07-08 17:49:09 -04:00
|
|
|
allow(Gitlab::BitbucketServerImport::ProjectCreator)
|
2018-12-05 19:43:29 -05:00
|
|
|
.to receive(:new).with(project_key, repo_slug, anything, project_name, user.namespace, user, anything)
|
2018-07-08 17:49:09 -04:00
|
|
|
.and_return(double(execute: build(:project)))
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post :create, params: { project: project_key, repository: repo_slug }, format: :json
|
2018-07-08 17:49:09 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(422)
|
2018-07-08 10:38:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an error when the server can't be contacted" do
|
2019-01-12 18:18:22 -05:00
|
|
|
expect(client).to receive(:repo).with(project_key, repo_slug).and_raise(::BitbucketServer::Connection::ConnectionError)
|
2018-07-08 17:57:30 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post :create, params: { project: project_key, repository: repo_slug }, format: :json
|
2018-07-08 17:57:30 -04:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(422)
|
2018-07-08 10:38:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST configure' do
|
2018-07-09 19:04:41 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(302)
|
|
|
|
expect(response).to redirect_to(status_import_bitbucket_server_path)
|
|
|
|
end
|
|
|
|
|
2018-07-08 10:38:07 -04:00
|
|
|
it 'sets the session variables' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post :configure, params: { personal_access_token: token, bitbucket_username: username, bitbucket_server_url: url }
|
2018-07-09 19:04:41 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
expect(response).to have_gitlab_http_status(302)
|
|
|
|
expect(response).to redirect_to(status_import_bitbucket_server_path)
|
2018-07-08 10:38:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET status' do
|
2018-07-09 19:04:41 -04:00
|
|
|
render_views
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:bitbucket_client).and_return(client)
|
|
|
|
|
2018-07-27 00:55:23 -04:00
|
|
|
@repo = double(slug: 'vim', project_key: 'asd', full_name: 'asd/vim', "valid?" => true, project_name: 'asd', browse_url: 'http://test', name: 'vim')
|
|
|
|
@invalid_repo = double(slug: 'invalid', project_key: 'foobar', full_name: 'asd/foobar', "valid?" => false, browse_url: 'http://bad-repo')
|
2018-11-05 18:37:21 -05:00
|
|
|
@created_repo = double(slug: 'created', project_key: 'existing', full_name: 'group/created', "valid?" => true, browse_url: 'http://existing')
|
2018-07-09 19:04:41 -04:00
|
|
|
assign_session_tokens
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns repository categories' do
|
2018-11-27 04:41:27 -05:00
|
|
|
created_project = create(:project, :import_finished, import_type: 'bitbucket_server', creator_id: user.id, import_source: @created_repo.browse_url)
|
2018-11-05 18:37:21 -05:00
|
|
|
repos = instance_double(BitbucketServer::Collection)
|
|
|
|
|
|
|
|
expect(repos).to receive(:partition).and_return([[@repo, @created_repo], [@invalid_repo]])
|
|
|
|
expect(repos).to receive(:current_page).and_return(1)
|
|
|
|
expect(repos).to receive(:next_page).and_return(2)
|
|
|
|
expect(repos).to receive(:prev_page).and_return(nil)
|
|
|
|
expect(client).to receive(:repos).and_return(repos)
|
2018-07-09 19:04:41 -04:00
|
|
|
|
|
|
|
get :status
|
|
|
|
|
|
|
|
expect(assigns(:already_added_projects)).to eq([created_project])
|
|
|
|
expect(assigns(:repos)).to eq([@repo])
|
|
|
|
expect(assigns(:incompatible_repos)).to eq([@invalid_repo])
|
2018-07-08 10:38:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET jobs' do
|
2018-07-09 19:04:41 -04:00
|
|
|
before do
|
|
|
|
assign_session_tokens
|
|
|
|
end
|
|
|
|
|
2018-07-08 10:38:07 -04:00
|
|
|
it 'returns a list of imported projects' do
|
2018-07-09 19:04:41 -04:00
|
|
|
created_project = create(:project, import_type: 'bitbucket_server', creator_id: user.id)
|
|
|
|
|
|
|
|
get :jobs
|
|
|
|
|
|
|
|
expect(json_response.count).to eq(1)
|
|
|
|
expect(json_response.first['id']).to eq(created_project.id)
|
|
|
|
expect(json_response.first['import_status']).to eq('none')
|
2018-07-08 10:38:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|