summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/import/jira_controller_spec.rb
blob: 5288c0fcf21301660f521064a9c0eace0b64fc85 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::Import::JiraController do
  include JiraServiceHelper

  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project) }
  let_it_be(:jira_project_key) { 'Test' }

  def ensure_correct_config
    sign_in(user)
    project.add_maintainer(user)
    stub_jira_integration_test
  end

  shared_examples 'redirect with error' do |error|
    it 'redirects to project issues path' do
      subject

      expect(response).to redirect_to(project_issues_path(project))
    end

    it 'renders a correct error' do
      subject

      expect(flash[:notice]).to eq(error)
    end
  end

  shared_examples 'template with no message' do
    it 'does not set any message' do
      subject

      expect(flash).to be_empty
    end

    it 'renders show template' do
      subject

      expect(response).to render_template(template)
    end
  end

  shared_examples 'users without permissions' do
    context 'with anonymous user' do
      it 'redirects to new user page' do
        subject

        expect(response).to redirect_to(new_user_session_path)
      end
    end

    context 'when loged user is a developer' do
      before do
        create(:jira_integration, project: project)
        stub_jira_integration_test

        sign_in(user)
        project.add_developer(user)
      end

      it_behaves_like 'redirect with error', 'You do not have permissions to run the import.'
    end
  end

  describe 'GET #show' do
    let(:template) { 'show' }

    subject { get :show, params: { namespace_id: project.namespace, project_id: project } }

    it_behaves_like 'users without permissions'

    context 'jira integration configuration' do
      before do
        sign_in(user)
        project.add_maintainer(user)
      end

      context 'when Jira service is not enabled for the project' do
        it 'does not query Jira service' do
          expect(project).not_to receive(:jira_integration)
        end

        it_behaves_like 'template with no message'
      end

      context 'when Jira service is not configured correctly for the project' do
        let_it_be(:jira_integration) { create(:jira_integration, project: project) }

        before do
          WebMock.stub_request(:get, 'https://jira.example.com/rest/api/2/serverInfo')
            .to_raise(JIRA::HTTPError.new(double(message: 'Some failure.')))
        end

        it_behaves_like 'template with no message'
      end
    end
  end
end