2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe Import::FogbugzController do
|
2015-09-25 12:07:36 +05:30
|
|
|
include ImportSpecHelper
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
2022-07-23 23:45:48 +05:30
|
|
|
let(:token) { FFaker::Lorem.characters(8) }
|
|
|
|
let(:uri) { 'https://example.com' }
|
2022-08-13 15:12:31 +05:30
|
|
|
let(:namespace_id) { 5 }
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
describe 'POST #callback' do
|
|
|
|
let(:xml_response) { %Q(<?xml version=\"1.0\" encoding=\"UTF-8\"?><response><token><![CDATA[#{token}]]></token></response>) }
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
before do
|
2019-07-31 22:56:46 +05:30
|
|
|
stub_request(:post, "https://example.com/api.asp").to_return(status: 200, body: xml_response, headers: {})
|
2022-08-13 15:12:31 +05:30
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
it 'attempts to contact Fogbugz server' do
|
2019-07-31 22:56:46 +05:30
|
|
|
post :callback, params: { uri: uri, email: 'test@example.com', password: 'mypassword' }
|
|
|
|
|
|
|
|
expect(session[:fogbugz_token]).to eq(token)
|
|
|
|
expect(session[:fogbugz_uri]).to eq(uri)
|
|
|
|
expect(response).to redirect_to(new_user_map_import_fogbugz_path)
|
|
|
|
end
|
2020-03-28 13:19:24 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
it 'preserves namespace_id query param on success' do
|
|
|
|
post :callback, params: { uri: uri, email: 'test@example.com', password: 'mypassword', namespace_id: namespace_id }
|
|
|
|
|
|
|
|
expect(response).to redirect_to(new_user_map_import_fogbugz_path(namespace_id: namespace_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to new page maintaining namespace_id when client raises standard error' do
|
|
|
|
namespace_id = 5
|
|
|
|
allow(::Gitlab::FogbugzImport::Client).to receive(:new).and_raise(StandardError)
|
|
|
|
|
|
|
|
post :callback, params: { uri: uri, email: 'test@example.com', password: 'mypassword', namespace_id: namespace_id }
|
|
|
|
|
|
|
|
expect(response).to redirect_to(new_import_fogbugz_url(namespace_id: namespace_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to new page form when client raises authentication exception' do
|
|
|
|
allow(::Gitlab::FogbugzImport::Client).to receive(:new).and_raise(::Fogbugz::AuthenticationException)
|
|
|
|
|
|
|
|
post :callback, params: { uri: uri, email: 'test@example.com', password: 'mypassword' }
|
|
|
|
|
|
|
|
expect(response).to redirect_to(new_import_fogbugz_url)
|
|
|
|
end
|
|
|
|
|
2020-03-28 13:19:24 +05:30
|
|
|
context 'verify url' do
|
|
|
|
shared_examples 'denies local request' do |reason|
|
|
|
|
it 'does not allow requests' do
|
|
|
|
post :callback, params: { uri: uri, email: 'test@example.com', password: 'mypassword' }
|
|
|
|
|
|
|
|
expect(response).to redirect_to(new_import_fogbugz_url)
|
|
|
|
expect(flash[:alert]).to eq("Specified URL cannot be used: \"#{reason}\"")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when host is localhost' do
|
|
|
|
let(:uri) { 'https://localhost:3000' }
|
|
|
|
|
|
|
|
include_examples 'denies local request', 'Requests to localhost are not allowed'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when host is on local network' do
|
|
|
|
let(:uri) { 'http://192.168.0.1/' }
|
|
|
|
|
|
|
|
include_examples 'denies local request', 'Requests to the local network are not allowed'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when host is ftp protocol' do
|
|
|
|
let(:uri) { 'ftp://testing' }
|
|
|
|
|
|
|
|
include_examples 'denies local request', 'Only allowed schemes are http, https'
|
|
|
|
end
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST #create_user_map' do
|
|
|
|
let(:user_map) do
|
|
|
|
{
|
|
|
|
"2" => {
|
|
|
|
"name" => "Test User",
|
|
|
|
"email" => "testuser@example.com",
|
|
|
|
"gitlab_user" => "3"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores the user map in the session' do
|
|
|
|
client = double(user_map: {})
|
|
|
|
expect(controller).to receive(:client).and_return(client)
|
|
|
|
|
|
|
|
post :create_user_map, params: { users: user_map }
|
|
|
|
|
|
|
|
expect(session[:fogbugz_user_map]).to eq(user_map)
|
|
|
|
expect(response).to redirect_to(status_import_fogbugz_path)
|
|
|
|
end
|
2022-08-13 15:12:31 +05:30
|
|
|
|
|
|
|
it 'preserves namespace_id query param' do
|
|
|
|
client = double(user_map: {})
|
|
|
|
expect(controller).to receive(:client).and_return(client)
|
|
|
|
|
|
|
|
post :create_user_map, params: { users: user_map, namespace_id: namespace_id }
|
|
|
|
|
|
|
|
expect(session[:fogbugz_user_map]).to eq(user_map)
|
|
|
|
expect(response).to redirect_to(status_import_fogbugz_path(namespace_id: namespace_id))
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
describe 'GET status' do
|
2022-01-26 12:08:38 +05:30
|
|
|
let(:repo) do
|
|
|
|
instance_double(Gitlab::FogbugzImport::Repository,
|
|
|
|
id: 'demo', name: 'vim', safe_name: 'vim', path: 'vim')
|
|
|
|
end
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
it 'redirects to new page form when client is invalid' do
|
|
|
|
stub_client(valid?: false)
|
|
|
|
|
|
|
|
get :status
|
|
|
|
|
|
|
|
expect(response).to redirect_to(new_import_fogbugz_path)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'import controller status' do
|
2022-08-13 15:12:31 +05:30
|
|
|
before do
|
|
|
|
stub_client(valid?: true)
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
let(:repo_id) { repo.id }
|
|
|
|
let(:import_source) { repo.name }
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:provider_name) { 'fogbugz' }
|
|
|
|
let(:client_repos_field) { :repos }
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
describe 'POST create' do
|
2022-07-23 23:45:48 +05:30
|
|
|
let(:repo_id) { 'FOGBUGZ_REPO_ID' }
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:client) { instance_double(Gitlab::FogbugzImport::Client, user_map: {}) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:client).and_return(client)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the new project' do
|
|
|
|
expect(Import::FogbugzService).to receive(:new).and_return(
|
|
|
|
instance_double(Import::FogbugzService, execute: ServiceResponse.success)
|
|
|
|
)
|
|
|
|
|
|
|
|
post :create, format: :json
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error when service reports an error' do
|
|
|
|
message = 'Error message'
|
|
|
|
status = :unprocessable_entity
|
|
|
|
|
|
|
|
expect(Import::FogbugzService).to receive(:new).and_return(
|
|
|
|
instance_double(Import::FogbugzService, execute: ServiceResponse.error(message: message, http_status: status))
|
|
|
|
)
|
|
|
|
|
|
|
|
post :create, format: :json
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(status)
|
|
|
|
expect(json_response).to eq({ 'errors' => message })
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it_behaves_like 'project import rate limiter'
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|