2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::ServicesController do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
2015-09-11 14:41:01 +05:30
|
|
|
let(:user) { create(:user) }
|
2019-02-15 15:39:39 +05:30
|
|
|
let(:service) { create(:jira_service, project: project) }
|
|
|
|
let(:service_params) { { username: 'username', password: 'password', url: 'http://example.com' } }
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
2018-11-18 11:00:15 +05:30
|
|
|
project.add_maintainer(user)
|
2019-09-04 21:01:54 +05:30
|
|
|
allow(Gitlab::UrlBlocker).to receive(:validate!).and_return([URI.parse('http://example.com'), nil])
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
put :test, params: project_params
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context 'when validations fail' do
|
2019-02-15 15:39:39 +05:30
|
|
|
let(:service_params) { { active: 'true', url: '' } }
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
it 'returns error messages in JSON response' do
|
2019-12-04 20:38:33 +05:30
|
|
|
put :test, params: project_params(service: service_params)
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(json_response['message']).to eq 'Validations failed.'
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(json_response['service_response']).to include "Url can't be blank"
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(response).to be_successful
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'success' do
|
|
|
|
context 'with empty project' do
|
|
|
|
let(:project) { create(:project) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'with chat notification service' do
|
|
|
|
let(:service) { project.create_microsoft_teams_service(webhook: 'http://webhook.com') }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'returns success' do
|
|
|
|
allow_any_instance_of(MicrosoftTeams::Notifier).to receive(:ping).and_return(true)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
put :test, params: project_params
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(response).to be_successful
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'returns success' do
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
|
|
|
|
.to_return(status: 200, body: '{}')
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
put :test, params: project_params(service: service_params)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(response).to be_successful
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'returns success' do
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
|
|
|
|
.to_return(status: 200, body: '{}')
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
put :test, params: project_params(service: service_params)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(response).to be_successful
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when service is configured for the first time' do
|
2019-12-04 20:38:33 +05:30
|
|
|
let(:service_params) do
|
|
|
|
{
|
|
|
|
'active' => '1',
|
|
|
|
'push_events' => '1',
|
|
|
|
'token' => 'token',
|
|
|
|
'project_url' => 'http://test.com'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
|
|
|
allow_any_instance_of(ServiceHook).to receive(:execute).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'persist the object' do
|
|
|
|
do_put
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(response).to be_successful
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(json_response).to be_empty
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(BuildkiteService.first).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the ServiceHook object' do
|
|
|
|
do_put
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(response).to be_successful
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(json_response).to be_empty
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(BuildkiteService.first.service_hook).to be_present
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def do_put
|
2019-12-04 20:38:33 +05:30
|
|
|
put :test, params: project_params(id: 'buildkite',
|
|
|
|
service: service_params)
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'failure' do
|
|
|
|
it 'returns success status code and the error message' do
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
|
|
|
|
.to_return(status: 404)
|
2015-10-24 18:46:33 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
put :test, params: project_params(service: service_params)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(response).to be_successful
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(json_response).to eq(
|
|
|
|
'error' => true,
|
|
|
|
'message' => 'Test failed.',
|
|
|
|
'service_response' => '',
|
|
|
|
'test_failed' => true
|
|
|
|
)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
describe 'PUT #update' do
|
2019-12-04 20:38:33 +05:30
|
|
|
describe 'as HTML' do
|
|
|
|
let(:service_params) { { active: true } }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
before do
|
|
|
|
put :update, params: project_params(service: service_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when param `active` is set to true' do
|
|
|
|
it 'activates the service and redirects to integrations paths' do
|
|
|
|
expect(response).to redirect_to(project_settings_integrations_path(project))
|
|
|
|
expect(flash[:notice]).to eq 'Jira activated.'
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when param `active` is set to false' do
|
|
|
|
let(:service_params) { { active: false } }
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it 'does not activate the service but saves the settings' do
|
|
|
|
expect(flash[:notice]).to eq 'Jira settings saved, but not activated.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when activating Jira service from a template' do
|
|
|
|
let(:service) do
|
|
|
|
create(:jira_service, project: project, template: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'activate Jira service from template' do
|
|
|
|
expect(flash[:notice]).to eq 'Jira activated.'
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
describe 'as JSON' do
|
|
|
|
before do
|
|
|
|
put :update, params: project_params(service: service_params, format: :json)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when update succeeds' do
|
|
|
|
let(:service_params) { { url: 'http://example.com' } }
|
|
|
|
|
|
|
|
it 'returns JSON response with no errors' do
|
|
|
|
expect(response).to be_successful
|
|
|
|
expect(json_response).to include('active' => true, 'errors' => {})
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when update fails' do
|
|
|
|
let(:service_params) { { url: '' } }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it 'returns JSON response with errors' do
|
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
|
|
|
expect(json_response).to include(
|
|
|
|
'active' => true,
|
|
|
|
'errors' => { 'url' => ['must be a valid URL', %{can't be blank}] }
|
|
|
|
)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
describe 'GET #edit' do
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
get :edit, params: project_params(id: 'jira')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with approved services' do
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'renders edit page' do
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(response).to be_successful
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def project_params(opts = {})
|
|
|
|
opts.reverse_merge(
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
id: service.to_param
|
|
|
|
)
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|