debian-mirror-gitlab/spec/controllers/projects/services_controller_spec.rb

111 lines
3.6 KiB
Ruby
Raw Normal View History

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) }
let(:service) { create(:service, project: project) }
before do
sign_in(user)
project.team << [user, :master]
2017-08-17 22:00:37 +05:30
2015-09-11 14:41:01 +05:30
controller.instance_variable_set(:@project, project)
controller.instance_variable_set(:@service, service)
end
2015-10-24 18:46:33 +05:30
shared_examples_for 'services controller' do |referrer|
before do
request.env["HTTP_REFERER"] = referrer
2015-09-11 14:41:01 +05:30
end
2015-10-24 18:46:33 +05:30
describe "#test" do
2017-08-17 22:00:37 +05:30
context 'when can_test? returns false' do
it 'renders 404' do
allow_any_instance_of(Service).to receive(:can_test?).and_return(false)
get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html
expect(response).to have_http_status(404)
end
end
2015-10-24 18:46:33 +05:30
context 'success' do
2017-08-17 22:00:37 +05:30
context 'with empty project' do
let(:project) { create(:empty_project) }
context 'with chat notification service' do
let(:service) { project.create_microsoft_teams_service(webhook: 'http://webhook.com') }
it 'redirects and show success message' do
allow_any_instance_of(MicrosoftTeams::Notifier).to receive(:ping).and_return(true)
get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html
expect(response).to redirect_to(root_path)
expect(flash[:notice]).to eq('We sent a request to the provided URL')
end
end
it 'redirects and show success message' do
expect(service).to receive(:test).and_return(success: true, result: 'done')
get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html
expect(response).to redirect_to(root_path)
expect(flash[:notice]).to eq('We sent a request to the provided URL')
end
end
2016-09-13 17:45:13 +05:30
it "redirects and show success message" do
2017-08-17 22:00:37 +05:30
expect(service).to receive(:test).and_return(success: true, result: 'done')
2015-10-24 18:46:33 +05:30
get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html
2017-08-17 22:00:37 +05:30
expect(response).to redirect_to(root_path)
2015-10-24 18:46:33 +05:30
expect(flash[:notice]).to eq('We sent a request to the provided URL')
end
end
context 'failure' do
2016-09-13 17:45:13 +05:30
it "redirects and show failure message" do
2017-08-17 22:00:37 +05:30
expect(service).to receive(:test).and_return(success: false, result: 'Bad test')
2015-10-24 18:46:33 +05:30
get :test, namespace_id: project.namespace.id, project_id: project.id, id: service.id, format: :html
2017-08-17 22:00:37 +05:30
expect(response).to redirect_to(root_path)
2015-10-24 18:46:33 +05:30
expect(flash[:alert]).to eq('We tried to send a request to the provided URL but an error occurred: Bad test')
end
2015-09-11 14:41:01 +05:30
end
end
end
2015-10-24 18:46:33 +05:30
describe 'referrer defined' do
it_should_behave_like 'services controller' do
let!(:referrer) { "/" }
end
end
describe 'referrer undefined' do
it_should_behave_like 'services controller' do
let!(:referrer) { nil }
end
end
2016-09-29 09:46:39 +05:30
describe 'PUT #update' do
context 'on successful update' do
it 'sets the flash' do
expect(service).to receive(:to_param).and_return('hipchat')
2017-08-17 22:00:37 +05:30
expect(service).to receive(:event_names).and_return(HipchatService.event_names)
2016-09-29 09:46:39 +05:30
put :update,
namespace_id: project.namespace.id,
project_id: project.id,
id: service.id,
service: { active: false }
expect(flash[:notice]).to eq 'Successfully updated.'
end
end
end
2015-09-11 14:41:01 +05:30
end