From 9d9c2d314c10347da4a0929db429c2c2bc24a64d Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Wed, 14 Sep 2016 01:10:58 +0800 Subject: Fix pipeline emails and add tests --- .../pipeline_email_service_spec.rb | 130 +++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 spec/models/project_services/pipeline_email_service_spec.rb (limited to 'spec/models/project_services') diff --git a/spec/models/project_services/pipeline_email_service_spec.rb b/spec/models/project_services/pipeline_email_service_spec.rb new file mode 100644 index 00000000000..38c2b25c88c --- /dev/null +++ b/spec/models/project_services/pipeline_email_service_spec.rb @@ -0,0 +1,130 @@ +require 'spec_helper' + +describe PipelinesEmailService do + let(:data) do + Gitlab::DataBuilder::Pipeline.build(create(:ci_pipeline)) + end + + let(:recipient) { 'test@gitlab.com' } + + def expect_pipeline_service + expect_any_instance_of(Ci::SendPipelineNotificationService) + end + + def receive_execute + receive(:execute).with([recipient]) + end + + describe 'Validations' do + context 'when service is active' do + before do + subject.active = true + end + + it { is_expected.to validate_presence_of(:recipients) } + + context 'when pusher is added' do + before do + subject.add_pusher = true + end + + it { is_expected.not_to validate_presence_of(:recipients) } + end + end + + context 'when service is inactive' do + before do + subject.active = false + end + + it { is_expected.not_to validate_presence_of(:recipients) } + end + end + + describe '#test_data' do + let(:build) { create(:ci_build) } + let(:project) { build.project } + let(:user) { create(:user) } + + before do + project.team << [user, :developer] + end + + it 'builds test data' do + data = subject.test_data(project, user) + + expect(data[:object_kind]).to eq('pipeline') + end + end + + describe '#test' do + before do + subject.recipients = recipient + end + + shared_examples 'sending email' do + it 'sends email' do + expect_pipeline_service.to receive_execute + + subject.test(data) + end + end + + it_behaves_like 'sending email' + + context 'when pipeline is succeeded' do + before do + data[:object_attributes][:status] = 'success' + end + + it_behaves_like 'sending email' + end + end + + describe '#execute' do + context 'with recipients' do + before do + subject.recipients = recipient + end + + it 'sends email for failed pipeline' do + data[:object_attributes][:status] = 'failed' + + expect_pipeline_service.to receive_execute + + subject.execute(data) + end + + it 'does not send email for succeeded pipeline' do + data[:object_attributes][:status] = 'success' + + expect_pipeline_service.not_to receive_execute + + subject.execute(data) + end + + context 'with notify_only_broken_pipelines on' do + before do + subject.notify_only_broken_pipelines = true + end + + it 'sends email for failed pipeline' do + data[:object_attributes][:status] = 'failed' + + expect_pipeline_service.to receive_execute + + subject.execute(data) + end + end + end + + it 'does not send email when recipients list is empty' do + subject.recipients = ' ,, ' + data[:object_attributes][:status] = 'failed' + + expect_pipeline_service.not_to receive_execute + + subject.execute(data) + end + end +end -- cgit v1.2.1