services_controller_spec.rb 5.5 KB
Newer Older
G
gfyoung 已提交
1 2
# frozen_string_literal: true

3 4 5
require 'spec_helper'

describe Projects::ServicesController do
6
  let(:project) { create(:project, :repository) }
7
  let(:user)    { create(:user) }
8 9
  let(:service) { create(:jira_service, project: project) }
  let(:service_params) { { username: 'username', password: 'password', url: 'http://example.com' } }
10 11 12

  before do
    sign_in(user)
13
    project.add_maintainer(user)
14 15
  end

16 17 18 19
  describe '#test' do
    context 'when can_test? returns false' do
      it 'renders 404' do
        allow_any_instance_of(Service).to receive(:can_test?).and_return(false)
20

B
blackst0ne 已提交
21
        put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param }
22

23
        expect(response).to have_gitlab_http_status(404)
24
      end
25
    end
26

27
    context 'when validations fail' do
28
      let(:service_params) { { active: 'true', url: '' } }
29 30

      it 'returns error messages in JSON response' do
B
blackst0ne 已提交
31
        put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }
32 33

        expect(json_response['message']).to eq "Validations failed."
34
        expect(json_response['service_response']).to include "Url can't be blank"
35 36 37 38
        expect(response).to have_gitlab_http_status(200)
      end
    end

39 40
    context 'success' do
      context 'with empty project' do
41
        let(:project) { create(:project) }
42

43 44
        context 'with chat notification service' do
          let(:service) { project.create_microsoft_teams_service(webhook: 'http://webhook.com') }
45

46 47
          it 'returns success' do
            allow_any_instance_of(MicrosoftTeams::Notifier).to receive(:ping).and_return(true)
48

B
blackst0ne 已提交
49
            put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param }
50

51
            expect(response.status).to eq(200)
52 53 54
          end
        end

55
        it 'returns success' do
56 57
          stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
            .to_return(status: 200, body: '{}')
58

B
blackst0ne 已提交
59
          put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }
60

61
          expect(response.status).to eq(200)
62 63 64
        end
      end

65
      it 'returns success' do
66 67
        stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
          .to_return(status: 200, body: '{}')
68

B
blackst0ne 已提交
69
        put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }
70

71
        expect(response.status).to eq(200)
72
      end
73 74 75 76 77 78 79 80 81

      context 'when service is configured for the first time' do
        before do
          allow_any_instance_of(ServiceHook).to receive(:execute).and_return(true)
        end

        it 'persist the object' do
          do_put

82 83
          expect(response).to have_gitlab_http_status(200)
          expect(json_response).to be_empty
84 85 86 87 88 89
          expect(BuildkiteService.first).to be_present
        end

        it 'creates the ServiceHook object' do
          do_put

90 91
          expect(response).to have_gitlab_http_status(200)
          expect(json_response).to be_empty
92 93 94 95
          expect(BuildkiteService.first.service_hook).to be_present
        end

        def do_put
B
blackst0ne 已提交
96 97 98 99 100 101
          put :test, params: {
                       namespace_id: project.namespace,
                       project_id: project,
                       id: 'buildkite',
                       service: { 'active' => '1', 'push_events' => '1', token: 'token', 'project_url' => 'http://test.com' }
                     }
102 103
        end
      end
104 105
    end

106
    context 'failure' do
J
Jarka Kadlecova 已提交
107
      it 'returns success status code and the error message' do
108 109
        stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
          .to_return(status: 404)
110

B
blackst0ne 已提交
111
        put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }
112

113 114 115 116 117 118 119
        expect(response).to have_gitlab_http_status(200)
        expect(json_response).to eq(
          'error' => true,
          'message' => 'Test failed.',
          'service_response' => '',
          'test_failed' => true
        )
120
      end
121 122
    end
  end
123 124

  describe 'PUT #update' do
125 126 127
    context 'when param `active` is set to true' do
      it 'activates the service and redirects to integrations paths' do
        put :update,
B
blackst0ne 已提交
128
          params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: true } }
129

130
        expect(response).to redirect_to(project_settings_integrations_path(project))
131
        expect(flash[:notice]).to eq 'JIRA activated.'
132 133
      end
    end
134

135 136
    context 'when param `active` is set to false' do
      it 'does not  activate the service but saves the settings' do
137
        put :update,
B
blackst0ne 已提交
138
          params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: false } }
139

140
        expect(flash[:notice]).to eq 'JIRA settings saved, but not activated.'
141 142
      end
    end
143

144 145 146 147 148 149 150 151 152
    context 'when activating JIRA service from a template' do
      let(:template_service) { create(:jira_service, project: project, template: true) }

      it 'activate JIRA service from template' do
        put :update, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: true } }

        expect(flash[:notice]).to eq 'JIRA activated.'
      end
    end
153 154 155 156
  end

  describe "GET #edit" do
    before do
J
James Fargher 已提交
157
      get :edit, params: { namespace_id: project.namespace, project_id: project, id: 'jira' }
158 159 160
    end

    context 'with approved services' do
161
      it 'renders edit page' do
162 163 164
        expect(response).to be_success
      end
    end
165
  end
166
end