debian-mirror-gitlab/spec/controllers/projects/import/jira_controller_spec.rb

233 lines
8 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
require 'spec_helper'
describe Projects::Import::JiraController do
2020-05-24 23:13:21 +05:30
include JiraServiceHelper
2020-04-08 14:13:33 +05:30
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
2020-04-22 19:07:51 +05:30
let_it_be(:jira_project_key) { 'Test' }
2020-04-08 14:13:33 +05:30
context 'with anonymous user' do
before do
stub_feature_flags(jira_issue_import: true)
end
context 'get show' do
it 'redirects to issues page' do
get :show, params: { namespace_id: project.namespace, project_id: project }
expect(response).to redirect_to(new_user_session_path)
end
end
context 'post import' do
it 'redirects to issues page' do
2020-04-22 19:07:51 +05:30
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: jira_project_key }
2020-04-08 14:13:33 +05:30
expect(response).to redirect_to(new_user_session_path)
end
end
end
context 'with logged in user' do
before do
sign_in(user)
project.add_maintainer(user)
end
context 'when feature flag not enabled' do
before do
stub_feature_flags(jira_issue_import: false)
end
context 'get show' do
it 'redirects to issues page' do
get :show, params: { namespace_id: project.namespace, project_id: project }
expect(response).to redirect_to(project_issues_path(project))
end
end
context 'post import' do
it 'redirects to issues page' do
2020-04-22 19:07:51 +05:30
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: jira_project_key }
2020-04-08 14:13:33 +05:30
expect(response).to redirect_to(project_issues_path(project))
end
end
end
context 'when feature flag enabled' do
before do
stub_feature_flags(jira_issue_import: true)
2020-04-22 19:07:51 +05:30
stub_feature_flags(jira_issue_import_vue: false)
2020-05-24 23:13:21 +05:30
stub_jira_service_test
2020-04-08 14:13:33 +05:30
end
2020-05-24 23:13:21 +05:30
context 'when Jira service is enabled for the project' do
2020-04-08 14:13:33 +05:30
let_it_be(:jira_service) { create(:jira_service, project: project) }
2020-04-22 19:07:51 +05:30
context 'when user is developer' do
let_it_be(:dev) { create(:user) }
before do
sign_in(dev)
project.add_developer(dev)
end
2020-04-08 14:13:33 +05:30
context 'get show' do
2020-04-22 19:07:51 +05:30
before do
get :show, params: { namespace_id: project.namespace.to_param, project_id: project }
end
2020-05-24 23:13:21 +05:30
it 'does not query Jira service' do
2020-04-22 19:07:51 +05:30
expect(project).not_to receive(:jira_service)
end
2020-04-08 14:13:33 +05:30
it 'renders show template' do
2020-04-22 19:07:51 +05:30
expect(response).to render_template(:show)
expect(assigns(:jira_projects)).not_to be_present
end
end
context 'post import' do
it 'returns 404' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: jira_project_key }
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
context 'when issues disabled' do
let_it_be(:disabled_issues_project) { create(:project, :public, :issues_disabled) }
context 'get show' do
it 'returs 404' do
get :show, params: { namespace_id: project.namespace.to_param, project_id: disabled_issues_project }
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'post import' do
it 'returs 404' do
post :import, params: { namespace_id: disabled_issues_project.namespace, project_id: disabled_issues_project, jira_project_key: jira_project_key }
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
2020-05-24 23:13:21 +05:30
context 'when running Jira import first time' do
2020-04-22 19:07:51 +05:30
context 'get show' do
before do
allow(JIRA::Resource::Project).to receive(:all).and_return(jira_projects)
expect(project.jira_imports).to be_empty
2020-04-08 14:13:33 +05:30
get :show, params: { namespace_id: project.namespace.to_param, project_id: project }
2020-04-22 19:07:51 +05:30
end
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
context 'when no projects have been retrieved from Jira' do
let(:jira_projects) { [] }
it 'render an error message' do
expect(flash[:alert]).to eq('No projects have been returned from Jira. Please check your Jira configuration.')
expect(response).to render_template(:show)
end
end
context 'when projects retrieved from Jira' do
let(:jira_projects) { [double(name: 'FOO project', key: 'FOO')] }
it 'renders show template' do
expect(response).to render_template(:show)
end
2020-04-08 14:13:33 +05:30
end
end
context 'post import' do
2020-05-24 23:13:21 +05:30
context 'when Jira project key is empty' do
2020-04-22 19:07:51 +05:30
it 'redirects back to show with an error' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: '' }
expect(response).to redirect_to(project_import_jira_path(project))
2020-05-24 23:13:21 +05:30
expect(flash[:alert]).to eq('No Jira project key has been provided.')
2020-04-22 19:07:51 +05:30
end
end
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
context 'when everything is ok' do
it 'creates import state' do
expect(project.latest_jira_import).to be_nil
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: jira_project_key }
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
project.reload
jira_import = project.latest_jira_import
expect(project.import_type).to eq 'jira'
expect(jira_import.status).to eq 'scheduled'
expect(jira_import.jira_project_key).to eq jira_project_key
expect(response).to redirect_to(project_import_jira_path(project))
end
2020-04-08 14:13:33 +05:30
end
end
end
context 'when import state is scheduled' do
2020-04-22 19:07:51 +05:30
let_it_be(:jira_import_state) { create(:jira_import_state, :scheduled, project: project) }
2020-04-08 14:13:33 +05:30
context 'get show' do
it 'renders import status' do
get :show, params: { namespace_id: project.namespace.to_param, project_id: project }
2020-04-22 19:07:51 +05:30
jira_import = project.latest_jira_import
expect(jira_import.status).to eq 'scheduled'
2020-04-08 14:13:33 +05:30
expect(flash.now[:notice]).to eq 'Import scheduled'
end
end
context 'post import' do
it 'uses the existing import data' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'New Project' }
2020-04-22 19:07:51 +05:30
expect(flash[:notice]).to eq('Jira import is already running.')
2020-04-08 14:13:33 +05:30
expect(response).to redirect_to(project_import_jira_path(project))
end
end
end
2020-05-24 23:13:21 +05:30
context 'when Jira import ran before' do
2020-04-22 19:07:51 +05:30
let_it_be(:jira_import_state) { create(:jira_import_state, :finished, project: project, jira_project_key: jira_project_key) }
2020-04-08 14:13:33 +05:30
context 'get show' do
it 'renders import status' do
allow(JIRA::Resource::Project).to receive(:all).and_return([])
get :show, params: { namespace_id: project.namespace.to_param, project_id: project }
2020-04-22 19:07:51 +05:30
expect(project.latest_jira_import.status).to eq 'finished'
2020-04-08 14:13:33 +05:30
expect(flash.now[:notice]).to eq 'Import finished'
end
end
context 'post import' do
it 'uses the existing import data' do
post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'New Project' }
project.reload
2020-04-22 19:07:51 +05:30
expect(project.latest_jira_import.status).to eq 'scheduled'
expect(project.jira_imports.size).to eq 2
expect(project.jira_imports.first.jira_project_key).to eq jira_project_key
expect(project.jira_imports.last.jira_project_key).to eq 'New Project'
2020-04-08 14:13:33 +05:30
expect(response).to redirect_to(project_import_jira_path(project))
end
end
end
end
end
end
end