From 99928aca755f4ccf98a58445a0176b80cd16159c Mon Sep 17 00:00:00 2001
From: Kamil Trzcinski <ayufan@ayufan.eu>
Date: Thu, 11 Aug 2016 17:23:35 +0200
Subject: [PATCH] Enhance a pipeline event tests to analyse number of returned
 builds

---
 .../pipeline_data_builder_spec.rb             |  8 +++-
 spec/models/ci/pipeline_spec.rb               | 39 +++++++++----------
 2 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/spec/lib/gitlab/data_builder/pipeline_data_builder_spec.rb b/spec/lib/gitlab/data_builder/pipeline_data_builder_spec.rb
index 24d39b318c..8a2f00c434 100644
--- a/spec/lib/gitlab/data_builder/pipeline_data_builder_spec.rb
+++ b/spec/lib/gitlab/data_builder/pipeline_data_builder_spec.rb
@@ -3,11 +3,15 @@ require 'spec_helper'
 describe Gitlab::DataBuilder::PipelineDataBuilder do
   let(:user) { create(:user) }
   let(:project) { create(:project) }
+
   let(:pipeline) do
     create(:ci_pipeline,
-           project: project, status: 'success',
-           sha: project.commit.sha, ref: project.default_branch)
+           project: project,
+           status: 'success',
+           sha: project.commit.sha,
+           ref: project.default_branch)
   end
+
   let!(:build) { create(:ci_build, pipeline: pipeline) }
 
   describe '.build' do
diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb
index ceae2e60f2..7da044d4f1 100644
--- a/spec/models/ci/pipeline_spec.rb
+++ b/spec/models/ci/pipeline_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'
 
 describe Ci::Pipeline, models: true do
   let(:project) { FactoryGirl.create :empty_project }
-  let(:pipeline) { FactoryGirl.create :ci_pipeline, project: project }
+  let(:pipeline) { FactoryGirl.create :ci_empty_pipeline, project: project }
 
   it { is_expected.to belong_to(:project) }
   it { is_expected.to belong_to(:user) }
@@ -18,6 +18,8 @@ describe Ci::Pipeline, models: true do
   it { is_expected.to respond_to :git_author_email }
   it { is_expected.to respond_to :short_sha }
 
+  it { is_expected.to delegate_method(:stages).to(:statuses) }
+
   describe '#valid_commit_sha' do
     context 'commit.sha can not start with 00000000' do
       before do
@@ -261,43 +263,40 @@ describe Ci::Pipeline, models: true do
     end
 
     before do
-      WebMock.stub_request(:post, hook.url)
-      pipeline.touch
       ProjectWebHookWorker.drain
     end
 
     context 'with pipeline hooks enabled' do
       let(:enabled) { true }
 
-      it 'executes pipeline_hook after touched' do
-        expect(WebMock).to have_requested(:post, hook.url).once
-      end
-
       context 'with multiple builds' do
-        let(:build_a) { create_build('a') }
-        let(:build_b) { create_build('b') }
+        let!(:build_a) { create_build('a') }
+        let!(:build_b) { create_build('b') }
 
-        before do
+        it 'fires exactly 3 hooks' do
+          stub_request('pending')
+          build_a.queue
+          build_b.queue
+
+          stub_request('running')
           build_a.run
           build_b.run
+
+          stub_request('success')
           build_a.success
           build_b.success
         end
 
-        it 'fires 3 hooks' do
-          %w(pending running success).each do |status|
-            expect(WebMock).to requested(status)
-          end
-        end
-
         def create_build(name)
           create(:ci_build, :pending, pipeline: pipeline, name: name)
         end
 
-        def requested(status)
-          have_requested(:post, hook.url).with do |req|
-            JSON.parse(req.body)['object_attributes']['status'] == status
-          end.once
+        def stub_request(status)
+          WebMock.stub_request(:post, hook.url).with do |req|
+            json_body = JSON.parse(req.body)
+            json_body['object_attributes']['status'] == status &&
+              json_body['builds'].length == 2
+          end
         end
       end
     end
-- 
2.30.9