services_controller_spec.rb 5.9 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
    allow(Gitlab::UrlBlocker).to receive(:validate!).and_return([URI.parse('http://example.com'), nil])
15 16
  end

17 18 19 20
  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)
21

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

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

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

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

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

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

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

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

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

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

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

60 61
          expect(Gitlab::HTTP).to receive(:get).with("/rest/api/2/serverInfo", any_args).and_call_original

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

64
          expect(response.status).to eq(200)
65 66 67
        end
      end

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

72 73
        expect(Gitlab::HTTP).to receive(:get).with("/rest/api/2/serverInfo", any_args).and_call_original

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

76
        expect(response.status).to eq(200)
77
      end
78 79 80 81 82 83 84 85 86

      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

87 88
          expect(response).to have_gitlab_http_status(200)
          expect(json_response).to be_empty
89 90 91 92 93 94
          expect(BuildkiteService.first).to be_present
        end

        it 'creates the ServiceHook object' do
          do_put

95 96
          expect(response).to have_gitlab_http_status(200)
          expect(json_response).to be_empty
97 98 99 100
          expect(BuildkiteService.first.service_hook).to be_present
        end

        def do_put
B
blackst0ne 已提交
101 102 103 104 105 106
          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' }
                     }
107 108
        end
      end
109 110
    end

111
    context 'failure' do
J
Jarka Kadlecova 已提交
112
      it 'returns success status code and the error message' do
113 114
        stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
          .to_return(status: 404)
115

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

118 119 120 121 122 123 124
        expect(response).to have_gitlab_http_status(200)
        expect(json_response).to eq(
          'error' => true,
          'message' => 'Test failed.',
          'service_response' => '',
          'test_failed' => true
        )
125
      end
126 127
    end
  end
128 129

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

135
        expect(response).to redirect_to(project_settings_integrations_path(project))
T
Takuya Noguchi 已提交
136
        expect(flash[:notice]).to eq 'Jira activated.'
137 138
      end
    end
139

140 141
    context 'when param `active` is set to false' do
      it 'does not  activate the service but saves the settings' do
142
        put :update,
B
blackst0ne 已提交
143
          params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: false } }
144

T
Takuya Noguchi 已提交
145
        expect(flash[:notice]).to eq 'Jira settings saved, but not activated.'
146 147
      end
    end
148

T
Takuya Noguchi 已提交
149
    context 'when activating Jira service from a template' do
150 151
      let(:template_service) { create(:jira_service, project: project, template: true) }

T
Takuya Noguchi 已提交
152
      it 'activate Jira service from template' do
153 154
        put :update, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: true } }

T
Takuya Noguchi 已提交
155
        expect(flash[:notice]).to eq 'Jira activated.'
156 157
      end
    end
158 159 160 161
  end

  describe "GET #edit" do
    before do
J
James Fargher 已提交
162
      get :edit, params: { namespace_id: project.namespace, project_id: project, id: 'jira' }
163 164 165
    end

    context 'with approved services' do
166
      it 'renders edit page' do
167 168 169
        expect(response).to be_success
      end
    end
170
  end
171
end