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) }
|
2017-05-22 06:07:12 -04:00
|
|
|
let(:service) { create(:hipchat_service, project: project) }
|
|
|
|
let(:hipchat_client) { { '#room' => double(send: true) } }
|
|
|
|
let(:service_params) { { token: 'hipchat_token_p', room: '#room' } }
|
2015-08-12 03:40:54 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
project.team << [user, :master]
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2015-08-12 03:40:54 -04:00
|
|
|
controller.instance_variable_set(:@project, project)
|
|
|
|
controller.instance_variable_set(:@service, service)
|
|
|
|
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
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
put :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
expect(response).to have_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
|
|
|
|
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
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
put :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id
|
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
|
|
|
|
expect(HipChat::Client).to receive(:new).with('hipchat_token_p', anything).and_return(hipchat_client)
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
put :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, 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
|
|
|
|
expect(HipChat::Client).to receive(:new).with('hipchat_token_p', anything).and_return(hipchat_client)
|
2017-04-24 11:23:51 -04:00
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
put :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, 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
|
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
|
2017-05-22 06:07:12 -04:00
|
|
|
expect(HipChat::Client).to receive(:new).with('hipchat_token_p', anything).and_raise('Bad test')
|
|
|
|
|
|
|
|
put :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, service: service_params
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(JSON.parse(response.body))
|
|
|
|
.to eq('error' => true, 'message' => 'Test failed.', 'service_response' => 'Bad test')
|
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,
|
|
|
|
namespace_id: project.namespace.id, project_id: project.id, id: service.id, service: { active: true }
|
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
expect(response).to redirect_to(project_settings_integrations_path(project))
|
2017-05-22 06:07:12 -04:00
|
|
|
expect(flash[:notice]).to eq 'HipChat activated.'
|
|
|
|
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,
|
2017-05-22 06:07:12 -04:00
|
|
|
namespace_id: project.namespace.id, project_id: project.id, id: service.id, service: { active: false }
|
2016-09-04 09:33:14 -04:00
|
|
|
|
2017-05-22 06:07:12 -04:00
|
|
|
expect(flash[:notice]).to eq 'HipChat settings saved, but not activated.'
|
2016-09-04 09:33:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-08-12 03:40:54 -04:00
|
|
|
end
|