summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/services_controller_spec.rb
blob: b4af153d77a61ffb614a639eece456f3124b0891 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'spec_helper'

describe Projects::ServicesController do
  let(:project) { create(:project, :repository) }
  let(:user)    { create(:user) }
  let(:service) { create(:jira_service, project: project) }
  let(:service_params) { { username: 'username', password: 'password', url: 'http://example.com' } }

  before do
    sign_in(user)
    project.add_maintainer(user)
  end

  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)

        put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param }

        expect(response).to have_gitlab_http_status(404)
      end
    end

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

      it 'returns error messages in JSON response' do
        put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }

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

    context 'success' do
      context 'with empty project' do
        let(:project) { create(:project) }

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

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

            put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param }

            expect(response.status).to eq(200)
          end
        end

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

          put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }

          expect(response.status).to eq(200)
        end
      end

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

        put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }

        expect(response.status).to eq(200)
      end

      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

          expect(response).to have_gitlab_http_status(200)
          expect(json_response).to be_empty
          expect(BuildkiteService.first).to be_present
        end

        it 'creates the ServiceHook object' do
          do_put

          expect(response).to have_gitlab_http_status(200)
          expect(json_response).to be_empty
          expect(BuildkiteService.first.service_hook).to be_present
        end

        def do_put
          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' }
                     }
        end
      end
    end

    context 'failure' do
      it 'returns success status code and the error message' do
        stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
          .to_return(status: 404)

        put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }

        expect(response).to have_gitlab_http_status(200)
        expect(json_response).to eq(
          'error' => true,
          'message' => _('Test failed.'),
          'service_response' => '',
          'test_failed' => true
        )
      end
    end
  end

  describe 'PUT #update' do
    context 'when param `active` is set to true' do
      it 'activates the service and redirects to integrations paths' do
        put :update,
          params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: true } }

        expect(response).to redirect_to(project_settings_integrations_path(project))
        expect(flash[:notice]).to eq _('JIRA activated.')
      end
    end

    context 'when param `active` is set to false' do
      it 'does not  activate the service but saves the settings' do
        put :update,
          params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: false } }

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

    context 'with a deprecated service' do
      let(:service) { create(:kubernetes_service, project: project) }

      before do
        put :update,
          params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { namespace: 'updated_namespace' } }
      end

      it 'should not update the service' do
        service.reload
        expect(service.namespace).not_to eq('updated_namespace')
      end
    end
  end

  describe "GET #edit" do
    before do
      get :edit, params: { namespace_id: project.namespace, project_id: project, id: service_id }
    end

    context 'with approved services' do
      let(:service_id) { 'jira' }

      it 'should render edit page' do
        expect(response).to be_success
      end
    end

    context 'with a deprecated service' do
      let(:service_id) { 'kubernetes' }

      it 'should render edit page' do
        expect(response).to be_success
      end
    end
  end
end