2015-08-12 03:40:54 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::ServicesController do
|
2017-01-25 16:44:33 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
2015-08-12 03:40:54 -04:00
|
|
|
let(:user) { create(:user) }
|
2018-12-12 11:30:18 -05:00
|
|
|
let(:service) { create(:jira_service, project: project) }
|
|
|
|
let(:service_params) { { username: 'username', password: 'password', url: 'http://example.com' } }
|
2015-08-12 03:40:54 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2015-08-12 03:40:54 -04:00
|
|
|
end
|
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
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)
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param }
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2017-04-24 11:23:51 -04:00
|
|
|
end
|
2017-05-22 06:07:12 -04:00
|
|
|
end
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2018-02-23 22:18:14 -05:00
|
|
|
context 'when validations fail' do
|
2018-12-12 11:30:18 -05:00
|
|
|
let(:service_params) { { active: 'true', url: '' } }
|
2018-02-23 22:18:14 -05:00
|
|
|
|
|
|
|
it 'returns error messages in JSON response' do
|
2018-12-17 17:52:17 -05:00
|
|
|
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }
|
2018-02-23 22:18:14 -05:00
|
|
|
|
|
|
|
expect(json_response['message']).to eq "Validations failed."
|
2018-12-12 11:30:18 -05:00
|
|
|
expect(json_response['service_response']).to include "Url can't be blank"
|
2018-02-23 22:18:14 -05:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
context 'success' do
|
|
|
|
context 'with empty project' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
context 'with chat notification service' do
|
|
|
|
let(:service) { project.create_microsoft_teams_service(webhook: 'http://webhook.com') }
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
it 'returns success' do
|
|
|
|
allow_any_instance_of(MicrosoftTeams::Notifier).to receive(:ping).and_return(true)
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param }
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-24 11:23:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
it 'returns success' do
|
2018-12-12 11:30:18 -05:00
|
|
|
stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
|
|
|
|
.to_return(status: 200, body: '{}')
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-10-20 03:28:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
it 'returns success' do
|
2018-12-12 11:30:18 -05:00
|
|
|
stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
|
|
|
|
.to_return(status: 200, body: '{}')
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-08-12 03:40:54 -04:00
|
|
|
end
|
2017-08-22 12:05:02 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-12-12 11:30:18 -05:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(json_response).to be_empty
|
2017-08-22 12:05:02 -04:00
|
|
|
expect(BuildkiteService.first).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the ServiceHook object' do
|
|
|
|
do_put
|
|
|
|
|
2018-12-12 11:30:18 -05:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(json_response).to be_empty
|
2017-08-22 12:05:02 -04:00
|
|
|
expect(BuildkiteService.first.service_hook).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_put
|
2018-12-17 17:52:17 -05:00
|
|
|
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' }
|
|
|
|
}
|
2017-08-22 12:05:02 -04:00
|
|
|
end
|
|
|
|
end
|
2015-10-20 03:28:28 -04:00
|
|
|
end
|
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
context 'failure' do
|
2017-05-29 05:29:43 -04:00
|
|
|
it 'returns success status code and the error message' do
|
2018-12-12 11:30:18 -05:00
|
|
|
stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
|
|
|
|
.to_return(status: 404)
|
2017-05-22 06:07:12 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params }
|
2017-05-22 06:07:12 -04:00
|
|
|
|
2018-12-12 11:30:18 -05:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(json_response).to eq(
|
|
|
|
'error' => true,
|
|
|
|
'message' => 'Test failed.',
|
|
|
|
'service_response' => '',
|
|
|
|
'test_failed' => true
|
|
|
|
)
|
2017-05-22 06:07:12 -04:00
|
|
|
end
|
2015-10-20 03:28:28 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-04 09:33:14 -04:00
|
|
|
|
|
|
|
describe 'PUT #update' do
|
2017-05-22 06:07:12 -04:00
|
|
|
context 'when param `active` is set to true' do
|
|
|
|
it 'activates the service and redirects to integrations paths' do
|
|
|
|
put :update,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: true } }
|
2017-05-22 06:07:12 -04:00
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
expect(response).to redirect_to(project_settings_integrations_path(project))
|
2018-12-12 11:30:18 -05:00
|
|
|
expect(flash[:notice]).to eq 'JIRA activated.'
|
2017-05-22 06:07:12 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-04 09:33:14 -04:00
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
context 'when param `active` is set to false' do
|
|
|
|
it 'does not activate the service but saves the settings' do
|
2016-09-04 09:33:14 -04:00
|
|
|
put :update,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: false } }
|
2016-09-04 09:33:14 -04:00
|
|
|
|
2018-12-12 11:30:18 -05:00
|
|
|
expect(flash[:notice]).to eq 'JIRA settings saved, but not activated.'
|
2016-09-04 09:33:14 -04:00
|
|
|
end
|
|
|
|
end
|
2018-01-04 04:33:51 -05:00
|
|
|
|
|
|
|
context 'with a deprecated service' do
|
|
|
|
let(:service) { create(:kubernetes_service, project: project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
put :update,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { namespace: 'updated_namespace' } }
|
2018-01-04 04:33:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not update the service' do
|
|
|
|
service.reload
|
|
|
|
expect(service.namespace).not_to eq('updated_namespace')
|
|
|
|
end
|
|
|
|
end
|
2019-02-05 07:30:06 -05:00
|
|
|
|
|
|
|
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
|
2018-01-04 04:33:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET #edit" do
|
|
|
|
before do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :edit, params: { namespace_id: project.namespace, project_id: project, id: service_id }
|
2018-01-04 04:33:51 -05:00
|
|
|
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
|
2016-09-04 09:33:14 -04:00
|
|
|
end
|
2015-08-12 03:40:54 -04:00
|
|
|
end
|