jira_controller_spec.rb 6.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
# frozen_string_literal: true

require 'spec_helper'

describe Projects::Import::JiraController do
  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project) }

  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
        post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'Test' }

        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
          post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'Test' }

          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)
62
        stub_feature_flags(jira_issue_import_vue: false)
63 64 65 66 67
      end

      context 'when jira service is enabled for the project' do
        let_it_be(:jira_service) { create(:jira_service, project: project) }

68
        context 'when running jira import first time' do
69
          context 'get show' do
70 71 72
            before do
              allow(JIRA::Resource::Project).to receive(:all).and_return(jira_projects)

73 74 75
              expect(project.import_state).to be_nil

              get :show, params: { namespace_id: project.namespace.to_param, project_id: project }
76 77 78 79 80 81 82 83 84 85 86 87 88
            end

            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 everything is ok' do
              let(:jira_projects) { [double(name: 'FOO project', key: 'FOO')] }
89

90 91 92
              it 'renders show template' do
                expect(response).to render_template(:show)
              end
93 94 95 96
            end
          end

          context 'post import' do
97 98 99
            context 'when jira project key is empty' do
              it 'redirects back to show with an error' do
                post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: '' }
100

101 102 103 104
                expect(response).to redirect_to(project_import_jira_path(project))
                expect(flash[:alert]).to eq('No jira project key has been provided.')
              end
            end
105

106 107
            context 'when everything is ok' do
              it 'creates import state' do
108
                expect(project.latest_jira_import).to be_nil
109

110 111 112 113
                post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'Test' }

                project.reload

114
                jira_import = project.latest_jira_import
115
                expect(project.import_type).to eq 'jira'
116 117
                expect(jira_import.status).to eq 'scheduled'
                expect(jira_import.jira_project_key).to eq 'Test'
118 119
                expect(response).to redirect_to(project_import_jira_path(project))
              end
120 121 122 123 124
            end
          end
        end

        context 'when import state is scheduled' do
125
          let_it_be(:jira_import_state) { create(:jira_import_state, :scheduled, project: project) }
126 127 128 129 130

          context 'get show' do
            it 'renders import status' do
              get :show, params: { namespace_id: project.namespace.to_param, project_id: project }

131 132
              jira_import = project.latest_jira_import
              expect(jira_import.status).to eq 'scheduled'
133 134 135 136 137 138
              expect(flash.now[:notice]).to eq 'Import scheduled'
            end
          end

          context 'post import' do
            it 'uses the existing import data' do
139 140
              post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'New Project' }

141
              expect(flash[:notice]).to eq('Jira import is already running.')
142 143 144 145 146 147
              expect(response).to redirect_to(project_import_jira_path(project))
            end
          end
        end

        context 'when jira import ran before' do
148
          let_it_be(:jira_import_state) { create(:jira_import_state, :finished, project: project, jira_project_key: 'Test') }
149 150 151 152 153 154

          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 }

155
              expect(project.latest_jira_import.status).to eq 'finished'
156 157 158 159 160 161
              expect(flash.now[:notice]).to eq 'Import finished'
            end
          end

          context 'post import' do
            it 'uses the existing import data' do
162 163 164
              post :import, params: { namespace_id: project.namespace, project_id: project, jira_project_key: 'New Project' }

              project.reload
165 166 167 168
              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 'Test'
              expect(project.jira_imports.last.jira_project_key).to eq 'New Project'
169 170 171 172 173 174 175 176
              expect(response).to redirect_to(project_import_jira_path(project))
            end
          end
        end
      end
    end
  end
end