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) }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:service) { create(:hipchat_service, project: project) }
|
|
|
|
let(:hipchat_client) { { '#room' => double(send: true) } }
|
|
|
|
let(:service_params) { { token: 'hipchat_token_p', room: '#room' } }
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
project.team << [user, :master]
|
|
|
|
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
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
put :test, namespace_id: project.namespace, project_id: project, id: service.to_param
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(response).to have_http_status(404)
|
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
|
|
|
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
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
put :test, namespace_id: project.namespace, project_id: project, id: service.to_param
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(response.status).to eq(200)
|
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
|
|
|
|
expect(HipChat::Client).to receive(:new).with('hipchat_token_p', anything).and_return(hipchat_client)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
put :test, namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(response.status).to eq(200)
|
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
|
|
|
|
expect(HipChat::Client).to receive(:new).with('hipchat_token_p', anything).and_return(hipchat_client)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
put :test, namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(response.status).to eq(200)
|
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
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(ServiceHook).to receive(:execute).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'persist the object' do
|
|
|
|
do_put
|
|
|
|
|
|
|
|
expect(BuildkiteService.first).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the ServiceHook object' do
|
|
|
|
do_put
|
2017-08-17 22:00:37 +05:30
|
|
|
|
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
|
|
|
|
put :test, namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
id: 'buildkite',
|
|
|
|
service: { 'active' => '1', 'push_events' => '1', token: 'token', 'project_url' => 'http://test.com' }
|
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
|
|
|
|
expect(HipChat::Client).to receive(:new).with('hipchat_token_p', anything).and_raise('Bad test')
|
2015-10-24 18:46:33 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
put :test, namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(JSON.parse(response.body))
|
|
|
|
.to eq('error' => true, 'message' => 'Test failed.', 'service_response' => 'Bad test')
|
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
describe 'PUT #update' do
|
2017-09-10 17:25:29 +05:30
|
|
|
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, 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 'HipChat activated.'
|
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when param `active` is set to false' do
|
|
|
|
it 'does not activate the service but saves the settings' do
|
2016-09-29 09:46:39 +05:30
|
|
|
put :update,
|
2017-09-10 17:25:29 +05:30
|
|
|
namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: false }
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(flash[:notice]).to eq 'HipChat settings saved, but not activated.'
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|